Commit 216870e0 authored by Tristan Cavelier's avatar Tristan Cavelier Committed by Sebastien Robin

JIO provides now a resarch method for jio storages.

If the storage does not search into document list by itself,
JIO will limit it anyway.
The storage should use the method researchDone() to tell to JIO that
the document list is already limited (avoid double limiting).
If the storage can not support limiting, it can also use
searchDocumentArray() provided by JIO (and no need to use researchDone()).
parent a882ef32
...@@ -318,6 +318,8 @@ ...@@ -318,6 +318,8 @@
priv.jio.getDocumentList({ priv.jio.getDocumentList({
'sort':{'lastModified':'descending', 'sort':{'lastModified':'descending',
'fileName':'ascending'}, 'fileName':'ascending'},
'limit':{begin:0,end:50},
// 'search':{fileName:'a'},
'maxtries':3, 'maxtries':3,
'callback':function (result) { 'callback':function (result) {
if (result.status === 'done') { if (result.status === 'done') {
......
...@@ -709,6 +709,7 @@ var JIO = ...@@ -709,6 +709,7 @@ var JIO =
priv.res = {'status':'done','message':''}; priv.res = {'status':'done','message':''};
priv.sorted = false; priv.sorted = false;
priv.limited = false; priv.limited = false;
priv.research_done = false;
//// end Private attributes //// end Private attributes
//// Private Methods //// Private Methods
...@@ -771,6 +772,11 @@ var JIO = ...@@ -771,6 +772,11 @@ var JIO =
priv.res.return_value = priv.res.return_value =
that.limitDocumentArray(priv.res.return_value); that.limitDocumentArray(priv.res.return_value);
} }
// check for research
if (!priv.research_done && typeof priv.job.search !== 'undefined') {
priv.res.return_value =
that.searchDocumentArray(priv.res.return_value);
}
}; };
priv.fail_removeDocument = function () { priv.fail_removeDocument = function () {
priv.res.message = 'Unable to removed document.'; priv.res.message = 'Unable to removed document.';
...@@ -964,7 +970,7 @@ var JIO = ...@@ -964,7 +970,7 @@ var JIO =
* Limits the document list. Clones only the document list between * Limits the document list. Clones only the document list between
* begin and end set in limit object in the job. * begin and end set in limit object in the job.
* @method limitDocumentArray * @method limitDocumentArray
* @param {array} documentarray The array we wont to limit * @param {array} documentarray The array we want to limit
* @return {array} The new document list * @return {array} The new document list
*/ */
that.limitDocumentArray = function (documentarray) { that.limitDocumentArray = function (documentarray) {
...@@ -981,6 +987,38 @@ var JIO = ...@@ -981,6 +987,38 @@ var JIO =
priv.limited = true; priv.limited = true;
}; };
/**
* Search the strings inside the document list. Clones the document list
* containing only the matched strings.
* @method searchDocumentArray
* @param {array} documentarray The array we want to search into.
* @return {array} The new document list.
*/
that.searchDocumentArray = function (documentarray) {
var i, k, newdocumentarray = [];
for (i = 0; i < documentarray.length; i += 1) {
for (k in priv.job.search) {
if (typeof documentarray[i][k] === 'undefined') {
continue;
}
if (documentarray[i][k].search(priv.job.search[k]) > -1) {
newdocumentarray.push(documentarray[i]);
break;
}
}
}
that.researchDone();
return newdocumentarray;
};
/**
* Tells to this storage that the research is already done.
* @method researchDone
*/
that.researchDone = function () {
priv.research_done = true;
};
//// end Public Methods //// end Public Methods
return that; return that;
}; };
......
/*! JIO - v0.1.0 - 2012-05-29 /*! JIO - v0.1.0 - 2012-05-29
* Copyright (c) 2012 Nexedi; Licensed */ * Copyright (c) 2012 Nexedi; Licensed */
var JIO=function(){var a=function(a,b){var c={job_method_object:{checkNameAvailability:{},saveDocument:{},loadDocument:{},getDocumentList:{},removeDocument:{}}},d={job_managing_method:{canSelect:function(a,b){return JSON.stringify(a.storage)===JSON.stringify(b.storage)&&JSON.stringify(a.applicant)===JSON.stringify(b.applicant)&&a.fileName===b.fileName?!0:!1},canRemoveFailOrDone:function(a,b){return a.status==="fail"||a.status==="done"?!0:!1},canEliminate:function(a,b){return a.status!=="ongoing"&&(a.method==="removeDocument"&&b.method==="saveDocument"||a.method==="saveDocument"&&b.method==="removeDocument")?!0:!1},canReplace:function(a,b){return a.status!=="ongoing"&&a.method===b.method&&a.date<b.date?!0:!1},cannotAccept:function(a,b){if(a.status!=="ongoing"){if(a.method==="removeDocument"&&b.method==="loadDocument")return!0}else{if(a.method===b.method==="loadDocument")return!0;if(a.method==="removeDocument"&&(b.method==="loadDocument"||b.method==="removeDocument"))return!0;if(a.method===b.method==="saveDocument"&&a.fileContent===b.fileContent)return!0;if(a.method===b.method==="getDocumentList"||a.method===b.method==="checkNameAvailability")return!0}return!1},mustWait:function(a,b){return a.method==="getDocumentList"||a.method==="checkNameAvailability"||b.method==="getDocumentList"||b.method==="checkNameAvailability"?!1:!0}},queue_id:1,storage_type_object:{},max_wait_time:1e4},e=function(a,b){var c;for(c in b)a[c]=b[c];return a},f,g,h,i,j,k,l,m;return f=function(a,c){var d={},e={},f={},g,h;return e.eventAction=function(a){return h=a&&f[a],h||(g=b.Callbacks(),h={publish:g.fire,subscribe:g.add,unsubscribe:g.remove},a&&(f[a]=h)),h},d.publish=function(a,b){e.eventAction(a).publish(b)},d.subscribe=function(a,b){return e.eventAction(a).subscribe(b),b},d.unsubscribe=function(a,b){e.eventAction(a).unsubscribe(b)},d},g=function(a,b){var c=e({},a);return c.id=0,c.status="initial",c.date=Date.now(),c},h=function(b,f){var h={},i={},j="jio/idarray";return h.init=function(c){var d,e=function(){},f;i.use_local_storage&&(f=a.getItem(j)||[],b.publisher&&(i.publisher=b.publisher),i.jio_id=c.jio_id,i.job_object_name="jio/jobobject/"+i.jio_id,f.push(i.jio_id),a.setItem(j,f)),i.job_object={},h.copyJobQueueToLocalStorage();for(d in i.recovered_job_object)i.recovered_job_object[d].callback=e,h.addJob(i.recovered_job_object[d])},h.close=function(){JSON.stringify(i.job_object)==="{}"&&a.deleteItem(i.job_object_name)},h.getNewQueueID=function(){var b=null,c=0,e=a.getItem(j)||[];for(b=0;b<e.length;b+=1)e[b]>=d.queue_id&&(d.queue_id=e[b]+1);return c=d.queue_id,d.queue_id++,c},h.recoverOlderJobObject=function(){var b=null,c=[],d=!1,e;if(i.use_local_storage){e=a.getItem(j)||[];for(b=0;b<e.length;b+=1)a.getItem("jio/id/"+e[b])<Date.now()-1e4?(a.deleteItem("jio/id/"+e[b]),i.recovered_job_object=a.getItem("jio/jobobject/"+e[b]),a.deleteItem("jio/jobobject/"+e[b]),d=!0):c.push(e[b]);d&&a.setItem(j,c)}},h.isThereJobsWhere=function(a){var b="id";if(!a)return!0;for(b in i.job_object)if(a(i.job_object[b]))return!0;return!1},h.copyJobQueueToLocalStorage=function(){return i.use_local_storage?a.setItem(i.job_object_name,i.job_object):!1},h.createJob=function(a){return h.addJob(g(a))},h.addJob=function(a){var b=!0,c=[],e=[],f=[],g=null,j="id";for(j in i.job_object){if(d.job_managing_method.canRemoveFailOrDone(i.job_object[j],a)){f.push(j);continue}if(d.job_managing_method.canSelect(i.job_object[j],a)){if(d.job_managing_method.canEliminate(i.job_object[j],a)){c.push(j);continue}if(d.job_managing_method.canReplace(i.job_object[j],a)){g=k({queue:h,job:i.job_object[j]}),g.replace(a),b=!1;break}if(d.job_managing_method.cannotAccept(i.job_object[j],a))return!1;if(d.job_managing_method.mustWait(i.job_object[j],a)){e.push(j);continue}}}if(b){for(j=0;j<c.length;j+=1)g=k({queue:h,job:i.job_object[c[j]]}),g.eliminate();if(e.length>0){a.status="wait",a.waitingFor={jobIdArray:e};for(j=0;j<e.length;j+=1)i.job_object[e[j]]&&(i.job_object[e[j]].maxtries=1)}for(j=0;j<f.length;j+=1)h.removeJob(i.job_object[f[j]]);a.id=i.job_id,a.tries=0,i.job_id++,i.job_object[a.id]=a}return h.copyJobQueueToLocalStorage(),!0},h.removeJob=function(a){var b=e({where:function(a){return!0}},a),c,d=!1,f="key";if(b.job)i.job_object[b.job.id]&&b.where(i.job_object[b.job.id])&&(delete i.job_object[b.job.id],d=!0);else for(f in i.job_object)b.where(i.job_object[f])&&(delete i.job_object[f],d=!0);d||console.error("No jobs was found, when trying to remove some."),h.copyJobQueueToLocalStorage()},h.resetAll=function(){var a="id";for(a in i.job_object)i.job_object[a].status="initial";h.copyJobQueueToLocalStorage()},h.invokeAll=function(){var a="id",b,c;for(a in i.job_object){c=!1;if(i.job_object[a].status==="initial")h.invoke(i.job_object[a]);else if(i.job_object[a].status==="wait"){c=!0;if(i.job_object[a].waitingFor.jobIdArray)for(b=0;b<i.job_object[a].waitingFor.jobIdArray.length;b+=1)if(i.job_object[i.job_object[a].waitingFor.jobIdArray[b]]){c=!1;break}i.job_object[a].waitingFor.time&&i.job_object[a].waitingFor.time>Date.now()&&(c=!1),c&&h.invoke(i.job_object[a])}}this.copyJobQueueToLocalStorage()},h.invoke=function(a){var b;if(!c.job_method_object[a.method])return!1;h.isThereJobsWhere(function(b){return b.method===a.method&&b.method==="initial"})?a.status="ongoing":(a.status="ongoing",i.publisher.publish(c.job_method_object[a.method]["start_"+a.method])),b=k({queue:this,job:a}),b.execute()},h.ended=function(a){var b=e({},a);h.removeJob({job:b});if(!c.job_method_object[b.method])return!1;if(!h.isThereJobsWhere(function(a){return a.method===b.method&&a.status==="ongoing"||a.status==="initial"})){i.publisher.publish(c.job_method_object[b.method]["stop_"+b.method]);return}},h.clean=function(){h.removeJob(undefined,{where:function(a){return a.status==="fail"}})},i.use_local_storage=b.options.use_local_storage,i.publisher=b.publisher,i.job_id=1,i.jio_id=0,i.job_object_name="",i.job_object={},i.recovered_job_object={},h},i=function(a,b){var c={},d={};return d.interval=200,d.id=null,d.queue=a.queue,c.setIntervalDelay=function(a){d.interval=a},c.start=function(){return d.id?!1:(d.id=setInterval(function(){d.queue.recoverOlderJobObject(),d.queue.invokeAll()},d.interval),!0)},c.stop=function(){return d.id?(clearInterval(d.id),d.id=null,!0):!1},c},j=function(){var b={},c={};return c.interval=400,c.id=null,b.start=function(a){return c.id?!1:(b.touch(a),c.id=setInterval(function(){b.touch(a)},c.interval),!0)},b.stop=function(){return c.id?(clearInterval(c.id),c.id=null,!0):!1},b.touch=function(b){a.setItem("jio/id/"+b,Date.now())},b},k=function(a){var b={},c={};return c.job=a.job,c.callback=a.job.callback,c.queue=a.queue,c.res={status:"done",message:""},c.sorted=!1,c.limited=!1,c.fail_checkNameAvailability=function(){c.res.message="Unable to check name availability."},c.done_checkNameAvailability=function(a){c.res.message=c.job.userName+" is "+(a?"":"not ")+"available.",c.res.return_value=a},c.fail_loadDocument=function(){c.res.message="Unable to load document."},c.done_loadDocument=function(a){c.res.message="Document loaded.",c.res.return_value=a,c.res.return_value.lastModified=(new Date(c.res.return_value.lastModified)).getTime(),c.res.return_value.creationDate=(new Date(c.res.return_value.creationDate)).getTime()},c.fail_saveDocument=function(){c.res.message="Unable to save document."},c.done_saveDocument=function(){c.res.message="Document saved."},c.fail_getDocumentList=function(){c.res.message="Unable to retrieve document list."},c.done_getDocumentList=function(a){var d;c.res.message="Document list received.",c.res.return_value=a;for(d=0;d<c.res.return_value.length;d+=1)typeof c.res.return_value[d].lastModified!="number"&&(c.res.return_value[d].lastModified=(new Date(c.res.return_value[d].lastModified)).getTime()),typeof c.res.return_value[d].creationDate!="number"&&(c.res.return_value[d].creationDate=(new Date(c.res.return_value[d].creationDate)).getTime());!c.sorted&&typeof c.job.sort!="undefined"&&b.sortDocumentArray(c.res.return_value),!c.limited&&typeof c.job.limit!="undefined"&&typeof c.job.limit.begin!="undefined"&&typeof c.job.limit.end!="undefined"&&(c.res.return_value=b.limitDocumentArray(c.res.return_value))},c.fail_removeDocument=function(){c.res.message="Unable to removed document."},c.done_removeDocument=function(){c.res.message="Document removed."},c.retryLater=function(){var a=c.job.tries*c.job.tries*1e3;a>d.max_wait_time&&(a=d.max_wait_time),c.job.status="wait",c.job.waitingFor={time:Date.now()+a}},b.cloneJob=function(){return e({},c.job)},b.getUserName=function(){return c.job.userName||""},b.getApplicantID=function(){return c.job.applicant.ID||""},b.getStorageUserName=function(){return c.job.storage.userName||""},b.getStoragePassword=function(){return c.job.storage.password||""},b.getStorageLocation=function(){return c.job.storage.location||""},b.getStorageArray=function(){return c.job.storage.storageArray||[]},b.getFileName=function(){return c.job.fileName||""},b.getFileContent=function(){return c.job.fileContent||""},b.cloneOptionObject=function(){return e({},c.job.options)},b.getMaxTries=function(){return c.job.maxtries},b.getTries=function(){return c.job.tries||0},b.setMaxTries=function(a){c.job.maxtries=a},b.addJob=function(a){return c.queue.createJob(a)},b.eliminate=function(){c.job.maxtries=1,c.job.tries=1,b.fail("Job Stopped!",0)},b.replace=function(a){c.job.tries=0,c.job.date=a.date,c.job.callback=a.callback,c.res.status="fail",c.res.message="Job Stopped!",c.res.error={},c.res.error.status=0,c.res.error.statusText="Replaced",c.res.error.message="The job was replaced by a newer one.",c["fail_"+c.job.method](),c.callback(c.res)},b.fail=function(a){c.res.status="fail",c.res.error=a,c.res.error.status=c.res.error.status||0,c.res.error.statusText=c.res.error.statusText||"Unknown Error",c.res.error.array=c.res.error.array||[],c.res.error.message=c.res.error.message||"",!c.job.maxtries||c.job.tries<c.job.maxtries?c.retryLater():(c.job.status="fail",c["fail_"+c.job.method](),c.queue.ended(c.job),c.callback(c.res))},b.done=function(a){c.job.status="done",c["done_"+c.job.method](a),c.queue.ended(c.job),c.callback(c.res)},b.execute=function(){return c.job.tries=b.getTries()+1,d.storage_type_object[c.job.storage.type]?d.storage_type_object[c.job.storage.type]({job:c.job,queue:c.queue})[c.job.method]():null},b.checkNameAvailability=function(){b.fail({status:0,statusText:"Undefined Method",message:"This method must be redefined!"})},b.loadDocument=function(){b.fail({status:0,statusText:"Undefined Method",message:"This method must be redefined!"})},b.saveDocument=function(){b.fail({status:0,statusText:"Undefined Method",message:"This method must be redefined!"})},b.getDocumentList=function(){b.fail({status:0,statusText:"Undefined Method",message:"This method must be redefined!"})},b.removeDocument=function(){b.fail({status:0,statusText:"Undefined Method",message:"This method must be redefined!"})},b.sortDocumentArray=function(a){a.sort(function(a,b){var d,e;for(d in c.job.sort){var f=c.job.sort[d]==="descending"?-1:1;if(a[d]===b[d])continue;return a[d]>b[d]?f:-f}return 0}),b.sortDone()},b.sortDone=function(){c.sorted=!0},b.limitDocumentArray=function(a){return b.limitDone(),a.slice(c.job.limit.begin,c.job.limit.end)},b.limitDone=function(){c.limited=!0},b},l=function(a,b){var c={},g={};g.wrongParametersError=function(a){var b="Method: "+a.method+", One or some parameters are undefined.";return console.error(b),a.callback({status:"fail",error:{status:0,statusText:"Undefined Parameter",message:b}}),null},c.getID=function(){return g.id},c.start=function(){return g.id!==0?!1:(g.id=g.queue.getNewQueueID(),g.queue.init({jio_id:g.id}),g.updater&&g.updater.start(g.id),g.listener.start(),g.ready=!0,c.isReady())},c.stop=function(){return g.queue.close(),g.listener.stop(),g.updater&&g.updater.stop(),g.ready=!1,g.id=0,!0},c.kill=function(){return g.queue.close(),g.listener.stop(),g.updater&&g.updater.stop(),g.ready=!1,!0},c.isReady=function(){return g.ready},c.publish=function(a,b){if(!c.isReady())return;return g.pubsub.publish(a,b)},c.subscribe=function(a,b){return g.pubsub.subscribe(a,b)},c.unsubscribe=function(a,b){return g.pubsub.unsubscribe(a,b)},c.checkNameAvailability=function(a){var b=e({userName:g.storage.userName,storage:g.storage,applicant:g.applicant,method:"checkNameAvailability",callback:function(){}},a);return c.isReady()&&b.userName&&b.storage&&b.applicant?g.queue.createJob(b):g.wrongParametersError(b)},c.saveDocument=function(a){var b=e({storage:g.storage,applicant:g.applicant,fileContent:"",method:"saveDocument",callback:function(){}},a);return c.isReady()&&b.fileName&&b.storage&&b.applicant?g.queue.createJob(b):g.wrongParametersError(b)},c.loadDocument=function(a){var b=e({storage:g.storage,applicant:g.applicant,method:"loadDocument",callback:function(){}},a);return c.isReady()&&b.fileName&&b.storage&&b.applicant?g.queue.createJob(b):g.wrongParametersError(b)},c.getDocumentList=function(a){var b=e({storage:g.storage,applicant:g.applicant,method:"getDocumentList",callback:function(){}},a);return c.isReady()&&b.storage&&b.applicant?g.queue.createJob(b):g.wrongParametersError(b)},c.removeDocument=function(a){var b=e({storage:g.storage,applicant:g.applicant,method:"removeDocument",callback:function(){}},a);return c.isReady()&&b.fileName&&b.storage&&b.applicant?g.queue.createJob(b):g.wrongParametersError(b)};var k=e({use_local_storage:!0},a.options);return typeof a.storage=="string"&&(a.storage=JSON.parse(a.storage)),typeof a.applicant=="string"&&(a.applicant=JSON.parse(a.applicant)),g.storage=a.storage,g.applicant=a.applicant,g.id=0,g.pubsub=f({options:k}),g.queue=h({publisher:g.pubsub,options:k}),g.listener=i({queue:g.queue,options:k}),g.ready=!1,k.use_local_storage?g.updater=j({options:k}):g.updater=null,g.storage&&!d.storage_type_object[g.storage.type]&&console.error('Unknown storage type "'+g.storage.type+'"'),c.start(),c},m=function(a,b){var f={};return f.createNew=function(a,b,c){var d=e({use_local_storage:!0},c);return l({storage:a,applicant:b,options:d})},f.newBaseStorage=function(a,b){return k(a,b)},f.addStorageType=function(a,b){return a&&b?(d.storage_type_object[a]=b,!0):!1},f.getGlobalObject=function(){return d},f.getConstObject=function(){return e({},c)},f},m()};return window.requirejs?(define("JIO",["LocalOrCookieStorage","jQuery"],a),undefined):a(LocalOrCookieStorage,jQuery)}(); var JIO=function(){var a=function(a,b){var c={job_method_object:{checkNameAvailability:{},saveDocument:{},loadDocument:{},getDocumentList:{},removeDocument:{}}},d={job_managing_method:{canSelect:function(a,b){return JSON.stringify(a.storage)===JSON.stringify(b.storage)&&JSON.stringify(a.applicant)===JSON.stringify(b.applicant)&&a.fileName===b.fileName?!0:!1},canRemoveFailOrDone:function(a,b){return a.status==="fail"||a.status==="done"?!0:!1},canEliminate:function(a,b){return a.status!=="ongoing"&&(a.method==="removeDocument"&&b.method==="saveDocument"||a.method==="saveDocument"&&b.method==="removeDocument")?!0:!1},canReplace:function(a,b){return a.status!=="ongoing"&&a.method===b.method&&a.date<b.date?!0:!1},cannotAccept:function(a,b){if(a.status!=="ongoing"){if(a.method==="removeDocument"&&b.method==="loadDocument")return!0}else{if(a.method===b.method==="loadDocument")return!0;if(a.method==="removeDocument"&&(b.method==="loadDocument"||b.method==="removeDocument"))return!0;if(a.method===b.method==="saveDocument"&&a.fileContent===b.fileContent)return!0;if(a.method===b.method==="getDocumentList"||a.method===b.method==="checkNameAvailability")return!0}return!1},mustWait:function(a,b){return a.method==="getDocumentList"||a.method==="checkNameAvailability"||b.method==="getDocumentList"||b.method==="checkNameAvailability"?!1:!0}},queue_id:1,storage_type_object:{},max_wait_time:1e4},e=function(a,b){var c;for(c in b)a[c]=b[c];return a},f,g,h,i,j,k,l,m;return f=function(a,c){var d={},e={},f={},g,h;return e.eventAction=function(a){return h=a&&f[a],h||(g=b.Callbacks(),h={publish:g.fire,subscribe:g.add,unsubscribe:g.remove},a&&(f[a]=h)),h},d.publish=function(a,b){e.eventAction(a).publish(b)},d.subscribe=function(a,b){return e.eventAction(a).subscribe(b),b},d.unsubscribe=function(a,b){e.eventAction(a).unsubscribe(b)},d},g=function(a,b){var c=e({},a);return c.id=0,c.status="initial",c.date=Date.now(),c},h=function(b,f){var h={},i={},j="jio/idarray";return h.init=function(c){var d,e=function(){},f;i.use_local_storage&&(f=a.getItem(j)||[],b.publisher&&(i.publisher=b.publisher),i.jio_id=c.jio_id,i.job_object_name="jio/jobobject/"+i.jio_id,f.push(i.jio_id),a.setItem(j,f)),i.job_object={},h.copyJobQueueToLocalStorage();for(d in i.recovered_job_object)i.recovered_job_object[d].callback=e,h.addJob(i.recovered_job_object[d])},h.close=function(){JSON.stringify(i.job_object)==="{}"&&a.deleteItem(i.job_object_name)},h.getNewQueueID=function(){var b=null,c=0,e=a.getItem(j)||[];for(b=0;b<e.length;b+=1)e[b]>=d.queue_id&&(d.queue_id=e[b]+1);return c=d.queue_id,d.queue_id++,c},h.recoverOlderJobObject=function(){var b=null,c=[],d=!1,e;if(i.use_local_storage){e=a.getItem(j)||[];for(b=0;b<e.length;b+=1)a.getItem("jio/id/"+e[b])<Date.now()-1e4?(a.deleteItem("jio/id/"+e[b]),i.recovered_job_object=a.getItem("jio/jobobject/"+e[b]),a.deleteItem("jio/jobobject/"+e[b]),d=!0):c.push(e[b]);d&&a.setItem(j,c)}},h.isThereJobsWhere=function(a){var b="id";if(!a)return!0;for(b in i.job_object)if(a(i.job_object[b]))return!0;return!1},h.copyJobQueueToLocalStorage=function(){return i.use_local_storage?a.setItem(i.job_object_name,i.job_object):!1},h.createJob=function(a){return h.addJob(g(a))},h.addJob=function(a){var b=!0,c=[],e=[],f=[],g=null,j="id";for(j in i.job_object){if(d.job_managing_method.canRemoveFailOrDone(i.job_object[j],a)){f.push(j);continue}if(d.job_managing_method.canSelect(i.job_object[j],a)){if(d.job_managing_method.canEliminate(i.job_object[j],a)){c.push(j);continue}if(d.job_managing_method.canReplace(i.job_object[j],a)){g=k({queue:h,job:i.job_object[j]}),g.replace(a),b=!1;break}if(d.job_managing_method.cannotAccept(i.job_object[j],a))return!1;if(d.job_managing_method.mustWait(i.job_object[j],a)){e.push(j);continue}}}if(b){for(j=0;j<c.length;j+=1)g=k({queue:h,job:i.job_object[c[j]]}),g.eliminate();if(e.length>0){a.status="wait",a.waitingFor={jobIdArray:e};for(j=0;j<e.length;j+=1)i.job_object[e[j]]&&(i.job_object[e[j]].maxtries=1)}for(j=0;j<f.length;j+=1)h.removeJob(i.job_object[f[j]]);a.id=i.job_id,a.tries=0,i.job_id++,i.job_object[a.id]=a}return h.copyJobQueueToLocalStorage(),!0},h.removeJob=function(a){var b=e({where:function(a){return!0}},a),c,d=!1,f="key";if(b.job)i.job_object[b.job.id]&&b.where(i.job_object[b.job.id])&&(delete i.job_object[b.job.id],d=!0);else for(f in i.job_object)b.where(i.job_object[f])&&(delete i.job_object[f],d=!0);d||console.error("No jobs was found, when trying to remove some."),h.copyJobQueueToLocalStorage()},h.resetAll=function(){var a="id";for(a in i.job_object)i.job_object[a].status="initial";h.copyJobQueueToLocalStorage()},h.invokeAll=function(){var a="id",b,c;for(a in i.job_object){c=!1;if(i.job_object[a].status==="initial")h.invoke(i.job_object[a]);else if(i.job_object[a].status==="wait"){c=!0;if(i.job_object[a].waitingFor.jobIdArray)for(b=0;b<i.job_object[a].waitingFor.jobIdArray.length;b+=1)if(i.job_object[i.job_object[a].waitingFor.jobIdArray[b]]){c=!1;break}i.job_object[a].waitingFor.time&&i.job_object[a].waitingFor.time>Date.now()&&(c=!1),c&&h.invoke(i.job_object[a])}}this.copyJobQueueToLocalStorage()},h.invoke=function(a){var b;if(!c.job_method_object[a.method])return!1;h.isThereJobsWhere(function(b){return b.method===a.method&&b.method==="initial"})?a.status="ongoing":(a.status="ongoing",i.publisher.publish(c.job_method_object[a.method]["start_"+a.method])),b=k({queue:this,job:a}),b.execute()},h.ended=function(a){var b=e({},a);h.removeJob({job:b});if(!c.job_method_object[b.method])return!1;if(!h.isThereJobsWhere(function(a){return a.method===b.method&&a.status==="ongoing"||a.status==="initial"})){i.publisher.publish(c.job_method_object[b.method]["stop_"+b.method]);return}},h.clean=function(){h.removeJob(undefined,{where:function(a){return a.status==="fail"}})},i.use_local_storage=b.options.use_local_storage,i.publisher=b.publisher,i.job_id=1,i.jio_id=0,i.job_object_name="",i.job_object={},i.recovered_job_object={},h},i=function(a,b){var c={},d={};return d.interval=200,d.id=null,d.queue=a.queue,c.setIntervalDelay=function(a){d.interval=a},c.start=function(){return d.id?!1:(d.id=setInterval(function(){d.queue.recoverOlderJobObject(),d.queue.invokeAll()},d.interval),!0)},c.stop=function(){return d.id?(clearInterval(d.id),d.id=null,!0):!1},c},j=function(){var b={},c={};return c.interval=400,c.id=null,b.start=function(a){return c.id?!1:(b.touch(a),c.id=setInterval(function(){b.touch(a)},c.interval),!0)},b.stop=function(){return c.id?(clearInterval(c.id),c.id=null,!0):!1},b.touch=function(b){a.setItem("jio/id/"+b,Date.now())},b},k=function(a){var b={},c={};return c.job=a.job,c.callback=a.job.callback,c.queue=a.queue,c.res={status:"done",message:""},c.sorted=!1,c.limited=!1,c.research_done=!1,c.fail_checkNameAvailability=function(){c.res.message="Unable to check name availability."},c.done_checkNameAvailability=function(a){c.res.message=c.job.userName+" is "+(a?"":"not ")+"available.",c.res.return_value=a},c.fail_loadDocument=function(){c.res.message="Unable to load document."},c.done_loadDocument=function(a){c.res.message="Document loaded.",c.res.return_value=a,c.res.return_value.lastModified=(new Date(c.res.return_value.lastModified)).getTime(),c.res.return_value.creationDate=(new Date(c.res.return_value.creationDate)).getTime()},c.fail_saveDocument=function(){c.res.message="Unable to save document."},c.done_saveDocument=function(){c.res.message="Document saved."},c.fail_getDocumentList=function(){c.res.message="Unable to retrieve document list."},c.done_getDocumentList=function(a){var d;c.res.message="Document list received.",c.res.return_value=a;for(d=0;d<c.res.return_value.length;d+=1)typeof c.res.return_value[d].lastModified!="number"&&(c.res.return_value[d].lastModified=(new Date(c.res.return_value[d].lastModified)).getTime()),typeof c.res.return_value[d].creationDate!="number"&&(c.res.return_value[d].creationDate=(new Date(c.res.return_value[d].creationDate)).getTime());!c.sorted&&typeof c.job.sort!="undefined"&&b.sortDocumentArray(c.res.return_value),!c.limited&&typeof c.job.limit!="undefined"&&typeof c.job.limit.begin!="undefined"&&typeof c.job.limit.end!="undefined"&&(c.res.return_value=b.limitDocumentArray(c.res.return_value)),!c.research_done&&typeof c.job.search!="undefined"&&(c.res.return_value=b.searchDocumentArray(c.res.return_value))},c.fail_removeDocument=function(){c.res.message="Unable to removed document."},c.done_removeDocument=function(){c.res.message="Document removed."},c.retryLater=function(){var a=c.job.tries*c.job.tries*1e3;a>d.max_wait_time&&(a=d.max_wait_time),c.job.status="wait",c.job.waitingFor={time:Date.now()+a}},b.cloneJob=function(){return e({},c.job)},b.getUserName=function(){return c.job.userName||""},b.getApplicantID=function(){return c.job.applicant.ID||""},b.getStorageUserName=function(){return c.job.storage.userName||""},b.getStoragePassword=function(){return c.job.storage.password||""},b.getStorageLocation=function(){return c.job.storage.location||""},b.getStorageArray=function(){return c.job.storage.storageArray||[]},b.getFileName=function(){return c.job.fileName||""},b.getFileContent=function(){return c.job.fileContent||""},b.cloneOptionObject=function(){return e({},c.job.options)},b.getMaxTries=function(){return c.job.maxtries},b.getTries=function(){return c.job.tries||0},b.setMaxTries=function(a){c.job.maxtries=a},b.addJob=function(a){return c.queue.createJob(a)},b.eliminate=function(){c.job.maxtries=1,c.job.tries=1,b.fail("Job Stopped!",0)},b.replace=function(a){c.job.tries=0,c.job.date=a.date,c.job.callback=a.callback,c.res.status="fail",c.res.message="Job Stopped!",c.res.error={},c.res.error.status=0,c.res.error.statusText="Replaced",c.res.error.message="The job was replaced by a newer one.",c["fail_"+c.job.method](),c.callback(c.res)},b.fail=function(a){c.res.status="fail",c.res.error=a,c.res.error.status=c.res.error.status||0,c.res.error.statusText=c.res.error.statusText||"Unknown Error",c.res.error.array=c.res.error.array||[],c.res.error.message=c.res.error.message||"",!c.job.maxtries||c.job.tries<c.job.maxtries?c.retryLater():(c.job.status="fail",c["fail_"+c.job.method](),c.queue.ended(c.job),c.callback(c.res))},b.done=function(a){c.job.status="done",c["done_"+c.job.method](a),c.queue.ended(c.job),c.callback(c.res)},b.execute=function(){return c.job.tries=b.getTries()+1,d.storage_type_object[c.job.storage.type]?d.storage_type_object[c.job.storage.type]({job:c.job,queue:c.queue})[c.job.method]():null},b.checkNameAvailability=function(){b.fail({status:0,statusText:"Undefined Method",message:"This method must be redefined!"})},b.loadDocument=function(){b.fail({status:0,statusText:"Undefined Method",message:"This method must be redefined!"})},b.saveDocument=function(){b.fail({status:0,statusText:"Undefined Method",message:"This method must be redefined!"})},b.getDocumentList=function(){b.fail({status:0,statusText:"Undefined Method",message:"This method must be redefined!"})},b.removeDocument=function(){b.fail({status:0,statusText:"Undefined Method",message:"This method must be redefined!"})},b.sortDocumentArray=function(a){a.sort(function(a,b){var d,e;for(d in c.job.sort){var f=c.job.sort[d]==="descending"?-1:1;if(a[d]===b[d])continue;return a[d]>b[d]?f:-f}return 0}),b.sortDone()},b.sortDone=function(){c.sorted=!0},b.limitDocumentArray=function(a){return b.limitDone(),a.slice(c.job.limit.begin,c.job.limit.end)},b.limitDone=function(){c.limited=!0},b.searchDocumentArray=function(a){var d,e,f=[];for(d=0;d<a.length;d+=1)for(e in c.job.search){if(typeof a[d][e]=="undefined")continue;if(a[d][e].search(c.job.search[e])>-1){f.push(a[d]);break}}return b.researchDone(),f},b.researchDone=function(){c.research_done=!0},b},l=function(a,b){var c={},g={};g.wrongParametersError=function(a){var b="Method: "+a.method+", One or some parameters are undefined.";return console.error(b),a.callback({status:"fail",error:{status:0,statusText:"Undefined Parameter",message:b}}),null},c.getID=function(){return g.id},c.start=function(){return g.id!==0?!1:(g.id=g.queue.getNewQueueID(),g.queue.init({jio_id:g.id}),g.updater&&g.updater.start(g.id),g.listener.start(),g.ready=!0,c.isReady())},c.stop=function(){return g.queue.close(),g.listener.stop(),g.updater&&g.updater.stop(),g.ready=!1,g.id=0,!0},c.kill=function(){return g.queue.close(),g.listener.stop(),g.updater&&g.updater.stop(),g.ready=!1,!0},c.isReady=function(){return g.ready},c.publish=function(a,b){if(!c.isReady())return;return g.pubsub.publish(a,b)},c.subscribe=function(a,b){return g.pubsub.subscribe(a,b)},c.unsubscribe=function(a,b){return g.pubsub.unsubscribe(a,b)},c.checkNameAvailability=function(a){var b=e({userName:g.storage.userName,storage:g.storage,applicant:g.applicant,method:"checkNameAvailability",callback:function(){}},a);return c.isReady()&&b.userName&&b.storage&&b.applicant?g.queue.createJob(b):g.wrongParametersError(b)},c.saveDocument=function(a){var b=e({storage:g.storage,applicant:g.applicant,fileContent:"",method:"saveDocument",callback:function(){}},a);return c.isReady()&&b.fileName&&b.storage&&b.applicant?g.queue.createJob(b):g.wrongParametersError(b)},c.loadDocument=function(a){var b=e({storage:g.storage,applicant:g.applicant,method:"loadDocument",callback:function(){}},a);return c.isReady()&&b.fileName&&b.storage&&b.applicant?g.queue.createJob(b):g.wrongParametersError(b)},c.getDocumentList=function(a){var b=e({storage:g.storage,applicant:g.applicant,method:"getDocumentList",callback:function(){}},a);return c.isReady()&&b.storage&&b.applicant?g.queue.createJob(b):g.wrongParametersError(b)},c.removeDocument=function(a){var b=e({storage:g.storage,applicant:g.applicant,method:"removeDocument",callback:function(){}},a);return c.isReady()&&b.fileName&&b.storage&&b.applicant?g.queue.createJob(b):g.wrongParametersError(b)};var k=e({use_local_storage:!0},a.options);return typeof a.storage=="string"&&(a.storage=JSON.parse(a.storage)),typeof a.applicant=="string"&&(a.applicant=JSON.parse(a.applicant)),g.storage=a.storage,g.applicant=a.applicant,g.id=0,g.pubsub=f({options:k}),g.queue=h({publisher:g.pubsub,options:k}),g.listener=i({queue:g.queue,options:k}),g.ready=!1,k.use_local_storage?g.updater=j({options:k}):g.updater=null,g.storage&&!d.storage_type_object[g.storage.type]&&console.error('Unknown storage type "'+g.storage.type+'"'),c.start(),c},m=function(a,b){var f={};return f.createNew=function(a,b,c){var d=e({use_local_storage:!0},c);return l({storage:a,applicant:b,options:d})},f.newBaseStorage=function(a,b){return k(a,b)},f.addStorageType=function(a,b){return a&&b?(d.storage_type_object[a]=b,!0):!1},f.getGlobalObject=function(){return d},f.getConstObject=function(){return e({},c)},f},m()};return window.requirejs?(define("JIO",["LocalOrCookieStorage","jQuery"],a),undefined):a(LocalOrCookieStorage,jQuery)}();
\ No newline at end of file \ No newline at end of file
...@@ -706,6 +706,7 @@ var JIO = ...@@ -706,6 +706,7 @@ var JIO =
priv.res = {'status':'done','message':''}; priv.res = {'status':'done','message':''};
priv.sorted = false; priv.sorted = false;
priv.limited = false; priv.limited = false;
priv.research_done = false;
//// end Private attributes //// end Private attributes
//// Private Methods //// Private Methods
...@@ -768,6 +769,11 @@ var JIO = ...@@ -768,6 +769,11 @@ var JIO =
priv.res.return_value = priv.res.return_value =
that.limitDocumentArray(priv.res.return_value); that.limitDocumentArray(priv.res.return_value);
} }
// check for research
if (!priv.research_done && typeof priv.job.search !== 'undefined') {
priv.res.return_value =
that.searchDocumentArray(priv.res.return_value);
}
}; };
priv.fail_removeDocument = function () { priv.fail_removeDocument = function () {
priv.res.message = 'Unable to removed document.'; priv.res.message = 'Unable to removed document.';
...@@ -961,7 +967,7 @@ var JIO = ...@@ -961,7 +967,7 @@ var JIO =
* Limits the document list. Clones only the document list between * Limits the document list. Clones only the document list between
* begin and end set in limit object in the job. * begin and end set in limit object in the job.
* @method limitDocumentArray * @method limitDocumentArray
* @param {array} documentarray The array we wont to limit * @param {array} documentarray The array we want to limit
* @return {array} The new document list * @return {array} The new document list
*/ */
that.limitDocumentArray = function (documentarray) { that.limitDocumentArray = function (documentarray) {
...@@ -978,6 +984,38 @@ var JIO = ...@@ -978,6 +984,38 @@ var JIO =
priv.limited = true; priv.limited = true;
}; };
/**
* Search the strings inside the document list. Clones the document list
* containing only the matched strings.
* @method searchDocumentArray
* @param {array} documentarray The array we want to search into.
* @return {array} The new document list.
*/
that.searchDocumentArray = function (documentarray) {
var i, k, newdocumentarray = [];
for (i = 0; i < documentarray.length; i += 1) {
for (k in priv.job.search) {
if (typeof documentarray[i][k] === 'undefined') {
continue;
}
if (documentarray[i][k].search(priv.job.search[k]) > -1) {
newdocumentarray.push(documentarray[i]);
break;
}
}
}
that.researchDone();
return newdocumentarray;
};
/**
* Tells to this storage that the research is already done.
* @method researchDone
*/
that.researchDone = function () {
priv.research_done = true;
};
//// end Public Methods //// end Public Methods
return that; return that;
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment