Commit 545ecb8d authored by Piotr Gawron's avatar Piotr Gawron
Browse files

SearchDbOverlay uses AbstractDbOverlay

parent a06f77d7
......@@ -29,6 +29,16 @@ SessionData.prototype.setSearchQuery = function(value) {
Cookies.set(key, JSON.stringify(value));
};
SessionData.prototype.setQuery = function(param) {
if (param.type === "drug") {
this.setDrugQuery(param.query);
} else if (param.type === "search") {
this.setSearchQuery(param.query);
} else {
throw new Error("Invalid query type: " + param.type);
}
};
SessionData.prototype.getSearchQuery = function() {
var key = this.getKey(SessionObjectType.SEARCH_QUERY);
var result = Cookies.get(key);
......
......@@ -34,8 +34,16 @@ AbstractDbOverlay.QueryType = {
SEARCH_BY_QUERY : "SEARCH_BY_QUERY",
};
AbstractDbOverlay.prototype.encodeQuery= function (type, arg0){
if (type === AbstractDbOverlay.QueryType.SEARCH_BY_TARGET) {
AbstractDbOverlay.prototype.encodeQuery= function (type, arg0, arg1){
if (type === AbstractDbOverlay.QueryType.SEARCH_BY_COORDINATES) {
var modelId = arg0;
var coordinates = arg1;
return JSON.stringify({
type:type,
modelId:modelId,
coordinates: coordinates
});
} else if (type === AbstractDbOverlay.QueryType.SEARCH_BY_TARGET) {
var target = arg0;
return JSON.stringify({
type:type,
......@@ -43,9 +51,11 @@ AbstractDbOverlay.prototype.encodeQuery= function (type, arg0){
});
} else if (type === AbstractDbOverlay.QueryType.SEARCH_BY_QUERY) {
var query = arg0;
var perfect = arg1;
return JSON.stringify({
type:type,
query:query
query:query,
perfect:perfect,
});
} else {
throw new Error("Unknown query type: "+type);
......@@ -57,10 +67,10 @@ AbstractDbOverlay.prototype.decodeQuery= function (query){
return JSON.parse(query);
};
AbstractDbOverlay.prototype.searchByQuery = function(originalQuery) {
AbstractDbOverlay.prototype.searchByQuery = function(originalQuery, perfect) {
var self = this;
var query = self.encodeQuery(AbstractDbOverlay.QueryType.SEARCH_BY_QUERY, originalQuery);
ServerConnector.getSessionData().setDrugQuery(query);
var query = self.encodeQuery(AbstractDbOverlay.QueryType.SEARCH_BY_QUERY, originalQuery, perfect);
ServerConnector.getSessionData().setQuery({type:self.getName(),query:query});
return new Promise(function(resolve, reject) {
var queries = self.splitQuery(originalQuery);
......@@ -87,7 +97,7 @@ AbstractDbOverlay.prototype.searchNamesByTarget = function(element) {
var self = this;
var query = self.encodeQuery(AbstractDbOverlay.QueryType.SEARCH_BY_TARGET, element);
ServerConnector.getSessionData().setDrugQuery(query);
ServerConnector.getSessionData().setQuery({type:self.getName(),query:query});
return new Promise(function(resolve, reject) {
if (self._elementsByQuery[query] !== undefined) {
......@@ -144,9 +154,11 @@ AbstractDbOverlay.prototype.refresh = function(){
AbstractDbOverlay.prototype.searchByEncodedQuery = function(originalQuery) {
var query = this.decodeQuery(originalQuery);
if (query.type === AbstractDbOverlay.QueryType.SEARCH_BY_QUERY) {
return this.searchByQuery(query.query);
return this.searchByQuery(query.query, query.perfect);
} else if (query.type === AbstractDbOverlay.QueryType.SEARCH_BY_TARGET) {
return this.searchByTarget(query.target);
} else if (query.type === AbstractDbOverlay.QueryType.SEARCH_BY_COORDINATES) {
return this.searchByCoordinates(query.modelId, query.coordinates);
} else {
throw new Error("Unknown type of query: "+query.type);
}
......
......@@ -2,57 +2,24 @@
var Promise = require("bluebird");
var AbstractDbOverlay = require('./AbstractDbOverlay');
var Alias = require('../data/Alias');
var IdentifiedElement = require('../data/IdentifiedElement');
var OverlayCollection = require('./OverlayCollection');
var Reaction = require('../data/Reaction');
var ServerConnector = require('../../ServerConnector');
function SearchDbOverlay(params) {
params.iconType= "marker";
params.iconColorStart = 0;
// call super constructor
OverlayCollection.call(this, params);
AbstractDbOverlay.call(this, params);
this.setIconType("marker");
this.setIconStart(0);
this._elementsByQuery = [];
this.registerListenerType('onSearch');
}
SearchDbOverlay.prototype = Object.create(OverlayCollection.prototype);
SearchDbOverlay.prototype = Object.create(AbstractDbOverlay.prototype);
SearchDbOverlay.prototype.constructor = SearchDbOverlay;
SearchDbOverlay.QueryType = {
SEARCH_BY_COORDINATES : "SEARCH_BY_COORDINATES",
SEARCH_BY_QUERY : "SEARCH_BY_QUERY",
};
SearchDbOverlay.prototype.encodeQuery= function (type, arg0, arg1){
if (type === SearchDbOverlay.QueryType.SEARCH_BY_COORDINATES) {
var modelId = arg0;
var coordinates = arg1;
return JSON.stringify({
type:type,
modelId:modelId,
coordinates: coordinates
});
} else if (type === SearchDbOverlay.QueryType.SEARCH_BY_QUERY) {
var query = arg0;
var perfect = arg1;
return JSON.stringify({
type:type,
query:query,
perfect: perfect
});
} else {
throw new Error("Unknown query type: "+type);
}
};
SearchDbOverlay.prototype.decodeQuery= function (query){
return JSON.parse(query);
};
SearchDbOverlay.prototype.getElementsByQuery = function(query) {
var self = this;
......@@ -94,7 +61,7 @@ SearchDbOverlay.prototype.getElementsByQuery = function(query) {
SearchDbOverlay.prototype.searchByCoordinates = function(modelId, coordinates) {
var self = this;
var query = self.encodeQuery(SearchDbOverlay.QueryType.SEARCH_BY_COORDINATES, modelId, coordinates);
var query = self.encodeQuery(AbstractDbOverlay.QueryType.SEARCH_BY_COORDINATES, modelId, coordinates);
ServerConnector.getSessionData().setSearchQuery(query);
......@@ -134,7 +101,7 @@ SearchDbOverlay.prototype.searchByCoordinates = function(modelId, coordinates) {
SearchDbOverlay.prototype.searchBySingleQuery = function(originalQuery, perfect) {
var self = this;
return new Promise(function(resolve, reject) {
var query = self.encodeQuery(SearchDbOverlay.QueryType.SEARCH_BY_QUERY, originalQuery, perfect);
var query = self.encodeQuery(AbstractDbOverlay.QueryType.SEARCH_BY_QUERY, originalQuery, perfect);
if (self._elementsByQuery[query] !== undefined) {
resolve(self._elementsByQuery[query]);
} else {
......@@ -155,44 +122,6 @@ SearchDbOverlay.prototype.searchBySingleQuery = function(originalQuery, perfect)
});
};
SearchDbOverlay.prototype.searchByQuery = function(originalQuery, perfect) {
var self = this;
var query = self.encodeQuery(SearchDbOverlay.QueryType.SEARCH_BY_QUERY, originalQuery, perfect);
ServerConnector.getSessionData().setSearchQuery(query);
return new Promise(function(resolve, reject) {
var queries = [];
if (perfect) {
queries.push(originalQuery);
} else {
queries = self.splitQuery(originalQuery);
}
var encodedQueries = [];
var promises = [];
for (var i=0;i<queries.length;i++) {
encodedQueries.push(self.encodeQuery(SearchDbOverlay.QueryType.SEARCH_BY_QUERY, queries[i], perfect));
promises.push(self.searchBySingleQuery(queries[i], perfect));
}
self.setQueries(encodedQueries);
var res;
Promise.all(promises).then(function(results){
res =results;
return self.callListeners('onSearch');
}).then(function(){
resolve(res);
}).catch(reject);
});
};
SearchDbOverlay.prototype.setQueries = function(queries){
this._queries = queries;
};
SearchDbOverlay.prototype.getQueries = function(){
return this._queries ;
};
SearchDbOverlay.prototype.getIdentifiedElements = function(){
var self = this;
......@@ -220,23 +149,9 @@ SearchDbOverlay.prototype.getIdentifiedElements = function(){
});
};
SearchDbOverlay.prototype.refresh = function(){
throw new Error("Refreshing shouldn't be called");
};
SearchDbOverlay.prototype.searchByEncodedQuery = function(originalQuery) {
var query = this.decodeQuery(originalQuery);
if (query.type === SearchDbOverlay.QueryType.SEARCH_BY_QUERY) {
return this.searchByQuery(query.query,query.perfect);
} else if (query.type === SearchDbOverlay.QueryType.SEARCH_BY_COORDINATES) {
return this.searchByCoordinates(query.modelId,query.coordinates);
} else {
throw new Error("Unknown type of query: "+query.type);
}
};
SearchDbOverlay.prototype.clear = function() {
return this.searchByQuery("");
SearchDbOverlay.prototype.getDetailDataByIdentifiedElement = function(element) {
var model = this.getMap().getSubmodelById(element.getModelId()).getModel();
return model.getByIdentifiedElement(element, true);
};
module.exports = SearchDbOverlay;
......@@ -2,6 +2,7 @@
var functions = require('./Functions');
var AbstractDbOverlay = require('./map/overlay/AbstractDbOverlay');
var CommentDbOverlay = require('./map/overlay/CommentDbOverlay');
var ControlType = require('./map/ControlType');
var CustomMap = require('./map/CustomMap');
......@@ -38,7 +39,7 @@ function processUrlGetParams(params) {
sessionData.setShowComments(true);
}
if (GuiConnector.getParams["search"] !== undefined) {
var query = SearchDbOverlay.prototype.encodeQuery(SearchDbOverlay.QueryType.SEARCH_BY_QUERY, GuiConnector.getParams["search"]);
var query = SearchDbOverlay.prototype.encodeQuery(AbstractDbOverlay.QueryType.SEARCH_BY_QUERY, GuiConnector.getParams["search"]);
sessionData.setSearchQuery(query);
}
......
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