Commit e2fd42c7 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

Merge branch '465-getting-list-of-reactions-by-element-id' into 'devel_12.0.x'

Resolve "getting list of reactions by element id doesn't work for big set"

See merge request minerva/core!362
parents 2eaec62f b34ccf07
......@@ -74,6 +74,7 @@ ServerConnector.init = function () {
self.removeListener("onDataLoadStop", listeners[i]);
}
self.MAX_NUMBER_OF_IDS_IN_GET_QUERY = 100;
};
ServerConnector.registerListenerType("onDataLoadStart");
ServerConnector.registerListenerType("onDataLoadStop");
......@@ -226,10 +227,19 @@ ServerConnector._sendRequest = function (params) {
};
ServerConnector.sendPostRequest = function (url, params) {
var formParams = {};
for (var key in params) {
if (params.hasOwnProperty(key)) {
formParams[key] = this.objectToRequestString(params[key]);
if (formParams[key] === undefined) {
formParams[key] = params[key];
}
}
}
return this.sendRequest({
method: "POST",
url: url,
form: params
form: formParams
});
};
......@@ -273,6 +283,25 @@ ServerConnector.getServerBaseUrl = function () {
return this._serverBaseUrl;
};
/**
*
* @param {Object} object
* @returns {string}
*/
ServerConnector.objectToRequestString = function (object) {
var value;
if (object instanceof Point) {
value = this.pointToString(object);
} else if (Object.prototype.toString.call(object) === '[object Array]') {
value = this.idsToString(object);
} else if (typeof object === 'string' || object instanceof String || !isNaN(object)) {
value = object.toString();
} else {
value = undefined;
}
return value;
};
ServerConnector.createGetParams = function (params, prefix) {
var sorted = [], key;
......@@ -290,18 +319,12 @@ ServerConnector.createGetParams = function (params, prefix) {
if (prefix !== undefined) {
key = prefix + "." + key;
}
if (value instanceof Point) {
value = this.pointToString(value);
} else if (Object.prototype.toString.call(value) === '[object Array]') {
value = this.idsToString(value);
} else if (typeof value === 'string' || value instanceof String || !isNaN(value)) {
} else {
result += this.createGetParams(value, key);
value = undefined;
}
if (value !== undefined && value !== "") {
result += key + "=" + value + "&";
var serializedValue = this.objectToRequestString(value);
if (serializedValue === undefined) {
result += this.createGetParams(value, key);
} else if (serializedValue !== undefined && serializedValue !== "") {
result += key + "=" + serializedValue + "&";
}
}
return result;
......@@ -1287,6 +1310,15 @@ ServerConnector.getOverlayById = function (overlayId, projectId) {
});
};
/**
*
* @param {number} [params.modelId]
* @param {number[]} [params.ids]
* @param {number[]} [params.participantId]
* @param {string[]} [params.columns]
* @param {string} [params.projectId]
* @returns {*}
*/
ServerConnector.getReactions = function (params) {
var self = this;
var queryParams = {
......@@ -1305,7 +1337,7 @@ ServerConnector.getReactions = function (params) {
};
return self.getProjectId(params.projectId).then(function (result) {
queryParams.projectId = result;
if (filterParams.id.length > 100 || filterParams.participantId.length > 100) {
if (filterParams.id.length > self.MAX_NUMBER_OF_IDS_IN_GET_QUERY || filterParams.participantId.length > self.MAX_NUMBER_OF_IDS_IN_GET_QUERY) {
return self.sendPostRequest(self.getReactionsUrl(queryParams), filterParams);
} else {
return self.sendGetRequest(self.getReactionsUrl(queryParams, filterParams));
......@@ -1345,7 +1377,7 @@ ServerConnector.getAliases = function (params) {
};
return self.getProjectId(params.projectId).then(function (result) {
queryParams.projectId = result;
if (filterParams.id.length > 100) {
if (filterParams.id.length > self.MAX_NUMBER_OF_IDS_IN_GET_QUERY) {
return self.sendPostRequest(self.getAliasesUrl(queryParams), filterParams);
} else {
return self.sendGetRequest(self.getAliasesUrl(queryParams, filterParams));
......
......@@ -13,6 +13,7 @@ var LayoutAlias = require('../../main/js/map/data/LayoutAlias');
var MapModel = require('../../main/js/map/data/MapModel');
var NetworkError = require('../../main/js/NetworkError');
var Project = require('../../main/js/map/data/Project');
var Point = require('../../main/js/map/canvas/Point');
var Reaction = require('../../main/js/map/data/Reaction');
var ServerConnector = require('../../main/js/ServerConnector');
var SecurityError = require('../../main/js/SecurityError');
......@@ -110,7 +111,7 @@ describe('ServerConnector', function () {
});
});
it('without ids', function () {
return ServerConnector.getReactions([]).then(function (result) {
return ServerConnector.getReactions({}).then(function (result) {
assert.equal(result.length, 28);
var reaction = result[0];
assert.ok(reaction instanceof Reaction);
......@@ -118,6 +119,32 @@ describe('ServerConnector', function () {
assert.equal(reaction.getModelId(), 15781);
});
});
it('with ids', function () {
return ServerConnector.getReactions({
modelId: 15781,
participantId: [329156, 329157, 329158, 329159, 329160, 329161, 329162, 329163, 329164, 329165, 329166, 329167, 329168, 329169, 329170, 329171, 329172, 329173, 329174, 329175, 329176, 329177, 329178, 329179, 329180, 329181, 329182, 329183, 329184, 329185]
}).then(function (result) {
assert.equal(result.length, 28);
var reaction = result[0];
assert.ok(reaction instanceof Reaction);
assert.equal(reaction.getId(), 153524);
assert.equal(reaction.getModelId(), 15781);
});
});
it('with ids when asking for too many elements for gett query', function () {
ServerConnector.MAX_NUMBER_OF_IDS_IN_GET_QUERY = 0;
return ServerConnector.getReactions({
modelId: 15781,
participantId: [329167]
}).then(function (result) {
assert.equal(result.length, 5);
var reaction = result[0];
assert.ok(reaction instanceof Reaction);
assert.equal(reaction.getId(), 153518);
assert.equal(reaction.getModelId(), 15781);
});
});
});
......@@ -420,4 +447,23 @@ describe('ServerConnector', function () {
});
describe('objectToRequestString', function () {
it('string', function () {
assert.equal("test_string", ServerConnector.objectToRequestString("test_string"));
});
it('number', function () {
assert.equal("12", ServerConnector.objectToRequestString(12));
});
it('point', function () {
assert.equal("0.00,10.00", ServerConnector.objectToRequestString(new Point(0, 10)));
});
it('array of ids', function () {
assert.equal("1,4", ServerConnector.objectToRequestString([1, 4]));
});
it('not sorted array of ids', function () {
assert.equal("1,4,5", ServerConnector.objectToRequestString([5, 1, 4]));
});
});
});
[{"centerPoint":{"x":142.99978766600722,"y":179.00261878591084},"hierarchyVisibilityLevel":null,"id":153518,"kineticLaw":null,"lines":[{"start":{"x":141.01308884552796,"y":203.5052375718217},"end":{"x":142.676524190943,"y":182.98953497836953},"type":"START"},{"start":{"x":143.32305114107143,"y":175.01570259345215},"end":{"x":144.98648648648648,"y":154.5},"type":"END"}],"modelId":15781,"modifiers":[],"notes":"","products":[{"aliasId":329167,"stoichiometry":null}],"reactants":[{"aliasId":329169,"stoichiometry":null}],"reactionId":"re8","references":[],"type":"State transition"},{"centerPoint":{"x":92.15973884958811,"y":136.13620918163832},"hierarchyVisibilityLevel":null,"id":153512,"kineticLaw":null,"lines":[{"start":{"x":75.73715058611361,"y":134.3476104598738},"end":{"x":88.18325303297577,"y":135.7031265679479},"type":"START"},{"start":{"x":96.13622466620043,"y":136.56929179532878},"end":{"x":108.58232711306259,"y":137.92480790340286},"type":"END"}],"modelId":15781,"modifiers":[],"notes":"","products":[{"aliasId":329167,"stoichiometry":null}],"reactants":[{"aliasId":329168,"stoichiometry":null}],"reactionId":"re4","references":[],"type":"State transition"},{"centerPoint":{"x":128.5,"y":47.0},"hierarchyVisibilityLevel":null,"id":153510,"kineticLaw":null,"lines":[{"start":{"x":92.0,"y":36.98039215686274},"end":{"x":124.64269540833152,"y":45.94113207287532},"type":"START"},{"start":{"x":132.35730459166848,"y":48.05886792712468},"end":{"x":165.0,"y":57.01960784313726},"type":"END"},{"start":{"x":144.03021943732247,"y":129.5},"end":{"x":131.99804583067973,"y":53.145215648491444},"type":"MIDDLE"}],"modelId":15781,"modifiers":[{"aliasId":329167,"stoichiometry":null}],"notes":"","products":[{"aliasId":329179,"stoichiometry":null}],"reactants":[{"aliasId":329173,"stoichiometry":null}],"reactionId":"re5","references":[],"type":"State transition"},{"centerPoint":{"x":172.17324895317853,"y":109.17253521126761},"hierarchyVisibilityLevel":null,"id":153504,"kineticLaw":null,"lines":[{"start":{"x":155.9662162162162,"y":129.5},"end":{"x":169.67962640764847,"y":112.300129590407},"type":"START"},{"start":{"x":174.6668714987086,"y":106.04494083212823},"end":{"x":188.38028169014086,"y":88.84507042253522},"type":"END"}],"modelId":15781,"modifiers":[],"notes":"","products":[{"aliasId":329179,"stoichiometry":null}],"reactants":[{"aliasId":329167,"stoichiometry":null}],"reactionId":"re3","references":[],"type":"State transition"},{"centerPoint":{"x":196.68292177751908,"y":142.47367216614504},"hierarchyVisibilityLevel":null,"id":153499,"kineticLaw":null,"lines":[{"start":{"x":180.0362865221489,"y":142.31809613572102},"end":{"x":192.68309645382215,"y":142.43629062106376},"type":"START"},{"start":{"x":200.68274710121602,"y":142.5110537112263},"end":{"x":213.32955703288923,"y":142.62924819656908},"type":"END"}],"modelId":15781,"modifiers":[],"notes":"","products":[{"aliasId":329172,"stoichiometry":null}],"reactants":[{"aliasId":329167,"stoichiometry":null}],"reactionId":"re10","references":[],"type":"State transition"}]
\ No newline at end of file
Supports Markdown
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