source: DASISH/t5.6/client/trunk/chrome/markingcollection/content/markingcollection/annotator-service/annotation-service.js @ 4547

Last change on this file since 4547 was 4547, checked in by olof, 10 years ago

adding dasish_aid for GET

File size: 7.2 KB
Line 
1var annotationFramework = (function() {
2    // default value for REST service
3    // SND backend for first tests
4    // var default_backend = "http://pelle.ssd.gu.se:8080/exist/rest/db/annotation-framework/api/annotations.xql";
5    // MPI backend
6    var default_backend = "https://lux17.mpi.nl/ds/webannotator";
7
8    return {
9        getDefaultBackend: function() {
10            return default_backend;
11        },
12        getBackend: function() {
13            // selected_state values: "" (empty value as long as Settings Dialog has not been opened) OR "WM_REST_DEFAULT" OR "WM_REST_UserDefined"
14            // user-specified value for REST service
15            var selected_state = nsPreferences.copyUnicharPref("wiredmarker.marker.initdata.url", "");
16            if (selected_state === "WM_REST_UserDefined") {
17                return nsPreferences.copyUnicharPref("wiredmarker.marker.initdata.url_format", "");
18            } else {
19                return default_backend;
20            }
21        },
22        getAnnotations: function(url, callback) {
23            annotationProxy.log('calling the annotation backend ');
24           
25            $.ajax({
26                type: "GET",
27                url: this.getBackend() + '/api/annotations?link='+url,
28                dataType: "xml",
29                beforeSend: function(xhr){xhr.setRequestHeader('Content-Type', 'application/xml');},
30                success: function(xml, textStatus, jqXHR) {
31                                   
32                   
33                    var annotations = Array();
34                   
35                    $xml = $.parseXML(jqXHR.responseText);
36                    annotationProxy.log($xml);
37                   
38                    jQuery($xml).find('annotationInfo').each(function() {
39                        annotationProxy.log(this);
40                        annotationProxy.log(this.getAttribute('ref'));
41                       
42                       
43                        annotations.push(this.getAttribute('ref'));
44                    });
45                   
46
47                    callback.call(undefined, annotations);
48                },
49                error: function(result){
50                    annotationProxy.log('ERROR calling the annotation backend '+result);
51                    Firebug.Console.log(result);
52                }
53            });
54        },
55        getAnnotation: function(annotationURL, callback) {
56            annotationProxy.log('getAnnotation '+annotationURL);
57            $.ajax({
58                type: "GET",
59                url: annotationURL,
60                dataType: "xml",
61                success: function(xml, textStatus, jqXHR) {
62                    $xml = $.parseXML(jqXHR.responseText);
63                    annotationProxy.log('GOT annotation in getAnnotation ');   
64                    annotationProxy.log($xml);
65                    var om_object = annotation2om_object($xml);
66                   
67                    callback.call(undefined, om_object);
68                },
69                error: function(error) {
70                    annotationProxy.log('ERROR in getAnnotation ');
71                    annotationProxy.log(error);
72                }
73            });
74        },
75        deleteAnnotationByOid: function(oid) {
76            Firebug.Console.log('entering DELETE for oid: '+oid);
77            var aid;
78            var aSql = 'select dasish_aid from om_object where oid="' + oid + '"';
79            var rtn = bitsObjectMng.Database.selectB("", aSql); // aMode = "" defaults to predefined value; aSql contains sql statement
80           
81            aid = rtn[0].dasish_aid;
82            Firebug.Console.log('Resolved the AID for oid: '+oid+' for aid: '+aid);
83            if (aid) { // ajax request only for annotations posted to and available in backend database
84                return $.ajax({
85                    url: this.getBackend() + '/api/annotations/' + aid,
86                    type: 'DELETE',
87                    error: function(jqXHR, status, thrownError) {
88                        // Handle any errors
89                        if (typeof Firebug !== 'undefined' && Firebug.Console) {
90                            Firebug.Console.log("+ + + + + + + + + + + + + + + + + + + + + + + +");
91                            Firebug.Console.log("Status Code (DELETE request): " + jqXHR.status);
92                            Firebug.Console.log("Error DELETE request: " + thrownError);
93                        }
94                    },
95                    success: function(result) {
96                        if (typeof Firebug !== 'undefined' && Firebug.Console) {
97                            Firebug.Console.log("DELETE request was successful.");
98                            Firebug.Console.log(result);
99                        }
100                    }
101                });
102            }
103        },
104        postAnnotation: function(annotation, oid) {
105            return $.ajax({
106                type: "POST",
107                url: this.getBackend() + '/api/annotations?store=true',
108                dataType: "xml",
109                data: annotation,
110                contentType: "application/xml",
111                error: function(jqXHR, status, thrownError) {
112                    // Handle any errors
113                    if (typeof Firebug !== 'undefined' && Firebug.Console) {
114                        Firebug.Console.log("+ + + + + + + + + + + + + + + + + + + + + + + +");
115                        Firebug.Console.log("Status Code: " + jqXHR.status);
116                        Firebug.Console.log("Error : " + thrownError);
117                    }
118                },
119                complete: function(jqXHR, status, responseText) {
120                    var response = jqXHR.responseText.match(/URI="(.+?)"/)[1].split('/');
121                    var aid = response[response.length - 1];
122
123                    if (typeof Firebug !== 'undefined' && Firebug.Console) {
124                        Firebug.Console.log("+ + + + + + + + + + + + + + + + + + + + + + + +");
125                        Firebug.Console.log("Status Code POST request: " + jqXHR.status);
126                        Firebug.Console.log("Response Body: " + jqXHR.responseText);
127                        Firebug.Console.log("+ + + + + + + + + + + + + + + + + + + + + + + +");
128                    }
129                    Firebug.Console.log("OID: " + oid);
130                    Firebug.Console.log("AID: " + aid);
131                    // Firebug.Console.log(bitsObjectMng.Database.getObject({oid: oid}));
132
133                    var aSql = 'update om_object set dasish_aid = "' + aid + '" where oid="' + oid + '"';
134                    Firebug.Console.log(aSql);
135                    // insert request to local sqlite database where aid gets inserted
136                    var rtn = bitsObjectMng.Database.cmd("", aSql); // aMode = "" defaults to predefined value; aSql contains sql statement
137                    Firebug.Console.log('UPDATE of AID done');
138                    Firebug.Console.log(rtn);
139                    // Database insert request is true if successful
140                    // Firebug.Console.log(rtn);
141
142                }
143            });
144        },
145        setBackend: function(url) {
146            this.backend = url;
147        }
148    }
149}());
Note: See TracBrowser for help on using the repository browser.