Author: ygrego Date: 2015-04-14 07:40:10 +0000 (Tue, 14 Apr 2015) New Revision: 1159 Url: http://forge.nuiton.org/projects/sandbox/repository/revisions/1159 Log: The method "getResults" has been modified, some parts are performed by using methods. Modified: oipf/js/impl/model/SearchResults.js Modified: oipf/js/impl/model/SearchResults.js =================================================================== --- oipf/js/impl/model/SearchResults.js 2015-04-13 09:23:12 UTC (rev 1158) +++ oipf/js/impl/model/SearchResults.js 2015-04-14 07:40:10 UTC (rev 1159) @@ -67,6 +67,16 @@ }, + _methodToCall: { + 0: "equals", + 1: "notEquals", + 2: "superior", + 3: "superiorOrEquals", + 4: "inferior", + 5: "inferiorOrEquals", + 6: "contains" + }, + /* * Description: * Perform the search and retrieve the specified subset of the items that match the query. @@ -86,14 +96,14 @@ throw new TypeError("Insufficient number of arguments"); } - if (!offset || !count || - !Number.isInteger(offset) || + if (!Number.isInteger(offset) || !Number.isInteger(count)) { throw new TypeError ("This function cannot be called with these arguments."); } this.offset = offset; + this._count = count; var self = this; var programmes = this._search._searchManager._metadata.programmes; @@ -101,25 +111,8 @@ * Verify some conditions before beginning of * search. */ - if (this._search._currentQuery && programmes && offset != null && count != null) { + if (this._search._currentQuery && programmes) { - 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 @@ -128,115 +121,14 @@ if (this._cachedResults.length != 0) { this._timeout() - .then(getResultFromInterval.bind(null, count)); + .then(this._getResultFromInterval.bind(this)); } else { - - if (this.isValidField(this._search._currentQuery._field) && - this.isValidComparator( - this._search._currentQuery._comparison)) { - - var loop = function (context, count){ - var time = new Date().getTime(); - for (var i = 0; i< programmes.length; i++) { - var programme = programmes[i]; - var methodToCall = { - 0: "equals", - 1: "notEquals", - 2: "superior", - 3: "superiorOrEquals", - 4: "inferior", - 5: "inferiorOrEquals", - 6: "contains" - }; - var field = context._search._currentQuery._field; - var comparison = context._search._currentQuery._comparison; - var value = context._search._currentQuery._value; - var methodName = methodToCall[comparison]; - - /* - * When the search corresponds to current programme - * the code differs from other usual query. - */ - if (context._search._currentQuery._type == "current") { - - var constraints = context._search. - _constraints.channels; - - if (constraints.length > 0 && - oipf.utils.isPresent(constraints, - programme.channel.name) && - programme.duration) { - - var stopTime = programme.startTime + - programme.duration*1000; - - if (time >= programme.startTime - && time <= stopTime) { - context._timeout() - .then(function() { - self._cachedResults.push(programme); - // self.push(programme); - // self.totalSize = self.length; - }) - .then(context._timeout.bind(self, 10000)) - .then(getResultFromInterval.bind(null, count)) - .then(context._fireEvent.bind(context, - createCustomEvent("MetadataSearch", - [context._search, context.SEARCH_FINISHED]))); - break; - } - } - - } else if (programme[field] && - oipf.utils[methodName].call(null, - programme[field], value)) { - - var constraints = context._search. - _constraints.channels; - - if(constraints.length > 0 && - oipf.utils.isPresent(constraints, - programme.channel.name)) { - - context._cachedResults.push(programme); - continue; - } - - if (constraints.length == 0) { - - context._cachedResults.push(programme); - // - } - } - } - }; - - this._timeout() - .then(loop.bind(null, this, count)) - .then(function() { - if (self._search._currentQuery._type == "undifferent") { - self._timeout() - .then(getResultFromInterval.bind(null, count)) - .then(self._fireEvent.bind(self, - createCustomEvent("MetadataSearch", - [self._search, self.SEARCH_FINISHED]))); - } - }); - -// if (this._search._ordering) { -// this._timeout() -// .then(function(){ -// getResultFromInterval(count); -// }); -// } - - - } else { - this._fireEvent(createCustomEvent("MetadataSearch", - [this._search, this.SEARCH_CANNOT_BE_COMPLETED])); - } + this._timeout() + .then(this.begin.bind(this, programmes)) + .then(this._timeout) + .then(this._getResultFromInterval.bind(this)); } } else { @@ -247,6 +139,21 @@ return false; }, + begin: function(programmes) { + /* + * When the search corresponds to current programme + * the code differs from other usual query. + */ + if (this._search._currentQuery._type == "current") { + + this._getCurrentProgramme(programmes); + + } else { + + this._getAnyProgramme(programmes); + } + }, + /* * Description: * Abort any outstanding request for results and remove any query, constraints or ordering @@ -264,6 +171,83 @@ this._search._removeOrdering(); }, + _getResultFromInterval: function() { + this.length = 0; + if (this._cachedResults) { + + for (var i = this.offset, l = this._count; i < l; i++) { + var programme = this._cachedResults[i]; + if (programme) { + this.push(programme); + + } else { + console.log("[Info]No results found."); + } + } + this._fireEvent(createCustomEvent("MetadataSearch", + [this._search, this.SEARCH_FINISHED])); + this.totalSize = this._cachedResults.length; + } + }, + + _getAnyProgramme: function(programmes) { + var field = this._search._currentQuery._field; + var comparison = this._search._currentQuery._comparison; + var value = this._search._currentQuery._value; + var methodName = this._methodToCall[comparison]; + + for (var i = 0, l = programmes.length; i < l; i++) { + var programme = programmes[i]; + if (programme[field] && methodName && + oipf.utils[methodName].call(null, programme[field], value)) { + + var constraints = this._search. + _constraints.channels; + + var next = true; + + if (constraints.length > 0 && + oipf.utils.isPresent(constraints, programme.channel.name)) { + + this._cachedResults.push(programme); + var next = false; + } + + if (next && constraints.length == 0) { + + this._cachedResults.push(programme); + + } + } + } + }, + + _getCurrentProgramme: function(programmes) { + var time = new Date().getTime(); + var constraints = this._search. + _constraints.channels; + + for (var i = 0, l = programmes.length; i < l; i++) { + var programme = programmes[i]; + + if (constraints.length > 0 && + oipf.utils.isPresent(constraints, programme.channel.name) && + programme.duration) { + + var stopTime = programme.startTime + + programme.duration*1000; + var self = this; + + if (time >= programme.startTime && time <= stopTime) { + + this._cachedResults.push(programme); + + i = l; + } + } + } + }, + /* * Description: * Return the item at position index in the collection of currently available results, or