Commit fbd23eb3 authored by Roque's avatar Roque

erp5_officejs: new storage "configuration"

- new storage layer on top of appcache storage to manage officejs configuration elemnts
- the elements listed in ".configuration" app manifest are got from appcache and stored in current app storage as documents
- storage cleaner to handle data migration
parent 14354bad
/*jslint indent:2, maxlen: 80, nomen: true */
/*global jIO, RSVP, window, URL, atob, btoa, Rusha */
(function (window, jIO, RSVP, URL, atob, btoa, Rusha) {
"use strict";
var rusha = new Rusha();
function decodeDocumentId(id, hateoas_appcache) {
var hateoas_section,
hateoas_section_and_view;
hateoas_section = "./" + hateoas_appcache + "/";
hateoas_section_and_view = hateoas_section + "definition_view/";
id = id.replace(hateoas_section_and_view, "");
id = atob(id);
return id;
}
function encodeDocumentId(id, hateoas_appcache) {
var hateoas_section,
hateoas_section_and_view;
id = btoa(id);
hateoas_section = "./" + hateoas_appcache + "/";
hateoas_section_and_view = hateoas_section + "definition_view/";
id = hateoas_section_and_view + id;
return id;
}
function processHateoasDict(raw_dict) {
var raw_field_list, type, parent, field_key, field_id, return_dict = {};
return_dict.raw_dict = raw_dict;
/*jslint nomen: true*/
if (raw_dict.hasOwnProperty("_embedded") &&
raw_dict._embedded.hasOwnProperty("_view")) {
raw_field_list = raw_dict._embedded._view;
type = raw_dict._links.type.name;
parent = raw_dict._links.parent.name;
return_dict.parent_relative_url = "portal_types/" + parent;
return_dict.portal_type = type;
for (field_key in raw_field_list) {
if (raw_field_list.hasOwnProperty(field_key)) {
field_id = "";
if (raw_field_list[field_key]["default"] !== undefined &&
raw_field_list[field_key]["default"] !== "") {
if (field_key.startsWith("my_")) {
field_id = field_key.replace("my_", "");
} else if (field_key.startsWith("your_")) {
field_id = field_key.replace("your_", "");
} else {
field_id = field_key;
}
return_dict[field_id] = raw_field_list[field_key]["default"];
}
}
}
} else {
// ignore non configuration elements
return raw_dict;
}
return return_dict;
}
function ConfigurationStorage(spec) {
if (spec.sub_storage.type !== "appcache") {
throw new Error("appcache substorage is mandatory for configuration " +
"storage");
}
this._sub_storage = jIO.createJIO(spec.sub_storage);
this._hateoas_appcache = spec.hateoas_appcache;
this._manifest = spec.manifest;
this._origin_url = spec.origin_url !== undefined ?
spec.origin_url : window.location.href;
this._version = spec.version || "";
this._prefix = spec.prefix || "./";
this._version = this._prefix + this._version;
this._hash = "";
}
ConfigurationStorage.prototype.get = function (id) {
var storage = this;
id = encodeDocumentId(id, storage._hateoas_appcache);
return storage._sub_storage.getAttachment(storage._origin_url,
id,
{"format": "json"})
.push(function (content) {
content = processHateoasDict(content);
content.hash = storage._hash;
return content;
});
};
ConfigurationStorage.prototype.hasCapacity = function () {
return true;
};
ConfigurationStorage.prototype.getAttachment = function () {
return this._sub_storage.getAttachment.apply(this._sub_storage, arguments);
};
ConfigurationStorage.prototype.allAttachments = function () {
return this._sub_storage.allAttachments.apply(this._sub_storage, arguments);
};
ConfigurationStorage.prototype.buildQuery = function () {
var storage = this,
result = [],
decoded_id,
attachment_id;
return storage.allAttachments(storage._origin_url)
.push(function (all_attachments) {
for (attachment_id in all_attachments) {
if (all_attachments.hasOwnProperty(attachment_id)) {
if (attachment_id !== storage._version &&
attachment_id !== storage._version + "/" &&
attachment_id !== storage._version + storage._manifest) {
decoded_id = decodeDocumentId(attachment_id,
storage._hateoas_appcache);
result.push({
'id': decoded_id,
'value': {hash: storage._hash},
'doc': {hash: storage._hash}
});
}
}
}
return result;
});
};
ConfigurationStorage.prototype.repair = function (app_version) {
var storage = this,
url = new URL(storage._manifest, new URL(storage._version,
storage._origin_url));
return new RSVP.Queue()
.push(function () {
return jIO.util.ajax({
type: "GET",
url: url
});
})
.push(function (response) {
var text = response.target.responseText;
//hash is attached to manifest text and app version
//if the app version has changed, then a cleanup was done in the storage
//documents must be updated to restore any potential missing document
storage._hash = rusha.digestFromString(text + app_version);
return storage._sub_storage.repair.apply(storage._sub_storage,
arguments);
});
};
jIO.addStorage('configuration', ConfigurationStorage);
}(window, jIO, RSVP, URL, atob, btoa, Rusha));
......@@ -12,6 +12,7 @@
<script src="jiodev.js" type="text/javascript"></script>
<script src="jio_ojs_storage.js" type="text/javascript"></script>
<script src="jio_appcachestorage.js"></script>
<script src="jio_configuration_storage.js"></script>
<!-- custom script -->
<script src="gadget_ojs_local_jio.js" type="text/javascript"></script>
......
......@@ -236,7 +236,7 @@
</item>
<item>
<key> <string>serial</string> </key>
<value> <string>976.52707.23546.23773</string> </value>
<value> <string>977.15823.10779.36625</string> </value>
</item>
<item>
<key> <string>state</string> </key>
......@@ -254,7 +254,7 @@
</tuple>
<state>
<tuple>
<float>1563367270.05</float>
<float>1567674891.35</float>
<string>UTC</string>
</tuple>
</state>
......
......@@ -234,7 +234,7 @@
</item>
<item>
<key> <string>serial</string> </key>
<value> <string>978.6623.19198.10018</string> </value>
<value> <string>978.21248.62721.2184</string> </value>
</item>
<item>
<key> <string>state</string> </key>
......@@ -252,7 +252,7 @@
</tuple>
<state>
<tuple>
<float>1567499774.35</float>
<float>1568041535.95</float>
<string>UTC</string>
</tuple>
</state>
......
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