Author: ygrego Date: 2015-04-10 08:20:10 +0000 (Fri, 10 Apr 2015) New Revision: 1141 Url: http://forge.nuiton.org/projects/sandbox/repository/revisions/1141 Log: The method "getResults" has been modified totally. The property "_cachedResults" is now initialized in constructor "init". Modified: oipf/js/impl/model/SearchResults.js Modified: oipf/js/impl/model/SearchResults.js =================================================================== --- oipf/js/impl/model/SearchResults.js 2015-04-09 21:25:05 UTC (rev 1140) +++ oipf/js/impl/model/SearchResults.js 2015-04-10 08:20:10 UTC (rev 1141) @@ -56,10 +56,11 @@ _search: null, init: function(search) { + this._cachedResults = []; this._timerManager = new TimerManager(); this._timeout = this._timerManager.createTimer.bind(this._timerManager, 0); this._search = search; - this.lentgh = 0; + this.length = 0; this.totalSize = 0; this._fireEvent = this._search._searchManager ._fireEvent.bind(this._search._searchManager); @@ -77,50 +78,52 @@ * - offset: The number of items at the start of the result set to be skipped before data is retrieved. * * - count: The number of results to retrieve. - * + * FIX-ME: Yannis - 08/04/2015 - Verification about results ordering constraint */ getResults: function(offset, count) { this.offset = offset; - + var self = this; var programmes = this._search._searchManager._metadata.programmes; /* * Verify some conditions before beginning of * search. */ - if (this._currentQuery && programmes && offset && count) { + if (this._search._currentQuery && programmes && offset != null && count != null) { + var getResultFromInterval = function(count) { + self.length = 0; + if (self._cachedResults) { + + for (var i = self.offset, l = count; i < l; i++) { + var programme = self._cachedResults[i]; + if (programme) { + self.push(programme); + } else { + self._fireEvent(createCustomEvent("MetadataSearch", + [self._search, + self.SEARCH_CANNOT_BE_COMPLETED])); + break; + } + } + } + }; /* * A verification must be done on the presence of cached results * in order to not restart a complete search when * that's not necessary. */ - if (this._cachedResults) { + if (this._cachedResults.length != 0) { - var getResultFromInterval = function(count) { - this.length = 0; - - for (var i = this.offset, l = count; i < l; i++) { - var programme = this._cachedResults[i]; - if (programme) { - this.push(programme); - } else { - this.fireEvent(createCustomEvent("MetadataSearch", - [this._search, - this.SEARCH_CANNOT_BE_COMPLETED])); - break; - } - } - }; - - this._timeout + this._timeout() .then(getResultFromInterval.bind(null, count)); } else { - if (this.isValidField(this._search._currentQuery.field) && + var time = new Date().getTime(); + if (this.isValidField(this._search._currentQuery._field) && this.isValidComparator( - this._search._currentQuery.comparison)) { + this._search._currentQuery._comparison)) { for (var i = 0; i< programmes.length; i++) { @@ -132,12 +135,12 @@ 3: "superiorOrEquals", 4: "inferior", 5: "inferiorOrEquals", - 6: "constains" + 6: "contains" }; - var field = this._search._currentQuery.field; - var comparison = this._search._currentQuery.comparison; - var value = this._search._currentQuery.value; + var field = this._search._currentQuery._field; + var comparison = this._search._currentQuery._comparison; + var value = this._search._currentQuery._value; var methodName = methodToCall[comparison]; /* @@ -145,25 +148,44 @@ * the code differs from other usual query. */ if (this._search._currentQuery._type == "current") { - var date = new Date(); - if (programme.duration) { + + var constraints = this._search. + _constraints.channels; + + if (constraints.length > 0 && + oipf.utils.isPresent(constraints, + programme.channel.name) && + programme.duration) { var stopTime = programme.startTime + - programme.duration; + programme.duration*1000; - if (date.getTime() >= programme.startTime - && date.getTime() <= stopTime) { - - this._timeout + if (time >= programme.startTime + && time <= stopTime) { + + this._timeout() .then(function() { - this._cachedResults.push(programme); - this.push(programme); - this.totalSize = this.length; + self._cachedResults.push(programme); +// self.push(programme); +// self.totalSize = self.length; console.log("[INFO] Program found."); - }); + }) + .then(this._timeout) + .then(getResultFromInterval.bind(null, count)) + .then(this._timeout) + .then(this._fireEvent.bind(this, + createCustomEvent("MetadataSearch", + [this._search, this.SEARCH_FINISHED]))); break; } + } else { + this._fireEvent( + createCustomEvent("MetadataSearch", + [this._search, + this.SEARCH_CANNOT_BE_COMPLETED])); + break; } + } else if (programme[field] && oipf.utils[methodName].call(null, programme[field], value)) { @@ -173,28 +195,34 @@ if(constraints.length > 0 && oipf.utils.isPresent(constraints, - programme.channel)) { + programme.channel.name)) { /* - * The results found are firstly put in a cache. + * The results found are firstly put in a cache, + * then gathered. */ - this._timeout + this._timeout() .then(function() { - this._cachedResults.push(programme); + self._cachedResults.push(programme); console.log("[INFO] Program found."); }); } } } - if (this._search._ordering) { - this._timeout - .then(function(){ - getResultFromInterval.bind(null, count); - }); - } else { - this._timeout - .then(getResultFromInterval.bind(null, count)); +// if (this._search._ordering) { +// this._timeout() +// .then(function(){ +// getResultFromInterval(count); +// }); +// } + if (this._search._currentQuery._type == "undifferent") { + this._timeout() + .then(getResultFromInterval.bind(null, count)) + .then(this._timeout) + .then(this._fireEvent.bind(this, + createCustomEvent("MetadataSearch", + [this._search, this.SEARCH_FINISHED]))); } } else {