Commit dfebe7c8 authored by Tristan Cavelier's avatar Tristan Cavelier

Fix bug: storages can now use jobManager correctly

parent 8b66f91e
...@@ -18,8 +18,8 @@ module.exports = function(grunt) { ...@@ -18,8 +18,8 @@ module.exports = function(grunt) {
'<file_strip_banner:../../src/<%= pkg.name %>/intro.js>', '<file_strip_banner:../../src/<%= pkg.name %>/intro.js>',
'<file_strip_banner:../../src/<%= pkg.name %>/exceptions.js>', '<file_strip_banner:../../src/<%= pkg.name %>/exceptions.js>',
// Jio wrapper top // Jio wrapper top
'<file_strip_banner:../../src/<%= pkg.name %>/jio.intro.js>',
'<file_strip_banner:../../src/<%= pkg.name %>/storages/storage.js>', '<file_strip_banner:../../src/<%= pkg.name %>/storages/storage.js>',
'<file_strip_banner:../../src/<%= pkg.name %>/storages/storageHandler.js>',
'<file_strip_banner:../../src/<%= pkg.name %>/commands/command.js>', '<file_strip_banner:../../src/<%= pkg.name %>/commands/command.js>',
'<file_strip_banner:../../src/<%= pkg.name %>/commands/allDocsCommand.js>', '<file_strip_banner:../../src/<%= pkg.name %>/commands/allDocsCommand.js>',
'<file_strip_banner:../../src/<%= pkg.name %>/commands/getCommand.js>', '<file_strip_banner:../../src/<%= pkg.name %>/commands/getCommand.js>',
...@@ -34,7 +34,6 @@ module.exports = function(grunt) { ...@@ -34,7 +34,6 @@ module.exports = function(grunt) {
'<file_strip_banner:../../src/<%= pkg.name %>/jobs/status/waitStatus.js>', '<file_strip_banner:../../src/<%= pkg.name %>/jobs/status/waitStatus.js>',
'<file_strip_banner:../../src/<%= pkg.name %>/jobs/job.js>', '<file_strip_banner:../../src/<%= pkg.name %>/jobs/job.js>',
'<file_strip_banner:../../src/<%= pkg.name %>/announcements/announcement.js>', '<file_strip_banner:../../src/<%= pkg.name %>/announcements/announcement.js>',
'<file_strip_banner:../../src/<%= pkg.name %>/jio.intro.js>',
// Singletons // Singletons
'<file_strip_banner:../../src/<%= pkg.name %>/activityUpdater.js>', '<file_strip_banner:../../src/<%= pkg.name %>/activityUpdater.js>',
'<file_strip_banner:../../src/<%= pkg.name %>/announcements/announcer.js>', '<file_strip_banner:../../src/<%= pkg.name %>/announcements/announcer.js>',
......
var newConflictManagerStorage = function ( spec, my ) { var newConflictManagerStorage = function ( spec, my ) {
var that = Jio.storage( spec, my, 'handler' ), priv = {};
spec = spec || {}; spec = spec || {};
my = my || {}; var that = my.basicStorage( spec, my ), priv = {};
var storage_exists = (spec.storage?true:false); var storage_exists = (spec.storage?true:false);
priv.secondstorage_spec = spec.storage || {type:'base'}; priv.secondstorage_spec = spec.storage || {type:'base'};
...@@ -42,7 +41,7 @@ var newConflictManagerStorage = function ( spec, my ) { ...@@ -42,7 +41,7 @@ var newConflictManagerStorage = function ( spec, my ) {
}; };
priv.saveNewRevision = function (command,path,content,success,error) { priv.saveNewRevision = function (command,path,content,success,error) {
that.addJob ('put',priv.secondstorage_spec,{_id:path,content:content}, that.addJob ('post',priv.secondstorage_spec,{_id:path,content:content},
command.cloneOption(),success,error); command.cloneOption(),success,error);
}; };
...@@ -72,6 +71,7 @@ var newConflictManagerStorage = function ( spec, my ) { ...@@ -72,6 +71,7 @@ var newConflictManagerStorage = function ( spec, my ) {
}; };
priv._revs = function (metadata,revision) { priv._revs = function (metadata,revision) {
if (!(metadata && revision)) { return null; }
if (metadata[revision]) { if (metadata[revision]) {
return {start:metadata[revision]._revisions.length, return {start:metadata[revision]._revisions.length,
ids:metadata[revision]._revisions}; ids:metadata[revision]._revisions};
...@@ -81,6 +81,7 @@ var newConflictManagerStorage = function ( spec, my ) { ...@@ -81,6 +81,7 @@ var newConflictManagerStorage = function ( spec, my ) {
}; };
priv._revs_info = function (metadata) { priv._revs_info = function (metadata) {
if (!metadata) { return null; }
var k, l = []; var k, l = [];
for (k in metadata) { for (k in metadata) {
l.push({ l.push({
......
var newCryptedStorage = function ( spec, my ) { var newCryptedStorage = function ( spec, my ) {
var that = Jio.storage( spec, my, 'handler' ), priv = {}; spec = spec || {};
var that = my.basicStorage( spec, my ), priv = {};
var is_valid_storage = (spec.storage?true:false); var is_valid_storage = (spec.storage?true:false);
......
var newDAVStorage = function ( spec, my ) { var newDAVStorage = function ( spec, my ) {
var that = Jio.storage( spec, my, 'base' ), priv = {}; spec = spec || {};
var that = my.basicStorage( spec, my ), priv = {};
priv.secureDocId = function (string) { priv.secureDocId = function (string) {
var split = string.split('/'), i; var split = string.split('/'), i;
...@@ -75,15 +76,7 @@ var newDAVStorage = function ( spec, my ) { ...@@ -75,15 +76,7 @@ var newDAVStorage = function ( spec, my ) {
return async; return async;
}; };
that.post = function (command) { priv.putOrPost = function (command,type) {
that.put(command);
};
/**
* Saves a document in the distant dav storage.
* @method put
*/
that.put = function (command) {
var secured_docid = priv.secureDocId(command.getDocId()); var secured_docid = priv.secureDocId(command.getDocId());
...@@ -92,7 +85,7 @@ var newDAVStorage = function ( spec, my ) { ...@@ -92,7 +85,7 @@ var newDAVStorage = function ( spec, my ) {
priv.secured_username + '/' + priv.secured_username + '/' +
priv.secured_applicationname + '/' + priv.secured_applicationname + '/' +
secured_docid, secured_docid,
type: 'PUT', type: type,
data: command.getDocContent(), data: command.getDocContent(),
async: true, async: true,
dataType: 'text', // TODO is it necessary ? dataType: 'text', // TODO is it necessary ?
...@@ -110,6 +103,18 @@ var newDAVStorage = function ( spec, my ) { ...@@ -110,6 +103,18 @@ var newDAVStorage = function ( spec, my ) {
that.retry(type); that.retry(type);
} }
} ); } );
};
that.post = function (command) {
priv.putOrPost (command,'POST');
};
/**
* Saves a document in the distant dav storage.
* @method put
*/
that.put = function (command) {
priv.putOrPost (command,'PUT');
}; // end put }; // end put
/** /**
...@@ -247,6 +252,7 @@ var newDAVStorage = function ( spec, my ) { ...@@ -247,6 +252,7 @@ var newDAVStorage = function ( spec, my ) {
dataType: 'xml', dataType: 'xml',
headers: {'Authorization': 'Basic '+Base64.encode( headers: {'Authorization': 'Basic '+Base64.encode(
priv.username + ':' + priv.password ), Depth: '1'}, priv.username + ':' + priv.password ), Depth: '1'},
// xhrFields: {withCredentials: 'true'}, // cross domain
success: function (xmlData) { success: function (xmlData) {
var response = $(xmlData).find( var response = $(xmlData).find(
'D\\:response, response' 'D\\:response, response'
......
var newIndexStorage = function ( spec, my ) { var newIndexStorage = function ( spec, my ) {
var that = Jio.storage( spec, my, 'handler' ), priv = {}; spec = spec || {};
var that = my.basicStorage( spec, my ), priv = {};
var validatestate_secondstorage = spec.storage || false; var validatestate_secondstorage = spec.storage || false;
priv.secondstorage_spec = spec.storage || {type:'base'}; priv.secondstorage_spec = spec.storage || {type:'base'};
......
...@@ -3,7 +3,8 @@ ...@@ -3,7 +3,8 @@
* It is a database located in the browser local storage. * It is a database located in the browser local storage.
*/ */
var newLocalStorage = function ( spec, my ) { var newLocalStorage = function ( spec, my ) {
var that = Jio.storage( spec, my, 'base' ), priv = {}; spec = spec || {};
var that = my.basicStorage( spec, my ), priv = {};
priv.secureDocId = function (string) { priv.secureDocId = function (string) {
var split = string.split('/'), i; var split = string.split('/'), i;
...@@ -179,7 +180,7 @@ var newLocalStorage = function ( spec, my ) { ...@@ -179,7 +180,7 @@ var newLocalStorage = function ( spec, my ) {
} }
LocalOrCookieStorage.setItem(path, doc); LocalOrCookieStorage.setItem(path, doc);
that.success ({ok:true,id:command.getDocId()}); that.success ({ok:true,id:command.getDocId()});
}); },5000);
}; // end put }; // end put
/** /**
...@@ -213,7 +214,7 @@ var newLocalStorage = function ( spec, my ) { ...@@ -213,7 +214,7 @@ var newLocalStorage = function ( spec, my ) {
} }
that.success (doc); that.success (doc);
} }
}); },5000);
}; // end get }; // end get
/** /**
...@@ -249,7 +250,7 @@ var newLocalStorage = function ( spec, my ) { ...@@ -249,7 +250,7 @@ var newLocalStorage = function ( spec, my ) {
} }
} }
that.success ({total_rows:new_array.length,rows:new_array}); that.success ({total_rows:new_array.length,rows:new_array});
}); },5000);
}; // end allDocs }; // end allDocs
/** /**
...@@ -270,7 +271,7 @@ var newLocalStorage = function ( spec, my ) { ...@@ -270,7 +271,7 @@ var newLocalStorage = function ( spec, my ) {
LocalOrCookieStorage.deleteItem(path); LocalOrCookieStorage.deleteItem(path);
priv.removeFileName(secured_docid); priv.removeFileName(secured_docid);
that.success ({ok:true,id:command.getDocId()}); that.success ({ok:true,id:command.getDocId()});
}); },5000);
}; // end remove }; // end remove
return that; return that;
......
var newReplicateStorage = function ( spec, my ) { var newReplicateStorage = function ( spec, my ) {
var that = Jio.storage( spec, my, 'handler' ), priv = {}; spec = spec || {};
var that = my.basicStorage( spec, my ), priv = {};
priv.return_value_array = []; priv.return_value_array = [];
priv.storagelist = spec.storagelist || []; priv.storagelist = spec.storagelist || [];
......
var jio = (function () { var jio = (function () {
"use strict";
var jio = function(spec, my) { var jio = function(spec) {
This diff is collapsed.
var jioNamespace = (function(spec, my) { var storage_type_object = { // -> 'key':constructorFunction
'base': function () {} // overidden by jio
};
var jioNamespace = (function(spec) {
var that = {}; var that = {};
spec = spec || {}; spec = spec || {};
my = my || {};
// Attributes // // Attributes //
var storage_type_o = { // -> 'key':constructorFunction
'base': storage,
'handler': storageHandler
};
// Methods // // Methods //
/**
* Returns a storage from a storage description.
* @method storage
* @param {object} spec The specifications.
* @param {object} my The protected object.
* @param {string} forcetype Force storage type
* @return {object} The storage object.
*/
that.storage = function(spec, my, forcetype) {
spec = spec || {};
my = my || {};
var type = forcetype || spec.type || 'base';
if (!storage_type_o[type]) {
throw invalidStorageType({type:type,
message:'Storage does not exists.'});
}
return storage_type_o[type](spec, my);
};
/** /**
* Creates a new jio instance. * Creates a new jio instance.
* @method newJio * @method newJio
...@@ -39,14 +18,19 @@ var jioNamespace = (function(spec, my) { ...@@ -39,14 +18,19 @@ var jioNamespace = (function(spec, my) {
* - {string} spec.storage.applicationname: The application name * - {string} spec.storage.applicationname: The application name
* @return {object} The new Jio instance. * @return {object} The new Jio instance.
*/ */
that.newJio = function(spec) { Object.defineProperty(that,"newJio",{
var storage = spec; configurable:false,enumerable:false,writable:false,value:
if (typeof storage === 'string') { function(spec) {
storage = JSON.parse (storage); var storage = spec, instance = null;
if (typeof storage === 'string') {
storage = JSON.parse (storage);
}
storage = storage || {type:'base'};
instance = jio(spec);
instance.start();
return instance;
} }
storage = storage || {type:'base'}; });
return jio(spec);
};
/** /**
* Add a storage type to jio. * Add a storage type to jio.
...@@ -54,13 +38,16 @@ var jioNamespace = (function(spec, my) { ...@@ -54,13 +38,16 @@ var jioNamespace = (function(spec, my) {
* @param {string} type The storage type * @param {string} type The storage type
* @param {function} constructor The associated constructor * @param {function} constructor The associated constructor
*/ */
that.addStorageType = function(type, constructor) { Object.defineProperty(that,"addStorageType",{
constructor = constructor || function(){return null;}; configurable:false,enumerable:false,writable:false,value:
if (storage_type_o[type]) { function(type, constructor) {
throw invalidStorageType({type:type,message:'Already known.'}); constructor = constructor || function(){return null;};
if (storage_type_object[type]) {
throw invalidStorageType({type:type,message:'Already known.'});
}
storage_type_object[type] = constructor;
} }
storage_type_o[type] = constructor; });
};
return that; return that;
}()); }());
var job = function(spec, my) { var job = function(spec) {
var that = {}; var that = {};
spec = spec || {}; spec = spec || {};
my = my || {};
// Attributes // // Attributes //
var priv = {}; var priv = {};
priv.id = my.jobIdHandler.nextId(); priv.id = jobIdHandler.nextId();
priv.command = spec.command; priv.command = spec.command;
priv.storage = spec.storage; priv.storage = spec.storage;
priv.status = initialStatus(); priv.status = initialStatus();
...@@ -76,7 +75,7 @@ var job = function(spec, my) { ...@@ -76,7 +75,7 @@ var job = function(spec, my) {
*/ */
that.waitForJob = function(job) { that.waitForJob = function(job) {
if (priv.status.getLabel() !== 'wait') { if (priv.status.getLabel() !== 'wait') {
priv.status = waitStatus({},my); priv.status = waitStatus({});
} }
priv.status.waitForJob(job); priv.status.waitForJob(job);
}; };
...@@ -99,7 +98,7 @@ var job = function(spec, my) { ...@@ -99,7 +98,7 @@ var job = function(spec, my) {
*/ */
that.waitForTime = function(ms) { that.waitForTime = function(ms) {
if (priv.status.getLabel() !== 'wait') { if (priv.status.getLabel() !== 'wait') {
priv.status = waitStatus({},my); priv.status = waitStatus({});
} }
priv.status.waitForTime(ms); priv.status.waitForTime(ms);
}; };
...@@ -124,7 +123,7 @@ var job = function(spec, my) { ...@@ -124,7 +123,7 @@ var job = function(spec, my) {
that.notAccepted = function () { that.notAccepted = function () {
priv.command.onEndDo (function () { priv.command.onEndDo (function () {
priv.status = failStatus(); priv.status = failStatus();
my.jobManager.terminateJob (that); jobManager.terminateJob (that);
}); });
priv.command.error ({ priv.command.error ({
status:11,statusText:'Not Accepted',error:'not_accepted', status:11,statusText:'Not Accepted',error:'not_accepted',
...@@ -171,7 +170,7 @@ var job = function(spec, my) { ...@@ -171,7 +170,7 @@ var job = function(spec, my) {
}); });
priv.command.onEndDo (function(status) { priv.command.onEndDo (function(status) {
priv.status = status; priv.status = status;
my.jobManager.terminateJob (that); jobManager.terminateJob (that);
}); });
priv.command.execute (priv.storage); priv.command.execute (priv.storage);
}; };
......
var jobIdHandler = (function(spec, my) { var jobIdHandler = (function(spec) {
var that = {}; var that = {};
spec = spec || {}; spec = spec || {};
my = my || {};
// Attributes // // Attributes //
var id = 0; var id = 0;
// Methods // // Methods //
......
var jobManager = (function(spec, my) { var jobManager = (function(spec) {
var that = {}; var that = {};
spec = spec || {}; spec = spec || {};
my = my || {};
// Attributes // // Attributes //
var job_array_name = 'jio/job_array'; var job_array_name = 'jio/job_array';
var priv = {}; var priv = {};
...@@ -10,9 +9,6 @@ var jobManager = (function(spec, my) { ...@@ -10,9 +9,6 @@ var jobManager = (function(spec, my) {
priv.interval = 200; priv.interval = 200;
priv.job_array = []; priv.job_array = [];
my.jobManager = that;
my.jobIdHandler = jobIdHandler;
// Methods // // Methods //
/** /**
* Get the job array name in the localStorage * Get the job array name in the localStorage
...@@ -140,11 +136,11 @@ var jobManager = (function(spec, my) { ...@@ -140,11 +136,11 @@ var jobManager = (function(spec, my) {
var i, jio_job_array; var i, jio_job_array;
jio_job_array = LocalOrCookieStorage.getItem('jio/job_array/'+id)||[]; jio_job_array = LocalOrCookieStorage.getItem('jio/job_array/'+id)||[];
for (i = 0; i < jio_job_array.length; i+= 1) { for (i = 0; i < jio_job_array.length; i+= 1) {
var command_object = command(jio_job_array[i].command, my); var command_object = command(jio_job_array[i].command);
if (command_object.canBeRestored()) { if (command_object.canBeRestored()) {
that.addJob ( job( that.addJob ( job(
{storage:jioNamespace.storage(jio_job_array[i].storage,my), {storage:jio.storage(jio_job_array[i].storage),
command:command_object}, my)); command:command_object}));
} }
} }
}; };
......
var jobRules = (function(spec, my) { var jobRules = (function(spec) {
var that = {}; var that = {};
// Attributes // // Attributes //
var priv = {}; var priv = {};
priv.compare = {}; priv.compare = {};
priv.action = {}; priv.action = {};
that.eliminate = function() { return 'eliminate'; }; Object.defineProperty(that,"eliminate",{
that.update = function() { return 'update'; }; configurable:false,enumerable:false,writable:false,value:
that.dontAccept = function() { return 'dont accept'; }; function() { return 'eliminate'; }
that.wait = function() { return 'wait'; }; });
that.none = function() { return 'none'; }; Object.defineProperty(that,"update",{
configurable:false,enumerable:false,writable:false,value:
function() { return 'update'; }
});
Object.defineProperty(that,"dontAccept",{
configurable:false,enumerable:false,writable:false,value:
function() { return 'dont accept'; }
});
Object.defineProperty(that,"wait",{
configurable:false,enumerable:false,writable:false,value:
function() { return 'wait'; }
});
Object.defineProperty(that,"none",{
configurable:false,enumerable:false,writable:false,value:
function() { return 'none'; }
});
that.default_action = that.none; that.default_action = that.none;
that.default_compare = function(job1,job2) { that.default_compare = function(job1,job2) {
return (job1.getCommand().getDocId() === return (job1.getCommand().getDocId() ===
...@@ -70,12 +85,15 @@ var jobRules = (function(spec, my) { ...@@ -70,12 +85,15 @@ var jobRules = (function(spec, my) {
* @param job2 {object} The new job. * @param job2 {object} The new job.
* @return {string} The action string. * @return {string} The action string.
*/ */
that.validateJobAccordingToJob = function(job1,job2) { Object.defineProperty(that,"validateJobAccordingToJob",{
if (priv.canCompare(job1,job2)) { configurable:false,enumerable:false,writable:false,value:
return {action:priv.getAction(job1,job2),job:job1}; function(job1,job2) {
if (priv.canCompare(job1,job2)) {
return {action:priv.getAction(job1,job2),job:job1};
}
return {action:that.default_action(job1,job2),job:job1};
} }
return {action:that.default_action(job1,job2),job:job1}; });
};
/** /**
* Adds a rule the action rules. * Adds a rule the action rules.
...@@ -85,12 +103,16 @@ var jobRules = (function(spec, my) { ...@@ -85,12 +103,16 @@ var jobRules = (function(spec, my) {
* @param method2 {string} The action label from the new job. * @param method2 {string} The action label from the new job.
* @param rule {function} The rule that return an action string. * @param rule {function} The rule that return an action string.
*/ */
that.addActionRule = function(method1,ongoing,method2,rule) { Object.defineProperty(that,"addActionRule",{
var ongoing_s = (ongoing?'on going':'not on going'); configurable:false,enumerable:false,writable:false,value:
priv.action[method1] = priv.action[method1] || {}; function(method1,ongoing,method2,rule) {
priv.action[method1][ongoing_s] = priv.action[method1][ongoing_s] || {}; var ongoing_s = (ongoing?'on going':'not on going');
priv.action[method1][ongoing_s][method2] = rule; priv.action[method1] = priv.action[method1] || {};
}; priv.action[method1][ongoing_s] =
priv.action[method1][ongoing_s] || {};
priv.action[method1][ongoing_s][method2] = rule;
}
});
/** /**
* Adds a rule the compare rules. * Adds a rule the compare rules.
...@@ -100,10 +122,13 @@ var jobRules = (function(spec, my) { ...@@ -100,10 +122,13 @@ var jobRules = (function(spec, my) {
* @param rule {function} The rule that return a boolean * @param rule {function} The rule that return a boolean
* - true if job1 and job2 can be compared, else false. * - true if job1 and job2 can be compared, else false.
*/ */
that.addCompareRule = function(method1,method2,rule) { Object.defineProperty(that,"addCompareRule",{
priv.compare[method1] = priv.compare[method1] || {}; configurable:false,enumerable:false,writable:false,value:
priv.compare[method1][method2] = rule; function(method1,method2,rule) {
}; priv.compare[method1] = priv.compare[method1] || {};
priv.compare[method1][method2] = rule;
}
});
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
// Adding some rules // Adding some rules
......
...@@ -7,12 +7,12 @@ var storage = function(spec, my) { ...@@ -7,12 +7,12 @@ var storage = function(spec, my) {
priv.type = spec.type || ''; priv.type = spec.type || '';
// Methods // // Methods //
that.getType = function() { Object.defineProperty(that,"getType",{
return priv.type; configurable:false,enumerable:false,writable:false,value:
}; function() {
that.setType = function(type) { return priv.type;
priv.type = type; }
}; });
/** /**
* Execute the command on this storage. * Execute the command on this storage.
...@@ -41,9 +41,10 @@ var storage = function(spec, my) { ...@@ -41,9 +41,10 @@ var storage = function(spec, my) {
that.validate = function () { that.validate = function () {
var mess = that.validateState(); var mess = that.validateState();
if (mess) { if (mess) {
that.error({status:0,statusText:'Invalid Storage', that.error({
error:'invalid_storage', status:0,statusText:'Invalid Storage',
message:mess,reason:mess}); error:'invalid_storage',
message:mess,reason:mess});
return false; return false;
} }
return true; return true;
...@@ -86,5 +87,31 @@ var storage = function(spec, my) { ...@@ -86,5 +87,31 @@ var storage = function(spec, my) {
that.error = function() {}; that.error = function() {};
that.end = function() {}; // terminate the current job. that.end = function() {}; // terminate the current job.
priv.newCommand = function (method, spec) {
var o = spec || {};
o.label = method;
return command (o, my);
};
that.addJob = function (method,storage_spec,doc,option,success,error) {
var command_opt = {
options: option,
callbacks:{success:success,error:error}
};
if (doc) {
if (method === 'get') {
command_opt.docid = doc;
} else {
command_opt.doc = doc;
}
}
jobManager.addJob (
job({
storage:my.storage(storage_spec||{}),
command:priv.newCommand(method,command_opt)
}, my)
);
};
return that; return that;
}; };
var storageHandler = function(spec, my) {
spec = spec || {};
my = my || {};
var that = storage( spec, my ), priv = {};
priv.newCommand = function (method, spec) {
var o = spec || {};
o.label = method;
return command (o, my);
};
that.addJob = function (method,storage_spec,doc,option,success,error) {
var command_opt = {
options: option,
callbacks:{success:success,error:error}
};
if (doc) {
if (method === 'get') {
command_opt.docid = doc;
} else {
command_opt.doc = doc;
}
}
my.jobManager.addJob (
job({
storage:jioNamespace.storage(storage_spec||{}),
command:priv.newCommand(method,command_opt)
}, my)
);
};
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