integration/js/ldt-serializer.js
changeset 41 3ec2343f2b85
child 42 40909e8d6855
equal deleted inserted replaced
40:76efa2333f31 41:3ec2343f2b85
       
     1 /* LDT Platform Serializer */
       
     2 
       
     3 if (typeof IriSP.serializers === "undefined") {
       
     4     IriSP.serializers = {}
       
     5 }
       
     6 
       
     7 IriSP.serializers.ldt = {
       
     8     types :  {
       
     9         media : {
       
    10             serialized_name : "medias",
       
    11             deserializer : function(_data, _source) {
       
    12                 var _res = new IriSP.Model.Media(_data.id, _source);
       
    13                 _res.video = (
       
    14                     typeof _data.url !== "undefined"
       
    15                     ? _data.url
       
    16                     : (
       
    17                         typeof _data.href !== "undefined"
       
    18                         ? _data.href
       
    19                         : null
       
    20                     )
       
    21                 );
       
    22                 if (typeof _data.meta.item !== "undefined" && _data.meta.item.name === "streamer") {
       
    23                     _res.streamer = _data.meta.item.value;
       
    24                 }
       
    25                 _res.title = _data.meta["dc:title"];
       
    26                 _res.description = _data.meta["dc:description"];
       
    27                 _res.setDuration(_data.meta["dc:duration"]);
       
    28                 _res.url = _data.meta.url;
       
    29                 if (typeof _data.meta.img !== "undefined" && _data.meta.img.src !== "undefined") {
       
    30                     _res.thumbnail = _data.meta.img.src;
       
    31                 }
       
    32                 return _res;        
       
    33             },
       
    34             serializer : function(_data, _source) {
       
    35                 return {
       
    36                     id : _data.id,
       
    37                     url : _data.video,
       
    38                     meta : {
       
    39                         "dc:title" : _data.title,
       
    40                         "dc:description" : _data.description,
       
    41                         "dc:duration" : _data.duration.milliseconds
       
    42                     }
       
    43                 }
       
    44             }
       
    45         },
       
    46         tag : {
       
    47             serialized_name : "tags",
       
    48             model_name : "tag",
       
    49             deserializer : function(_data, _source) {
       
    50                 var _res = new IriSP.Model.Tag(_data.id, _source);
       
    51                 _res.title = _data.meta["dc:title"];
       
    52                 return _res;        
       
    53             },
       
    54             serializer : function(_data, _source) {
       
    55                 return {
       
    56                     id : _data.id,
       
    57                     meta : {
       
    58                         "dc:title" : _data.title
       
    59                     }
       
    60                 }
       
    61             }
       
    62         },
       
    63         annotationType : {
       
    64             serialized_name : "annotation-types",
       
    65             deserializer : function(_data, _source) {
       
    66                 var _res = new IriSP.Model.AnnotationType(_data.id, _source);
       
    67                 _res.title = _data["dc:title"];
       
    68                 _res.description = _data["dc:description"];
       
    69                 return _res;        
       
    70             },
       
    71             serializer : function(_data, _source) {
       
    72                 return {
       
    73                     id : _data.id,
       
    74                     "dc:title" : _data.title,
       
    75                     "dc:description" : _data.description
       
    76                 }
       
    77             }
       
    78         },
       
    79         annotation : {
       
    80             serialized_name : "annotations",
       
    81             deserializer : function(_data, _source) {
       
    82                 var _res = new IriSP.Model.Annotation(_data.id, _source);
       
    83                 _res.title = _data.content.title || "";
       
    84                 _res.description = _data.content.description || "";
       
    85                 if (typeof _data.content.img !== "undefined" && _data.content.img.src !== "undefined") {
       
    86                     _res.thumbnail = _data.content.img.src;
       
    87                 }
       
    88                 _res.created = IriSP.Model.isoToDate(_data.meta["dc:created"]);
       
    89                 if (typeof _data.color !== "undefined") {
       
    90                     var _c = parseInt(_data.color).toString(16);
       
    91                     while (_c.length < 6) {
       
    92                         _c = '0' + _c;
       
    93                     }
       
    94                     _res.color = '#' + _c;
       
    95                 }
       
    96                 _res.setMedia(_data.media);
       
    97                 _res.setAnnotationType(_data.meta["id-ref"]);
       
    98                 _res.setTags(IriSP._(_data.tags).pluck("id-ref"));
       
    99                 _res.keywords = _res.getTagTexts();
       
   100                 _res.setBegin(_data.begin);
       
   101                 _res.setEnd(_data.end);
       
   102                 _res.creator = _data.meta["dc:creator"] || "";
       
   103                 _res.project = _data.meta.project || "";
       
   104                 if (typeof _data.meta["dc:source"] !== "undefined" && typeof _data.meta["dc:source"].content !== "undefined") {
       
   105                     _res.source = JSON.parse(_data.meta["dc:source"].content);
       
   106                 }
       
   107                 if (typeof _data.content.audio !== "undefined" && _data.content.audio.href) {
       
   108                     _res.audio = _data.content.audio;
       
   109                 }
       
   110                 return _res;
       
   111             },
       
   112             serializer : function(_data, _source) {
       
   113                 return {
       
   114                     id : _data.id,
       
   115                     begin : _data.begin.milliseconds,
       
   116                     end : _data.end.milliseconds,
       
   117                     content : {
       
   118                         title : _data.title,
       
   119                         description : _data.description,
       
   120                         audio : _data.audio
       
   121                     },
       
   122                     media : _data.media.id,
       
   123                     meta : {
       
   124                         "id-ref" : _data.annotationType.id,
       
   125                         "dc:created" : IriSP.Model.dateToIso(_data.created),
       
   126                         "dc:creator" : _data.creator,
       
   127                         project : _source.projectId
       
   128                     },
       
   129                     tags : IriSP._(_data.tag.id).map(function(_id) {
       
   130                        return {
       
   131                            "id-ref" : _id
       
   132                        } 
       
   133                     })
       
   134                 }
       
   135             }
       
   136         },
       
   137         mashup : {
       
   138             serialized_name : "lists",
       
   139             deserializer : function(_data, _source) {
       
   140                 if (typeof _data.meta !== "object" || typeof _data.meta.listtype !== "string" || _data.meta.listtype !== "mashup") {
       
   141                     return undefined;
       
   142                 }
       
   143                 var _res = new IriSP.Model.Mashup(_data.id, _source);
       
   144                 _res.title = _data.meta["dc:title"];
       
   145                 _res.description = _data.meta["dc:description"];
       
   146                 _res.creator = _data.meta["dc:creator"];
       
   147                 _res.setAnnotationsById(_data.items);
       
   148                 return _res;        
       
   149             },
       
   150             serializer : function(_data, _source) {
       
   151                 return {
       
   152                     meta : {
       
   153                         "dc:title": _data.title,
       
   154                         "dc:description": _data.description,
       
   155                         listtype: "mashup"
       
   156                     },
       
   157                     items: _data.segments.map(function(_annotation) {
       
   158                         return _id;
       
   159                     }),
       
   160                     id: _data.id
       
   161                 }
       
   162             }
       
   163         }
       
   164     },
       
   165     serialize : function(_source) {
       
   166         var _res = {},
       
   167             _this = this;
       
   168         _source.forEach(function(_list, _typename) {
       
   169             if (typeof _this.types[_typename] !== "undefined") {
       
   170                 _res[_this.types[_typename].serialized_name] = _list.map(function(_el) {
       
   171                     return _this.types[_typename].serializer(_el, _source);
       
   172                 });
       
   173             }
       
   174         });
       
   175         return JSON.stringify(_res);
       
   176     },
       
   177     loadData : function(_url, _callback) {
       
   178         IriSP.jQuery.getJSON(_url, _callback)
       
   179     },
       
   180     deSerialize : function(_data, _source) {
       
   181         if (typeof _data !== "object" || _data === null) {
       
   182             return;
       
   183         }
       
   184         IriSP._(this.types).forEach(function(_type, _typename) {
       
   185             var _listdata = _data[_type.serialized_name],
       
   186                 _list = new IriSP.Model.List(_source.directory);
       
   187             if (typeof _listdata !== "undefined" && _listdata !== null) {
       
   188                 if (_listdata.hasOwnProperty("length")) {
       
   189                     var _l = _listdata.length;
       
   190                     for (var _i = 0; _i < _l; _i++) {
       
   191                         var _element = _type.deserializer(_listdata[_i], _source);
       
   192                         if (typeof _element !== "undefined" && _element) {
       
   193                             _list.push(_element);
       
   194                         }
       
   195                     }
       
   196                 } else {
       
   197                     var _element = _type.deserializer(_listdata, _source);
       
   198                     if (typeof _element !== "undefined" && _element) {
       
   199                         _list.push(_element);
       
   200                     }
       
   201                 }
       
   202             }
       
   203             _source.addList(_typename, _list);
       
   204         });
       
   205         
       
   206         if (typeof _data.meta !== "undefined") {
       
   207             _source.projectId = _data.meta.id;
       
   208         }
       
   209         
       
   210         if (typeof _data.meta !== "undefined" && typeof _data.meta.main_media !== "undefined" && typeof _data.meta.main_media["id-ref"] !== "undefined") {
       
   211             _source.currentMedia = _source.getElement(_data.meta.main_media["id-ref"]);
       
   212         }
       
   213     }
       
   214 }
       
   215