Author: ygrego Date: 2015-04-08 10:15:15 +0000 (Wed, 08 Apr 2015) New Revision: 1111 Url: http://forge.nuiton.org/projects/sandbox/repository/revisions/1111 Log: Global refactoring of "main block else" in method "getResults". During the search, severals test are performed, when a programme is found this one is put in cache. At the end the expected results are gathered (extract from cache) and a event are dispatched. Modified: oipf/js/impl/model/SearchResults.js Modified: oipf/js/impl/model/SearchResults.js =================================================================== --- oipf/js/impl/model/SearchResults.js 2015-04-08 09:52:39 UTC (rev 1110) +++ oipf/js/impl/model/SearchResults.js 2015-04-08 10:15:15 UTC (rev 1111) @@ -96,78 +96,107 @@ * that's not necessary. */ if (this._cachedResults) { - - 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; - } + + 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 + .then(getResultFromInterval.bind(null, count)); - this._timeout - .then(getResultFromInterval.bind(null, count)); - } else { if (this.isValidField(this._search._currentQuery.field) && this.isValidComparator( this._search._currentQuery.comparison)) { - var startTime = this._currentQuery.startTime; - for (var i = 0; i< programmes.length; i++) { + var programme = programmes[i]; - var date = new Date(); - /* - * The user searches information on the current program, broadcasted on the current channel. - * In this case don't need start time of program, the search must be proceeded on current time. - */ - if (startTime == null) { - if (programme.duration) { - var stopTime = startTime + programme.duration; - if (date.getTime() >= startTime && date.getTime() <= stopTime) { + var methodToCall = { + 0: "equals", + 1: "notEquals", + 2: "superior", + 3: "superiorOrEquals", + 4: "inferior", + 5: "inferiorOrEquals", + 6: "constains" + }; + + var field = this._search._currentQuery.field; + var comparison = this._search._currentQuery.comparison; + var value = this._search._currentQuery.value; + var methodName = methodToCall[comparison]; + + /* + * When the search corresponds to current programme + * the code differs from other usual query. + */ + if (this._search._currentQuery._type == "current") { + var date = new Date(); + if (programme.duration) { + + var stopTime = programme.startTime + + programme.duration; + + if (date.getTime() >= programme.startTime + && date.getTime() <= stopTime) { + + this._timeout + .then(function() { + this._cachedResults.push(programme); + this.push(programme); + this.totalSize = this.length; console.log("[INFO] Program found."); - // Usefull to retrieve severals programs when user wants more than one programmes information. - var currentProgramIndex = i; - break; - } + }); + break; } - - } else { //The search use the start time like a filter to retrieve the required program information. - - if (date.getTime() == startTime) { - currentProgramIndex = i; - break; } + } else if (programme[field] && + oipf.utils[methodName].call(null, + programme[field], value)) { + + var constraints = this._search. + _constraints.channels; + + if(constraints.length > 0 && + oipf.utils.isPresent(constraints, + programme.channel)) { + + /* + * The results found are firstly put in a cache. + */ + this._timeout + .then(function() { + this._cachedResults.push(programme); + console.log("[INFO] Program found."); + }); + } } - console.log("[INFO] Program not yet found."); } - - var self = this; - setTimeout(function() { - console.log("<<<<<<<<<<<<<<", new Date(), ">>>>>>>>>>>>>>>>", currentProgramIndex); - //When user want more than one result into his search. - if (currentProgramIndex != undefined) { - for (var i = 0; i < count; i++) { - self.push(programmes[currentProgramIndex]); - currentProgramIndex++; - } - self._search._searchManager.onMetadataSearch && self._search._searchManager.onMetadataSearch(self._search, 0); - } else { - self._search._searchManager.onMetadataSearch && self._search._searchManager.onMetadataSearch(self._search, 4); - } - - }, 0); + if (this._search._ordering) { + this._timeout + .then(function(){ + getResultFromInterval.bind(null, count); + }); + } else { + this._timeout + .then(getResultFromInterval.bind(null, count)); + } + } else { this._fireEvent(createCustomEvent("MetadataSearch", [this._search, this.SEARCH_CANNOT_BE_COMPLETED]));