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) {
'<file_strip_banner:../../src/<%= pkg.name %>/intro.js>',
'<file_strip_banner:../../src/<%= pkg.name %>/exceptions.js>',
// 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/storageHandler.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/getCommand.js>',
......@@ -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/job.js>',
'<file_strip_banner:../../src/<%= pkg.name %>/announcements/announcement.js>',
'<file_strip_banner:../../src/<%= pkg.name %>/jio.intro.js>',
// Singletons
'<file_strip_banner:../../src/<%= pkg.name %>/activityUpdater.js>',
'<file_strip_banner:../../src/<%= pkg.name %>/announcements/announcer.js>',
......
var newConflictManagerStorage = function ( spec, my ) {
var that = Jio.storage( spec, my, 'handler' ), priv = {};
spec = spec || {};
my = my || {};
var that = my.basicStorage( spec, my ), priv = {};
var storage_exists = (spec.storage?true:false);
priv.secondstorage_spec = spec.storage || {type:'base'};
......@@ -42,7 +41,7 @@ var newConflictManagerStorage = function ( spec, my ) {
};
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);
};
......@@ -72,6 +71,7 @@ var newConflictManagerStorage = function ( spec, my ) {
};
priv._revs = function (metadata,revision) {
if (!(metadata && revision)) { return null; }
if (metadata[revision]) {
return {start:metadata[revision]._revisions.length,
ids:metadata[revision]._revisions};
......@@ -81,6 +81,7 @@ var newConflictManagerStorage = function ( spec, my ) {
};
priv._revs_info = function (metadata) {
if (!metadata) { return null; }
var k, l = [];
for (k in metadata) {
l.push({
......
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 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) {
var split = string.split('/'), i;
......@@ -75,15 +76,7 @@ var newDAVStorage = function ( spec, my ) {
return async;
};
that.post = function (command) {
that.put(command);
};
/**
* Saves a document in the distant dav storage.
* @method put
*/
that.put = function (command) {
priv.putOrPost = function (command,type) {
var secured_docid = priv.secureDocId(command.getDocId());
......@@ -92,7 +85,7 @@ var newDAVStorage = function ( spec, my ) {
priv.secured_username + '/' +
priv.secured_applicationname + '/' +
secured_docid,
type: 'PUT',
type: type,
data: command.getDocContent(),
async: true,
dataType: 'text', // TODO is it necessary ?
......@@ -110,6 +103,18 @@ var newDAVStorage = function ( spec, my ) {
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
/**
......@@ -247,6 +252,7 @@ var newDAVStorage = function ( spec, my ) {
dataType: 'xml',
headers: {'Authorization': 'Basic '+Base64.encode(
priv.username + ':' + priv.password ), Depth: '1'},
// xhrFields: {withCredentials: 'true'}, // cross domain
success: function (xmlData) {
var response = $(xmlData).find(
'D\\:response, response'
......
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;
priv.secondstorage_spec = spec.storage || {type:'base'};
......
......@@ -3,7 +3,8 @@
* It is a database located in the browser local storage.
*/
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) {
var split = string.split('/'), i;
......@@ -179,7 +180,7 @@ var newLocalStorage = function ( spec, my ) {
}
LocalOrCookieStorage.setItem(path, doc);
that.success ({ok:true,id:command.getDocId()});
});
},5000);
}; // end put
/**
......@@ -213,7 +214,7 @@ var newLocalStorage = function ( spec, my ) {
}
that.success (doc);
}
});
},5000);
}; // end get
/**
......@@ -249,7 +250,7 @@ var newLocalStorage = function ( spec, my ) {
}
}
that.success ({total_rows:new_array.length,rows:new_array});
});
},5000);
}; // end allDocs
/**
......@@ -270,7 +271,7 @@ var newLocalStorage = function ( spec, my ) {
LocalOrCookieStorage.deleteItem(path);
priv.removeFileName(secured_docid);
that.success ({ok:true,id:command.getDocId()});
});
},5000);
}; // end remove
return that;
......
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.storagelist = spec.storagelist || [];
......
var jio = (function () {
"use strict";
var jio = function(spec, my) {
var jio = function(spec) {
// Class jio
var that = {};
var that = {}, priv = {};
spec = spec || {};
my = my || {};
// Attributes //
var priv = {};
var jio_id_array_name = 'jio/id_array';
priv.id = null;
my.jobManager = jobManager;
my.jobIdHandler = jobIdHandler;
priv.storage_spec = spec;
// initialize //
......@@ -31,38 +27,79 @@
};
// Methods //
that.start = function() {
/**
* 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.
*/
Object.defineProperty(that,"storage",{
configurable:false,enumerable:false,writable:false,value:
function(spec, my, forcetype) {
spec = spec || {};
my = my || {};
my.basicStorage = storage;
my.storage = that.storage; // NOTE : or proxy storage
var type = forcetype || spec.type || 'base';
if (type === 'base') {
return storage(spec, my);
}
if (!storage_type_object[type]) {
throw invalidStorageType(
{type:type,message:'Storage does not exists.'});
}
return storage_type_object[type](spec, my);
}
});
Object.defineProperty(that,"start",{
configurable:false,enumerable:false,writable:false,value:
function() {
priv.init();
activityUpdater.start();
jobManager.start();
};
that.stop = function() {
}
});
Object.defineProperty(that,"stop",{
configurable:false,enumerable:false,writable:false,value:
function() {
jobManager.stop();
};
that.close = function() {
}
});
Object.defineProperty(that,"close",{
configurable:false,enumerable:false,writable:false,value:
function() {
activityUpdater.stop();
jobManager.stop();
priv.id = null;
};
that.start();
}
});
/**
* Returns the jio id.
* @method getId
* @return {number} The jio id.
*/
that.getId = function() {
Object.defineProperty(that,"getId",{
configurable:false,enumerable:false,writable:false,value:
function() {
return priv.id;
};
}
});
/**
* Returns the jio job rules object used by the job manager.
* @method getJobRules
* @return {object} The job rules object
*/
that.getJobRules = function() {
Object.defineProperty(that,"getJobRules",{
configurable:false,enumerable:false,writable:false,value:
function() {
return jobRules;
};
}
});
/**
* Checks if the storage description is valid or not.
......@@ -70,13 +107,19 @@
* @param {object} description The description object.
* @return {boolean} true if ok, else false.
*/
that.validateStorageDescription = function(description) {
return jioNamespace.storage(description, my).isValid();
};
Object.defineProperty(that,"validateStorageDescription",{
configurable:false,enumerable:false,writable:false,value:
function(description) {
return that.storage(description).isValid();
}
});
that.getJobArray = function () {
Object.defineProperty(that,"getJobArray",{
configurable:false,enumerable:false,writable:false,value:
function () {
return jobManager.serialized();
};
}
});
priv.getParam = function (list,nodoc) {
var param = {}, i = 0;
......@@ -110,33 +153,36 @@
priv.addJob = function (commandCreator,spec) {
jobManager.addJob(
job({storage:jioNamespace.storage(priv.storage_spec,my),
command:commandCreator(spec,my)},my));
job({storage:that.storage(priv.storage_spec),
command:commandCreator(spec)}));
};
// /**
// * Post a document.
// * @method post
// * @param {object} doc The document {"content":}.
// * @param {object} options (optional) Contains some options:
// * - {number} max_retry The number max of retries, 0 = infinity.
// * - {boolean} revs Include revision history of the document.
// * - {boolean} revs_info Retreive the revisions.
// * - {boolean} conflicts Retreive the conflict list.
// * @param {function} callback (optional) The callback(err,response).
// * @param {function} error (optional) The callback on error, if this
// * callback is given in parameter, "callback" is changed as "success",
// * called on success.
// */
// that.post = function() {
// var param = priv.getParam(arguments);
// param.options.max_retry = param.options.max_retry || 0;
// priv.addJob(postCommand,{
// doc:param.doc,
// options:param.options,
// callbacks:{success:param.success,error:param.error}
// });
// };
/**
* Post a document.
* @method post
* @param {object} doc The document {"content":}.
* @param {object} options (optional) Contains some options:
* - {number} max_retry The number max of retries, 0 = infinity.
* - {boolean} revs Include revision history of the document.
* - {boolean} revs_info Retreive the revisions.
* - {boolean} conflicts Retreive the conflict list.
* @param {function} callback (optional) The callback(err,response).
* @param {function} error (optional) The callback on error, if this
* callback is given in parameter, "callback" is changed as "success",
* called on success.
*/
Object.defineProperty(that,"post",{
configurable:false,enumerable:false,writable:false,value:
function() {
var param = priv.getParam(arguments);
param.options.max_retry = param.options.max_retry || 0;
priv.addJob(postCommand,{
doc:param.doc,
options:param.options,
callbacks:{success:param.success,error:param.error}
});
}
});
/**
* Put a document.
......@@ -152,7 +198,9 @@
* callback is given in parameter, "callback" is changed as "success",
* called on success.
*/
that.put = function() {
Object.defineProperty(that,"put",{
configurable:false,enumerable:false,writable:false,value:
function() {
var param = priv.getParam(arguments);
param.options.max_retry = param.options.max_retry || 0;
priv.addJob(putCommand,{
......@@ -160,7 +208,8 @@
options:param.options,
callbacks:{success:param.success,error:param.error}
});
};
}
});
/**
* Get a document.
......@@ -178,7 +227,9 @@
* callback is given in parameter, "callback" is changed as "success",
* called on success.
*/
that.get = function() {
Object.defineProperty(that,"get",{
configurable:false,enumerable:false,writable:false,value:
function() {
var param = priv.getParam(arguments);
param.options.max_retry = param.options.max_retry || 3;
param.options.metadata_only = (
......@@ -189,7 +240,8 @@
options:param.options,
callbacks:{success:param.success,error:param.error}
});
};
}
});
/**
* Remove a document.
......@@ -205,7 +257,9 @@
* callback is given in parameter, "callback" is changed as "success",
* called on success.
*/
that.remove = function() {
Object.defineProperty(that,"remove",{
configurable:false,enumerable:false,writable:false,value:
function() {
var param = priv.getParam(arguments);
param.options.max_retry = param.options.max_retry || 0;
priv.addJob(removeCommand,{
......@@ -213,7 +267,8 @@
options:param.options,
callbacks:{success:param.success,error:param.error}
});
};
}
});
/**
* Get a list of documents.
......@@ -230,7 +285,9 @@
* callback is given in parameter, "callback" is changed as "success",
* called on success.
*/
that.allDocs = function() {
Object.defineProperty(that,"allDocs",{
configurable:false,enumerable:false,writable:false,value:
function() {
var param = priv.getParam(arguments,'no doc');
param.options.max_retry = param.options.max_retry || 3;
param.options.metadata_only = (
......@@ -240,7 +297,8 @@
options:param.options,
callbacks:{success:param.success,error:param.error}
});
};
}
});
return that;
}; // End Class jio
var jioNamespace = (function(spec, my) {
var storage_type_object = { // -> 'key':constructorFunction
'base': function () {} // overidden by jio
};
var jioNamespace = (function(spec) {
var that = {};
spec = spec || {};
my = my || {};
// Attributes //
var storage_type_o = { // -> 'key':constructorFunction
'base': storage,
'handler': storageHandler
};
// 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.
* @method newJio
......@@ -39,14 +18,19 @@ var jioNamespace = (function(spec, my) {
* - {string} spec.storage.applicationname: The application name
* @return {object} The new Jio instance.
*/
that.newJio = function(spec) {
var storage = spec;
Object.defineProperty(that,"newJio",{
configurable:false,enumerable:false,writable:false,value:
function(spec) {
var storage = spec, instance = null;
if (typeof storage === 'string') {
storage = JSON.parse (storage);
}
storage = storage || {type:'base'};
return jio(spec);
};
instance = jio(spec);
instance.start();
return instance;
}
});
/**
* Add a storage type to jio.
......@@ -54,13 +38,16 @@ var jioNamespace = (function(spec, my) {
* @param {string} type The storage type
* @param {function} constructor The associated constructor
*/
that.addStorageType = function(type, constructor) {
Object.defineProperty(that,"addStorageType",{
configurable:false,enumerable:false,writable:false,value:
function(type, constructor) {
constructor = constructor || function(){return null;};
if (storage_type_o[type]) {
if (storage_type_object[type]) {
throw invalidStorageType({type:type,message:'Already known.'});
}
storage_type_o[type] = constructor;
};
storage_type_object[type] = constructor;
}
});
return that;
}());
var job = function(spec, my) {
var job = function(spec) {
var that = {};
spec = spec || {};
my = my || {};
// Attributes //
var priv = {};
priv.id = my.jobIdHandler.nextId();
priv.id = jobIdHandler.nextId();
priv.command = spec.command;
priv.storage = spec.storage;
priv.status = initialStatus();
......@@ -76,7 +75,7 @@ var job = function(spec, my) {
*/
that.waitForJob = function(job) {
if (priv.status.getLabel() !== 'wait') {
priv.status = waitStatus({},my);
priv.status = waitStatus({});
}
priv.status.waitForJob(job);
};
......@@ -99,7 +98,7 @@ var job = function(spec, my) {
*/
that.waitForTime = function(ms) {
if (priv.status.getLabel() !== 'wait') {
priv.status = waitStatus({},my);
priv.status = waitStatus({});
}
priv.status.waitForTime(ms);
};
......@@ -124,7 +123,7 @@ var job = function(spec, my) {
that.notAccepted = function () {
priv.command.onEndDo (function () {
priv.status = failStatus();
my.jobManager.terminateJob (that);
jobManager.terminateJob (that);
});
priv.command.error ({
status:11,statusText:'Not Accepted',error:'not_accepted',
......@@ -171,7 +170,7 @@ var job = function(spec, my) {
});
priv.command.onEndDo (function(status) {
priv.status = status;
my.jobManager.terminateJob (that);
jobManager.terminateJob (that);
});
priv.command.execute (priv.storage);
};
......
var jobIdHandler = (function(spec, my) {
var jobIdHandler = (function(spec) {
var that = {};
spec = spec || {};
my = my || {};
// Attributes //
var id = 0;
// Methods //
......
var jobManager = (function(spec, my) {
var jobManager = (function(spec) {
var that = {};
spec = spec || {};
my = my || {};
// Attributes //
var job_array_name = 'jio/job_array';
var priv = {};
......@@ -10,9 +9,6 @@ var jobManager = (function(spec, my) {
priv.interval = 200;
priv.job_array = [];
my.jobManager = that;
my.jobIdHandler = jobIdHandler;
// Methods //
/**
* Get the job array name in the localStorage
......@@ -140,11 +136,11 @@ var jobManager = (function(spec, my) {
var i, jio_job_array;
jio_job_array = LocalOrCookieStorage.getItem('jio/job_array/'+id)||[];
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()) {
that.addJob ( job(
{storage:jioNamespace.storage(jio_job_array[i].storage,my),
command:command_object}, my));
{storage:jio.storage(jio_job_array[i].storage),
command:command_object}));
}
}
};
......
var jobRules = (function(spec, my) {
var jobRules = (function(spec) {
var that = {};
// Attributes //
var priv = {};
priv.compare = {};
priv.action = {};
that.eliminate = function() { return 'eliminate'; };
that.update = function() { return 'update'; };
that.dontAccept = function() { return 'dont accept'; };
that.wait = function() { return 'wait'; };
that.none = function() { return 'none'; };
Object.defineProperty(that,"eliminate",{
configurable:false,enumerable:false,writable:false,value:
function() { return 'eliminate'; }
});
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_compare = function(job1,job2) {
return (job1.getCommand().getDocId() ===
......@@ -70,12 +85,15 @@ var jobRules = (function(spec, my) {
* @param job2 {object} The new job.
* @return {string} The action string.
*/
that.validateJobAccordingToJob = function(job1,job2) {
Object.defineProperty(that,"validateJobAccordingToJob",{
configurable:false,enumerable:false,writable:false,value:
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};
};
}
});
/**
* Adds a rule the action rules.
......@@ -85,12 +103,16 @@ var jobRules = (function(spec, my) {
* @param method2 {string} The action label from the new job.
* @param rule {function} The rule that return an action string.
*/
that.addActionRule = function(method1,ongoing,method2,rule) {
Object.defineProperty(that,"addActionRule",{
configurable:false,enumerable:false,writable:false,value:
function(method1,ongoing,method2,rule) {
var ongoing_s = (ongoing?'on going':'not on going');
priv.action[method1] = priv.action[method1] || {};
priv.action[method1][ongoing_s] = priv.action[method1][ongoing_s] || {};
priv.action[method1][ongoing_s] =
priv.action[method1][ongoing_s] || {};
priv.action[method1][ongoing_s][method2] = rule;
};
}
});
/**
* Adds a rule the compare rules.
......@@ -100,10 +122,13 @@ var jobRules = (function(spec, my) {
* @param rule {function} The rule that return a boolean
* - true if job1 and job2 can be compared, else false.
*/
that.addCompareRule = function(method1,method2,rule) {
Object.defineProperty(that,"addCompareRule",{
configurable:false,enumerable:false,writable:false,value:
function(method1,method2,rule) {
priv.compare[method1] = priv.compare[method1] || {};
priv.compare[method1][method2] = rule;
};
}
});
////////////////////////////////////////////////////////////////////////////
// Adding some rules
......
......@@ -7,12 +7,12 @@ var storage = function(spec, my) {
priv.type = spec.type || '';
// Methods //
that.getType = function() {
Object.defineProperty(that,"getType",{
configurable:false,enumerable:false,writable:false,value:
function() {
return priv.type;
};
that.setType = function(type) {
priv.type = type;
};
}
});
/**
* Execute the command on this storage.
......@@ -41,7 +41,8 @@ var storage = function(spec, my) {
that.validate = function () {
var mess = that.validateState();
if (mess) {
that.error({status:0,statusText:'Invalid Storage',
that.error({
status:0,statusText:'Invalid Storage',
error:'invalid_storage',
message:mess,reason:mess});
return false;
......@@ -86,5 +87,31 @@ var storage = function(spec, my) {
that.error = function() {};
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;
};
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