Newer
Older
var Promise = require("bluebird");
var request = require('request');
var Chemical = require('./map/data/Chemical');
var Comment = require('./map/data/Comment');
var Drug = require('./map/data/Drug');
var ConfigurationType = require('./ConfigurationType');
var IdentifiedElement = require('./map/data/IdentifiedElement');
var LayoutAlias = require('./map/data/LayoutAlias');
var LayoutData = require('./map/data/LayoutData');
var LayoutReaction = require('./map/data/LayoutReaction');
var Project = require('./map/data/Project');
var ReferenceGenome = require('./map/data/ReferenceGenome');
var SessionData = require('./SessionData');
var User = require('./map/data/User');
var GuiConnector = require('./GuiConnector');
/**
* This object contains methods that will communicate with server.
*/
ServerConnector._configurationParam = [];
ServerConnector.getMinOverlayColorInt = function() {
return self.getLoggedUser().then(function(user) {
userColor = user.getMinColor();
return self.getConfigurationParam(ConfigurationType.MIN_COLOR_VAL);
if (userColor === null || userColor === undefined || userColor === "") {
color = systemMinColor;
}
color = parseInt(color, 16);
/* jslint bitwise: true */
color = (color & 0xFFFFFF);
return color;
ServerConnector.getMaxOverlayColorInt = function() {
return self.getLoggedUser().then(function(user) {
userColor = user.getMaxColor();
return self.getConfigurationParam(ConfigurationType.MAX_COLOR_VAL);
if (userColor === null || userColor === undefined || userColor === "") {
color = systemMaxColor;
}
color = parseInt(color, 16);
/* jslint bitwise: true */
color = (color & 0xFFFFFF);
return color;
ServerConnector.readFile = function(url) {
return new Promise(function(resolve, reject) {
request.get(url, function(error, response, body) {
if (error) {
reject(error);
} else if (response.statusCode !== 200) {
reject(response);
}
});
});
};
ServerConnector.sendPostRequest = function(url, params) {
return new Promise(function(resolve, reject) {
request.post({
url : url,
form : params
}, function(error, response, body) {
if (error) {
reject(error);
} else if (response.statusCode !== 200) {
reject(response);
} else {
resolve(body);
}
});
});
};
ServerConnector.sendPutRequest = function(url, params) {
return new Promise(function(resolve, reject) {
request.put({
url : url,
form : params
}, function(error, response, body) {
if (error) {
reject(error);
} else if (response.statusCode !== 200) {
reject(response);
} else {
resolve(body);
}
});
});
};
ServerConnector.sendDeleteRequest = function(url, params) {
return new Promise(function(resolve, reject) {
request.del({
url : url,
form : params
}, function(error, response, body) {
if (error) {
reject(error);
} else if (response.statusCode !== 200) {
reject(response);
} else {
resolve(body);
}
});
});
};
ServerConnector.getToken = function() {
var self = this;
var token = self.getSessionData(null).getToken();
if (token === undefined) {
if (self.getSessionData().getProject() === null) {
return self.isValidToken(token).then(function(isOk) {
if (isOk) {
return token;
} else {
return self.login();
}
});
ServerConnector.getApiBaseUrl = function() {
return this.getServerBaseUrl() + "/api/";
ServerConnector.getServerBaseUrl = function() {
if (this._serverBaseUrl === undefined) {
var url = "" + window.location.href;
if (!url.endsWith("/")) {
url = url.substr(0, url.lastIndexOf("/") + 1);
}
this._serverBaseUrl = url;
}
return this._serverBaseUrl;
};
ServerConnector.createGetParams = function(params) {
var sorted = [], key;
for (key in params) {
if (params.hasOwnProperty(key)) {
sorted.push(key);
}
}
sorted.sort();
for (var i = 0; i < sorted.length; i++) {
if (params[sorted[i]] !== undefined) {
result += sorted[i] + "=" + params[sorted[i]] + "&";
}
return result;
ServerConnector.getApiUrl = function(paramObj) {
var type = paramObj.type;
var method = paramObj.method;
var params = this.createGetParams(paramObj.params);
var result = this.getApiBaseUrl() + "/" + type + "/" + method;
if (params !== "") {
result += "?" + params;
return result;
ServerConnector.getProjectUrl = function(projectId, token) {
return this.getApiUrl({
type : "project",
method : "getMetaData",
params : {
projectId : projectId,
token : token,
},
});
ServerConnector.getImageConvertersUrl = function(params) {
return this.getApiUrl({
type : "configuration",
method : "getImageFormats",
params : {
token : params.token,
ServerConnector.getModelConvertersUrl = function(params) {
return this.getApiUrl({
type : "configuration",
method : "getModelFormats",
params : {
token : params.token,
ServerConnector.getPublicationsUrl = function(params) {
var projectId = params.projectId;
var token = params.token;
var start = params.start || 0;
var length = params.length || 10;
return this.getApiUrl({
type : "project",
method : "getPublications",
params : {
projectId : projectId,
token : token,
start : start,
length : length,
},
});
};
ServerConnector.getReferenceGenomeUrl = function(params) {
return this.getApiUrl({
type : "genomics",
method : "getReferenceGenome",
params : {
token : params.token,
type : params.type,
version : params.version,
organism : params.organism,
},
});
};
ServerConnector.loginUrl = function() {
return this.getApiUrl({
type : "user",
method : "login"
ServerConnector.getSuggestedQueryListUrl = function(params) {
return this.getApiUrl({
type : "project",
method : "getSuggestedQueryList",
params : params,
ServerConnector.addCommentUrl = function() {
return this.getApiUrl({
type : "comment",
method : "addComment",
ServerConnector.addOverlayUrl = function() {
return this.getApiUrl({
type : "overlay",
method : "addOverlay",
});
};
ServerConnector.updateOverlayUrl = function() {
return this.getApiUrl({
type : "overlay",
method : "updateOverlay",
});
};
ServerConnector.deleteOverlayUrl = function() {
return this.getApiUrl({
type : "overlay",
method : "removeOverlay",
ServerConnector.getOverlaysUrl = function(projectId, token) {
return this.getApiUrl({
type : "overlay",
method : "getOverlayList",
params : {
projectId : projectId,
token : token,
},
});
ServerConnector.getOverlayTypesUrl = function(params) {
return this.getApiUrl({
type : "overlay",
method : "getOverlayTypes",
params : {
token : params.token,
ServerConnector.getCommentsUrl = function(params) {
var elementId = params.elementId;
var elementType = params.elementType;
var columns = this.columnsToString(params.columns);
var projectId = params.projectId;
var token = params.token;
return this.getApiUrl({
type : "comment",
method : "getCommentList",
params : {
projectId : projectId,
columns : columns,
elementId : elementId,
elementType : elementType,
token : token
},
ServerConnector.getOverlayByIdUrl = function(overlayId, projectId, token) {
return this.getApiUrl({
type : "overlay",
method : "getOverlayById",
params : {
projectId : projectId,
token : token,
overlayId : overlayId,
},
});
};
ServerConnector.getOverlayElementsUrl = function(overlayId, projectId, token) {
return this.getApiUrl({
type : "overlay",
method : "getOverlayElements",
params : {
projectId : projectId,
token : token,
overlayId : overlayId,
},
});
ServerConnector.getFullOverlayElementUrl = function(params, token) {
return this.getApiUrl({
type : "overlay",
method : "getOverlayElement",
params : {
projectId : params.projectId,
token : token,
overlayId : params.overlay.getId(),
modelId : params.element.getModelId(),
elementId : params.element.getId(),
elementType : params.element.getType(),
},
});
};
ServerConnector.idsToString = function(ids) {
var result = "";
if (ids !== undefined) {
ids.sort(function(a, b) {
return a - b;
});
for (var i = 0; i < ids.length; i++) {
if (result !== "") {
result = result + "," + ids[i];
} // we ignore duplicates
return result;
};
ServerConnector.pointToString = function(point) {
return point.x.toFixed(2) + "," + point.y.toFixed(2);
ServerConnector.columnsToString = function(columns) {
return "";
return columns;
};
ServerConnector.getReactionsUrl = function(reactionIds, projectId, token, columns) {
var id = this.idsToString(reactionIds);
columns = this.columnsToString(columns);
return this.getApiUrl({
type : "project",
method : "getReactions",
params : {
projectId : projectId,
token : token,
columns : columns,
id : id,
},
});
ServerConnector.getAliasesUrl = function(params) {
var id = this.idsToString(params.ids);
var columns = this.columnsToString(params.columns);
var projectId = params.projectId;
var token = params.token;
return this.getApiUrl({
type : "project",
method : "getElements",
params : {
projectId : projectId,
columns : columns,
id : id,
token : token
},
ServerConnector.getConfigurationUrl = function(token) {
var result = this.getApiUrl({
type : "configuration",
method : "getAllValues",
params : {
token : token,
},
});
return result;
ServerConnector.getClosestElementsByCoordinatesUrl = function(params) {
var coordinates = this.pointToString(params.coordinates);
var projectId = params.projectId;
var modelId = params.modelId;
var token = params.token;
return this.getApiUrl({
type : "project",
method : "getClosestElementsByCoordinates",
params : {
projectId : projectId,
coordinates : coordinates,
modelId : modelId,
count : count,
token : token
},
ServerConnector.getElementsByQueryUrl = function(params) {
var query = params.query;
var projectId = params.projectId;
var token = params.token;
var perfectMatch = params.perfectMatch;
return this.getApiUrl({
type : "project",
method : "getElementsByQuery",
params : {
projectId : projectId,
query : query,
perfectMatch : perfectMatch,
token : token
},
ServerConnector.getDrugsByQueryUrl = function(params) {
var query = params.query;
var projectId = params.projectId;
var token = params.token;
return this.getApiUrl({
type : "drug",
method : "getDrugsByQuery",
params : {
projectId : projectId,
query : query,
token : token
},
});
};
ServerConnector.getDrugsByTargetUrl = function(params) {
var query = params.query;
var projectId = params.projectId;
var token = params.token;
var columns = this.idsToString(params.columns);
var targetId = params.target.getId();
var targetType = params.target.getType();
return this.getApiUrl({
type : "drug",
method : "getDrugsByTarget",
params : {
projectId : projectId,
query : query,
columns : columns,
token : token,
targetId : targetId,
targetType : targetType,
},
ServerConnector.getMiRnasByQueryUrl = function(params) {
var query = params.query;
var projectId = params.projectId;
var token = params.token;
return this.getApiUrl({
type : "miRna",
method : "getMiRnasByQuery",
params : {
projectId : projectId,
query : query,
token : token
},
ServerConnector.getOverlaySourceUrl = function(params) {
var overlayId = params.overlayId;
var projectId = params.projectId;
var token = params.token;
return this.getApiUrl({
type : "overlay",
method : "getOverlaySource",
params : {
overlayId : overlayId,
projectId : projectId,
token : token
},
ServerConnector.getImageUrl = function(params) {
var projectId = params.projectId;
var token = params.token;
var polygonString = params.polygonString;
var modelId = params.modelId;
var handlerClass = params.handlerClass;
var backgroundOverlayId = params.backgroundOverlayId.replace(/cv/g, '');
var zoomLevel = params.zoomLevel;
var overlayIds = this.idsToString(params.overlayIds);
return this.getApiUrl({
type : "project",
method : "getModelAsImage",
params : {
polygonString : polygonString,
modelId : modelId,
handlerClass : handlerClass,
backgroundOverlayId : backgroundOverlayId,
zoomLevel : zoomLevel,
overlayIds : overlayIds,
projectId : projectId,
token : token,
},
ServerConnector.getModelPartUrl = function(params) {
var projectId = params.projectId;
var token = params.token;
var polygonString = params.polygonString;
var modelId = params.modelId;
var handlerClass = params.handlerClass;
var backgroundOverlayId = params.backgroundOverlayId.replace(/cv/g, '');
var zoomLevel = params.zoomLevel;
var overlayIds = this.idsToString(params.overlayIds);
return this.getApiUrl({
type : "project",
method : "getModelAsModelFile",
params : {
polygonString : polygonString,
modelId : modelId,
handlerClass : handlerClass,
backgroundOverlayId : backgroundOverlayId,
zoomLevel : zoomLevel,
overlayIds : overlayIds,
projectId : projectId,
token : token,
},
ServerConnector.getProjectSourceUrl = function(params) {
var projectId = params.projectId;
var token = params.token;
return this.getApiUrl({
type : "project",
method : "getProjectSource",
params : {
projectId : projectId,
token : token
},
ServerConnector.getMiRnasByTargetUrl = function(params) {
var query = params.query;
var projectId = params.projectId;
var token = params.token;
var columns = this.idsToString(params.columns);
var targetId = params.target.getId();
var targetType = params.target.getType();
return this.getApiUrl({
type : "miRna",
method : "getMiRnasByTarget",
params : {
projectId : projectId,
query : query,
columns : columns,
token : token,
targetId : targetId,
targetType : targetType,
},
ServerConnector.getChemicalsByQueryUrl = function(params) {
var query = params.query;
var projectId = params.projectId;
var token = params.token;
return this.getApiUrl({
type : "chemical",
method : "getChemicalsByQuery",
params : {
projectId : projectId,
query : query,
token : token
},
});
};
ServerConnector.getChemicalsByTargetUrl = function(params) {
var query = params.query;
var projectId = params.projectId;
var token = params.token;
var columns = this.idsToString(params.columns);
var targetId = params.target.getId();
var targetType = params.target.getType();
return this.getApiUrl({
type : "chemical",
method : "getChemicalsByTarget",
params : {
projectId : projectId,
query : query,
columns : columns,
token : token,
targetId : targetId,
targetType : targetType,
},
ServerConnector.getUserUrl = function(params) {
var userId = params.userId;
var token = params.token;
return this.getApiUrl({
type : "user",
method : "getUser",
params : {
userId : userId,
token : token,
},
ServerConnector.isValidTokenUrl = function(params) {
var token = params.token;
return this.getApiUrl({
type : "user",
method : "tokenStatus",
params : {
token : token,
},
ServerConnector.getConfigurationParam = function(paramId) {
var self = this;
return new Promise(function(resolve, reject) {
if (paramId === undefined) {
reject(new Error("Invalid param identifier"));
} else if (self._configurationParam[paramId] !== undefined) {
resolve(self._configurationParam[paramId]);
} else {
return self.getToken().then(function(token) {
return self.readFile(self.getConfigurationUrl(token));
}).then(function(content) {
var configs = JSON.parse(content);
for (var i = 0; i < configs.length; i++) {
var conf = configs[i];
var type = conf.type;
var value = conf.value;
self._configurationParam[type] = value;
}
self._configurationParam[ConfigurationType.LEGEND_FILES] = [];
if (self._configurationParam["LENGEND_FILE_1"] !== undefined) {
self._configurationParam[ConfigurationType.LEGEND_FILES].push(self._configurationParam["LENGEND_FILE_1"]);
}
if (self._configurationParam["LENGEND_FILE_2"] !== undefined) {
self._configurationParam[ConfigurationType.LEGEND_FILES].push(self._configurationParam["LENGEND_FILE_2"]);
}
if (self._configurationParam["LENGEND_FILE_3"] !== undefined) {
self._configurationParam[ConfigurationType.LEGEND_FILES].push(self._configurationParam["LENGEND_FILE_3"]);
}
if (self._configurationParam["LENGEND_FILE_4"] !== undefined) {
self._configurationParam[ConfigurationType.LEGEND_FILES].push(self._configurationParam["LENGEND_FILE_4"]);
}
if (self._configurationParam[paramId] === undefined) {
reject(new Error("Cannot find param config: " + paramId));
resolve(self._configurationParam[paramId]);
});
};
ServerConnector.getProject = function(projectId) {
return self.getProjectId(projectId).then(function(result) {
projectId = result;
return self.getToken();
}).then(function(token) {
return self.readFile(self.getProjectUrl(projectId, token));
}).then(function(content) {
project = new Project(content);
return self.getOverlays(projectId);
}).then(function(overlays) {
project.getModel().addLayouts(overlays);
return project;
ServerConnector.getLoggedUser = function() {
var self = this;
if (self._loggedUser !== undefined) {
return Promise.resolve(self._loggedUser);
} else {
return self.getUser().then(function(user) {
self._loggedUser = user;
return self._loggedUser;
});
}
};
ServerConnector.getUser = function(userId) {
return self.readFile(self.getUserUrl({
token : token,
userId : userId
}));
}).then(function(content) {
var obj = JSON.parse(content);
return new User(obj);
});
};
ServerConnector.getOverlays = function(projectId) {
var self = this;
return new Promise(function(resolve, reject) {
self.getProjectId(projectId).then(function(result) {
projectId = result;
return self.getToken();
}).then(function(token) {
return self.readFile(self.getOverlaysUrl(projectId, token));
}).then(function(content) {
var arr = JSON.parse(content);
var result = [];
for (var i = 0; i < arr.length; i++) {
var layout = new LayoutData(arr[i]);
result.push(layout);
}
resolve(result);
}, reject);
});
};
ServerConnector.getOverlayElements = function(layoutId, projectId) {
var self = this;
if (layoutId === undefined) {
throw new Error("Layout id must be defined");
var token = null;
return self.getToken().then(function(result) {
token = result;
return self.getProjectId(projectId);
}).then(function(result) {
projectId = result;
return self.readFile(self.getOverlayElementsUrl(layoutId, projectId, token));
}).then(function(content) {
var arr = JSON.parse(content);
var result = [];
for (var i = 0; i < arr.length; i++) {
var element = arr[i];
if (element.type === "REACTION") {
result.push(new LayoutReaction(element.overlayContent));
} else if (element.type === "ALIAS") {
result.push(new LayoutAlias(element.overlayContent));
} else {
throw new Error("Unknown element type: " + element.type);
ServerConnector.getFullOverlayElement = function(params) {
var self = this;
return self.getToken().then(function(result) {
token = result;
return self.getProjectId(params.projectId);
}).then(function(result) {
params.projectId = result;
return self.readFile(self.getFullOverlayElementUrl(params, token));
}).then(function(content) {
var element = JSON.parse(content);
var result = null;
if (element.type === "REACTION") {
result = new LayoutReaction(element.overlayContent);
} else if (element.type === "ALIAS") {
result = new LayoutAlias(element.overlayContent);
} else {
throw new Error("Unknown element type: " + element.type);
}
return result;
ServerConnector.getProjectId = function(projectId) {
var self = this;
return new Promise(function(resolve, reject) {
if (projectId === undefined || projectId === null || projectId === "") {
if (GuiConnector.getParams['id'] !== undefined) {
resolve(GuiConnector.getParams['id']);
} else {
self.getConfigurationParam(ConfigurationType.DEFAULT_MAP).then(function(defaultMap) {
resolve(defaultMap);
}, reject);
}
} else {
ServerConnector.getLogoImg = function() {
return this.getConfigurationParam(ConfigurationType.LOGO_IMG);
};
ServerConnector.getLogoLink = function() {
return this.getConfigurationParam(ConfigurationType.LOGO_LINK);
};
ServerConnector.getOverlayById = function(layoutId, projectId) {
var self = this;
return new Promise(function(resolve, reject) {
self.getProjectId(projectId).then(function(projectId) {
self.getToken().then(function(token) {
self.readFile(self.getOverlayByIdUrl(layoutId, projectId, token)).then(function(content) {
var result = new LayoutData(JSON.parse(content));
resolve(result);
}, reject);
}, reject);
}, reject);
});
};
ServerConnector.getReactions = function(reactionIds, projectId, columns) {
var self = this;
return self.getProjectId(projectId).then(function(result) {
projectId = result;
return self.getToken();
}).then(function(token) {
return self.readFile(self.getReactionsUrl(reactionIds, projectId, token, columns));
}).then(function(content) {
var result = [];
for (var i = 0; i < array.length; i++) {
result.push(new Reaction(array[i]));
}
return result;
ServerConnector.getAliases = function(aliasIds, projectId, columns) {
var self = this;
return self.getProjectId(projectId).then(function(result) {
projectId = result;
return self.getToken();
}).then(function(token) {
return self.readFile(self.getAliasesUrl({
ids : aliasIds,
projectId : projectId,
token : token,
columns : columns
}));
var result = [];
for (var i = 0; i < array.length; i++) {
result.push(new Alias(array[i]));
}
return result;
});
};
ServerConnector.getLightComments = function(params) {
params.columns = [ "id", "elementId", "modelId", "type", "icon", "removed" ];
return this.getComments(params);
};
ServerConnector.getComments = function(params) {
return self.getProjectId(params.projectId).then(function(result) {
return self.getToken();
}).then(function(token) {
return self.readFile(self.getCommentsUrl(params));
}).then(function(content) {
var result = [];
for (var i = 0; i < array.length; i++) {
result.push(new Comment(array[i]));
}
return result;
ServerConnector.getLightAliases = function(aliasIds, projectId) {
return this.getAliases(aliasIds, projectId, "id,bounds,modelId");
ServerConnector.getSessionData = function(project) {
if (this._sessionData === undefined) {
this._sessionData = new SessionData(project);
if (project !== undefined && this._sessionData.getProject() === null) {
ServerConnector.getClosestElementsByCoordinates = function(params) {
var self = this;
return self.getProjectId(params.projectId).then(function(result) {
params.projectId = result;
return self.getToken();
}).then(function(token) {
params.token = token;