added a function to search annotations to the json serializer. popcorn-port
authorhamidouk
Wed, 26 Oct 2011 11:32:21 +0200
branchpopcorn-port
changeset 147 955119f901b4
parent 146 b99527037c89
child 148 5e877acd85ca
added a function to search annotations to the json serializer.
src/js/serializers/JSONSerializer.js
unittests/tests/JSONSerializer.js
--- a/src/js/serializers/JSONSerializer.js	Wed Oct 26 11:32:01 2011 +0200
+++ b/src/js/serializers/JSONSerializer.js	Wed Oct 26 11:32:21 2011 +0200
@@ -36,3 +36,39 @@
 IriSP.JSONSerializer.prototype.currentMedia = function() {  
   return this._data.medias[0]; /* FIXME: don't hardcode it */
 };
+
+/* this function searches for an annotation which matches title, description and keyword 
+   "" matches any field. 
+*/    
+IriSP.JSONSerializer.prototype.searchAnnotations = function(title, description, keyword) {
+    var rTitle;
+    var rDescription;
+    var rKeyword;
+    
+    /* match anything if given the empty string */
+    if (title == "")
+      title = ".*";
+    if (description == "")
+      description = ".*";
+    if (keyword == "")
+      keyword = ".*";
+    
+    rTitle = new RegExp(title, "i");  
+    rDescription = new RegExp(description, "i");  
+    rKeyword = new RegExp(keyword, "i");  
+    
+    var ret_array = [];
+    
+    var i;
+    for (i in this._data.annotations) {
+      var annotation = this._data.annotations[i];
+      
+      if (rTitle.test(annotation.content.title) && 
+          rDescription.test(annotation.content.description)) {
+          /* FIXME : implement keyword support */
+          ret_array.push(annotation);
+      }
+    }
+    
+    return ret_array;
+}
\ No newline at end of file
--- a/unittests/tests/JSONSerializer.js	Wed Oct 26 11:32:01 2011 +0200
+++ b/unittests/tests/JSONSerializer.js	Wed Oct 26 11:32:21 2011 +0200
@@ -39,6 +39,8 @@
       ser.sync(spy_callback);
       
       equals(this.xhr.requests.length, 1, "the mock ajax object should have received the request");
+      equals(this.xhr.requests[0].url, "/url", "the requested url is correct");
+
       this.xhr.requests[0].respond(200, { "Content-Type": "application/json" },
                              response_string);
         
@@ -61,9 +63,39 @@
 
     test("currentMedia should return the current media", function() {
       var ser = new IriSP.JSONSerializer(this.dt, "/url");
-      /* FIXME: actually get something instead of monkey-patching the struct */
+
       ser._data = {}
       ser._data.medias = [0];
       equal(ser.currentMedia(), 0, "currentMedia() returns the correct value");
-    });    
+    });
+
+    test("test annotation search", function() {
+      var ser = new IriSP.JSONSerializer(this.dt, "../test/test.json");
+            
+      ser._data = { annotations : [
+      {"content": {        
+        "description": "professeur", 
+        "title": "garrigou"
+        }},
+      { "content": {        
+        "description": "interview", 
+        "title": "Revue de presse - Hervé Gardette"
+      }},
+      {"content": {        
+        "description": "concept", 
+        "title": "idée"
+      }},
+      { "content": {        
+        "description": "", 
+        "title": "sans titre"
+      }}
+      ]};
+      
+      equal(ser.searchAnnotations("GarriGOU", "", "").length, 1, "requesting on title works");
+      equal(ser.searchAnnotations("", "IntErView", "").length, 1, "requesting on description works");      
+      equal(ser.searchAnnotations("", "", "").length, 4, "empty request works");
+      equal(ser.searchAnnotations("idée", "concept", "").length, 1, "specific request works");
+      
+      
+    });
 };
\ No newline at end of file