Added Cinecast serializer, with imports management new-model
authorveltr
Mon, 16 Apr 2012 19:10:32 +0200
branchnew-model
changeset 864 5e76a06b961c
parent 860 7fd843e0dc4e
child 866 3bf7aa8216e5
Added Cinecast serializer, with imports management
src/js/model.js
src/js/serializers/CinecastSerializer.js
src/js/serializers/PlatformSerializer.js
test/model/test.html
--- a/src/js/model.js	Fri Apr 13 18:08:19 2012 +0200
+++ b/src/js/model.js	Mon Apr 16 19:10:32 2012 +0200
@@ -72,6 +72,10 @@
     }
 }
 
+IriSP.Model.List.prototype.hasId = function(_id) {
+    return (IriSP._(this.contents).indexOf(_id) !== -1);
+}
+
 IriSP.Model.List.prototype.each = function(_callback) {
     var _this = this;
     IriSP._(this.contents).each(function(_id) {
@@ -95,10 +99,19 @@
     return _res;
 }
 
+IriSP.Model.List.prototype.sortBy = function(_callback) {
+    var _this = this,
+        _res = new IriSP.Model.List(this.directory);
+    _res.contents = IriSP._(this.contents).sortBy(function(_id) {
+        return _callback.call(_this, _this.getElement(_id), _id);
+    });
+    return _res;
+}
+
 IriSP.Model.List.prototype.searchByTitle = function(_text) {
     var _rgxp = new RegExp('(' + _text.replace(/(\W)/gm,'\\$1') + ')','gim');
     return this.filter(function(_element) {
-        return _rgxp.test(_element.text);
+        return _rgxp.test(_element.title);
     });
 }
 
@@ -112,12 +125,12 @@
 IriSP.Model.List.prototype.searchByTextFields = function(_text) {
     var _rgxp = new RegExp('(' + _text.replace(/(\W)/gm,'\\$1') + ')','gim');
     return this.filter(function(_element) {
-        return _rgxp.test(_element.description);
+        return _rgxp.test(_element.description) || _rgxp.test(_element.title);
     });
 }
 
 IriSP.Model.List.prototype.addId = function(_id) {
-    if (this.contents.indexOf(_id) === -1) {
+    if (!this.hasId(_id)) {
         this.contents.push(_id);
     }
 }
@@ -227,10 +240,23 @@
     }
 }
 
+IriSP.Model.Element.prototype.getRelated = function(_elementType) {
+    var _this = this;
+    return this.source.getList(_elementType).filter(function(_el) {
+        var _ref = _el[_this.elementType];
+        if (_ref.isList) {
+            return _ref.contents.hasId(_this.id);
+        }
+        else {
+            return _ref.contents === _this.id;
+        }
+    });
+}
+
 /* */
 
-IriSP.Model.Media = function(_id, _directory) {
-    IriSP.Model.Element.call(this, _id, _directory);
+IriSP.Model.Media = function(_id, _source) {
+    IriSP.Model.Element.call(this, _id, _source);
     this.elementType = 'media';
     this.duration = new IriSP.Model.Time();
     this.url = '';
@@ -242,29 +268,41 @@
     this.duration.milliseconds = _durationMs;
 }
 
+IriSP.Model.Media.prototype.getAnnotations = function() {
+    return this.getRelated("annotation");
+}
+
 /* */
 
-IriSP.Model.Tag = function(_id, _directory) {
-    IriSP.Model.Element.call(this, _id, _directory);
+IriSP.Model.Tag = function(_id, _source) {
+    IriSP.Model.Element.call(this, _id, _source);
     this.elementType = 'tag';
 }
 
 IriSP.Model.Tag.prototype = new IriSP.Model.Element();
 
+IriSP.Model.Tag.prototype.getAnnotations = function() {
+    return this.getRelated("annotation");
+}
+
 /* */
 
-IriSP.Model.AnnotationType = function(_id, _directory) {
-    IriSP.Model.Element.call(this, _id, _directory);
+IriSP.Model.AnnotationType = function(_id, _source) {
+    IriSP.Model.Element.call(this, _id, _source);
     this.elementType = 'annotationType';
 }
 
 IriSP.Model.AnnotationType.prototype = new IriSP.Model.Element();
 
+IriSP.Model.AnnotationType.prototype.getAnnotations = function() {
+    return this.getRelated("annotation");
+}
+
 /* Annotation
  * */
 
-IriSP.Model.Annotation = function(_id, _directory) {
-    IriSP.Model.Element.call(this, _id, _directory);
+IriSP.Model.Annotation = function(_id, _source) {
+    IriSP.Model.Element.call(this, _id, _source);
     this.elementType = 'annotation';
     this.begin = new IriSP.Model.Time();
     this.end = new IriSP.Model.Time();
@@ -384,17 +422,18 @@
 
 IriSP.Model.Source.prototype.setDefaultCurrentMedia = function() {
     if (typeof this.currentMedia === "undefined") {
-        this.currentMedia = this.getList("media")[0];
+        this.currentMedia = this.getList("media").getElementAt(0);
     }
 }
 
-IriSP.Model.Source.prototype.listNamespaces = function() {
+IriSP.Model.Source.prototype.listNamespaces = function(_excludeSelf) {
     var _this = this,
-        _nsls = [];
+        _nsls = [],
+        _excludeSelf = (typeof _excludeSelf !== "undefined" && _excludeSelf);
     this.each(function(_list) {
         IriSP._(_list.contents).each(function(_id) {
             var _ns = _id.replace(/:.*$/,'');
-            if (_nsls.indexOf(_ns) === -1) {
+            if (IriSP._(_nsls).indexOf(_ns) === -1 && (!_excludeSelf || _ns !== _this.namespace)) {
                 _nsls.push(_ns);
             }
         })
@@ -433,6 +472,17 @@
     return this.getList("media");
 }
 
+IriSP.Model.Source.prototype.getAnnotationTypes = function() {
+    return this.getList("annotationType");
+}
+
+IriSP.Model.Source.prototype.getAnnotationTypeByTitle = function(_title) {
+    var _res = this.getAnnotationTypes().searchByTitle(_title);
+    if (_res.length() > 0) {
+        return _res.getElementAt(0);
+    }
+}
+
 IriSP.Model.Source.prototype.getDuration = function() {
     return this.currentMedia.duration;
 }
--- a/src/js/serializers/CinecastSerializer.js	Fri Apr 13 18:08:19 2012 +0200
+++ b/src/js/serializers/CinecastSerializer.js	Mon Apr 16 19:10:32 2012 +0200
@@ -0,0 +1,182 @@
+if (typeof IriSP.serializers === "undefined") {
+    IriSP.serializers = {}
+}
+
+IriSP.serializers.cinecast = {
+    types :  {
+        media : {
+            serialized_name : "medias",
+            model_name : "media",
+            deserializer : function(_data, _source) {
+                var _res = new IriSP.Model.Media(_data.id, _source);
+                _res.url = _data.href;
+                _res.title = _data.meta.title;
+                _res.description = _data.meta.synopsis;
+                _res.setDuration(_data.meta.duration);
+                return _res;        
+            },
+            serializer : function(_data, _source) {
+                return {
+                    id : _source.unNamespace(_data.id),
+                    url : _data.url,
+                    meta : {
+                        title : _data.title,
+                        synopsis : _data.description,
+                        duration : _data.duration.milliseconds
+                    }
+                }
+            }
+        },
+        tag : {
+            serialized_name : "tags",
+            model_name : "tag",
+            deserializer : function(_data, _source) {
+                var _res = new IriSP.Model.Tag(_data.id, _source);
+                _res.title = _data.meta.description;
+                return _res;        
+            },
+            serializer : function(_data, _source) {
+                return {
+                    id : _source.unNamespace(_data.id),
+                    meta : {
+                        description : _data.title
+                    }
+                }
+            }
+        },
+        annotationType : {
+            serialized_name : "annotation_types",
+            deserializer : function(_data, _source) {
+                var _res = new IriSP.Model.AnnotationType(_data.id, _source);
+                _res.title = _source.getNamespaced(_data.id).name;
+                _res.description = _data.meta.description;
+                return _res;        
+            },
+            serializer : function(_data, _source) {
+                return {
+                    id : _source.unNamespace(_data.id),
+                    meta : {
+                        description : _data.description
+                    }
+                }
+            }
+        },
+        annotation : {
+            serialized_name : "annotations",
+            deserializer : function(_data, _source) {
+                var _res = new IriSP.Model.Annotation(_data.id, _source);
+                _res.title = _data.meta.creator_name;
+                _res.description = _data.content.data;
+                _res.created = IriSP.Model.isoToDate(_data.meta.created);
+                var _c = parseInt(_data.color).toString(16);
+                while (_c.length < 6) {
+                    _c = '0' + _c;
+                }
+                _res.color = '#' + _c;
+                _res.setMedia(_data.media, _source);
+                _res.setAnnotationType(_data.type);
+                _res.setTags(IriSP._(_data.tags).map(function(_t) {
+                    if (typeof _source.contents.tag === "undefined") {
+                        _source.contents.tag = new IriSP.Model.List(_source.directory);
+                    }
+                    if (_source.contents.tag.hasId(_t)) {
+                        return _t;
+                    } else {
+                        var _id = _t.toLowerCase()
+                            .replace(/#/g,'')
+                            .replace(/^(\d)/,'_$1')
+                            .replace(/[áâäàã]/g,'a')
+                            .replace(/ç/g,'c')
+                            .replace(/[éèêë]/g,'e')
+                            .replace(/[íìîï]/g,'i')
+                            .replace(/ñ/g,'n')
+                            .replace(/[óòôöõ]/g,'o')
+                            .replace(/œ/g,'oe')
+                            .replace(/[úùûü]/g,'u')
+                            .replace(/ÿ/g,'y')
+                            .replace(/[^A-Za-z0-9_]/g,''),
+                            _tag = new IriSP.Model.Tag(_id, _source);
+                        _tag.title = _t;
+                        _source.contents.tag.addElement(_tag);
+                        return _id;
+                    }
+                }));
+                _res.setBegin(_data.begin);
+                _res.setEnd(_data.end);
+                _res.creator = _data.meta.creator;
+                return _res;
+            },
+            serializer : function(_data, _source) {
+                return {
+                    id : _source.unNamespace(_data.id),
+                    content : {
+                        data : _data.description
+                    },
+                    begin : _data.begin.milliseconds,
+                    end : _data.begin.milliseconds,
+                    media : _source.unNamespace(_data.media.contents),
+                    type : _source.unNamespace(_data.annotationType.contents),
+                    meta : {
+                        created : IriSP.Model.dateToIso(_data.created),
+                        creator : _data.creator,
+                        creator_name : _data.title
+                    },
+                    tags : _data.getTags().map(function(_el) {
+                        return _source.unNamespace(_el.id)
+                    })
+                }
+            }
+        }
+    },
+    serialize : function(_source) {
+        var _res = {
+                format : "http://advene.org/ns/cinelab/"
+            },
+            _this = this,
+            _nsls = _source.listNamespaces(true);
+        _res.imports = [];
+        for (var _i = 0; _i < _nsls.length; _i++) {
+           if (typeof _source.directory.namespaces[_nsls[_i]] !== "undefined") {
+               _res.imports.push({
+                   id : _nsls[_i],
+                   url : _source.directory.namespaces[_nsls[_i]]
+               })
+           } 
+        }
+        _source.each(function(_list, _typename) {
+            if (typeof _this.types[_typename] !== "undefined") {
+                _res[_this.types[_typename].serialized_name] = _list.map(function(_el) {
+                    return _this.types[_typename].serializer(_el, _source);
+                });
+            }
+        });
+        return _res;
+    },
+    deSerialize : function(_data, _source) {
+        if (typeof _data.imports !== "undefined") {
+            IriSP._(_data.imports).each(function(_import) {
+                _source.directory.namespaces[_import.id] = _import.url;
+            })
+        }
+        IriSP._(this.types).each(function(_type, _typename) {
+            var _listdata = _data[_type.serialized_name];
+            if (typeof _listdata !== "undefined") {
+                var _list = new IriSP.Model.List(_source.directory);
+                if (_listdata.hasOwnProperty("length")) {
+                    var _l = _listdata.length;
+                    for (var _i = 0; _i < _l; _i++) {
+                        _list.addElement(_type.deserializer(_listdata[_i], _source));
+                    }
+                } else {
+                    _list.addElement(_type.deserializer(_listdata, _source));
+                }
+                _source.addList(_typename, _list);
+            }
+        });
+        
+        if (typeof _data.meta !== "undefined" && typeof _data.meta.main_media !== "undefined" && typeof _data.meta.main_media["id-ref"] !== "undefined") {
+            _source.setCurrentMediaId(_data.meta.main_media["id-ref"]);
+        }
+        _source.setDefaultCurrentMedia();
+    }
+}
\ No newline at end of file
--- a/src/js/serializers/PlatformSerializer.js	Fri Apr 13 18:08:19 2012 +0200
+++ b/src/js/serializers/PlatformSerializer.js	Mon Apr 16 19:10:32 2012 +0200
@@ -7,17 +7,17 @@
         media : {
             serialized_name : "medias",
             model_name : "media",
-            deserializer : function(_data, _container) {
-                var _res = new IriSP.Model.Media(_data.id, _container);
+            deserializer : function(_data, _source) {
+                var _res = new IriSP.Model.Media(_data.id, _source);
                 _res.url = _data.href;
                 _res.title = _data.meta["dc:title"];
                 _res.description = _data.meta["dc:description"];
                 _res.setDuration(_data.meta["dc:duration"]);
                 return _res;        
             },
-            serializer : function(_data, _container) {
+            serializer : function(_data, _source) {
                 return {
-                    id : _container.unNamespace(_data.id),
+                    id : _source.unNamespace(_data.id),
                     href : _data.url,
                     meta : {
                         "dc:title" : _data.title,
@@ -30,31 +30,31 @@
         tag : {
             serialized_name : "tags",
             model_name : "tag",
-            deserializer : function(_data, _container) {
-                var _res = new IriSP.Model.Tag(_data.id, _container);
+            deserializer : function(_data, _source) {
+                var _res = new IriSP.Model.Tag(_data.id, _source);
                 _res.title = _data.meta["dc:title"];
                 return _res;        
             },
-            serializer : function(_data, _container) {
+            serializer : function(_data, _source) {
                 return {
-                    id : _container.unNamespace(_data.id),
+                    id : _source.unNamespace(_data.id),
                     meta : {
                         "dc:title" : _data.title
                     }
                 }
             }
         },
-        annotationTypes : {
+        annotationType : {
             serialized_name : "annotation-types",
-            deserializer : function(_data, _container) {
-                var _res = new IriSP.Model.AnnotationType(_data.id, _container);
+            deserializer : function(_data, _source) {
+                var _res = new IriSP.Model.AnnotationType(_data.id, _source);
                 _res.title = _data["dc:title"];
                 _res.description = _data["dc:description"];
                 return _res;        
             },
-            serializer : function(_data, _container) {
+            serializer : function(_data, _source) {
                 return {
-                    id : _container.unNamespace(_data.id),
+                    id : _source.unNamespace(_data.id),
                     "dc:title" : _data.title,
                     "dc:description" : _data.description
                 }
@@ -62,8 +62,8 @@
         },
         annotation : {
             serialized_name : "annotations",
-            deserializer : function(_data, _container) {
-                var _res = new IriSP.Model.Annotation(_data.id, _container);
+            deserializer : function(_data, _source) {
+                var _res = new IriSP.Model.Annotation(_data.id, _source);
                 _res.title = _data.content.title;
                 _res.description = _data.content.description;
                 _res.created = IriSP.Model.isoToDate(_data.meta["dc:created"]);
@@ -72,58 +72,68 @@
                     _c = '0' + _c;
                 }
                 _res.color = '#' + _c;
-                _res.setMedia(_data.media, _container);
+                _res.setMedia(_data.media, _source);
                 _res.setAnnotationType(_data.meta["id-ref"]);
                 _res.setTags(IriSP._(_data.tags).pluck("id-ref"));
                 _res.setBegin(_data.begin);
                 _res.setEnd(_data.end);
+                _res.creator = _data.meta["dc:creator"];
                 return _res;
             },
-            serializer : function(_data, _container) {
+            serializer : function(_data, _source) {
                 return {
-                    id : _container.unNamespace(_data.id),
+                    id : _source.unNamespace(_data.id),
                     content : {
                         title : _data.title,
                         description : _data.description
                     },
-                    media : _container.unNamespace(_data.media.contents),
+                    media : _source.unNamespace(_data.media.contents),
                     meta : {
-                        "id-ref" : _container.unNamespace(_data.annotationType.contents),
-                        "dc:created" : IriSP.Model.dateToIso(_data.created)
+                        "id-ref" : _source.unNamespace(_data.annotationType.contents),
+                        "dc:created" : IriSP.Model.dateToIso(_data.created),
+                        "dc:creator" : _data.creator
                     },
                     tags : _data.getTags().map(function(_el, _id) {
                        return {
-                           "id-ref" : _container.unNamespace(_id)
+                           "id-ref" : _source.unNamespace(_id)
                        } 
                     })
                 }
             }
         }
     },
-    serialize : function(_container) {
+    serialize : function(_source) {
         var _res = {},
             _this = this;
-        _container.each(function(_list, _typename) {
-            _res[_this.types[_typename].serialized_name] = _list.map(function(_el) {
-                return _this.types[_typename].serializer(_el, _container);
-            });
+        _source.each(function(_list, _typename) {
+            if (typeof _this.types[_typename] !== "undefined") {
+                _res[_this.types[_typename].serialized_name] = _list.map(function(_el) {
+                    return _this.types[_typename].serializer(_el, _source);
+                });
+            }
         });
         return _res;
     },
-    deSerialize : function(_data, _container) {
+    deSerialize : function(_data, _source) {
         IriSP._(this.types).each(function(_type, _typename) {
-            if (typeof _data[_type.serialized_name] !== "undefined") {
-                var _list = new IriSP.Model.List(_container.directory);
-                IriSP._(_data[_type.serialized_name]).each(function(_el) {
-                    _list.addElement(_type.deserializer(_el, _container));
-                });
-                _container.addList(_typename, _list);
+            var _listdata = _data[_type.serialized_name];
+            if (typeof _listdata !== "undefined") {
+                var _list = new IriSP.Model.List(_source.directory);
+                if (_listdata.hasOwnProperty("length")) {
+                    var _l = _listdata.length;
+                    for (var _i = 0; _i < _l; _i++) {
+                        _list.addElement(_type.deserializer(_listdata[_i], _source));
+                    }
+                } else {
+                    _list.addElement(_type.deserializer(_listdata, _source));
+                }
+                _source.addList(_typename, _list);
             }
         });
         
         if (typeof _data.meta !== "undefined" && typeof _data.meta.main_media !== "undefined" && typeof _data.meta.main_media["id-ref"] !== "undefined") {
-            _container.setCurrentMediaId(_data.meta.main_media["id-ref"]);
+            _source.setCurrentMediaId(_data.meta.main_media["id-ref"]);
         }
-        _container.setDefaultCurrentMedia();
+        _source.setDefaultCurrentMedia();
     }
 }
\ No newline at end of file
--- a/test/model/test.html	Fri Apr 13 18:08:19 2012 +0200
+++ b/test/model/test.html	Mon Apr 16 19:10:32 2012 +0200
@@ -11,19 +11,29 @@
         </script>
         <script type="text/javascript" src="../../src/js/model.js"></script>
         <script type="text/javascript" src="../../src/js/serializers/PlatformSerializer.js"></script>
+        <script type="text/javascript" src="../../src/js/serializers/CinecastSerializer.js"></script>
         <script type="text/javascript">
             IriSP.jQuery = jQuery;
             IriSP._ = _;
             var _directory = new IriSP.Model.Directory();
             var _source = _directory.remoteSource({
-                url: "../integration/polemic_fr.json",
+                //url: "../integration/polemic_fr.json",
+                url : '../integration/allocine_dossier_independant/json_examples/movie32.json',
                 namespace: "metadataplayer",
-                serializer: IriSP.serializers.platform
+                serializer: IriSP.serializers.cinecast
             });
             function showExport() {
                 console.log(_source.serialize());
                 $("body").html(JSON.stringify(_source.serialize()).replace(/(\[|\{)/g,'$1<ul><li>').replace(/(\]|\})/g,'</li></ul>$1').replace(/,/g,',</li><li>'))
             }
+            function benchmark(name, callback) {
+                document.write('<p>Running benchmark ' + name + '</p>');
+                var b = new Date();
+                for (var i = 0; i < 100000; i++) {
+                    callback(i);
+                }
+                document.write('<p>Took ' + (new Date() - b) + 'ms</p>');
+            }
         </script>
         <style type="text/css">
             ul, li {