src/js/serializers/ldt_annotate.js
branchplatform-restapi
changeset 974 560afb14296d
parent 924 64c2eaafe5e2
child 975 35aadec4131b
equal deleted inserted replaced
973:638fe8541a2e 974:560afb14296d
     3 if (typeof IriSP.serializers === "undefined") {
     3 if (typeof IriSP.serializers === "undefined") {
     4     IriSP.serializers = {}
     4     IriSP.serializers = {}
     5 }
     5 }
     6 
     6 
     7 IriSP.serializers.ldt_annotate = {
     7 IriSP.serializers.ldt_annotate = {
     8     types :  {
     8     serializeAnnotation : function(_data, _source) {
     9         annotation : {
     9         var _annType = _data.getAnnotationType();
    10             serialized_name : "annotations",
    10         return {
    11             serializer : function(_data, _source) {
    11             begin: _data.begin.milliseconds,
    12                 var _annType = _data.getAnnotationType();
    12             end: _data.end.milliseconds,
    13                 return {
    13             content: {
    14                     begin: _data.begin.milliseconds,
    14                 data: _data.description,
    15                     end: _data.end.milliseconds,
    15                 audio: _data.audio
    16                     content: {
    16             },
    17                         data: _data.description,
    17             tags: _data.getTagTexts(),
    18                         audio: _data.audio
    18             media: _data.getMedia().id,
    19                     },
    19             type_title: _annType.title,
    20                     tags: _data.getTagTexts(),
    20             type: ( typeof _annType.dont_send_id !== "undefined" && _annType.dont_send_id ? "" : _annType.id ),
    21                     media: _data.getMedia().id,
    21             meta: {
    22                     type_title: _annType.title,
    22                 created: _data.created,
    23                     type: ( typeof _annType.dont_send_id !== "undefined" && _annType.dont_send_id ? "" : _annType.id )
    23                 creator: _data.creator
    24                 }
       
    25             }
    24             }
    26         }
    25         }
    27     },
    26     },
    28     serialize : function(_source) {
    27     serialize : function(_source) {
    29         var _res = {},
    28         var _this = this
    30             _this = this;
    29             _res = {
    31         _source.forEach(function(_list, _typename) {
    30                 "objects": _source.getAnnotations().map(function(_annotation) {
    32             if (typeof _this.types[_typename] !== "undefined") {
    31                     return _this.serializeAnnotation(_annotation, _source);
    33                 _res[_this.types[_typename].serialized_name] = _list.map(function(_el) {
    32                 })
    34                     return _this.types[_typename].serializer(_el, _source);
    33             };
    35                 });
       
    36             }
       
    37         });
       
    38         _res.meta = {
       
    39             creator: _source.creator,
       
    40             created: _source.created
       
    41         }
       
    42         return JSON.stringify(_res);
    34         return JSON.stringify(_res);
    43     },
    35     },
    44     deSerialize : function(_data, _source) {
    36     deSerialize : function(_data, _source) {
    45         if (typeof _data == "string") {
    37         if (typeof _data == "string") {
    46             _data = JSON.parse(_data);
    38             _data = JSON.parse(_data);
    47         }
    39         }
       
    40         
    48         _source.addList('tag', new IriSP.Model.List(_source.directory));
    41         _source.addList('tag', new IriSP.Model.List(_source.directory));
    49         _source.addList('annotationType', new IriSP.Model.List(_source.directory));
    42         _source.addList('annotationType', new IriSP.Model.List(_source.directory));
    50         _source.addList('annotation', new IriSP.Model.List(_source.directory));
    43         _source.addList('annotation', new IriSP.Model.List(_source.directory));
    51         if (typeof _data.annotations == "object" && _data.annotations && _data.annotations.length) {
    44         IriSP._(_data.objects).each(function(_anndata) {
    52             var _anndata = _data.annotations[0],
    45             var _ann = new IriSP.Model.Annotation(_anndata.id, _source);
    53                 _ann = new IriSP.Model.Annotation(_anndata.id, _source);
       
    54             _ann.description = _anndata.content.data || "";
    46             _ann.description = _anndata.content.data || "";
    55             _ann.title = _data.creator || "";
    47             _ann.title = "";
    56             _ann.created = new Date(_data.meta.created);
    48             _ann.creator = _anndata.meta.creator || "";
       
    49             _ann.created = new Date(_anndata.meta.created);
    57             _ann.setMedia(_anndata.media, _source);
    50             _ann.setMedia(_anndata.media, _source);
    58             var _anntypes = _source.getAnnotationTypes(true).searchByTitle(_anndata.type_title);
    51             var _anntype = _source.getElement(_anndata.type);
    59             if (_anntypes.length) {
    52             if (!_anntype) {
    60                 var _anntype = _anntypes[0];
    53                 _anntype = new IriSP.Model.AnnotationType(_anndata.type, _source);
    61             } else {
       
    62                 var _anntype = new IriSP.Model.AnnotationType(_anndata.type, _source);
       
    63                 _anntype.title = _anndata.type_title;
    54                 _anntype.title = _anndata.type_title;
    64                 _source.getAnnotationTypes().push(_anntype);
    55                 _source.getAnnotationTypes().push(_anntype);
    65             }
    56             }
    66             _ann.setAnnotationType(_anntype.id);
    57             _ann.setAnnotationType(_anntype.id);
    67             var _tagIds = IriSP._(_anndata.tags).map(function(_title) {
    58             var _tagIds = IriSP._(_anndata.tags).map(function(_title) {
    68                 var _tags = _source.getTags(true).searchByTitle(_title);
    59                 var _tags = _source.getTags(true).searchByTitle(_title, true);
    69                 if (_tags.length) {
    60                 if (_tags.length) {
    70                     var _tag = _tags[0];
    61                     var _tag = _tags[0];
    71                 }
    62                 }
    72                 else {
    63                 else {
    73                     _tag = new IriSP.Model.Tag(_title.replace(/\W/g,'_'),_source);
    64                     _tag = new IriSP.Model.Tag(_title.replace(/\W/g,'_'),_source);
    77                 return _tag.id;
    68                 return _tag.id;
    78             });
    69             });
    79             _ann.setTags(_tagIds);
    70             _ann.setTags(_tagIds);
    80             _ann.setBegin(_anndata.begin);
    71             _ann.setBegin(_anndata.begin);
    81             _ann.setEnd(_anndata.end);
    72             _ann.setEnd(_anndata.end);
    82             _ann.creator = _data.meta.creator;
       
    83             if (typeof _anndata.content.audio !== "undefined" && _anndata.content.audio.href) {
    73             if (typeof _anndata.content.audio !== "undefined" && _anndata.content.audio.href) {
    84                 _ann.audio = _anndata.content.audio;
    74                 _ann.audio = _anndata.content.audio;
    85             }
    75             }
    86             _source.getAnnotations().push(_ann);
    76             _source.getAnnotations().push(_ann);
    87         }
    77         });
    88     }
    78     }
    89 }
    79 }