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