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

next pass at synchronization, still broken

parent 2a7e54ec
......@@ -5564,13 +5564,58 @@
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
* @param {object} response Response object to be synced
* @param {string} storage Name of storage
**/
storage.sync = function (response, storage) {
var i, record, use, repair, promise_list = [];
storage.sync = function (response, storage_name) {
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) {
record = response.data.rows[i];
......@@ -5586,12 +5631,27 @@
}
if (repair) {
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)
.then(function () {
.then(function (response_list) {
if (response_list.length > 0) {
util.loader("", "status_dict.synced", "check");
}
return undefined;
});
};
......@@ -5770,11 +5830,10 @@
// TODO: generate common promise handler for add/remove/clone/export/etc
// TODO: make form validation and captcha generic
storage.add = function (config) {
var property, replace, obj, value, form_to_submit, formData,
valid, pass_id, validate_portal_type_fields, form_data_set, decode;
var property, replace, obj, value, form_to_submit,
valid, pass_id, validate_portal_type_fields, decode, data;
form_to_submit = document.getElementById(config.id);
formData = new FormData();
valid = storage.validate(config);
if (valid === undefined) {
......@@ -5791,30 +5850,23 @@
validate_portal_type_fields = true;
}
for (property in valid) {
if (valid.hasOwnProperty(property)) {
value = valid[property];
// prepare to store
// TODO: add id to captcha fields missing it...
if (property !== "undefined") {
if (config.gadget.state.force_formdata) {
if (property !== "identifier") {
form_data_set = true;
formData.append(property.replace(replace, ""), value);
}
} else {
// overwrite portal_type if forced
if (property !== "identifier") {
obj[property.replace(replace, "")] = value;
} else {
obj[property] = value;
}
// force formData
if (config.gadget.state.force_formdata) {
data = storage.parseForm(valid, replace);
} else {
for (property in valid) {
if (valid.hasOwnProperty(property)) {
value = valid[property];
// prepare to store
if (property !== "undefined") {
obj[property.replace(replace, "")] = value;
}
}
}
}
// force new object to be created
// force new
if (config.gadget.state.create_new) {
delete obj.identifier;
}
......@@ -5831,7 +5883,7 @@
"validate_type": validate_portal_type_fields,
// hacked view
"view": config.gadget.state.view,
"form_data": form_data_set ? formData : undefined,
"form_data": data || undefined,
"fetch": config.gadget.state.url_pointer
}
})
......@@ -7227,7 +7279,6 @@
pass = reply.pass;
if (reply.response) {
// TODO: remove once no need for sample data
if (pass.needs_fields) {
delete pass.needs_fields;
......@@ -7278,6 +7329,7 @@
util.error("store: field definintions not found.");
}
}
// if we have form_data we continue
if (pass.form_data === undefined) {
// loop over item and create a JIO record
......@@ -7314,6 +7366,8 @@
// store
promises[i] = store[method || "post"](obj, hacked_view || {})
.then(function (answer) {
console.log("OUT")
console.log(answer)
return answer;
})
.fail(util.error);
......
......@@ -45,19 +45,6 @@
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
* flags indicating missing or non-coherent elements.
* @param {Object} obj to clone
......@@ -91,7 +78,6 @@
return r;
};
/**
* Executes a sequence of *then* callbacks. It acts like
* `smth().then(callback).then(callback)...`. The first callback is called
......@@ -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:
*
* _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
*
......@@ -458,14 +444,14 @@
}
base._conflict_list.push(record);
}
// need to add record
// record does not exists, must add
} else {
must_add = true;
}
}
// add missing records
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) {
record._missing = true;
}
......@@ -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) {
rows = rows || [];
for (l = 0; l < rows.length; l += 1) {
......@@ -492,66 +478,27 @@
};
ReplicateStorage.prototype.repair = function (command, param) {
var i, promise_list, length, index, that;
promise_list = [];
length = this._storage_list.length;
console.log("repairing")
console.log(command)
console.log(param)
// var promise_list = [], index, that, length = this._storage_list.length;
// that = this;
// if (typeof param._id !== 'string' || !param._id) {
// command.success();
// return;
// }
// for (index = 0; index < length; index += 1) {
// promise_list[index] =
// success(command.storage(this._storage_list[index]).get(param));
// }
// 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();
// }]);
// NOTE: we can't pass full document, because of select_list...
for (i = 0; i < length; i += 1) {
promise_list[i] = command.storage(this._storage_list[i]).put(param);
}
sequence([function () {
return all(promise_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);
......
......@@ -62,6 +62,7 @@
"not_found": "Page not found",
"uploading": "Uploading",
"sync": "Synchronizing",
"synced": "Synchronized",
"fetching": "Fetching Data",
"storing": "Storing Data",
"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