src/js/serializers/CinecastSerializer.js
branchnew-model
changeset 904 510ebab76fa3
parent 903 d9da52e20f7f
child 905 029a4efe9e24
equal deleted inserted replaced
903:d9da52e20f7f 904:510ebab76fa3
     1 /* Cinecast Cinelab Serializer */
       
     2 
       
     3 if (typeof IriSP.serializers === "undefined") {
       
     4     IriSP.serializers = {}
       
     5 }
       
     6 
       
     7 IriSP.serializers.cinecast = {
       
     8     types :  {
       
     9         media : {
       
    10             serialized_name : "medias",
       
    11             model_name : "media",
       
    12             deserializer : function(_data, _source) {
       
    13                 var _res = new IriSP.Model.Media(_data.id, _source);
       
    14                 _res.video = _data.url;
       
    15                 _res.title = _data.meta.title;
       
    16                 _res.description = _data.meta.synopsis;
       
    17                 _res.setDuration(_data.meta.duration);
       
    18                 return _res;        
       
    19             },
       
    20             serializer : function(_data, _source) {
       
    21                 return {
       
    22                     id : _source.unNamespace(_data.id),
       
    23                     url : _data.video,
       
    24                     meta : {
       
    25                         title : _data.title,
       
    26                         synopsis : _data.description,
       
    27                         duration : _data.duration.milliseconds
       
    28                     }
       
    29                 }
       
    30             }
       
    31         },
       
    32         tag : {
       
    33             serialized_name : "tags",
       
    34             model_name : "tag",
       
    35             deserializer : function(_data, _source) {
       
    36                 var _res = new IriSP.Model.Tag(_data.id, _source);
       
    37                 _res.title = _data.meta.description;
       
    38                 return _res;        
       
    39             },
       
    40             serializer : function(_data, _source) {
       
    41                 return {
       
    42                     id : _source.unNamespace(_data.id),
       
    43                     meta : {
       
    44                         description : _data.title
       
    45                     }
       
    46                 }
       
    47             }
       
    48         },
       
    49         annotationType : {
       
    50             serialized_name : "annotation_types",
       
    51             deserializer : function(_data, _source) {
       
    52                 var _res = new IriSP.Model.AnnotationType(_data.id, _source);
       
    53                 _res.title = _source.getNamespaced(_data.id).name;
       
    54                 _res.description = _data.meta.description;
       
    55                 return _res;        
       
    56             },
       
    57             serializer : function(_data, _source) {
       
    58                 return {
       
    59                     id : _source.unNamespace(_data.id),
       
    60                     meta : {
       
    61                         description : _data.description
       
    62                     }
       
    63                 }
       
    64             }
       
    65         },
       
    66         annotation : {
       
    67             serialized_name : "annotations",
       
    68             deserializer : function(_data, _source) {
       
    69                 var _res = new IriSP.Model.Annotation(_data.id, _source);
       
    70                 _res.title = _data.meta.creator_name;
       
    71                 _res.description = _data.content.data;
       
    72                 _res.created = IriSP.Model.isoToDate(_data.meta.created);
       
    73                 _res.setMedia(_data.media, _source);
       
    74                 _res.setAnnotationType(_data.type);
       
    75                 _res.setTags(IriSP._(_data.tags).map(function(_t) {
       
    76                     if (typeof _source.contents.tag === "undefined") {
       
    77                         _source.contents.tag = new IriSP.Model.List(_source.directory);
       
    78                     }
       
    79                     if (_source.contents.tag.hasId(_t)) {
       
    80                         return _t;
       
    81                     } else {
       
    82                         var _id = _t.toLowerCase()
       
    83                             .replace(/#/g,'')
       
    84                             .replace(/^(\d)/,'_$1')
       
    85                             .replace(/[áâäàã]/g,'a')
       
    86                             .replace(/ç/g,'c')
       
    87                             .replace(/[éèêë]/g,'e')
       
    88                             .replace(/[íìîï]/g,'i')
       
    89                             .replace(/ñ/g,'n')
       
    90                             .replace(/[óòôöõ]/g,'o')
       
    91                             .replace(/œ/g,'oe')
       
    92                             .replace(/[úùûü]/g,'u')
       
    93                             .replace(/ÿ/g,'y')
       
    94                             .replace(/[^A-Za-z0-9_]/g,''),
       
    95                             _tag = new IriSP.Model.Tag(_id, _source);
       
    96                         _tag.title = _t;
       
    97                         _source.contents.tag.push(_tag);
       
    98                         return _id;
       
    99                     }
       
   100                 }));
       
   101                 _res.setBegin(_data.begin);
       
   102                 _res.setEnd(_data.end);
       
   103                 _res.creator = _data.meta.creator;
       
   104                 return _res;
       
   105             },
       
   106             serializer : function(_data, _source) {
       
   107                 return {
       
   108                     id : _source.unNamespace(_data.id),
       
   109                     content : {
       
   110                         data : _data.description
       
   111                     },
       
   112                     begin : _data.begin.milliseconds,
       
   113                     end : _data.begin.milliseconds,
       
   114                     media : _source.unNamespace(_data.media.id),
       
   115                     type : _source.unNamespace(_data.annotationType.id),
       
   116                     meta : {
       
   117                         created : IriSP.Model.dateToIso(_data.created),
       
   118                         creator : _data.creator,
       
   119                         creator_name : _data.title
       
   120                     },
       
   121                     tags : _data.tag.id.map(function(_id) {
       
   122                         return _source.unNamespace(_id)
       
   123                     })
       
   124                 }
       
   125             }
       
   126         }
       
   127     },
       
   128     serialize : function(_source) {
       
   129         var _res = {
       
   130                 format : "http://advene.org/ns/cinelab/"
       
   131             },
       
   132             _this = this,
       
   133             _nsls = _source.listNamespaces(true);
       
   134         _res.imports = [];
       
   135         for (var _i = 0; _i < _nsls.length; _i++) {
       
   136            if (typeof _source.directory.namespaces[_nsls[_i]] !== "undefined") {
       
   137                _res.imports.push({
       
   138                    id : _nsls[_i],
       
   139                    url : _source.directory.namespaces[_nsls[_i]]
       
   140                })
       
   141            } 
       
   142         }
       
   143         _source.forEach(function(_list, _typename) {
       
   144             if (typeof _this.types[_typename] !== "undefined") {
       
   145                 _res[_this.types[_typename].serialized_name] = _list.map(function(_el) {
       
   146                     return _this.types[_typename].serializer(_el, _source);
       
   147                 });
       
   148             }
       
   149         });
       
   150         return _res;
       
   151     },
       
   152     loadData : function(_url, _callback) {
       
   153         IriSP.jQuery.getJSON(_url, _callback)
       
   154     },
       
   155     deSerialize : function(_data, _source) {
       
   156         if (typeof _data !== "object" || _data === null) {
       
   157             return;
       
   158         }
       
   159         if (typeof _data.imports !== "undefined") {
       
   160             IriSP._(_data.imports).forEach(function(_import) {
       
   161                 _source.directory.namespaces[_import.id] = _import.url;
       
   162             })
       
   163         }
       
   164         IriSP._(this.types).forEach(function(_type, _typename) {
       
   165             var _listdata = _data[_type.serialized_name];
       
   166             if (typeof _listdata !== "undefined" && _listdata !== null) {
       
   167                 var _list = new IriSP.Model.List(_source.directory);
       
   168                 if (_listdata.hasOwnProperty("length")) {
       
   169                     var _l = _listdata.length;
       
   170                     for (var _i = 0; _i < _l; _i++) {
       
   171                         _list.push(_type.deserializer(_listdata[_i], _source));
       
   172                     }
       
   173                 } else {
       
   174                     _list.push(_type.deserializer(_listdata, _source));
       
   175                 }
       
   176                 _source.addList(_typename, _list);
       
   177             }
       
   178         });
       
   179         
       
   180         if (typeof _data.meta !== "undefined" && typeof _data.meta.main_media !== "undefined" && typeof _data.meta.main_media["id-ref"] !== "undefined") {
       
   181             _source.setCurrentMediaId(_data.meta.id);
       
   182         }
       
   183         _source.setDefaultCurrentMedia();
       
   184     }
       
   185 }
       
   186