1 Components.utils.import("resource://stmodules/STRequests.jsm");
  2 Components.utils.import("resource://stmodules/Logger.jsm");
  3 Components.utils.import("resource://stmodules/Deserializer.jsm");	
  4 Components.utils.import("resource://stmodules/stEvtMgr.jsm");
  5 
  6 Components.utils.import("resource://stmodules/Context.jsm");
  7 
  8 EXPORTED_SYMBOLS = [ "SemTurkeyHTTPLegacy", "STRequests" ];
  9 
 10 var service = STRequests.Annotation;
 11 var serviceName = service.serviceName;
 12 
 13 /**
 14  * create and annotate an istance with drag n drop
 15  * 
 16  * @member STRequests.Annotation
 17  * @return
 18  */
 19 function createAndAnnotate(clsQName, instanceQName, urlPage, title) {
 20 	Logger.debug('[SERVICE_Annotation.jsm] createAndAnnotate');
 21 	var clsQName = "clsQName=" + clsQName;
 22 	var instanceQName = "instanceQName=" + instanceQName;
 23 	var urlPage = "urlPage=" + urlPage;
 24 	var title = "title=" + title;
 25 	var contextAsArray = this.context.getContextValuesForHTTPGetAsArray();
 26 	var reply = SemTurkeyHTTPLegacy.GET(serviceName, service.createAndAnnotateRequest, clsQName, instanceQName, urlPage, title, contextAsArray);
 27 	var resArray = new Array();
 28 	resArray["class"] = Deserializer.createURI(reply.getElementsByTagName("Class")[0]);
 29 	resArray["instance"] = Deserializer.createURI(reply.getElementsByTagName("Instance")[0]);
 30 	return resArray;
 31 }
 32 
 33 
 34 /**
 35  * given qname <code>instanceQName</code> of an existing instance, it adds an annotation according to the
 36  * other arguments of the method
 37  * 
 38  * @member STRequests.Annotation
 39  * @param instanceQName
 40  * @param text
 41  * @param urlPage
 42  * @param title
 43  * @return
 44  */
 45 function createFurtherAnnotation(instanceQName,text,urlPage,title) {
 46 	Logger.debug('[SERVICE_Annotation.jsm] createFurtherAnnotationRequest');
 47 	var instanceQName = "instanceQName=" + instanceQName;
 48 	var text = "text=" + text;
 49 	var urlPage = "urlPage=" + urlPage;
 50 	var title = "title=" + title;
 51 	var contextAsArray = this.context.getContextValuesForHTTPGetAsArray();
 52 	return SemTurkeyHTTPLegacy.GET(serviceName, service.createFurtherAnnotationRequest, instanceQName, text, urlPage, title, contextAsArray);
 53 }
 54 
 55 
 56 
 57 /**
 58  * as for <code>relateAndAnnotate</code> with some of its arguments set for binding annotated value to an
 59  * existing resource
 60  * 
 61  * @member STRequests.Annotation
 62  * @param instanceQName
 63  * @param propertyQName
 64  * @param objectQName
 65  * @param urlPage
 66  * @param title
 67  * @param lexicalization
 68  * @return
 69  */
 70 function relateAndAnnotateBindAnnot(instanceQName,propertyQName,objectQName,urlPage,title,lexicalization) {
 71 	return relateAndAnnotate(instanceQName,propertyQName,objectQName,urlPage,title,null,null,null,lexicalization); 
 72 }
 73 
 74 /**
 75  * as for <code>relateAndAnnotate</code> with some of its arguments set for creating an element
 76  * (resource/literal) from the annotated text
 77  * 
 78  * @member STRequests.Annotation
 79  * @param instanceQName
 80  * @param propertyQName
 81  * @param objectQName
 82  * @param objectClsName
 83  * @param urlPage
 84  * @param title
 85  * @param lang
 86  * @param type
 87  * @return
 88  */
 89 function relateAndAnnotateBindCreate(instanceQName,propertyQName,objectQName,urlPage,title,objectClsName,lang,type) {
 90 	return relateAndAnnotate(instanceQName,propertyQName,objectQName,urlPage,title,objectClsName,lang,type); 
 91 }
 92 
 93 
 94 /**
 95  * this method is used to get information from web pages and use it to:
 96  * <ul>
 97  * <li>relate existing objects in the ontology with new objects got from the web </li>
 98  * <li>take the annotation related to the new object</li>
 99  * </ul>
100  * 
101  * @member STRequests.Annotation
102  * @param instanceQName
103  *            the qname of the selected object which is related to the annotated one
104  * @param propertyQName
105  *            the property which is used to qualify the selected object with new annotated information
106  * @param objectQName
107  *            the annotated object (this may be an ontology object, or a literal)
108  * @param objectClsName
109  *            if objectQName is a resource which is to be created, then this is the class associated that
110  *            resource
111  * @param urlPage
112  *            the url of the web page from which the annotation has been taken
113  * @param title
114  *            the title of the web page from which the annotation has been taken
115  * @param lang
116  *            the iso code for th language of the annotated text
117  * @param type
118  *            used to qualify the nature of the annotated object; this argument is mandatory only if the
119  *            property specified by <code>propertyQName</code>is a standard property, otherwise the nature
120  *            of the object is desumed from the nature of the property (resource for object properties and
121  *            literal in other cases).<br/> In case of a standard property, "literal" must be specified,
122  *            otherwise a resource is added to the property
123  * 
124  * @param lexicalization
125  * @return
126  */
127 function relateAndAnnotate(instanceQName,propertyQName,objectQName,urlPage,title,objectClsName,lang,type,lexicalization) {
128 	var instanceQName = "instanceQName=" + instanceQName;
129 	var propertyQName = "propertyQName=" + propertyQName;
130 	var objectQName = "objectQName=" + objectQName;
131 	var urlPage = "urlPage=" + urlPage;
132 	var title = "title=" + title;
133 	var contextAsArray = this.context.getContextValuesForHTTPGetAsArray();
134 	if(typeof lexicalization != 'undefined'){
135 		var op = "op=bindAnnot";
136 		var lex = "lexicalization=" + lexicalization;
137 		return SemTurkeyHTTPLegacy.GET(serviceName, service.relateAndAnnotateRequest, 
138 	    		instanceQName,
139 	    		propertyQName,
140 	    		objectQName,
141 	    		lex,
142 	    		urlPage, 
143 	    		title,
144 	    		op,
145 	    		contextAsArray);
146 	}else{
147 		var op = "op=bindCreate";
148 		if(lang != ""){
149 			var lang = "lang=" + lang;
150 			if(typeof type != 'undefined'){
151 				var type = "type=" +type;
152 				if(typeof objectClsName != 'undefined'){
153 					var objectClsName = "objectClsName=" + objectClsName;
154 					return SemTurkeyHTTPLegacy.GET(serviceName, service.relateAndAnnotateRequest, 
155 		    			instanceQName,
156 		    			propertyQName,
157 		    			objectQName,
158 		    			objectClsName, 
159 		    			urlPage, 
160 		    			title,
161 		    			lang,
162 		    			type,
163 		    			op,
164 		    			contextAsArray);
165 		    	}else{
166 		    		return SemTurkeyHTTPLegacy.GET(serviceName, service.relateAndAnnotateRequest, 
167 		    			instanceQName,
168 		    			propertyQName,
169 		    			objectQName,
170 		    			urlPage, 
171 		    			title,
172 		    			lang,
173 		    			type,
174 		    			op,
175 		    			contextAsArray);
176 		    	}
177 	    	} else{
178 		    	if(typeof objectClsName != 'undefined'){
179 					var objectClsName = "objectClsName=" + objectClsName;
180 					return SemTurkeyHTTPLegacy.GET(serviceName, service.relateAndAnnotateRequest, 
181 		    			instanceQName,
182 		    			propertyQName,
183 		    			objectQName,
184 		    			objectClsName, 
185 		    			urlPage, 
186 		    			title,
187 		    			lang,
188 		    			op,
189 		    			contextAsArray);
190 		    	}else{
191 		    		return SemTurkeyHTTPLegacy.GET(serviceName, service.relateAndAnnotateRequest, 
192 		    			instanceQName,
193 		    			propertyQName,
194 		    			objectQName,
195 		    			urlPage, 
196 		    			title,
197 		    			lang,
198 		    			op,
199 		    			contextAsArray);
200 		    	}
201 		    }
202 		}else{
203 			if(typeof type != 'undefined'){
204 				var type = "type=" +type;
205 				if(typeof objectClsName != 'undefined'){
206 					var objectClsName = "objectClsName=" + objectClsName;
207 					return SemTurkeyHTTPLegacy.GET(serviceName, service.relateAndAnnotateRequest, 
208 		    			instanceQName,
209 		    			propertyQName,
210 		    			objectQName,
211 		    			objectClsName, 
212 		    			urlPage, 
213 		    			title,
214 		    			type,
215 		    			op,
216 		    			contextAsArray);
217 		    	}else{
218 		    		return SemTurkeyHTTPLegacy.GET(serviceName, service.relateAndAnnotateRequest, 
219 		    			instanceQName,
220 		    			propertyQName,
221 		    			objectQName,
222 		    			urlPage, 
223 		    			title,
224 		    			type,
225 		    			op,contextAsArray);
226 		    	}
227     		} else{
228 	    		if(typeof objectClsName != 'undefined'){
229 					var objectClsName = "objectClsName=" + objectClsName;
230 					return SemTurkeyHTTPLegacy.GET(serviceName, service.relateAndAnnotateRequest, 
231 	    				instanceQName,
232 	    				propertyQName,
233 	    				objectQName,
234 	    				objectClsName, 
235 	    				urlPage, 
236 	    				title,
237 	    				op,
238 	    				contextAsArray);
239 		    	}else{
240 		    		return SemTurkeyHTTPLegacy.GET(serviceName, service.relateAndAnnotateRequest, 
241 		    			instanceQName,
242 		    			propertyQName,
243 		    			objectQName,
244 		    			urlPage, 
245 		    			title,
246 		    			op,
247 		    			contextAsArray);
248 		    	}
249 		    }
250 		}
251 	}
252    }
253 
254 /**
255  * given page <code>urlPage</code>, this method tells if the page contains annotations
256  * 
257  * @member STRequests.Annotation
258  * @param urlPage
259  * @return
260  */
261 function chkAnnotation(urlPage) {
262 	var urlPage="urlPage="+urlPage;
263 	var contextAsArray = this.context.getContextValuesForHTTPGetAsArray();
264 	return SemTurkeyHTTPLegacy.GET(serviceName, service.chkAnnotationsRequest,urlPage,contextAsArray);
265 }
266 
267  /** given page <code>urlPage</code>, this method tells if the page has topics
268  * 
269  * @member STRequests.Annotation
270  * @param urlPage
271  * @return
272  */
273 function chkBookmarks(urlPage) {
274 	var urlPage="urlPage="+urlPage;
275 	var contextAsArray = this.context.getContextValuesForHTTPGetAsArray();
276 	return SemTurkeyHTTPLegacy.GET(serviceName, service.chkBookmarksRequest,urlPage,contextAsArray);
277 }
278 
279 /**
280  * given page <code>urlPage</code>, it returns all annotations previously taken on that page
281  * 
282  * @member STRequests.Annotation
283  * @param urlPage
284  * @return
285  */
286 function getPageAnnotations(urlPage) {
287 	var urlPage="urlPage="+urlPage;
288 	var contextAsArray = this.context.getContextValuesForHTTPGetAsArray();
289 	var reponseXML = SemTurkeyHTTPLegacy.GET(serviceName, service.getPageAnnotationsRequest,urlPage,contextAsArray);
290 	var annotations = [];
291 	var annotationsXML = reponseXML.getElementsByTagName("Annotation");
292 	
293 	for (var i = 0 ; i < annotationsXML.length ; i++) {
294 		var annXML = annotationsXML[i];
295 		
296 		annotations.push(
297 			{
298 				id : annXML.getAttribute("id"),
299 				resource : annXML.getAttribute("resource"),
300 				value : annXML.getAttribute("value")
301 			}
302 		);
303 	}
304 	
305 	return annotations;
306 }
307 
308 /**
309  * @member STRequests.Annotation
310  * @param urlPage
311  * @param instanceQName
312  * @param text
313  * @param title
314  * @return
315  */
316 function addAnnotation(urlPage,instanceQName,text,title){
317 	var urlPage="urlPage="+urlPage;
318 	var instanceQName="instanceQName="+instanceQName;
319 	var text="text="+text;
320 	var title="title="+title;
321 	var contextAsArray = this.context.getContextValuesForHTTPGetAsArray();
322 	return SemTurkeyHTTPLegacy.GET(serviceName, service.addAnnotationRequest,urlPage,instanceQName,text,title,contextAsArray);
323 }
324 
325 /**
326  * @member STRequests.Annotation
327  * @param urlPage
328  * @param title
329  * @param topics
330  * @return
331  */
332 function bookmarkPage(urlPage, title, topics){
333 	var urlPage_p="urlPage="+urlPage;
334 	var title_p="title="+title;
335 	var topics_p = "topics=";
336 	
337 	for (var i = 0 ; i < topics.length ; i++) {
338 		topics_p += topics[i];
339 	}
340 	var contextAsArray = this.context.getContextValuesForHTTPGetAsArray();
341 	return Deserializer.createRDFArray(SemTurkeyHTTPLegacy.GET(serviceName, service.bookmarkPageRequest,urlPage_p,title_p,topics_p,contextAsArray));
342 }
343 
344 function getPageTopics(urlPage) {
345 	var urlPage_p = "urlPage=" + urlPage;
346 	var contextAsArray = this.context.getContextValuesForHTTPGetAsArray();
347 	return Deserializer.createRDFArray(SemTurkeyHTTPLegacy.GET(serviceName, service.getPageTopicsRequest,urlPage_p,contextAsArray));
348 }
349 
350 function getBookmarksByTopic(topic) {
351 	var topic_p = "topic=" + topic;
352 	var contextAsArray = this.context.getContextValuesForHTTPGetAsArray();
353 	return SemTurkeyHTTPLegacy.GET(serviceName, service.getBookmarksByTopicRequest,topic_p,contextAsArray);
354 }
355 
356 function removeBookmark(urlPage, topic) {
357 	var urlPage_p = "urlPage=" + urlPage;
358 	var topic_p = "topic=" + topic;
359 	
360 	var contextAsArray = this.context.getContextValuesForHTTPGetAsArray();
361 	var reply = SemTurkeyHTTPLegacy.GET(serviceName, service.removeBookmarkRequest, urlPage_p, topic_p,contextAsArray);
362 
363 	if (!reply.isFail()) {
364 		evtMgr.fireEvent("bookmarkRemoved", {
365 			getPageURL : function() {return urlPage;},
366 			getTopic : function() {return topic;}
367 			});
368 	}
369 	
370 	return reply;
371 }
372 
373 /**
374  * 
375  * @member STRequests.RangeAnnotation
376  * @param id
377  * @return
378  */
379 function removeAnnotation(id) {
380 	var id_p = "id=" + id;
381 
382 	var contextAsArray = this.context.getContextValuesForHTTPGetAsArray();
383 	var responseXML = SemTurkeyHTTPLegacy.GET(serviceName, service.removeAnnotationRequest, id_p,contextAsArray);
384 	
385 	if (!responseXML.isFail()) {
386 		var annotation;
387 		
388 		evtMgr.fireEvent("annotationDeleted", {
389 			getId : function(){return id;}
390 		});
391 	}
392 
393 	return responseXML;
394 }
395 
396 /**
397  * 
398  * @member STRequests.Annotation
399  * @param resource
400  * @return
401  */
402 function getAnnotatedContentResources(resource) {
403 	var resource_p = "resource=" + resource;
404 
405 	var contextAsArray = this.context.getContextValuesForHTTPGetAsArray();
406 	var responseXML = SemTurkeyHTTPLegacy.GET(serviceName, service.getAnnotatedContentResourcesRequest, resource_p,contextAsArray);
407 	
408 	var response = [];
409 	
410 	var urlElements = responseXML.getElementsByTagName("URL");
411 	
412 	for (var i = 0 ; i < urlElements.length ; i++) {
413 		var obj = {};
414 		obj.title = urlElements[i].getAttribute("title");
415 		obj.value = urlElements[i].getAttribute("value");
416 		
417 		response.push(obj);
418 	}
419 
420 	return response;
421 }
422 
423 // Annotation SERVICE INITIALIZATION
424 //this return an implementation for Project with a specified context
425 service.prototype.getAPI = function(specifiedContext){
426 	var newObj = new service();
427 	newObj.context = specifiedContext;
428 	return newObj;
429 }
430 service.prototype.chkAnnotation = chkAnnotation;
431 service.prototype.chkBookmarks = chkBookmarks;
432 service.prototype.getPageAnnotations = getPageAnnotations;
433 service.prototype.createAndAnnotate = createAndAnnotate;
434 service.prototype.relateAndAnnotateBindAnnot = relateAndAnnotateBindAnnot;
435 service.prototype.relateAndAnnotateBindCreate = relateAndAnnotateBindCreate;
436 service.prototype.createFurtherAnnotation = createFurtherAnnotation;
437 service.prototype.addAnnotation=addAnnotation;
438 service.prototype.bookmarkPage=bookmarkPage;
439 service.prototype.getPageTopics=getPageTopics;
440 service.prototype.getBookmarksByTopic=getBookmarksByTopic;
441 service.prototype.removeBookmark=removeBookmark;
442 service.prototype.removeAnnotation = removeAnnotation;
443 service.prototype.getAnnotatedContentResources = getAnnotatedContentResources;
444 service.prototype.context = new Context();  // set the default context
445 service.constructor = service;
446 service.__proto__ = service.prototype;