Commit 56afb3e3 authored by Sven Franck's avatar Sven Franck

next pass at synchronization, still broken

parent 2a7e54ec
...@@ -5564,13 +5564,58 @@ ...@@ -5564,13 +5564,58 @@
storage = {}; storage = {};
/** /**
* First pass at sync * Make a formData object and replace prefixes
* @method parseForm
* @param {object} obj Form to parse
* @param {string} clean id of the form
* @return {object} FormData object
*/
storage.parseForm = function (obj, clean) {
var formData, property, value;
formData = new FormData();
for (property in obj) {
if (obj.hasOwnProperty(property)) {
value = obj[property];
switch (property) {
case "undefined":
case "identifier":
break;
default:
formData.append(property.replace(clean, ""), value);
break;
}
}
}
return formData;
};
/**
* Synchronize storages
* @method sync * @method sync
* @param {object} response Response object to be synced * @param {object} response Response object to be synced
* @param {string} storage Name of storage * @param {string} storage Name of storage
**/ **/
storage.sync = function (response, storage) { storage.sync = function (response, storage_name) {
var i, record, use, repair, promise_list = []; var i, record, use, repair, promise_list,
j, len, storage_list, data, send, clearOut, type;
promise_list = [];
clearOut = function (obj) {
var property, new_obj = {};
for (property in obj) {
if (obj.hasOwnProperty(property)) {
if (property.slice(0,1) !== "_") {
new_obj[property] = obj[property];
}
}
}
// but...
new_obj.identifier = obj._id;
return new_obj;
};
for (i = 0; i < response.data.total_rows; i += 1) { for (i = 0; i < response.data.total_rows; i += 1) {
record = response.data.rows[i]; record = response.data.rows[i];
...@@ -5586,12 +5631,27 @@ ...@@ -5586,12 +5631,27 @@
} }
if (repair) { if (repair) {
util.loader("", "status_dict.sync"); util.loader("", "status_dict.sync");
promise_list[i] = app.storage_dict[storage].repair(use); // promise_list[i] = app.storage_dict[storage_name].repair(use);
send = clearOut(use);
type = send.identifier.slice(0,1).toUpperCase() + send.identifier.split("/")[0].substring(1);
// HACK: since ERP5 will not allow JIO sync, we do it manually...
promise_list[i] = app.store({
"response": [send],
"pass": {
"type": type,
"reply": true,
"view": "web_view_category"
}
});
} }
} }
return RSVP.all(promise_list) return RSVP.all(promise_list)
.then(function () { .then(function (response_list) {
if (response_list.length > 0) {
util.loader("", "status_dict.synced", "check");
}
return undefined; return undefined;
}); });
}; };
...@@ -5770,11 +5830,10 @@ ...@@ -5770,11 +5830,10 @@
// TODO: generate common promise handler for add/remove/clone/export/etc // TODO: generate common promise handler for add/remove/clone/export/etc
// TODO: make form validation and captcha generic // TODO: make form validation and captcha generic
storage.add = function (config) { storage.add = function (config) {
var property, replace, obj, value, form_to_submit, formData, var property, replace, obj, value, form_to_submit,
valid, pass_id, validate_portal_type_fields, form_data_set, decode; valid, pass_id, validate_portal_type_fields, decode, data;
form_to_submit = document.getElementById(config.id); form_to_submit = document.getElementById(config.id);
formData = new FormData();
valid = storage.validate(config); valid = storage.validate(config);
if (valid === undefined) { if (valid === undefined) {
...@@ -5791,30 +5850,23 @@ ...@@ -5791,30 +5850,23 @@
validate_portal_type_fields = true; validate_portal_type_fields = true;
} }
for (property in valid) { // force formData
if (valid.hasOwnProperty(property)) { if (config.gadget.state.force_formdata) {
value = valid[property]; data = storage.parseForm(valid, replace);
// prepare to store } else {
// TODO: add id to captcha fields missing it... for (property in valid) {
if (property !== "undefined") { if (valid.hasOwnProperty(property)) {
if (config.gadget.state.force_formdata) { value = valid[property];
if (property !== "identifier") { // prepare to store
form_data_set = true;
formData.append(property.replace(replace, ""), value); if (property !== "undefined") {
} obj[property.replace(replace, "")] = value;
} else {
// overwrite portal_type if forced
if (property !== "identifier") {
obj[property.replace(replace, "")] = value;
} else {
obj[property] = value;
}
} }
} }
} }
} }
// force new object to be created // force new
if (config.gadget.state.create_new) { if (config.gadget.state.create_new) {
delete obj.identifier; delete obj.identifier;
} }
...@@ -5831,7 +5883,7 @@ ...@@ -5831,7 +5883,7 @@
"validate_type": validate_portal_type_fields, "validate_type": validate_portal_type_fields,
// hacked view // hacked view
"view": config.gadget.state.view, "view": config.gadget.state.view,
"form_data": form_data_set ? formData : undefined, "form_data": data || undefined,
"fetch": config.gadget.state.url_pointer "fetch": config.gadget.state.url_pointer
} }
}) })
...@@ -7227,7 +7279,6 @@ ...@@ -7227,7 +7279,6 @@
pass = reply.pass; pass = reply.pass;
if (reply.response) { if (reply.response) {
// TODO: remove once no need for sample data // TODO: remove once no need for sample data
if (pass.needs_fields) { if (pass.needs_fields) {
delete pass.needs_fields; delete pass.needs_fields;
...@@ -7278,6 +7329,7 @@ ...@@ -7278,6 +7329,7 @@
util.error("store: field definintions not found."); util.error("store: field definintions not found.");
} }
} }
// if we have form_data we continue // if we have form_data we continue
if (pass.form_data === undefined) { if (pass.form_data === undefined) {
// loop over item and create a JIO record // loop over item and create a JIO record
...@@ -7314,6 +7366,8 @@ ...@@ -7314,6 +7366,8 @@
// store // store
promises[i] = store[method || "post"](obj, hacked_view || {}) promises[i] = store[method || "post"](obj, hacked_view || {})
.then(function (answer) { .then(function (answer) {
console.log("OUT")
console.log(answer)
return answer; return answer;
}) })
.fail(util.error); .fail(util.error);
......
...@@ -45,19 +45,6 @@ ...@@ -45,19 +45,6 @@
addStorageFunction = require('jio').addStorage; addStorageFunction = require('jio').addStorage;
/** /**
* Test if the a value is a date
*
* @param {String,Number,Date} date The date to test
* @return {Boolean} true if success, else false
*/
function isDate(date) {
if (!isNaN((new Date(date === null ? undefined : date)).getTime())) {
return true;
}
return false;
}
/**
* Make a clean clone of an object with clean referring to removal of all * Make a clean clone of an object with clean referring to removal of all
* flags indicating missing or non-coherent elements. * flags indicating missing or non-coherent elements.
* @param {Object} obj to clone * @param {Object} obj to clone
...@@ -91,7 +78,6 @@ ...@@ -91,7 +78,6 @@
return r; return r;
}; };
/** /**
* Executes a sequence of *then* callbacks. It acts like * Executes a sequence of *then* callbacks. It acts like
* `smth().then(callback).then(callback)...`. The first callback is called * `smth().then(callback).then(callback)...`. The first callback is called
...@@ -237,11 +223,11 @@ ...@@ -237,11 +223,11 @@
} }
/** /**
* Test a response across multiple storages. Returns first valid document * Test a GET response across multiple storages. Returns first valid document
* together with the following parameters: * together with the following parameters:
* *
* _missing = true > when a storage returns a 404 for this document * _missing = true > when a storage returns a 404 for this document
* _version_list = [] > when a file has multiple (conflicting) versions * _conflict_list = [] > all docs in conflict (including "self")
* *
* Promises are cancelled only by calling * Promises are cancelled only by calling
* *
...@@ -458,14 +444,14 @@ ...@@ -458,14 +444,14 @@
} }
base._conflict_list.push(record); base._conflict_list.push(record);
} }
// need to add record // record does not exists, must add
} else { } else {
must_add = true; must_add = true;
} }
} }
// add missing records // add missing records
if (must_add) { if (must_add) {
// ok on first storage iteration (i), afterwards 404 // if on 2+ iteration, this means a 404 on previous storages
if (i > 0) { if (i > 0) {
record._missing = true; record._missing = true;
} }
...@@ -475,7 +461,7 @@ ...@@ -475,7 +461,7 @@
} }
} }
// if one storage returns rows = 0, make sure all records are labelled // if a storage returns rows = 0, make sure all records are labelled
if (total === 0) { if (total === 0) {
rows = rows || []; rows = rows || [];
for (l = 0; l < rows.length; l += 1) { for (l = 0; l < rows.length; l += 1) {
...@@ -492,66 +478,27 @@ ...@@ -492,66 +478,27 @@
}; };
ReplicateStorage.prototype.repair = function (command, param) { ReplicateStorage.prototype.repair = function (command, param) {
var i, promise_list, length, index, that;
promise_list = [];
length = this._storage_list.length;
console.log("repairing") // NOTE: we can't pass full document, because of select_list...
console.log(command) for (i = 0; i < length; i += 1) {
console.log(param) promise_list[i] = command.storage(this._storage_list[i]).put(param);
}
// var promise_list = [], index, that, length = this._storage_list.length; sequence([function () {
// that = this; return all(promise_list);
// if (typeof param._id !== 'string' || !param._id) { }, function (answers) {
// command.success(); var i;
// return; for (i = 0; i < answers.length; i += 1) {
// } if (answers[i].result !== "success") {
// for (index = 0; index < length; index += 1) { return command.error(409);
// promise_list[index] = }
// success(command.storage(this._storage_list[index]).get(param)); }
// } command.success();
// sequence([function () { }]);
// return all(promise_list);
// }, function (answers) {
// var i, list = [], winner = null;
// for (i = 0; i < answers.length; i += 1) {
// if (answers[i].result === "success") {
// if (isDate(answers[i].data.modified)) {
// list[i] = answers[i].data;
// if (winner === null ||
// new Date(winner.modified) <
// new Date(answers[i].data.modified)) {
// winner = answers[i].data;
// }
// }
// } else if (answers[i].status === 404) {
// list[i] = 0;
// }
// }
// for (i = 0; i < list.length; i += 1) {
// if (list[i] && new Date(list[i].modified) < new Date(winner.modified)) {
// list[i] = success(command.storage(that._storage_list[i]).put(winner));
// } else if (list[i] === 0) {
// list[i] = dictUpdate({}, winner);
// delete list[i]._id;
// list[i] =
// success(command.storage(that._storage_list[i]).post(list[i]));
// }
// }
// list = list.reduce(function (previous, current) {
// if (current) {
// previous.push(current);
// }
// return previous;
// }, []);
// return all(list);
// }, function (answers) {
// var i;
// for (i = 0; i < answers.length; i += 1) {
// if (answers[i].result !== "success") {
// return command.error(409);
// }
// }
// command.success();
// }]);
}; };
addStorageFunction('replicate', ReplicateStorage); addStorageFunction('replicate', ReplicateStorage);
......
...@@ -62,6 +62,7 @@ ...@@ -62,6 +62,7 @@
"not_found": "Page not found", "not_found": "Page not found",
"uploading": "Uploading", "uploading": "Uploading",
"sync": "Synchronizing", "sync": "Synchronizing",
"synced": "Synchronized",
"fetching": "Fetching Data", "fetching": "Fetching Data",
"storing": "Storing Data", "storing": "Storing Data",
"downloading": "Downloading", "downloading": "Downloading",
......
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