]> git.argeo.org Git - gpl/argeo-slc.git/blob - server/org.argeo.slc.ria/src/argeo-ria-src/class/org/argeo/ria/remote/RemoteNotifier.js
be97aee069b2a29d0b60a903a68c7845cae62cd5
[gpl/argeo-slc.git] / server / org.argeo.slc.ria / src / argeo-ria-src / class / org / argeo / ria / remote / RemoteNotifier.js
1 /**
2 * This can be triggered at the end of a IO Request. In that case, it contains
3 * a data type as an identifier, and the request response itself.
4 * Can be used this way to listen for data changes from various parts of the application.
5 */
6 qx.Class.define("org.argeo.ria.remote.RemoteNotifier",
7 {
8 extend : qx.core.Object,
9
10 construct : function(uri, pollService, addService, removeService){
11 this.base(arguments);
12 this.setUri(uri);
13 this.setPollService(pollService);
14 this.setAddService(addService);
15 this.setRemoveService(removeService);
16 },
17 properties :{
18 uri : {
19 check : "String"
20 },
21 pollService : {check : "String"},
22 addService : {check : "String"},
23 removeService : {check : "String"},
24 eventParamName : {check : "String", init:"eventType"},
25 eventXPath : {check : "String", init:"//event"},
26 eventTypeXPath : {check : "String", init:"@type"},
27 eventDataXPath : {check : "String", init:"@data"},
28 answerStatusXPath : {check : "String", init:"slc:execution-answer/slc:status"},
29 timeout : {
30 init : 20000
31 },
32 errorTimeout : {
33 init : 5000
34 },
35 interrupt : {
36 check : "Boolean",
37 init : false
38 }
39 },
40 members : {
41 addListener : function(eventType, eventParamName){
42 var req = this._getRequest(this.getAddService());
43 req.setParameter(this.getEventParamName(), eventType);
44 req.send();
45 },
46 removeListener : function(eventType, eventParamName){
47 var req = this._getRequest(this.getRemoveService());
48 req.setParameter(this.getEventParamName(), eventType);
49 req.send();
50 },
51 startPolling : function(){
52 this.setInterrupt(false);
53 var req = this._getRequest(this.getPollService());
54 req.setParameter("timeout", "10");
55 req.addListener("completed", this._pollHandler, this);
56 req.send();
57 },
58 stopPolling : function(){
59 this.setInterrupt(true);
60 },
61 _poll : function(){
62 if(this.getInterrupt()) return;
63 var req = this._getRequest(this.getPollService());
64 req.setParameter("timeout", this.getTimeout());
65 req.setTimeout(this.getTimeout() + 5000);
66 req.addListener("completed", this._pollHandler, this);
67 req.addListener("failed", this._errorHandler, this);
68 req.addListener("timeout", this._errorHandler, this);
69 req.addListener("aborted", this._errorHandler, this);
70 req.send();
71 },
72 _pollHandler : function(response){
73 // Parse response
74 var status = org.argeo.ria.util.Element.getSingleNodeText(response.getContent(), this.getAnswerStatusXPath());
75 if(status && status == "ERROR"){
76 this._errorHandler();
77 return;
78 }
79 var messages = org.argeo.ria.util.Element.selectNodes(response.getContent(), this.getEventXPath());
80 if(messages){
81 for(var i=0;i<messages.length;i++){
82 try{
83 var eventType = org.argeo.ria.util.Element.getSingleNodeText(messages[i], this.getEventTypeXPath());
84 var eventData = org.argeo.ria.util.Element.getSingleNodeText(messages[i], this.getEventDataXPath());
85 org.argeo.ria.event.UIBus.getInstance().dispatchEvent(eventType, eventData);
86 }catch(e){
87 this.error(e);
88 }
89 }
90 }
91 this._poll();
92 },
93 _errorHandler : function(){
94 // Wait an try again later
95 qx.event.Timer.once(this._poll, this, this.getErrorTimeout());
96 },
97 _getRequest : function(service, method, type){
98 return new qx.io.remote.Request(
99 this.getUri()+service,
100 method || "GET",
101 type||"application/xml"
102 );
103 }
104 }
105 });