Author: ygrego Date: 2015-04-03 09:48:31 +0000 (Fri, 03 Apr 2015) New Revision: 1066 Url: http://forge.nuiton.org/projects/sandbox/repository/revisions/1066 Log: Refactoring of method "getResults". Modified: oipf/js/impl/model/SearchResults.js Modified: oipf/js/impl/model/SearchResults.js =================================================================== --- oipf/js/impl/model/SearchResults.js 2015-04-03 08:55:49 UTC (rev 1065) +++ oipf/js/impl/model/SearchResults.js 2015-04-03 09:48:31 UTC (rev 1066) @@ -70,103 +70,46 @@ */ getResults: function(offset, count) { this.offset = offset; - var idStr = "-id", - displayNameStr = "display-name", - channelStr = "-channel", - stopStr = "-stop", - startStr = "-start", - currentChannelId = undefined, - channels = metadata.tv.channel, - displayName, - programs = metadata.tv.programme, - pattern = /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})\s\+(\d{2})/; + var programmes = this._search._searchManager._metadata; - //Found the associated id of current channel in metadata because channel is referenced by id in program information - for (var i = 0; i<channels.length; i++) { - - item = channels[i]; - displayName = item[displayNameStr]; - - if (displayName === this._currentQuery.channel.name) { - currentChannelId = item[idStr]; - break; - } - } - - if (!currentChannelId) { - console.log("[INFO] This channel don't have id, this search have to abort."); - return; - } - console.log("[INFO] Current channel id found."); - var startTime = this._currentQuery.startTime; - //Found programs of current channel only - for (var i = 0; i< programs.length; i++) { - var item = programs[i]; + for (var i = 0; i< programmes.length; i++) { + var programme = programmes[i]; + var currentDate = 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 (currentDate.getTime() >= startTime && currentDate.getTime() <= stopTime) { + console.log("[INFO] Program found."); + // Usefull to retrieve severals programs when user wants more than one programmes information. + var currentProgramIndex = i; + break; + } + } - if (item[channelStr] === currentChannelId) { // && date + } else { //The search use the start time like a filter to retrieve the required program information. - var tmpSSTime = item[startStr]; - tmpSSTime = pattern.exec(tmpSSTime); - console.log(tmpSSTime); - var dateTmp = new Date(); - - /*year = parseInt(tmpSSTime[1]); - month = parseInt(tmpSSTime[2]); - day = parseInt(tmpSSTime[3]);*/ - var year = dateTmp.getFullYear(); - var month = dateTmp.getUTCMonth(); - var day = dateTmp.getUTCDate(); - var hs = parseInt(tmpSSTime[4]); - var mins = parseInt(tmpSSTime[5]); - var secs = parseInt(tmpSSTime[6]); - - var startDate = new Date(year, month, day, hs, mins, secs); - - tmpSSTime = item[stopStr]; - tmpSSTime = pattern.exec(tmpSSTime); - console.log(tmpSSTime); - - hs = parseInt(tmpSSTime[4]); - mins = parseInt(tmpSSTime[5]); - secs = parseInt(tmpSSTime[6]); - - var stopDate = new Date(year, month, day, hs, mins, secs); - - var currentDate = 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 ( (currentDate.getTime() >= startDate.getTime()) && (currentDate.getTime() <= stopDate.getTime()) ) { - console.log("[INFO] Program found."); - var currentProgramIndex = i; // Usefull to retrieve severals programs when user want more than one programs information. - break; - } - - } else { //The search use the start time like a filter to retrieve the required program information. - - if (this._currentQuery.starTime == startDate.getTime()) { - var currentProgramIndex = i; // Usefull to retrieve severals programs when user want more than one programs information. - break; - } + if (currentDate.getTime() == startTime) { + currentProgramIndex = i; + break; } - console.log("[INFO] Program not yet 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) { + if (currentProgramIndex != undefined) { for (var i = 0; i < count; i++) { - self.push(programs[currentProgramIndex]); + self.push(programmes[currentProgramIndex]); currentProgramIndex++; } self._search._searchManager.onMetadataSearch && self._search._searchManager.onMetadataSearch(self._search, 0);