Commit 91b42259 authored by Romain Courteaud's avatar Romain Courteaud

Make erp5 storage compliant with jio API.

All ERP5 callable can be represented as attachment.
Editing (FormData) is done with putAttachment.

It is not possible to directly create/modify.
parent 7cdfd471
...@@ -7,14 +7,13 @@ ...@@ -7,14 +7,13 @@
// { // {
// type: "erp5" // type: "erp5"
// url: {string} // url: {string}
// default_view: {string} (optional)
// } // }
/*jslint nomen: true */ /*jslint nomen: true */
/*global jIO, UriTemplate, FormData, RSVP, URI, /*global jIO, UriTemplate, FormData, RSVP, URI,
module */ Blob, btoa */
(function (jIO, UriTemplate, FormData, RSVP, URI) { (function (jIO, UriTemplate, RSVP, URI, Blob) {
"use strict"; "use strict";
function getSiteDocument(storage) { function getSiteDocument(storage) {
...@@ -45,7 +44,7 @@ ...@@ -45,7 +44,7 @@
"url": UriTemplate.parse(site_hal._links.traverse.href) "url": UriTemplate.parse(site_hal._links.traverse.href)
.expand({ .expand({
relative_url: param._id, relative_url: param._id,
view: options._view || storage._default_view view: options._view
}), }),
"xhrFields": { "xhrFields": {
withCredentials: true withCredentials: true
...@@ -61,42 +60,112 @@ ...@@ -61,42 +60,112 @@
"which contains more than one character."); "which contains more than one character.");
} }
this._url = spec.url; this._url = spec.url;
this._default_view = spec.default_view || "view";
} }
ERP5Storage.prototype.get = function (param, options) { ERP5Storage.prototype.get = function (param) {
return getDocumentAndHateoas(this, param, options) return getDocumentAndHateoas(this, param)
.push(function (response) { .push(function (response) {
var result = JSON.parse(response.target.responseText); var result = JSON.parse(response.target.responseText),
attachments = {
view: {},
links: {}
},
key;
// action_type;
result._id = param._id; result._id = param._id;
result.portal_type = result._links.type.name; result.portal_type = result._links.type.name;
// delete result._embedded;
// delete result._links; result._attachments = attachments;
// delete result._debug;
// Remove all ERP5 hateoas links / convert them into jIO ID
for (key in result) {
if (result.hasOwnProperty(key)) {
if (key.indexOf("_") === 0) {
delete result[key];
}
}
}
return result; return result;
}); });
}; };
ERP5Storage.prototype.put = function (metadata, options) { ERP5Storage.prototype.getAttachment = function (param) {
return getDocumentAndHateoas(this, metadata, options) var action = param._attachment;
.push(function (result) {
result = JSON.parse(result.target.responseText); if (action === "view") {
var put_action = result._embedded._view._actions.put, return getDocumentAndHateoas(this, param, {"_view": param._attachment})
renderer_form = result._embedded._view, .push(function (response) {
var result = JSON.parse(response.target.responseText);
result._id = param._id;
result.portal_type = result._links.type.name;
// Remove all ERP5 hateoas links / convert them into jIO ID
// XXX Change default action to an jio urn with attachment name inside
// if Base_edit, do put URN
// if others, do post URN (ie, unique new attachment name)
// XXX Except this attachment name should be generated when
return new Blob(
[JSON.stringify(result)],
{"type": 'application/hal+json'}
);
});
}
if (action === "links") {
return getDocumentAndHateoas(this, param)
.push(function (response) {
return new Blob(
[JSON.stringify(JSON.parse(response.target.responseText))],
{"type": 'application/hal+json'}
);
});
}
if (action.indexOf(this._url) === 0) {
return new RSVP.Queue()
.push(function () {
return jIO.util.ajax({
"type": "GET",
"url": action,
"xhrFields": {
withCredentials: true
}
});
})
.push(function (evt) {
var result = JSON.parse(evt.target.responseText);
result._id = param._id;
return new Blob(
[JSON.stringify(result)],
{"type": evt.target.getResponseHeader("Content-Type")}
);
});
}
throw new Error("ERP5: not support get attachment: " + action);
};
ERP5Storage.prototype.putAttachment = function (metadata) {
// Assert we use a callable on a document from the ERP5 site
if (metadata._attachment.indexOf(this._url) !== 0) {
throw new Error("Can not store outside ERP5: " +
metadata._attachment);
}
return new RSVP.Queue()
.push(function () {
return jIO.util.readBlobAsText(metadata._blob);
})
.push(function (evt) {
var form_data = JSON.parse(evt.target.result),
data = new FormData(), data = new FormData(),
key; key;
data.append(renderer_form.form_id.key, for (key in form_data) {
renderer_form.form_id['default']); if (form_data.hasOwnProperty(key)) {
for (key in metadata) { data.append(key, form_data[key]);
if (metadata.hasOwnProperty(key)) {
if (key !== "_id") {
data.append(key, metadata[key]);
}
} }
} }
return jIO.util.ajax({ return jIO.util.ajax({
"type": put_action.method, "type": "POST",
"url": put_action.href, "url": metadata._attachment,
"data": data, "data": data,
"xhrFields": { "xhrFields": {
withCredentials: true withCredentials: true
...@@ -158,4 +227,4 @@ ...@@ -158,4 +227,4 @@
jIO.addStorage("erp5", ERP5Storage); jIO.addStorage("erp5", ERP5Storage);
}(jIO, UriTemplate, FormData, RSVP, URI)); }(jIO, UriTemplate, RSVP, URI, Blob));
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