Commit e4e18e6c authored by Romain Courteaud's avatar Romain Courteaud

ERP5Storage: draft version of bulk to get multiple documents in one request

parent 31ab09f1
......@@ -70,15 +70,9 @@
"TextAreaField": null
};
function extractPropertyFromForm(context, id) {
return context.getAttachment(id, "view")
.push(function (blob) {
return jIO.util.readBlobAsText(blob);
})
.push(function (evt) {
return JSON.parse(evt.target.result);
})
.push(function (json) {
function extractPropertyFromFormJSON(json) {
return new RSVP.Queue()
.push(function () {
var form = json._embedded._view,
converted_json = {
portal_type: json.portal_type
......@@ -116,6 +110,19 @@
});
}
function extractPropertyFromForm(context, id) {
return context.getAttachment(id, "view")
.push(function (blob) {
return jIO.util.readBlobAsText(blob);
})
.push(function (evt) {
return JSON.parse(evt.target.result);
})
.push(function (json) {
return extractPropertyFromFormJSON(json);
});
}
// XXX docstring
function ERP5Storage(spec) {
if (typeof spec.url !== "string" || !spec.url) {
......@@ -126,20 +133,75 @@
this._default_view_reference = spec.default_view_reference;
}
function convertJSONToGet(json) {
var key,
result = json.data;
// Remove all ERP5 hateoas links / convert them into jIO ID
for (key in result) {
if (result.hasOwnProperty(key)) {
if (!result[key]) {
delete result[key];
}
}
}
return result;
}
ERP5Storage.prototype.get = function (id) {
return extractPropertyFromForm(this, id)
.push(function (result) {
var key;
result = result.data;
// Remove all ERP5 hateoas links / convert them into jIO ID
for (key in result) {
if (result.hasOwnProperty(key)) {
if (!result[key]) {
delete result[key];
}
return convertJSONToGet(result);
});
};
ERP5Storage.prototype.bulk = function (request_list) {
var i,
storage = this,
bulk_list = [];
for (i = 0; i < request_list.length; i += 1) {
if (request_list[i].method !== "get") {
throw new Error("ERP5Storage: not supported " +
request_list[i].method + " in bulk");
}
bulk_list.push({
relative_url: request_list[i].parameter_list[0],
view: storage._default_view_reference
});
}
return getSiteDocument(storage)
.push(function (site_hal) {
var form_data = new FormData();
form_data.append("bulk_list", JSON.stringify(bulk_list));
return jIO.util.ajax({
"type": "POST",
"url": site_hal._actions.bulk.href,
"data": form_data,
// "headers": {
// "Content-Type": "application/json"
// },
"xhrFields": {
withCredentials: true
}
});
})
.push(function (response) {
var result_list = [],
hateoas = JSON.parse(response.target.responseText);
function pushResult(json) {
json.portal_type = json._links.type.name;
return extractPropertyFromFormJSON(json)
.push(function (json2) {
return convertJSONToGet(json2);
});
}
return result;
for (i = 0; i < hateoas.result_list.length; i += 1) {
result_list.push(pushResult(hateoas.result_list[i]));
}
return RSVP.all(result_list);
});
};
......
......@@ -14,6 +14,7 @@
traverse_template = domain + "?mode=traverse{&relative_url,view}",
search_template = domain + "?mode=search{&query,select_list*,limit*}",
add_url = domain + "lets?add=somedocument",
bulk_url = domain + "lets?run=bulk",
root_hateoas = JSON.stringify({
"_links": {
traverse: {
......@@ -28,6 +29,9 @@
"_actions": {
add: {
href: add_url
},
bulk: {
href: bulk_url
}
}
});
......@@ -1485,4 +1489,190 @@
});
});
/////////////////////////////////////////////////////////////////
// erp5Storage.bulk
/////////////////////////////////////////////////////////////////
module("erp5Storage.bulk", {
setup: function () {
this.server = sinon.fakeServer.create();
this.server.autoRespond = true;
this.server.autoRespondAfter = 5;
this.spy = sinon.spy(FormData.prototype, "append");
this.jio = jIO.createJIO({
type: "erp5",
url: domain,
default_view_reference: "bar_view"
});
},
teardown: function () {
this.server.restore();
delete this.server;
this.spy.restore();
delete this.spy;
}
});
test("bulk get ERP5 document list", function () {
var id = "person_module/20150119_azerty",
id2 = "person_module/20150219_azerty",
context = this,
document_hateoas = JSON.stringify({
// Kept property
"title": "foo",
// Remove all _ properties
"_bar": "john doo",
"_links": {
type: {
name: "Person"
}
},
"_embedded": {
"_view": {
form_id: {
key: "form_id",
"default": "Base_view"
},
my_title: {
key: "field_my_title",
"default": "foo",
editable: true,
type: "StringField"
},
my_id: {
key: "field_my_id",
"default": "",
editable: true,
type: "StringField"
},
my_title_non_editable: {
key: "field_my_title_non_editable",
"default": "foo",
editable: false,
type: "StringField"
},
my_start_date: {
key: "field_my_start_date",
"default": "foo",
editable: true,
type: "DateTimeField"
},
your_reference: {
key: "field_your_title",
"default": "bar",
editable: true,
type: "StringField"
},
sort_index: {
key: "field_sort_index",
"default": "foobar",
editable: true,
type: "StringField"
},
"_actions": {
put: {
href: "one erp5 url"
}
}
}
}
}),
document_hateoas2 = JSON.stringify({
// Kept property
"title": "foo2",
// Remove all _ properties
"_bar": "john doo2",
"_links": {
type: {
name: "Person"
}
},
"_embedded": {
"_view": {
form_id: {
key: "form_id",
"default": "Base_view"
},
"_actions": {
put: {
href: "one erp5 url"
}
}
}
}
}),
bulk_hateoas = JSON.parse(root_hateoas),
server = this.server;
bulk_hateoas.result_list = [
JSON.parse(document_hateoas),
JSON.parse(document_hateoas2)
];
bulk_hateoas = JSON.stringify(bulk_hateoas);
this.server.respondWith("GET", domain, [200, {
"Content-Type": "application/hal+json"
}, root_hateoas]);
this.server.respondWith("POST", bulk_url, [200, {
"Content-Type": "application/hal+json"
}, bulk_hateoas]);
stop();
expect(15);
this.jio.bulk([{
method: "get",
parameter_list: [id]
}, {
method: "get",
parameter_list: [id2]
}])
.then(function (result_list) {
equal(server.requests.length, 2);
equal(server.requests[0].method, "GET");
equal(server.requests[0].url, domain);
equal(server.requests[0].requestBody, undefined);
equal(server.requests[0].withCredentials, true);
equal(server.requests[1].method, "POST");
equal(server.requests[1].url, bulk_url);
// XXX Check form data
ok(server.requests[1].requestBody instanceof FormData);
ok(context.spy.calledOnce, "FormData.append count " +
context.spy.callCount);
equal(context.spy.firstCall.args[0], "bulk_list", "First append call");
equal(context.spy.firstCall.args[1],
JSON.stringify([{
relative_url: "person_module/20150119_azerty",
view: "bar_view"
}, {
relative_url: "person_module/20150219_azerty",
view: "bar_view"
}]),
"First append call");
equal(server.requests[1].withCredentials, true);
var result = result_list[0],
result2 = result_list[1];
equal(result_list.length, 2);
deepEqual(result, {
portal_type: "Person",
title: "foo"
}, "Check document");
deepEqual(result2, {
portal_type: "Person"
}, "Check document2");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
}(jIO, QUnit, Blob, sinon, encodeURIComponent, FormData));
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