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

extracing abstrac db overlay from drug ovelray

parent 3a0efb1d
"use strict";
var logger = require('../../logger');
var Promise = require("bluebird");
var IdentifiedElement = require('../data/IdentifiedElement');
var OverlayCollection = require('./OverlayCollection');
var ServerConnector = require('../../ServerConnector');
function AbstractDbOverlay(params) {
// call super constructor
OverlayCollection.call(this, params);
this.setIconType(params.iconType);
this.setIconStart(params.iconColorStart);
this._elementsByQuery = [];
this._drugsByQuery = [];
this.registerListenerType('onSearch');
this.registerListenerType('onTargetVisibilityChange');
this.setQueries([]);
}
AbstractDbOverlay.prototype = Object.create(OverlayCollection.prototype);
AbstractDbOverlay.prototype.constructor = AbstractDbOverlay;
AbstractDbOverlay.QueryType = {
SEARCH_BY_COORDINATES : "SEARCH_BY_COORDINATES",
SEARCH_BY_TARGET : "SEARCH_BY_TARGET",
SEARCH_BY_QUERY : "SEARCH_BY_QUERY",
};
AbstractDbOverlay.prototype.encodeQuery= function (type, arg0){
if (type === AbstractDbOverlay.QueryType.SEARCH_BY_TARGET) {
var target = arg0;
return JSON.stringify({
type:type,
target:target,
});
} else if (type === AbstractDbOverlay.QueryType.SEARCH_BY_QUERY) {
var query = arg0;
return JSON.stringify({
type:type,
query:query
});
} else {
throw new Error("Unknown query type: "+type);
}
};
AbstractDbOverlay.prototype.decodeQuery= function (query){
return JSON.parse(query);
};
AbstractDbOverlay.prototype.searchByQuery = function(originalQuery) {
var self = this;
var query = self.encodeQuery(AbstractDbOverlay.QueryType.SEARCH_BY_QUERY, originalQuery);
ServerConnector.getSessionData().setDrugQuery(query);
return new Promise(function(resolve, reject) {
var queries = self.splitQuery(originalQuery);
var encodedQueries = [];
var promises = [];
for (var i=0;i<queries.length;i++) {
encodedQueries.push(self.encodeQuery(AbstractDbOverlay.QueryType.SEARCH_BY_QUERY, queries[i]));
promises.push(self.searchBySingleQuery(queries[i]));
}
self.setQueries(encodedQueries);
var res;
Promise.all(promises).then(function(results){
res =results;
return self.callListeners('onSearch');
}).then(function(){
resolve(res);
}).catch(reject);
});
};
AbstractDbOverlay.prototype.searchNamesByTarget = function(element) {
var self = this;
var query = self.encodeQuery(AbstractDbOverlay.QueryType.SEARCH_BY_TARGET, element);
ServerConnector.getSessionData().setDrugQuery(query);
return new Promise(function(resolve, reject) {
if (self._elementsByQuery[query] !== undefined) {
resolve(self._elementsByQuery[query]);
} else {
return self.getNamesByTargetFromServer({
target: element
}).then(function(drugNames) {
self._elementsByQuery[query] = drugNames;
resolve(self._elementsByQuery[query]);
}).catch(reject);
}
});
};
AbstractDbOverlay.prototype.setQueries = function(queries){
this._queries = queries;
};
AbstractDbOverlay.prototype.getQueries = function(){
return this._queries ;
};
AbstractDbOverlay.prototype.createIdentifiedElementsForTargetingClass = function(element, colourId){
var self = this;
var result =[];
var iconCounter =1;
var targets = element.getTargets();
for (var k=0;k<targets.length;k++) {
var target = targets[k];
if (target.isVisible()) {
var elements = target.getTargetElements();
for (var l=0;l<elements.length;l++) {
var element = elements[l];
var ie = new IdentifiedElement(element);
if (element.getType() === "ALIAS") {
ie.setIcon(self.getIcon(colourId, iconCounter));
} else if (element.getType() !== "REACTION") {
throw new Error("Unknown element type: "+element.getType());
}
result.push(ie);
}
}
iconCounter++;
}
return result;
};
AbstractDbOverlay.prototype.refresh = function(){
throw new Error("Refreshing shouldn't be called");
};
AbstractDbOverlay.prototype.searchByEncodedQuery = function(originalQuery) {
var query = this.decodeQuery(originalQuery);
if (query.type === AbstractDbOverlay.QueryType.SEARCH_BY_QUERY) {
return this.searchByQuery(query.query);
} else if (query.type === AbstractDbOverlay.QueryType.SEARCH_BY_TARGET) {
return this.searchByTarget(query.target);
} else {
throw new Error("Unknown type of query: "+query.type);
}
};
AbstractDbOverlay.prototype.clear = function() {
return this.searchByQuery("");
};
AbstractDbOverlay.prototype.getDetailDataByIdentifiedElement = function(element, general) {
var self = this;
if (general) {
return self.searchNamesByTarget(element);
} else {
return new Promise(function(resolve){
var result =[];
var queries =self.getQueries();
for (var i=0;i<queries.length;i++) {
var drugs = self._drugsByQuery[queries[i]];
for (var j=0;j<drugs.length;j++){
var drug = drugs[j];
var targets = drug.getTargets();
for (var k=0;k<targets.length;k++) {
var elements = targets[k].getTargetElements();
for (var l=0;l<elements.length;l++) {
if (element.equals(elements[l])) {
result.push(drug.getName());
}
}
}
}
}
resolve(result);
});
}
};
module.exports = AbstractDbOverlay;
......@@ -5,55 +5,21 @@ var logger = require('../../logger');
var Promise = require("bluebird");
var IdentifiedElement = require('../data/IdentifiedElement');
var OverlayCollection = require('./OverlayCollection');
var AbstractDbOverlay = require('./AbstractDbOverlay');
var ServerConnector = require('../../ServerConnector');
function DrugDbOverlay(params) {
params.iconType= "drug";
params.iconColorStart = 5
// call super constructor
OverlayCollection.call(this, params);
AbstractDbOverlay.call(this, params);
this.setIconType("drug");
this.setIconStart(5);
this._elementsByQuery = [];
this._drugsByQuery = [];
this.registerListenerType('onSearch');
this.registerListenerType('onTargetVisibilityChange');
this.setQueries([]);
}
DrugDbOverlay.prototype = Object.create(OverlayCollection.prototype);
DrugDbOverlay.prototype = Object.create(AbstractDbOverlay.prototype);
DrugDbOverlay.prototype.constructor = DrugDbOverlay;
DrugDbOverlay.QueryType = {
SEARCH_BY_TARGET : "SEARCH_BY_TARGET",
SEARCH_BY_QUERY : "SEARCH_BY_QUERY",
};
DrugDbOverlay.prototype.encodeQuery= function (type, arg0){
if (type === DrugDbOverlay.QueryType.SEARCH_BY_TARGET) {
var target = arg0;
return JSON.stringify({
type:type,
target:target,
});
} else if (type === DrugDbOverlay.QueryType.SEARCH_BY_QUERY) {
var query = arg0;
return JSON.stringify({
type:type,
query:query
});
} else {
throw new Error("Unknown query type: "+type);
}
};
DrugDbOverlay.prototype.decodeQuery= function (query){
return JSON.parse(query);
};
DrugDbOverlay.prototype.getElementsByQuery = function(query) {
var self = this;
return new Promise(function(resolve){
......@@ -95,7 +61,7 @@ DrugDbOverlay.prototype.getElementsByQuery = function(query) {
DrugDbOverlay.prototype.searchBySingleQuery = function(originalQuery) {
var self = this;
return new Promise(function(resolve, reject) {
var query = self.encodeQuery(DrugDbOverlay.QueryType.SEARCH_BY_QUERY, originalQuery);
var query = self.encodeQuery(AbstractDbOverlay.QueryType.SEARCH_BY_QUERY, originalQuery);
if (self._elementsByQuery[query] !== undefined) {
resolve(self._elementsByQuery[query]);
} else {
......@@ -123,86 +89,6 @@ DrugDbOverlay.prototype.searchBySingleQuery = function(originalQuery) {
});
};
DrugDbOverlay.prototype.searchByQuery = function(originalQuery) {
var self = this;
var query = self.encodeQuery(DrugDbOverlay.QueryType.SEARCH_BY_QUERY, originalQuery);
ServerConnector.getSessionData().setDrugQuery(query);
return new Promise(function(resolve, reject) {
var queries = self.splitQuery(originalQuery);
var encodedQueries = [];
var promises = [];
for (var i=0;i<queries.length;i++) {
encodedQueries.push(self.encodeQuery(DrugDbOverlay.QueryType.SEARCH_BY_QUERY, queries[i]));
promises.push(self.searchBySingleQuery(queries[i]));
}
self.setQueries(encodedQueries);
var res;
Promise.all(promises).then(function(results){
res =results;
return self.callListeners('onSearch');
}).then(function(){
resolve(res);
}).catch(reject);
});
};
DrugDbOverlay.prototype.searchNamesByTarget = function(element) {
var self = this;
var query = self.encodeQuery(DrugDbOverlay.QueryType.SEARCH_BY_TARGET, element);
ServerConnector.getSessionData().setDrugQuery(query);
return new Promise(function(resolve, reject) {
if (self._elementsByQuery[query] !== undefined) {
resolve(self._elementsByQuery[query]);
} else {
return ServerConnector.getDrugNamesByTarget({
target: element
}).then(function(drugNames) {
self._elementsByQuery[query] = drugNames;
resolve(self._elementsByQuery[query]);
}).catch(reject);
}
});
};
DrugDbOverlay.prototype.setQueries = function(queries){
this._queries = queries;
};
DrugDbOverlay.prototype.getQueries = function(){
return this._queries ;
};
DrugDbOverlay.prototype.createIdentifiedElementsForDrug = function(drug, colourId){
var self = this;
var result =[];
var iconCounter =1;
var targets = drug.getTargets();
for (var k=0;k<targets.length;k++) {
var target = targets[k];
if (target.isVisible()) {
var elements = target.getTargetElements();
for (var l=0;l<elements.length;l++) {
var element = elements[l];
var ie = new IdentifiedElement(element);
if (element.getType() === "ALIAS") {
ie.setIcon(self.getIcon(colourId, iconCounter));
} else if (element.getType() !== "REACTION") {
throw new Error("Unknown element type: "+element.getType());
}
result.push(ie);
}
}
iconCounter++;
}
return result;
};
DrugDbOverlay.prototype.getIdentifiedElements = function(){
var self = this;
......@@ -217,7 +103,7 @@ DrugDbOverlay.prototype.getIdentifiedElements = function(){
for (var j=0;j<drugs.length;j++) {
var drug = drugs[j];
var drugElements = self.createIdentifiedElementsForDrug(drug, colourId);
var drugElements = self.createIdentifiedElementsForTargetingClass(drug, colourId);
result.push.apply(result, drugElements);
......@@ -228,24 +114,9 @@ DrugDbOverlay.prototype.getIdentifiedElements = function(){
});
};
DrugDbOverlay.prototype.refresh = function(){
throw new Error("Refreshing shouldn't be called");
};
DrugDbOverlay.prototype.searchByEncodedQuery = function(originalQuery) {
var query = this.decodeQuery(originalQuery);
if (query.type === DrugDbOverlay.QueryType.SEARCH_BY_QUERY) {
return this.searchByQuery(query.query);
} else if (query.type === DrugDbOverlay.QueryType.SEARCH_BY_TARGET) {
return this.searchByTarget(query.target);
} else {
throw new Error("Unknown type of query: "+query.type);
}
};
DrugDbOverlay.prototype.clear = function() {
return this.searchByQuery("");
};
DrugDbOverlay.prototype.getNamesByTargetFromServer = function(param) {
return ServerConnector.getDrugNamesByTarget(param);
}
DrugDbOverlay.prototype.getDetailDataByIdentifiedElement = function(element, general) {
var self = this;
......
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