Commit cc8c320f authored by Tristan Cavelier's avatar Tristan Cavelier

Updating localstorage put method

parent 0536cec8
...@@ -210,6 +210,30 @@ var newLocalStorage = function ( spec, my ) { ...@@ -210,6 +210,30 @@ var newLocalStorage = function ( spec, my ) {
return obj; return obj;
}; };
/**
* Update [doc] the document object and remove [doc] keys
* which are not in [new_doc]. It only changes [doc] keys not starting
* with an underscore.
* ex: doc: {key:value1,_key:value2} with
* new_doc: {key:value3,_key:value4} updates
* doc: {key:value3,_key:value2}.
* @param {object} doc The original document object.
* @param {object} new_doc The new document object
*/
priv.documentObjectUpdate = function (doc, new_doc) {
var k;
for (k in doc) {
if (k[0] !== '_') {
delete doc[k];
}
}
for (k in new_doc) {
if (k[0] !== '_') {
doc[k] = new_doc[k];
}
}
};
/** /**
* Create a document in the local storage. * Create a document in the local storage.
* It will store the file in 'jio/local/USR/APP/FILE_NAME'. * It will store the file in 'jio/local/USR/APP/FILE_NAME'.
...@@ -285,37 +309,66 @@ var newLocalStorage = function ( spec, my ) { ...@@ -285,37 +309,66 @@ var newLocalStorage = function ( spec, my ) {
* @method put * @method put
*/ */
that.put = function (command) { that.put = function (command) {
var now = Date.now();
// wait a little in order to simulate asynchronous saving // wait a little in order to simulate asynchronous saving
setTimeout (function () { setTimeout (function () {
var secured_docid = priv.secureDocId(command.getDocId()), var docid, doc, docpath, attmtid, attmt, attmtpath, prev_rev;
doc = null, path = docid = command.getDocId();
'jio/local/'+priv.secured_username+'/'+ prev_rev = command.getDocInfo('_rev');
priv.secured_applicationname+'/'+ docpath ='jio/local/'+priv.secured_username+'/'+
secured_docid; priv.secured_applicationname+'/'+docid;
if (!priv.checkSecuredDocId(
secured_docid,command.getDocId(),'put')) {return;}
// reading // reading
doc = LocalOrCookieStorage.getItem(path); doc = localstorage.getItem(docpath);
if (!doc) { if (!doc) {
// create document that.error({
doc = { status:404,statusText:'Not found',error:'not_found',
_id: command.getDocId(), message:'Document not found.',
content: command.getDocContent(), reason:'document not found'
_creation_date: Date.now(), });
_last_modified: Date.now() return;
}; }
if (!priv.userExists(priv.secured_username)) { if (doc._rev !== prev_rev) {
priv.addUser (priv.secured_username); // want to update an older document
that.error({
status:409,statusText:'Conflict',error:'conflict',
message:'Document update conflict.',
reason:'document update conflict.'
});
return;
} }
priv.addFileName(secured_docid); // it is the good document
attmtid = command.getAttachmentId();
if (attmtid) {
attmtpath = docpath+'/'+attmtid;
// this is an attachment
attmt = localstorage.getItem(attmtpath);
if (!attmt) {
// there is no attachment to update
that.error({
status:404,statusText:'Not found',error:'not_found',
message:'Document is missing attachment.',
reason:'document is missing attachment'
});
return;
}
// updating attachment
doc._attachments[attmtid].revpos =
parseInt(doc._rev.split('-')[0],10);
localstorage.setItem(attmtpath,command.getContent());
} else { } else {
// overwriting // update document metadata
doc.content = command.getDocContent(); priv.documentObjectUpdate(doc,command.cloneDoc());
doc._last_modified = Date.now();
} }
localStorage.setItem(path, doc); doc._rev = priv.generateNextRev(prev_rev, ''+doc+' '+now+'');
that.success ({ok:true,id:command.getDocId()}); localstorage.setItem(docpath, doc);
that.success (
priv.manageOptions(
{ok:true,id:docid,rev:doc._rev},
command,
doc
)
);
}); });
}; // end put }; // end put
......
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