Commit 5644e5b4 authored by Tristan Cavelier's avatar Tristan Cavelier

Add jio.getJobArray method to JIO.

parent c9ec180f
......@@ -1049,7 +1049,7 @@ var jobManager = (function(spec, my) {
priv.id = spec.id;
priv.interval_id = null;
priv.interval = 200;
priv.job_a = [];
priv.job_array = [];
my.jobManager = that;
my.jobIdHandler = jobIdHandler;
......@@ -1079,8 +1079,8 @@ var jobManager = (function(spec, my) {
*/
priv.copyJobArrayToLocal = function() {
var new_a = [], i;
for (i = 0; i < priv.job_a.length; i+= 1) {
new_a.push(priv.job_a[i].serialized());
for (i = 0; i < priv.job_array.length; i+= 1) {
new_a.push(priv.job_array[i].serialized());
}
LocalOrCookieStorage.setItem(priv.getJobArrayName(),new_a);
};
......@@ -1091,13 +1091,13 @@ var jobManager = (function(spec, my) {
* @param {object} job The job object.
*/
priv.removeJob = function(job) {
var i, tmp_job_a = [];
for (i = 0; i < priv.job_a.length; i+= 1) {
if (priv.job_a[i] !== job) {
tmp_job_a.push(priv.job_a[i]);
var i, tmp_job_array = [];
for (i = 0; i < priv.job_array.length; i+= 1) {
if (priv.job_array[i] !== job) {
tmp_job_array.push(priv.job_array[i]);
}
}
priv.job_a = tmp_job_a;
priv.job_array = tmp_job_array;
priv.copyJobArrayToLocal();
};
......@@ -1119,8 +1119,8 @@ var jobManager = (function(spec, my) {
if (priv.interval_id === null) {
priv.interval_id = setInterval (function() {
priv.restoreOldJio();
for (i = 0; i < priv.job_a.length; i+= 1) {
that.execute(priv.job_a[i]);
for (i = 0; i < priv.job_array.length; i+= 1) {
that.execute(priv.job_array[i]);
}
},priv.interval);
}
......@@ -1134,7 +1134,7 @@ var jobManager = (function(spec, my) {
if (priv.interval_id !== null) {
clearInterval(priv.interval_id);
priv.interval_id = null;
if (priv.job_a.length === 0) {
if (priv.job_array.length === 0) {
LocalOrCookieStorage.deleteItem(priv.getJobArrayName());
}
}
......@@ -1242,8 +1242,8 @@ var jobManager = (function(spec, my) {
*/
that.jobIdExists = function(id) {
var i;
for (i = 0; i < priv.job_a.length; i+= 1) {
if (priv.job_a[i].getId() === id) {
for (i = 0; i < priv.job_array.length; i+= 1) {
if (priv.job_array[i].getId() === id) {
return true;
}
}
......@@ -1265,21 +1265,21 @@ var jobManager = (function(spec, my) {
* @param {object} job The new job.
*/
that.addJob = function(job) {
var result_a = that.validateJobAccordingToJobList (priv.job_a,job);
var result_a = that.validateJobAccordingToJobList (priv.job_array,job);
priv.appendJob (job,result_a);
};
/**
* Generate a result array containing action string to do with the good job.
* @method validateJobAccordingToJobList
* @param {array} job_a A job array.
* @param {array} job_array A job array.
* @param {object} job The new job to compare with.
* @return {array} A result array.
*/
that.validateJobAccordingToJobList = function(job_a,job) {
that.validateJobAccordingToJobList = function(job_array,job) {
var i, result_a = [];
for (i = 0; i < job_a.length; i+= 1) {
result_a.push(jobRules.validateJobAccordingToJob (job_a[i],job));
for (i = 0; i < job_array.length; i+= 1) {
result_a.push(jobRules.validateJobAccordingToJob (job_array[i],job));
}
return result_a;
};
......@@ -1295,7 +1295,7 @@ var jobManager = (function(spec, my) {
*/
priv.appendJob = function(job,result_a) {
var i;
if (priv.job_a.length !== result_a.length) {
if (priv.job_array.length !== result_a.length) {
throw new RangeError("Array out of bound");
}
for (i = 0; i < result_a.length; i+= 1) {
......@@ -1318,10 +1318,18 @@ var jobManager = (function(spec, my) {
default: break;
}
}
priv.job_a.push(job);
priv.job_array.push(job);
priv.copyJobArrayToLocal();
};
that.serialized = function () {
var a = [], i, job_array = priv.job_array || [];
for (i = 0; i < job_array.length; i+= 1) {
a.push(job_array[i].serialized());
}
return a;
};
return that;
}());
......@@ -1590,6 +1598,10 @@ var jobRules = (function(spec, my) {
return jioNamespace.storage(description, my).isValid();
};
that.getJobArray = function () {
return jobManager.serialized();
};
/**
* Save a document.
* @method saveDocument
......
/*! JIO - v0.1.0 - 2012-06-15
* Copyright (c) 2012 Nexedi; Licensed */
var jio=function(){var a=function(a,b){var c={};return a=a||{},b=b||{},c.name="jioException",c.message=a.message||"Unknown Reason.",c.toString=function(){return c.name+": "+c.message},c},b=function(b,c){var d=a(b,c);b=b||{};var e=b.command;return d.name="invalidCommandState",d.toString=function(){return d.name+": "+e.getLabel()+", "+d.message},d},c=function(b,c){var d=a(b,c);b=b||{};var e=b.storage.getType();return d.name="invalidStorage",d.toString=function(){return d.name+": "+'Type "'+e+'", '+d.message},d},d=function(b,c){var d=a(b,c),e=b.type;return d.name="invalidStorageType",d.toString=function(){return d.name+": "+e+", "+d.message},d},e=function(b,c){var d=a(b,c);return d.name="jobNotReadyException",d},f=function(b,c){var d=a(b,c);return d.name="tooMuchTriesJobException",d},g=function(b,c){var d=a(b,c);return d.name="invalidJobException",d},h=function(a,b){var d={};a=a||{},b=b||{};var e={};return e.type=a.type||"",d.getType=function(){return e.type},d.setType=function(a){e.type=a},d.execute=function(a){d.validate(a),d.done=a.done,d.fail=a.fail,a.executeOn(d)},d.isValid=function(){return!0},d.validate=function(a){var b=d.validateState();if(b)throw c({storage:d,message:b});a.validate(d)},d.serialized=function(){return{type:d.getType()}},d.saveDocument=function(a){throw c({storage:d,message:"Unknown storage."})},d.loadDocument=function(a){d.saveDocument()},d.removeDocument=function(a){d.saveDocument()},d.getDocumentList=function(a){d.saveDocument()},d.validateState=function(){return""},d.done=function(){},d.fail=function(){},d},i=function(a,b){a=a||{},b=b||{};var c=h(a,b);return c.newCommand=function(a,c){var d=c||{};return d.label=a,j(d,b)},c.newStorage=function(a){var c=a||{};return x.storage(c,b)},c.addJob=function(a,c){b.jobManager.addJob(u({storage:a,command:c},b))},c},j=function(a,c){var d={};a=a||{},c=c||{};var e={};return e.commandlist={saveDocument:n,loadDocument:l,removeDocument:m,getDocumentList:k},a.label&&e.commandlist[a.label]?(e.label=a.label,delete a.label,e.commandlist[e.label](a,c)):(e.path=a.path||"",e.tried=0,e.option=a.option||{},e.respond=e.option.onResponse||function(){},e.done=e.option.onDone||function(){},e.fail=e.option.onFail||function(){},e.retry=function(){d.setMaxRetry(-1),d.fail({status:0,statusText:"Fail Retry",message:"Impossible to retry."})},e.end=function(){},d.getLabel=function(){return"command"},d.getPath=function(){return e.path},d.getOption=function(a){return e.option[a]},d.validate=function(a){d.validateState()},d.getTried=function(){return e.tried},d.setMaxRetry=function(a){e.option.max_retry=a},d.execute=function(a){d.validate(a),e.tried++,a.execute(d)},d.executeOn=function(a){},d.validateState=function(){if(e.path==="")throw b({command:d,message:"Path is empty"})},d.done=function(a){e.respond({status:p(),value:a}),e.done(a),e.end()},d.fail=function(a){e.option.max_retry===0||e.tried<e.option.max_retry?e.retry():(e.respond({status:q(),error:a}),e.fail(a),e.end())},d.onResponseDo=function(a){if(a)e.respond=a;else return e.respond},d.onDoneDo=function(a){if(a)e.done=a;else return e.done},d.onFailDo=function(a){if(a)e.fail=a;else return e.fail},d.onEndDo=function(a){e.end=a},d.onRetryDo=function(a){e.retry=a},d.serialized=function(){return{label:d.getLabel(),tried:e.tried,max_retry:e.max_retry,path:e.path,option:d.cloneOption()}},d.canBeRestored=function(){return!0},d.clone=function(){return j(d.serialized(),c)},d.cloneOption=function(){var a,b={};for(a in e.option)b[a]=e.option[a];return b},d)},k=function(a,b){var c=j(a,b);a=a||{},b=b||{},c.getLabel=function(){return"getDocumentList"},c.executeOn=function(a){a.getDocumentList(c)},c.canBeRestored=function(){return!1};var d=c.done;return c.done=function(a){var b;if(a)for(b=0;b<a.length;b+=1)typeof a[b].last_modified!="number"&&(a[b].last_modified=(new Date(a[b].last_modified)).getTime()),typeof a[b].creation_date!="number"&&(a[b].creation_date=(new Date(a[b].creation_date)).getTime());d(a)},c},l=function(a,b){var c=j(a,b);a=a||{},b=b||{},c.getLabel=function(){return"loadDocument"},c.executeOn=function(a){a.loadDocument(c)},c.canBeRestored=function(){return!1};var d=c.done;return c.done=function(a){a&&(typeof a.last_modified!="number"&&(a.last_modified=(new Date(a.last_modified)).getTime()),typeof a.creation_date!="number"&&(a.creation_date=(new Date(a.creation_date)).getTime())),d(a)},c},m=function(a,b){var c=j(a,b);return a=a||{},b=b||{},c.getLabel=function(){return"removeDocument"},c.executeOn=function(a){a.removeDocument(c)},c},n=function(a,c){var d=j(a,c);a=a||{},c=c||{};var e={};e.content=a.content,d.getLabel=function(){return"saveDocument"},d.getContent=function(){return e.content};var f=d.validate;d.validate=function(a){if(typeof e.content!="string")throw b({command:d,message:"No data to save"});f(a)},d.executeOn=function(a){a.saveDocument(d)};var g=d.serialized;return d.serialized=function(){var a=g();return a.content=e.content,a},d},o=function(a,b){var c={};return a=a||{},b=b||{},c.getLabel=function(){return"job status"},c.canStart=function(){},c.canRestart=function(){},c.serialized=function(){return{label:c.getLabel()}},c.isWaitStatus=function(){return!1},c.isDone=function(){return!1},c},p=function(a,b){var c=o(a,b);return a=a||{},b=b||{},c.getLabel=function(){return"done"},c.canStart=function(){return!1},c.canRestart=function(){return!1},c.isDone=function(){return!0},c},q=function(a,b){var c=o(a,b);return a=a||{},b=b||{},c.getLabel=function(){return"fail"},c.canStart=function(){return!1},c.canRestart=function(){return!0},c},r=function(a,b){var c=o(a,b);return a=a||{},b=b||{},c.getLabel=function(){return"initial"},c.canStart=function(){return!0},c.canRestart=function(){return!0},c},s=function(a,b){var c=o(a,b);return a=a||{},b=b||{},c.getLabel=function(){return"on going"},c.canStart=function(){return!1},c.canRestart=function(){return!1},c},t=function(a,b){var c=o(a,b);a=a||{},b=b||{};var d={};return d.job_id_a=a.job_id_array||[],d.threshold=0,c.getLabel=function(){return"wait"},d.refreshJobIdArray=function(){var a=[],c;for(c=0;c<d.job_id_a.length;c+=1)b.jobManager.jobIdExists(d.job_id_a[c])&&a.push(d.job_id_a[c]);d.job_id_a=a},c.waitForJob=function(a){var b;for(b=0;b<d.job_id_a.length;b+=1)if(d.job_id_a[b]===a.getId())return;d.job_id_a.push(a.getId())},c.dontWaitForJob=function(a){var b,c=[];for(b=0;b<d.job_id_a.length;b+=1)d.job_id_a[b]!==a.getId()&&c.push(d.job_id_a[b]);d.job_id_a=c},c.waitForTime=function(a){d.threshold=Date.now()+a},c.stopWaitForTime=function(){d.threshold=0},c.canStart=function(){return d.refreshJobIdArray(),d.job_id_a.length===0&&Date.now()>=d.threshold},c.canRestart=function(){return c.canStart()},c.serialized=function(){return{label:c.getLabel(),waitfortime:d.threshold,waitforjob:d.job_id_a}},c.isWaitStatus=function(){return!0},c},u=function(a,b){var c={};a=a||{},b=b||{};var d={};return d.id=b.jobIdHandler.nextId(),d.command=a.command,d.storage=a.storage,d.status=r(),d.date=new Date,function(){if(!d.storage)throw g({job:c,message:"No storage set"});if(!d.command)throw g({job:c,message:"No command set"})}(),c.getCommand=function(){return d.command},c.getStatus=function(){return d.status},c.getId=function(){return d.id},c.getStorage=function(){return d.storage},c.getDate=function(){return d.date},c.isReady=function(){return d.tried===0?d.status.canStart():d.status.canRestart()},c.serialized=function(){return{id:d.id,date:d.date.getTime(),status:d.status.serialized(),command:d.command.serialized(),storage:d.storage.serialized()}},c.waitForJob=function(a){d.status.getLabel()!=="wait"&&(d.status=t({},b)),d.status.waitForJob(a)},c.dontWaitFor=function(a){d.status.getLabel()==="wait"&&d.status.dontWaitForJob(a)},c.waitForTime=function(a){d.status.getLabel()!=="wait"&&(d.status=t({},b)),d.status.waitForTime(a)},c.stopWaitForTime=function(){d.status.getLabel()==="wait"&&d.status.stopWaitForTime()},c.update=function(a){d.command.setMaxRetry(-1),d.command.fail({status:0,statusText:"Replaced",message:"Job has been replaced by another one."}),d.date=a.getDate(),d.command=a.getCommand(),d.status=a.getStatus()},c.execute=function(){if(d.max_retry!==0&&d.tried>=d.max_retry)throw f({job:c,message:"The job was invoked too much time."});if(!c.isReady())throw e({message:"Can not execute this job."});d.status=s(),d.command.onRetryDo(function(){var a=d.command.getTried();a=a*a*200,a>1e4&&(a=1e4),c.waitForTime(a)}),d.command.onEndDo(function(){b.jobManager.terminateJob(c)}),d.command.execute(d.storage)},c},v=function(a,b){var c={};a=a||{},b=b||{};var d=[],e=a.name||"",f=a.announcer||{};return c.add=function(a){d.push(a)},c.remove=function(a){var b,c=[];for(b=0;b<d.length;b+=1)d[b]!==a&&c.push(d[b]);d=c},c.register=function(){f.register(c)},c.unregister=function(){f.unregister(c)},c.trigger=function(a){var b;for(b=0;b<d.length;b++)d[b].apply(null,a)},c},w=function(a,b){var c=function(a,b){var c={};a=a||{},b=b||{};var d={};return d.id=a.id||0,d.interval=400,d.interval_id=null,d.touch=function(){LocalOrCookieStorage.setItem("jio/id/"+d.id,Date.now())},c.setId=function(a){d.id=a},c.setIntervalDelay=function(a){d.interval=a},c.getIntervalDelay=function(){return d.interval},c.start=function(){d.interval_id||(d.touch(),d.interval_id=setInterval(function(){d.touch()},d.interval))},c.stop=function(){d.interval_id!==null&&(clearInterval(d.interval_id),d.interval_id=null)},c}(),d=function(a,b){var c={};a=a||{},b=b||{};var d={};return c.register=function(a){d[a]||(d[a]=v())},c.unregister=function(a){d[a]&&delete d[a]},c.at=function(a){return d[a]},c.on=function(a,b){c.register(a),c.at(a).add(b)},c.trigger=function(a,b){c.at(a).trigger(b)},c}(),e=function(a,b){var c={};a=a||{},b=b||{};var d=0;return c.nextId=function(){return d=d+1,d},c}(),f=function(a,b){var c={};a=a||{},b=b||{};var d="jio/job_array",f={};return f.id=a.id,f.interval_id=null,f.interval=200,f.job_a=[],b.jobManager=c,b.jobIdHandler=e,f.getJobArrayName=function(){return d+"/"+f.id},f.getJobArray=function(){return LocalOrCookieStorage.getItem(f.getJobArrayName())||[]},f.copyJobArrayToLocal=function(){var a=[],b;for(b=0;b<f.job_a.length;b+=1)a.push(f.job_a[b].serialized());LocalOrCookieStorage.setItem(f.getJobArrayName(),a)},f.removeJob=function(a){var b,c=[];for(b=0;b<f.job_a.length;b+=1)f.job_a[b]!==a&&c.push(f.job_a[b]);f.job_a=c,f.copyJobArrayToLocal()},c.setId=function(a){f.id=a},c.start=function(){var a;f.interval_id===null&&(f.interval_id=setInterval(function(){f.restoreOldJio();for(a=0;a<f.job_a.length;a+=1)c.execute(f.job_a[a])},f.interval))},c.stop=function(){f.interval_id!==null&&(clearInterval(f.interval_id),f.interval_id=null,f.job_a.length===0&&LocalOrCookieStorage.deleteItem(f.getJobArrayName()))},f.restoreOldJio=function(){var a,b;f.lastrestore=f.lastrestore||0;if(f.lastrestore>Date.now()-2e3)return;b=LocalOrCookieStorage.getItem("jio/id_array")||[];for(a=0;a<b.length;a+=1)f.restoreOldJioId(b[a]);f.lastrestore=Date.now()},f.restoreOldJioId=function(a){var b;b=LocalOrCookieStorage.getItem("jio/id/"+a)||0,b<Date.now()-1e4&&(f.restoreOldJobFromJioId(a),f.removeOldJioId(a),f.removeJobArrayFromJioId(a))},f.restoreOldJobFromJioId=function(a){var d,e;e=LocalOrCookieStorage.getItem("jio/job_array/"+a)||[];for(d=0;d<e.length;d+=1){var f=j(e[d].command,b);f.canBeRestored()&&c.addJob(u({storage:x.storage(e[d].storage,b),command:f},b))}},f.removeOldJioId=function(a){var b,c,d=[];c=LocalOrCookieStorage.getItem("jio/id_array")||[];for(b=0;b<c.length;b+=1)c[b]!==a&&d.push(c[b]);LocalOrCookieStorage.setItem("jio/id_array",d),LocalOrCookieStorage.deleteItem("jio/id/"+a)},f.removeJobArrayFromJioId=function(a){LocalOrCookieStorage.deleteItem("jio/job_array/"+a)},c.execute=function(a){try{a.execute()}catch(b){switch(b.name){case"jobNotReadyException":break;case"tooMuchTriesJobException":break;default:throw b}}f.copyJobArrayToLocal()},c.jobIdExists=function(a){var b;for(b=0;b<f.job_a.length;b+=1)if(f.job_a[b].getId()===a)return!0;return!1},c.terminateJob=function(a){f.removeJob(a)},c.addJob=function(a){var b=c.validateJobAccordingToJobList(f.job_a,a);f.appendJob(a,b)},c.validateJobAccordingToJobList=function(a,b){var c,d=[];for(c=0;c<a.length;c+=1)d.push(g.validateJobAccordingToJob(a[c],b));return d},f.appendJob=function(a,b){var c;if(f.job_a.length!==b.length)throw new RangeError("Array out of bound");for(c=0;c<b.length;c+=1)if(b[c].action==="dont accept")return;for(c=0;c<b.length;c+=1)switch(b[c].action){case"eliminate":f.removeJob(b[c].job);break;case"update":b[c].job.update(a),f.copyJobArrayToLocal();return;case"wait":a.waitForJob(b[c].job);break;default:}f.job_a.push(a),f.copyJobArrayToLocal()},c}(),g=function(a,b){var c={},d={};return d.compare={},d.action={},c.eliminate=function(){return"eliminate"},c.update=function(){return"update"},c.dontAccept=function(){return"dont accept"},c.wait=function(){return"wait"},c.none=function(){return"none"},c.default_action=c.none,c.default_compare=function(a,b){return a.getCommand().getPath()===b.getCommand().getPath()&&JSON.stringify(a.getStorage().serialized())===JSON.stringify(b.getStorage().serialized())},d.getAction=function(a,b){var e,f,g;return e=a.getCommand().getLabel(),f=b.getCommand().getLabel(),g=a.getStatus().getLabel()==="on going"?"on going":"not on going",d.action[e]&&d.action[e][g]&&d.action[e][g][f]?d.action[e][g][f](a,b):c.default_action(a,b)},d.canCompare=function(a,b){var e=a.getCommand().getLabel(),f=b.getCommand().getLabel();return d.compare[e]&&d.compare[f]?d.compare[e][f](a,b):c.default_compare(a,b)},c.validateJobAccordingToJob=function(a,b){return d.canCompare(a,b)?{action:d.getAction(a,b),job:a}:{action:c.default_action(a,b),job:a}},c.addActionRule=function(a,b,c,e){var f=b?"on going":"not on going";d.action[a]=d.action[a]||{},d.action[a][f]=d.action[a][f]||{},d.action[a][f][c]=e},c.addCompareRule=function(a,b,c){d.compare[a]=d.compare[a]||{},d.compare[a][b]=c},c.addActionRule("saveDocument",!0,"saveDocument",function(a,b){return a.getCommand().getContent()===b.getCommand().getContent()?c.dontAccept():c.wait()}),c.addActionRule("saveDocument",!0,"loadDocument",c.wait),c.addActionRule("saveDocument",!0,"removeDocument",c.wait),c.addActionRule("saveDocument",!1,"saveDocument",c.update),c.addActionRule("saveDocument",!1,"loadDocument",c.wait),c.addActionRule("saveDocument",!1,"removeDocument",c.eliminate),c.addActionRule("loadDocument",!0,"saveDocument",c.wait),c.addActionRule("loadDocument",!0,"loadDocument",c.dontAccept),c.addActionRule("loadDocument",!0,"removeDocument",c.wait),c.addActionRule("loadDocument",!1,"saveDocument",c.wait),c.addActionRule("loadDocument",!1,"loadDocument",c.update),c.addActionRule("loadDocument",!1,"removeDocument",c.wait),c.addActionRule("removeDocument",!0,"loadDocument",c.dontAccept),c.addActionRule("removeDocument",!0,"removeDocument",c.dontAccept),c.addActionRule("removeDocument",!1,"saveDocument",c.eliminate),c.addActionRule("removeDocument",!1,"loadDocument",c.dontAccept),c.addActionRule("removeDocument",!1,"removeDocument",c.update),c.addActionRule("getDocumentList",!0,"getDocumentList",c.dontAccept),c.addActionRule("getDocumentList",!1,"getDocumentList",c.update),c}(),h={};a=a||{},b=b||{};var i={},o="jio/id_array";return i.id=null,b.jobManager=f,b.jobIdHandler=e,i.storage=x.storage(a,b),i.init=function(){if(i.id===null){var a,b=LocalOrCookieStorage.getItem(o)||[];i.id=1;for(a=0;a<b.length;a+=1)b[a]>=i.id&&(i.id=b[a]+1);b.push(i.id),LocalOrCookieStorage.setItem(o,b),c.setId(i.id),f.setId(i.id)}},h.start=function(){i.init(),c.start(),f.start()},h.stop=function(){f.stop()},h.close=function(){c.stop(),f.stop(),i.id=null},h.start(),h.getId=function(){return i.id},h.getJobRules=function(){return g},h.validateStorageDescription=function(a){return x.storage(a,b).isValid()},h.saveDocument=function(a,c,d,e){d=d||{},d.onResponse=d.onResponse||function(){},d.onDone=d.onDone||function(){},d.onFail=d.onFail||function(){},d.max_retry=d.max_retry||0,f.addJob(u({storage:e?x.storage(e,b):i.storage,command:n({path:a,content:c,option:d},b)},b))},h.loadDocument=function(a,c,d){c=c||{},c.onResponse=c.onResponse||function(){},c.onDone=c.onDone||function(){},c.onFail=c.onFail||function(){},c.max_retry=c.max_retry||0,c.metadata_only=c.metadata_only!==undefined?c.metadata_only:!1,f.addJob(u({storage:d?x.storage(d,b):i.storage,command:l({path:a,option:c},b)},b))},h.removeDocument=function(a,c,d){c=c||{},c.onResponse=c.onResponse||function(){},c.onDone=c.onDone||function(){},c.onFail=c.onFail||function(){},c.max_retry=c.max_retry||0,f.addJob(u({storage:d?x.storage(d,b):i.storage,command:m({path:a,option:c},b)},b))},h.getDocumentList=function(a,c,d){c=c||{},c.onResponse=c.onResponse||function(){},c.onDone=c.onDone||function(){},c.onFail=c.onFail||function(){},c.max_retry=c.max_retry||0,c.metadata_only=c.metadata_only!==undefined?c.metadata_only:!0,f.addJob(u({storage:d?x.storage(d,b):i.storage,command:k({path:a,option:c},b)},b))},h},x=function(a,b){var c={};a=a||{},b=b||{};var e={base:h,handler:i};return c.storage=function(a,b,c){a=a||{},b=b||{};var f=c||a.type||"base";if(!e[f])throw d({type:f,message:"Storage does not exists."});return e[f](a,b)},c.newJio=function(a){var b=a;return typeof b=="string"&&(b=JSON.parse(b)),b=b||{type:"base"},w(a)},c.addStorageType=function(a,b){b=b||function(){return null};if(e[a])throw d({type:a,message:"Already known."});e[a]=b},c}();return x}();
\ No newline at end of file
var jio=function(){var a=function(a,b){var c={};return a=a||{},b=b||{},c.name="jioException",c.message=a.message||"Unknown Reason.",c.toString=function(){return c.name+": "+c.message},c},b=function(b,c){var d=a(b,c);b=b||{};var e=b.command;return d.name="invalidCommandState",d.toString=function(){return d.name+": "+e.getLabel()+", "+d.message},d},c=function(b,c){var d=a(b,c);b=b||{};var e=b.storage.getType();return d.name="invalidStorage",d.toString=function(){return d.name+": "+'Type "'+e+'", '+d.message},d},d=function(b,c){var d=a(b,c),e=b.type;return d.name="invalidStorageType",d.toString=function(){return d.name+": "+e+", "+d.message},d},e=function(b,c){var d=a(b,c);return d.name="jobNotReadyException",d},f=function(b,c){var d=a(b,c);return d.name="tooMuchTriesJobException",d},g=function(b,c){var d=a(b,c);return d.name="invalidJobException",d},h=function(a,b){var d={};a=a||{},b=b||{};var e={};return e.type=a.type||"",d.getType=function(){return e.type},d.setType=function(a){e.type=a},d.execute=function(a){d.validate(a),d.done=a.done,d.fail=a.fail,a.executeOn(d)},d.isValid=function(){return!0},d.validate=function(a){var b=d.validateState();if(b)throw c({storage:d,message:b});a.validate(d)},d.serialized=function(){return{type:d.getType()}},d.saveDocument=function(a){throw c({storage:d,message:"Unknown storage."})},d.loadDocument=function(a){d.saveDocument()},d.removeDocument=function(a){d.saveDocument()},d.getDocumentList=function(a){d.saveDocument()},d.validateState=function(){return""},d.done=function(){},d.fail=function(){},d},i=function(a,b){a=a||{},b=b||{};var c=h(a,b);return c.newCommand=function(a,c){var d=c||{};return d.label=a,j(d,b)},c.newStorage=function(a){var c=a||{};return x.storage(c,b)},c.addJob=function(a,c){b.jobManager.addJob(u({storage:a,command:c},b))},c},j=function(a,c){var d={};a=a||{},c=c||{};var e={};return e.commandlist={saveDocument:n,loadDocument:l,removeDocument:m,getDocumentList:k},a.label&&e.commandlist[a.label]?(e.label=a.label,delete a.label,e.commandlist[e.label](a,c)):(e.path=a.path||"",e.tried=0,e.option=a.option||{},e.respond=e.option.onResponse||function(){},e.done=e.option.onDone||function(){},e.fail=e.option.onFail||function(){},e.retry=function(){d.setMaxRetry(-1),d.fail({status:0,statusText:"Fail Retry",message:"Impossible to retry."})},e.end=function(){},d.getLabel=function(){return"command"},d.getPath=function(){return e.path},d.getOption=function(a){return e.option[a]},d.validate=function(a){d.validateState()},d.getTried=function(){return e.tried},d.setMaxRetry=function(a){e.option.max_retry=a},d.execute=function(a){d.validate(a),e.tried++,a.execute(d)},d.executeOn=function(a){},d.validateState=function(){if(e.path==="")throw b({command:d,message:"Path is empty"})},d.done=function(a){e.respond({status:p(),value:a}),e.done(a),e.end()},d.fail=function(a){e.option.max_retry===0||e.tried<e.option.max_retry?e.retry():(e.respond({status:q(),error:a}),e.fail(a),e.end())},d.onResponseDo=function(a){if(a)e.respond=a;else return e.respond},d.onDoneDo=function(a){if(a)e.done=a;else return e.done},d.onFailDo=function(a){if(a)e.fail=a;else return e.fail},d.onEndDo=function(a){e.end=a},d.onRetryDo=function(a){e.retry=a},d.serialized=function(){return{label:d.getLabel(),tried:e.tried,max_retry:e.max_retry,path:e.path,option:d.cloneOption()}},d.canBeRestored=function(){return!0},d.clone=function(){return j(d.serialized(),c)},d.cloneOption=function(){var a,b={};for(a in e.option)b[a]=e.option[a];return b},d)},k=function(a,b){var c=j(a,b);a=a||{},b=b||{},c.getLabel=function(){return"getDocumentList"},c.executeOn=function(a){a.getDocumentList(c)},c.canBeRestored=function(){return!1};var d=c.done;return c.done=function(a){var b;if(a)for(b=0;b<a.length;b+=1)typeof a[b].last_modified!="number"&&(a[b].last_modified=(new Date(a[b].last_modified)).getTime()),typeof a[b].creation_date!="number"&&(a[b].creation_date=(new Date(a[b].creation_date)).getTime());d(a)},c},l=function(a,b){var c=j(a,b);a=a||{},b=b||{},c.getLabel=function(){return"loadDocument"},c.executeOn=function(a){a.loadDocument(c)},c.canBeRestored=function(){return!1};var d=c.done;return c.done=function(a){a&&(typeof a.last_modified!="number"&&(a.last_modified=(new Date(a.last_modified)).getTime()),typeof a.creation_date!="number"&&(a.creation_date=(new Date(a.creation_date)).getTime())),d(a)},c},m=function(a,b){var c=j(a,b);return a=a||{},b=b||{},c.getLabel=function(){return"removeDocument"},c.executeOn=function(a){a.removeDocument(c)},c},n=function(a,c){var d=j(a,c);a=a||{},c=c||{};var e={};e.content=a.content,d.getLabel=function(){return"saveDocument"},d.getContent=function(){return e.content};var f=d.validate;d.validate=function(a){if(typeof e.content!="string")throw b({command:d,message:"No data to save"});f(a)},d.executeOn=function(a){a.saveDocument(d)};var g=d.serialized;return d.serialized=function(){var a=g();return a.content=e.content,a},d},o=function(a,b){var c={};return a=a||{},b=b||{},c.getLabel=function(){return"job status"},c.canStart=function(){},c.canRestart=function(){},c.serialized=function(){return{label:c.getLabel()}},c.isWaitStatus=function(){return!1},c.isDone=function(){return!1},c},p=function(a,b){var c=o(a,b);return a=a||{},b=b||{},c.getLabel=function(){return"done"},c.canStart=function(){return!1},c.canRestart=function(){return!1},c.isDone=function(){return!0},c},q=function(a,b){var c=o(a,b);return a=a||{},b=b||{},c.getLabel=function(){return"fail"},c.canStart=function(){return!1},c.canRestart=function(){return!0},c},r=function(a,b){var c=o(a,b);return a=a||{},b=b||{},c.getLabel=function(){return"initial"},c.canStart=function(){return!0},c.canRestart=function(){return!0},c},s=function(a,b){var c=o(a,b);return a=a||{},b=b||{},c.getLabel=function(){return"on going"},c.canStart=function(){return!1},c.canRestart=function(){return!1},c},t=function(a,b){var c=o(a,b);a=a||{},b=b||{};var d={};return d.job_id_a=a.job_id_array||[],d.threshold=0,c.getLabel=function(){return"wait"},d.refreshJobIdArray=function(){var a=[],c;for(c=0;c<d.job_id_a.length;c+=1)b.jobManager.jobIdExists(d.job_id_a[c])&&a.push(d.job_id_a[c]);d.job_id_a=a},c.waitForJob=function(a){var b;for(b=0;b<d.job_id_a.length;b+=1)if(d.job_id_a[b]===a.getId())return;d.job_id_a.push(a.getId())},c.dontWaitForJob=function(a){var b,c=[];for(b=0;b<d.job_id_a.length;b+=1)d.job_id_a[b]!==a.getId()&&c.push(d.job_id_a[b]);d.job_id_a=c},c.waitForTime=function(a){d.threshold=Date.now()+a},c.stopWaitForTime=function(){d.threshold=0},c.canStart=function(){return d.refreshJobIdArray(),d.job_id_a.length===0&&Date.now()>=d.threshold},c.canRestart=function(){return c.canStart()},c.serialized=function(){return{label:c.getLabel(),waitfortime:d.threshold,waitforjob:d.job_id_a}},c.isWaitStatus=function(){return!0},c},u=function(a,b){var c={};a=a||{},b=b||{};var d={};return d.id=b.jobIdHandler.nextId(),d.command=a.command,d.storage=a.storage,d.status=r(),d.date=new Date,function(){if(!d.storage)throw g({job:c,message:"No storage set"});if(!d.command)throw g({job:c,message:"No command set"})}(),c.getCommand=function(){return d.command},c.getStatus=function(){return d.status},c.getId=function(){return d.id},c.getStorage=function(){return d.storage},c.getDate=function(){return d.date},c.isReady=function(){return d.tried===0?d.status.canStart():d.status.canRestart()},c.serialized=function(){return{id:d.id,date:d.date.getTime(),status:d.status.serialized(),command:d.command.serialized(),storage:d.storage.serialized()}},c.waitForJob=function(a){d.status.getLabel()!=="wait"&&(d.status=t({},b)),d.status.waitForJob(a)},c.dontWaitFor=function(a){d.status.getLabel()==="wait"&&d.status.dontWaitForJob(a)},c.waitForTime=function(a){d.status.getLabel()!=="wait"&&(d.status=t({},b)),d.status.waitForTime(a)},c.stopWaitForTime=function(){d.status.getLabel()==="wait"&&d.status.stopWaitForTime()},c.update=function(a){d.command.setMaxRetry(-1),d.command.fail({status:0,statusText:"Replaced",message:"Job has been replaced by another one."}),d.date=a.getDate(),d.command=a.getCommand(),d.status=a.getStatus()},c.execute=function(){if(d.max_retry!==0&&d.tried>=d.max_retry)throw f({job:c,message:"The job was invoked too much time."});if(!c.isReady())throw e({message:"Can not execute this job."});d.status=s(),d.command.onRetryDo(function(){var a=d.command.getTried();a=a*a*200,a>1e4&&(a=1e4),c.waitForTime(a)}),d.command.onEndDo(function(){b.jobManager.terminateJob(c)}),d.command.execute(d.storage)},c},v=function(a,b){var c={};a=a||{},b=b||{};var d=[],e=a.name||"",f=a.announcer||{};return c.add=function(a){d.push(a)},c.remove=function(a){var b,c=[];for(b=0;b<d.length;b+=1)d[b]!==a&&c.push(d[b]);d=c},c.register=function(){f.register(c)},c.unregister=function(){f.unregister(c)},c.trigger=function(a){var b;for(b=0;b<d.length;b++)d[b].apply(null,a)},c},w=function(a,b){var c=function(a,b){var c={};a=a||{},b=b||{};var d={};return d.id=a.id||0,d.interval=400,d.interval_id=null,d.touch=function(){LocalOrCookieStorage.setItem("jio/id/"+d.id,Date.now())},c.setId=function(a){d.id=a},c.setIntervalDelay=function(a){d.interval=a},c.getIntervalDelay=function(){return d.interval},c.start=function(){d.interval_id||(d.touch(),d.interval_id=setInterval(function(){d.touch()},d.interval))},c.stop=function(){d.interval_id!==null&&(clearInterval(d.interval_id),d.interval_id=null)},c}(),d=function(a,b){var c={};a=a||{},b=b||{};var d={};return c.register=function(a){d[a]||(d[a]=v())},c.unregister=function(a){d[a]&&delete d[a]},c.at=function(a){return d[a]},c.on=function(a,b){c.register(a),c.at(a).add(b)},c.trigger=function(a,b){c.at(a).trigger(b)},c}(),e=function(a,b){var c={};a=a||{},b=b||{};var d=0;return c.nextId=function(){return d=d+1,d},c}(),f=function(a,b){var c={};a=a||{},b=b||{};var d="jio/job_array",f={};return f.id=a.id,f.interval_id=null,f.interval=200,f.job_array=[],b.jobManager=c,b.jobIdHandler=e,f.getJobArrayName=function(){return d+"/"+f.id},f.getJobArray=function(){return LocalOrCookieStorage.getItem(f.getJobArrayName())||[]},f.copyJobArrayToLocal=function(){var a=[],b;for(b=0;b<f.job_array.length;b+=1)a.push(f.job_array[b].serialized());LocalOrCookieStorage.setItem(f.getJobArrayName(),a)},f.removeJob=function(a){var b,c=[];for(b=0;b<f.job_array.length;b+=1)f.job_array[b]!==a&&c.push(f.job_array[b]);f.job_array=c,f.copyJobArrayToLocal()},c.setId=function(a){f.id=a},c.start=function(){var a;f.interval_id===null&&(f.interval_id=setInterval(function(){f.restoreOldJio();for(a=0;a<f.job_array.length;a+=1)c.execute(f.job_array[a])},f.interval))},c.stop=function(){f.interval_id!==null&&(clearInterval(f.interval_id),f.interval_id=null,f.job_array.length===0&&LocalOrCookieStorage.deleteItem(f.getJobArrayName()))},f.restoreOldJio=function(){var a,b;f.lastrestore=f.lastrestore||0;if(f.lastrestore>Date.now()-2e3)return;b=LocalOrCookieStorage.getItem("jio/id_array")||[];for(a=0;a<b.length;a+=1)f.restoreOldJioId(b[a]);f.lastrestore=Date.now()},f.restoreOldJioId=function(a){var b;b=LocalOrCookieStorage.getItem("jio/id/"+a)||0,b<Date.now()-1e4&&(f.restoreOldJobFromJioId(a),f.removeOldJioId(a),f.removeJobArrayFromJioId(a))},f.restoreOldJobFromJioId=function(a){var d,e;e=LocalOrCookieStorage.getItem("jio/job_array/"+a)||[];for(d=0;d<e.length;d+=1){var f=j(e[d].command,b);f.canBeRestored()&&c.addJob(u({storage:x.storage(e[d].storage,b),command:f},b))}},f.removeOldJioId=function(a){var b,c,d=[];c=LocalOrCookieStorage.getItem("jio/id_array")||[];for(b=0;b<c.length;b+=1)c[b]!==a&&d.push(c[b]);LocalOrCookieStorage.setItem("jio/id_array",d),LocalOrCookieStorage.deleteItem("jio/id/"+a)},f.removeJobArrayFromJioId=function(a){LocalOrCookieStorage.deleteItem("jio/job_array/"+a)},c.execute=function(a){try{a.execute()}catch(b){switch(b.name){case"jobNotReadyException":break;case"tooMuchTriesJobException":break;default:throw b}}f.copyJobArrayToLocal()},c.jobIdExists=function(a){var b;for(b=0;b<f.job_array.length;b+=1)if(f.job_array[b].getId()===a)return!0;return!1},c.terminateJob=function(a){f.removeJob(a)},c.addJob=function(a){var b=c.validateJobAccordingToJobList(f.job_array,a);f.appendJob(a,b)},c.validateJobAccordingToJobList=function(a,b){var c,d=[];for(c=0;c<a.length;c+=1)d.push(g.validateJobAccordingToJob(a[c],b));return d},f.appendJob=function(a,b){var c;if(f.job_array.length!==b.length)throw new RangeError("Array out of bound");for(c=0;c<b.length;c+=1)if(b[c].action==="dont accept")return;for(c=0;c<b.length;c+=1)switch(b[c].action){case"eliminate":f.removeJob(b[c].job);break;case"update":b[c].job.update(a),f.copyJobArrayToLocal();return;case"wait":a.waitForJob(b[c].job);break;default:}f.job_array.push(a),f.copyJobArrayToLocal()},c.serialized=function(){var a=[],b,c=f.job_array||[];for(b=0;b<c.length;b+=1)a.push(c[b].serialized());return a},c}(),g=function(a,b){var c={},d={};return d.compare={},d.action={},c.eliminate=function(){return"eliminate"},c.update=function(){return"update"},c.dontAccept=function(){return"dont accept"},c.wait=function(){return"wait"},c.none=function(){return"none"},c.default_action=c.none,c.default_compare=function(a,b){return a.getCommand().getPath()===b.getCommand().getPath()&&JSON.stringify(a.getStorage().serialized())===JSON.stringify(b.getStorage().serialized())},d.getAction=function(a,b){var e,f,g;return e=a.getCommand().getLabel(),f=b.getCommand().getLabel(),g=a.getStatus().getLabel()==="on going"?"on going":"not on going",d.action[e]&&d.action[e][g]&&d.action[e][g][f]?d.action[e][g][f](a,b):c.default_action(a,b)},d.canCompare=function(a,b){var e=a.getCommand().getLabel(),f=b.getCommand().getLabel();return d.compare[e]&&d.compare[f]?d.compare[e][f](a,b):c.default_compare(a,b)},c.validateJobAccordingToJob=function(a,b){return d.canCompare(a,b)?{action:d.getAction(a,b),job:a}:{action:c.default_action(a,b),job:a}},c.addActionRule=function(a,b,c,e){var f=b?"on going":"not on going";d.action[a]=d.action[a]||{},d.action[a][f]=d.action[a][f]||{},d.action[a][f][c]=e},c.addCompareRule=function(a,b,c){d.compare[a]=d.compare[a]||{},d.compare[a][b]=c},c.addActionRule("saveDocument",!0,"saveDocument",function(a,b){return a.getCommand().getContent()===b.getCommand().getContent()?c.dontAccept():c.wait()}),c.addActionRule("saveDocument",!0,"loadDocument",c.wait),c.addActionRule("saveDocument",!0,"removeDocument",c.wait),c.addActionRule("saveDocument",!1,"saveDocument",c.update),c.addActionRule("saveDocument",!1,"loadDocument",c.wait),c.addActionRule("saveDocument",!1,"removeDocument",c.eliminate),c.addActionRule("loadDocument",!0,"saveDocument",c.wait),c.addActionRule("loadDocument",!0,"loadDocument",c.dontAccept),c.addActionRule("loadDocument",!0,"removeDocument",c.wait),c.addActionRule("loadDocument",!1,"saveDocument",c.wait),c.addActionRule("loadDocument",!1,"loadDocument",c.update),c.addActionRule("loadDocument",!1,"removeDocument",c.wait),c.addActionRule("removeDocument",!0,"loadDocument",c.dontAccept),c.addActionRule("removeDocument",!0,"removeDocument",c.dontAccept),c.addActionRule("removeDocument",!1,"saveDocument",c.eliminate),c.addActionRule("removeDocument",!1,"loadDocument",c.dontAccept),c.addActionRule("removeDocument",!1,"removeDocument",c.update),c.addActionRule("getDocumentList",!0,"getDocumentList",c.dontAccept),c.addActionRule("getDocumentList",!1,"getDocumentList",c.update),c}(),h={};a=a||{},b=b||{};var i={},o="jio/id_array";return i.id=null,b.jobManager=f,b.jobIdHandler=e,i.storage=x.storage(a,b),i.init=function(){if(i.id===null){var a,b=LocalOrCookieStorage.getItem(o)||[];i.id=1;for(a=0;a<b.length;a+=1)b[a]>=i.id&&(i.id=b[a]+1);b.push(i.id),LocalOrCookieStorage.setItem(o,b),c.setId(i.id),f.setId(i.id)}},h.start=function(){i.init(),c.start(),f.start()},h.stop=function(){f.stop()},h.close=function(){c.stop(),f.stop(),i.id=null},h.start(),h.getId=function(){return i.id},h.getJobRules=function(){return g},h.validateStorageDescription=function(a){return x.storage(a,b).isValid()},h.getJobArray=function(){return f.serialized()},h.saveDocument=function(a,c,d,e){d=d||{},d.onResponse=d.onResponse||function(){},d.onDone=d.onDone||function(){},d.onFail=d.onFail||function(){},d.max_retry=d.max_retry||0,f.addJob(u({storage:e?x.storage(e,b):i.storage,command:n({path:a,content:c,option:d},b)},b))},h.loadDocument=function(a,c,d){c=c||{},c.onResponse=c.onResponse||function(){},c.onDone=c.onDone||function(){},c.onFail=c.onFail||function(){},c.max_retry=c.max_retry||0,c.metadata_only=c.metadata_only!==undefined?c.metadata_only:!1,f.addJob(u({storage:d?x.storage(d,b):i.storage,command:l({path:a,option:c},b)},b))},h.removeDocument=function(a,c,d){c=c||{},c.onResponse=c.onResponse||function(){},c.onDone=c.onDone||function(){},c.onFail=c.onFail||function(){},c.max_retry=c.max_retry||0,f.addJob(u({storage:d?x.storage(d,b):i.storage,command:m({path:a,option:c},b)},b))},h.getDocumentList=function(a,c,d){c=c||{},c.onResponse=c.onResponse||function(){},c.onDone=c.onDone||function(){},c.onFail=c.onFail||function(){},c.max_retry=c.max_retry||0,c.metadata_only=c.metadata_only!==undefined?c.metadata_only:!0,f.addJob(u({storage:d?x.storage(d,b):i.storage,command:k({path:a,option:c},b)},b))},h},x=function(a,b){var c={};a=a||{},b=b||{};var e={base:h,handler:i};return c.storage=function(a,b,c){a=a||{},b=b||{};var f=c||a.type||"base";if(!e[f])throw d({type:f,message:"Storage does not exists."});return e[f](a,b)},c.newJio=function(a){var b=a;return typeof b=="string"&&(b=JSON.parse(b)),b=b||{type:"base"},w(a)},c.addStorageType=function(a,b){b=b||function(){return null};if(e[a])throw d({type:a,message:"Already known."});e[a]=b},c}();return x}();
\ No newline at end of file
......@@ -74,6 +74,10 @@
return jioNamespace.storage(description, my).isValid();
};
that.getJobArray = function () {
return jobManager.serialized();
};
/**
* Save a document.
* @method saveDocument
......
......@@ -8,7 +8,7 @@ var jobManager = (function(spec, my) {
priv.id = spec.id;
priv.interval_id = null;
priv.interval = 200;
priv.job_a = [];
priv.job_array = [];
my.jobManager = that;
my.jobIdHandler = jobIdHandler;
......@@ -38,8 +38,8 @@ var jobManager = (function(spec, my) {
*/
priv.copyJobArrayToLocal = function() {
var new_a = [], i;
for (i = 0; i < priv.job_a.length; i+= 1) {
new_a.push(priv.job_a[i].serialized());
for (i = 0; i < priv.job_array.length; i+= 1) {
new_a.push(priv.job_array[i].serialized());
}
LocalOrCookieStorage.setItem(priv.getJobArrayName(),new_a);
};
......@@ -50,13 +50,13 @@ var jobManager = (function(spec, my) {
* @param {object} job The job object.
*/
priv.removeJob = function(job) {
var i, tmp_job_a = [];
for (i = 0; i < priv.job_a.length; i+= 1) {
if (priv.job_a[i] !== job) {
tmp_job_a.push(priv.job_a[i]);
var i, tmp_job_array = [];
for (i = 0; i < priv.job_array.length; i+= 1) {
if (priv.job_array[i] !== job) {
tmp_job_array.push(priv.job_array[i]);
}
}
priv.job_a = tmp_job_a;
priv.job_array = tmp_job_array;
priv.copyJobArrayToLocal();
};
......@@ -78,8 +78,8 @@ var jobManager = (function(spec, my) {
if (priv.interval_id === null) {
priv.interval_id = setInterval (function() {
priv.restoreOldJio();
for (i = 0; i < priv.job_a.length; i+= 1) {
that.execute(priv.job_a[i]);
for (i = 0; i < priv.job_array.length; i+= 1) {
that.execute(priv.job_array[i]);
}
},priv.interval);
}
......@@ -93,7 +93,7 @@ var jobManager = (function(spec, my) {
if (priv.interval_id !== null) {
clearInterval(priv.interval_id);
priv.interval_id = null;
if (priv.job_a.length === 0) {
if (priv.job_array.length === 0) {
LocalOrCookieStorage.deleteItem(priv.getJobArrayName());
}
}
......@@ -201,8 +201,8 @@ var jobManager = (function(spec, my) {
*/
that.jobIdExists = function(id) {
var i;
for (i = 0; i < priv.job_a.length; i+= 1) {
if (priv.job_a[i].getId() === id) {
for (i = 0; i < priv.job_array.length; i+= 1) {
if (priv.job_array[i].getId() === id) {
return true;
}
}
......@@ -224,21 +224,21 @@ var jobManager = (function(spec, my) {
* @param {object} job The new job.
*/
that.addJob = function(job) {
var result_a = that.validateJobAccordingToJobList (priv.job_a,job);
var result_a = that.validateJobAccordingToJobList (priv.job_array,job);
priv.appendJob (job,result_a);
};
/**
* Generate a result array containing action string to do with the good job.
* @method validateJobAccordingToJobList
* @param {array} job_a A job array.
* @param {array} job_array A job array.
* @param {object} job The new job to compare with.
* @return {array} A result array.
*/
that.validateJobAccordingToJobList = function(job_a,job) {
that.validateJobAccordingToJobList = function(job_array,job) {
var i, result_a = [];
for (i = 0; i < job_a.length; i+= 1) {
result_a.push(jobRules.validateJobAccordingToJob (job_a[i],job));
for (i = 0; i < job_array.length; i+= 1) {
result_a.push(jobRules.validateJobAccordingToJob (job_array[i],job));
}
return result_a;
};
......@@ -254,7 +254,7 @@ var jobManager = (function(spec, my) {
*/
priv.appendJob = function(job,result_a) {
var i;
if (priv.job_a.length !== result_a.length) {
if (priv.job_array.length !== result_a.length) {
throw new RangeError("Array out of bound");
}
for (i = 0; i < result_a.length; i+= 1) {
......@@ -277,9 +277,17 @@ var jobManager = (function(spec, my) {
default: break;
}
}
priv.job_a.push(job);
priv.job_array.push(job);
priv.copyJobArrayToLocal();
};
that.serialized = function () {
var a = [], i, job_array = priv.job_array || [];
for (i = 0; i < job_array.length; i+= 1) {
a.push(job_array[i].serialized());
}
return a;
};
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