Commit d8604d34 authored by Romain Courteaud's avatar Romain Courteaud

ERP5Storage: extract local_roles from query string

ERP5 requires local_roles to be provided as an explicit parameter.
parent 81ed810e
......@@ -10,9 +10,11 @@
// }
/*jslint nomen: true, unparam: true */
/*global jIO, UriTemplate, FormData, RSVP, URI, Blob*/
/*global jIO, UriTemplate, FormData, RSVP, URI, Blob, objectToSearchText,
SimpleQuery, ComplexQuery*/
(function (jIO, UriTemplate, FormData, RSVP, URI, Blob) {
(function (jIO, UriTemplate, FormData, RSVP, URI, Blob, objectToSearchText,
SimpleQuery, ComplexQuery) {
"use strict";
function getSiteDocument(storage) {
......@@ -398,6 +400,38 @@
(name === "sort"));
};
function isSingleLocalRoles(parsed_query) {
if ((parsed_query instanceof SimpleQuery) &&
(parsed_query.key === 'local_roles')) {
// local_roles:"Assignee"
return parsed_query.value;
}
}
function isMultipleLocalRoles(parsed_query) {
var i,
sub_query,
is_multiple = true,
local_role_list = [];
if ((parsed_query instanceof ComplexQuery) &&
(parsed_query.operator === 'OR')) {
for (i = 0; i < parsed_query.query_list.length; i += 1) {
sub_query = parsed_query.query_list[i];
if ((sub_query instanceof SimpleQuery) &&
(sub_query.key === 'local_roles')) {
local_role_list.push(sub_query.value);
} else {
is_multiple = false;
}
}
if (is_multiple) {
// local_roles:"Assignee" OR local_roles:"Assignor"
return local_role_list;
}
}
}
ERP5Storage.prototype.buildQuery = function (options) {
// if (typeof options.query !== "string") {
// options.query = (options.query ?
......@@ -406,15 +440,63 @@
// }
return getSiteDocument(this)
.push(function (site_hal) {
var query = options.query,
i,
parsed_query,
sub_query,
result_list,
local_roles;
if (options.query) {
parsed_query = jIO.QueryFactory.create(options.query);
result_list = isSingleLocalRoles(parsed_query);
if (result_list) {
query = undefined;
local_roles = result_list;
} else {
result_list = isMultipleLocalRoles(parsed_query);
if (result_list) {
query = undefined;
local_roles = result_list;
} else if ((parsed_query instanceof ComplexQuery) &&
(parsed_query.operator === 'AND')) {
// portal_type:"Person" AND local_roles:"Assignee"
for (i = 0; i < parsed_query.query_list.length; i += 1) {
sub_query = parsed_query.query_list[i];
result_list = isSingleLocalRoles(sub_query);
if (result_list) {
local_roles = result_list;
parsed_query.query_list.splice(i, 1);
query = objectToSearchText(parsed_query);
i = parsed_query.query_list.length;
} else {
result_list = isMultipleLocalRoles(sub_query);
if (result_list) {
local_roles = result_list;
parsed_query.query_list.splice(i, 1);
query = objectToSearchText(parsed_query);
i = parsed_query.query_list.length;
}
}
}
}
}
}
return jIO.util.ajax({
"type": "GET",
"url": UriTemplate.parse(site_hal._links.raw_search.href)
.expand({
query: options.query,
query: query,
// XXX Force erp5 to return embedded document
select_list: options.select_list || ["title", "reference"],
limit: options.limit,
sort_on: options.sort_on
sort_on: options.sort_on,
local_roles: local_roles
}),
"xhrFields": {
withCredentials: true
......@@ -446,4 +528,5 @@
jIO.addStorage("erp5", ERP5Storage);
}(jIO, UriTemplate, FormData, RSVP, URI, Blob));
}(jIO, UriTemplate, FormData, RSVP, URI, Blob, objectToSearchText,
SimpleQuery, ComplexQuery));
......@@ -12,7 +12,8 @@
module = QUnit.module,
domain = "https://example.org",
traverse_template = domain + "?mode=traverse{&relative_url,view}",
search_template = domain + "?mode=search{&query,select_list*,limit*}",
search_template = domain + "?mode=search{&query,select_list*,limit*," +
"local_roles*}",
add_url = domain + "lets?add=somedocument",
bulk_url = domain + "lets?run=bulk",
root_hateoas = JSON.stringify({
......@@ -1056,6 +1057,266 @@
});
});
test("extract simple single local_roles", function () {
var search_url = domain + "?mode=search&" +
"select_list=destination&select_list=source&limit=5" +
"&local_roles=Assignee",
search_hateoas = JSON.stringify({
"_embedded": {
"contents": []
}
}),
server = this.server;
this.server.respondWith("GET", domain, [200, {
"Content-Type": "application/hal+json"
}, root_hateoas]);
this.server.respondWith("GET", search_url, [200, {
"Content-Type": "application/hal+json"
}, search_hateoas]);
stop();
expect(10);
this.jio.allDocs({
limit: [5],
select_list: ["destination", "source"],
query: 'local_roles:"Assignee"'
})
.then(function (result) {
deepEqual(result, {
data: {
rows: [],
total_rows: 0
}
}, "Check document");
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, "GET");
equal(server.requests[1].url, search_url);
equal(server.requests[1].requestBody, undefined);
equal(server.requests[1].withCredentials, true);
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("extract simple multiple local_roles", function () {
var search_url = domain + "?mode=search&" +
"select_list=destination&select_list=source&limit=5&" +
"local_roles=Assignee&" +
"local_roles=Assignor",
search_hateoas = JSON.stringify({
"_embedded": {
"contents": []
}
}),
server = this.server;
this.server.respondWith("GET", domain, [200, {
"Content-Type": "application/hal+json"
}, root_hateoas]);
this.server.respondWith("GET", search_url, [200, {
"Content-Type": "application/hal+json"
}, search_hateoas]);
stop();
expect(10);
this.jio.allDocs({
limit: [5],
select_list: ["destination", "source"],
query: 'local_roles:"Assignee" OR local_roles:"Assignor"'
})
.then(function (result) {
deepEqual(result, {
data: {
rows: [],
total_rows: 0
}
}, "Check document");
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, "GET");
equal(server.requests[1].url, search_url);
equal(server.requests[1].requestBody, undefined);
equal(server.requests[1].withCredentials, true);
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("extract complex AND single local_roles", function () {
var search_url = domain + "?mode=search&" +
"query=%28%20portal_type%3A%20%20%22Person%22%20%29&" +
"select_list=destination&select_list=source&limit=5&" +
"local_roles=Assignee",
search_hateoas = JSON.stringify({
"_embedded": {
"contents": []
}
}),
server = this.server;
this.server.respondWith("GET", domain, [200, {
"Content-Type": "application/hal+json"
}, root_hateoas]);
this.server.respondWith("GET", search_url, [200, {
"Content-Type": "application/hal+json"
}, search_hateoas]);
stop();
expect(10);
this.jio.allDocs({
limit: [5],
select_list: ["destination", "source"],
query: 'portal_type:"Person" AND local_roles:"Assignee"'
})
.then(function (result) {
deepEqual(result, {
data: {
rows: [],
total_rows: 0
}
}, "Check document");
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, "GET");
equal(server.requests[1].url, search_url);
equal(server.requests[1].requestBody, undefined);
equal(server.requests[1].withCredentials, true);
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("extract complex OR single local_roles", function () {
var search_url = domain + "?mode=search&" +
"query=portal_type%3A%22Person%22%20OR%20" +
"local_roles%3A%22Assignee%22&" +
"select_list=destination&select_list=source&limit=5",
search_hateoas = JSON.stringify({
"_embedded": {
"contents": []
}
}),
server = this.server;
this.server.respondWith("GET", domain, [200, {
"Content-Type": "application/hal+json"
}, root_hateoas]);
this.server.respondWith("GET", search_url, [200, {
"Content-Type": "application/hal+json"
}, search_hateoas]);
stop();
expect(10);
this.jio.allDocs({
limit: [5],
select_list: ["destination", "source"],
query: 'portal_type:"Person" OR local_roles:"Assignee"'
})
.then(function (result) {
deepEqual(result, {
data: {
rows: [],
total_rows: 0
}
}, "Check document");
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, "GET");
equal(server.requests[1].url, search_url);
equal(server.requests[1].requestBody, undefined);
equal(server.requests[1].withCredentials, true);
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("extract sub multiple local_roles", function () {
var search_url = domain + "?mode=search&" +
"query=%28%20portal_type%3A%20%20%22Person%22%20%29&" +
"select_list=destination&select_list=source&limit=5&" +
"local_roles=Assignee&local_roles=Assignor",
search_hateoas = JSON.stringify({
"_embedded": {
"contents": []
}
}),
server = this.server;
this.server.respondWith("GET", domain, [200, {
"Content-Type": "application/hal+json"
}, root_hateoas]);
this.server.respondWith("GET", search_url, [200, {
"Content-Type": "application/hal+json"
}, search_hateoas]);
stop();
expect(10);
this.jio.allDocs({
limit: [5],
select_list: ["destination", "source"],
query: 'portal_type:"Person" AND (local_roles:"Assignee" OR ' +
'local_roles:"Assignor")'
})
.then(function (result) {
deepEqual(result, {
data: {
rows: [],
total_rows: 0
}
}, "Check document");
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, "GET");
equal(server.requests[1].url, search_url);
equal(server.requests[1].requestBody, undefined);
equal(server.requests[1].withCredentials, true);
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// erp5Storage.put
/////////////////////////////////////////////////////////////////
......
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