source: ISOcat/trunk/mod-ISOcat-interface-gi/interface/JSXAPPS/ISOcat/js/org/isocat/data/CR.js @ 2029

Last change on this file since 2029 was 2029, checked in by mwindhouwer, 12 years ago

Initial import of all the *cats, i.e., ISOcat, RELcat and SCHEMAcat.

File size: 10.0 KB
Line 
1jsx3.util.Logger.getLogger("org.isocat").info("Loading class[org.isocat.data.CR]");
2
3/**
4 * The CR (Change Request) class
5 * @author Menzo.Windhouwer@mpi.nl
6 */
7jsx3.lang.Class.defineClass("org.isocat.data.CR", null, [], function(CR, CR_prototype) {
8       
9        /**
10         * The class logger
11         */
12        CR.log = jsx3.util.Logger.getLogger("org.isocat.data.CR");
13       
14        /**
15         * The CR key
16         */
17        CR_prototype._key;
18       
19        /**
20         * The CR status
21         */
22        CR_prototype._status;
23
24        /**
25         * The CR id
26         */
27        CR_prototype._id;
28       
29        /**
30         * The CR name
31         */
32        CR_prototype._name;
33       
34        /**
35         * The CR scope
36         */
37        CR_prototype._scope;
38       
39        /**
40         * Constructor
41         */
42        CR_prototype.init = function(id) {
43                org.isocat.data.CR.log.info("initialize CR["+this+"]");
44               
45                if (typeof(id) == 'undefined') {
46                        this._status = "NEW";
47                        this._id     = "0";
48                        this._name   = null;
49                        this._scope  = null;
50            this._key = "CR-"+jsx3.xml.CDF.getKey();   
51                } else {
52                        this._status = "EXISTS";
53                        this._id     = id;
54                        this._name   = null;
55                        this._scope  = null;
56            this._key = "CR-"+id+"-"+jsx3.xml.CDF.getKey();     
57                }       
58
59                org.isocat.data.CR.log.info("- key["+this._key+"]");
60                org.isocat.data.CR.log.info("- status["+this._status+"]");
61                org.isocat.data.CR.log.info("- id["+this._id+"]");
62               
63                // put an 'empty' CR CDF doc in the cache
64                var doc = jsx3.xml.CDF.Document.newDocument();
65                org.isocat.APP.getCache().setDocument(this.cacheId(),doc);
66               
67                org.isocat.data.CR.log.info("initialized CR["+this+"]");
68               
69                // publish the initialization message
70                org.isocat.publish("org.isocat.data.CR.init", this);
71        };
72       
73        CR_prototype.status = function(status) {
74                if (typeof(status) != "undefined") {
75                        this._status = status;
76                }
77                return this._status;
78        }
79       
80        CR_prototype.key = function() {
81                return this._key;
82        }
83       
84        CR_prototype.cacheId = function() {
85                return this.key();
86        }
87       
88        CR_prototype.id = function() {
89                return this._id;
90        }
91       
92        CR_prototype.name = function() {
93                return this._name;
94        }
95       
96        CR_prototype.uri = function(format) {
97                return "http://localhost:8080/isocat/rest/cr/"+escape(this.id())+format;
98        }
99       
100        CR_prototype.cdf = function(doc) {
101                if (typeof(doc)!='undefined') {
102                        org.isocat.APP.getCache().setDocument(this.cacheId(),doc);
103                }
104                return org.isocat.APP.getCache().getDocument(this.cacheId());
105        }
106       
107        CR_prototype.send = function(method) {
108                var req = null;
109                var doc = this.cdf();
110                var out = org.isocat.transformXML(doc,"CDF2CR_xsl");
111                if (out == null) {
112                        org.isocat.APP.alert("Failed to save the CR","Sorry, an error occurred and the CR couldn't be saved.",null,"OK");
113                } else {
114                        org.isocat.data.CR.log.info("CR["+out+"]");
115                       
116                        var outDoc = new jsx3.xml.Document();
117                        outDoc.loadXML(out);
118
119                        var uri = this.uri(".xml");
120                        org.isocat.data.CR.log.info("accessing service uri["+uri+"]["+method+"]");
121
122                        req = new jsx3.net.Request().open(method,uri,false);
123                        req.setRequestHeader("Content-Type","application/xml");// IE doesn't set this automagically (FF does), and otherwise NK won't pass it on to the ISOcat accessors
124                        req.setRequestHeader("Accept","application/xml");
125                        req.send(outDoc.toString());
126                }
127                return req;
128        };
129       
130        CR_prototype.scope = function(reload) {
131            var load = (this._scope == null);
132            if (!load && typeof(reload)!='undefined')
133                load = reload;
134                if (load) {
135                        var uri = this.uri("/scope.xml");
136                        org.isocat.data.CR.log.info("CR scope["+uri+"]");
137                        var req = new jsx3.net.Request().open("GET",uri,false);
138                        req.send();
139                if (req.getStatus()==200) {
140                this._scope = req.getResponseXML();
141                }
142                }
143                org.isocat.data.CR.log.info("CR scope["+this._scope+"]");
144                return this._scope;
145        };
146       
147        CR_prototype.roles = function() {
148           org.isocat.data.CR.log.info("CR["+this.key()+"]["+this.id()+"] user["+org.isocat.profile.id()+"] roles");
149           var result = new Array();
150           for (var roles = this.scope().selectNodeIterator("//account[@ref='"+org.isocat.profile.id()+"']/@cr-role");roles.hasNext();) {
151               var role = roles.next().getValue();
152               org.isocat.data.CR.log.info("CR["+this.key()+"]["+this.id()+"] user["+org.isocat.profile.id()+"] role["+role+"]");
153               result.push(role);
154           }
155           for (var roles = this.scope().selectNodeIterator("//account[@ref='"+org.isocat.profile.id()+"']/@role");roles.hasNext();) {
156               var role = roles.next().getValue();
157               org.isocat.data.CR.log.info("CR["+this.key()+"]["+this.id()+"] user["+org.isocat.profile.id()+"] role["+role+"]");
158               result.push(role);
159           }
160           return result;
161        };
162       
163        // CREATE
164        CR_prototype.toCDF = function(grp,tdg,descr,dcs,copies) {
165                // put the info in a CDF doc
166                var doc = jsx3.xml.CDF.Document.newDocument();
167                // - user
168                doc.insertRecord({jsxid:"initiator",ref:"user-"+org.isocat.profile.id(),jsxtext:org.isocat.profile.record.login})
169                // - group
170                doc.insertRecord({jsxid:"group"});
171                doc.insertRecordNode(grp.cdf().getRecordNode("meta").cloneNode(true),"group");
172                doc.insertRecordNode(grp.cdf().getRecordNode("members").cloneNode(true),"group");
173                // -tdg
174                doc.insertRecord({jsxid:"tdg",ref:tdg});
175                // -descr
176                doc.insertRecord({jsxid:"log"});
177                doc.insertRecord({jsxid:jsx3.xml.CDF.getKey(),jsxtext:descr},"log");
178                // -dcs
179                doc.insertRecord({jsxid:"dcs",jsxtext:dcs.name(),id:dcs.id(),copy:copies});
180                org.isocat.data.CR.log.info("CR["+doc+"]");
181               
182                return doc;
183        };
184       
185        CR_prototype.create = function(grp,tdg,descr,dcs,copies) {
186                if (this.status() == "NEW") {
187                        this.cdf(this.toCDF(grp,tdg,descr,dcs,copies));
188                       
189                        var req = this.send("PUT");
190                        if (req != null) {
191                                this.created(req);
192                        } else {
193                                org.isocat.publish("org.isocat.CR.create-failed", this.key());
194                        }
195                } else {
196                        org.isocat.data.CR.log.warn("calling CR["+this.key()+"].create(...) for an EXISTING CR");
197                }
198                return this.cacheId();
199        };
200       
201        CR_prototype.created = function(req) {
202                var status = req.getStatus();
203                org.isocat.data.CR.log.info("org.isocat.CR.create service request returned HTTP status["+status+"]");
204               
205                if (status==400) {
206                        org.isocat.APP.alert("Failed to submit the CR","The CR might have been invalid!",null,"OK");
207                        org.isocat.data.CR.log.info("failed to create the CR["+this.key()+"]:status["+status+"]");
208                        org.isocat.publish("org.isocat.CR.create-failed", this.type()+"-"+this.key());
209                } else if (status!=201) {
210                        org.isocat.APP.alert("Failed to save the CR","Sorry, an error occurred on the server and the CR couldn't be submitted.",null,"OK");
211                        org.isocat.data.CR.log.info("failed to submit the CR["+this.key()+"]:status["+status+"]");
212                        org.isocat.publish("org.isocat.CR.create-failed", this.key());
213                } else {
214                        org.isocat.data.CR.log.info("org.isocat.CR.create service request returned response["+req.getResponseText()+"]");
215                        var doc = org.isocat.transformXML(req.getResponseXML(),"CR2CDF_xsl");
216                        var cdf = org.isocat.convertXML2CDF(doc);
217                        this.cdf(cdf);
218                        this._status = "EXISTS";
219                        this._id = this.cdf().getRecord("meta").id;
220                        this._name = this.cdf().getRecord("meta").jsxtext;
221                        org.isocat.publish("org.isocat.CR.created", this.key());
222                        org.isocat.publish("org.isocat.WS.reload",org.isocat.profile.user());
223                }
224        };
225       
226        // RETRIEVE
227        CR_prototype.retrieve = function(params) {
228                if (this.status() != "NEW") {
229                        // create the URI
230                        var uri = this.uri(".xml");
231                        if (params!=null && typeof(params)!='undefined') {
232                                if (uri.indexOf('?') < 0) {
233                                        uri += "?";
234                                } else {
235                                        uri += "&";
236                                }
237                                uri += params;
238                        }
239                        // load the CR
240                        org.isocat.asyncLoadDynamicCDFDocument(new jsx3.net.URI(uri),"CR2CDF_xsl",null,this.cacheId(),this,this.retrieved);
241                } else {
242                        org.isocat.data.CR.log.warn("calling CR["+this._key+"].retrieve(...) for a NEW CR");
243                }
244                return this.cacheId();
245        };
246
247        CR_prototype.retrieved = function(doc,req) {
248                if (doc!=null) {
249                        this.cdf(doc);
250                        this._name = this.cdf().getRecord("meta").jsxtext;
251                        org.isocat.data.CR.log.info("The CR service call returned a valid response, stored in the cache["+this.cacheId()+"].");
252                        org.isocat.publish("org.isocat.CR.retrieved",this.key());
253                } else {
254                        org.isocat.data.CR.log.info("The CR service call failed. The HTTP Status code is: " + req.getStatus());
255                        org.isocat.publish("org.isocat.CR.retrieve-failed",this.key());
256                }
257        };
258       
259        // UPDATE
260        CR_prototype.updateStatus = function(status,msg) {
261            this.cdf().getRecordNode("meta").setAttribute("status",status);
262            this.cdf().insertRecord({jsxid:"action",action:"status",jsxtext:msg});
263            this.update();
264        };
265       
266        CR_prototype.updateVotes = function(votes) {
267           this.cdf().insertRecord({jsxid:"action",action:"vote"});
268           org.isocat.replaceSubCDFDocument(this.cdf(),"jsxroot","votes",votes,true);
269           this.update();
270        };
271
272    CR_prototype.update = function(status,msg) {   
273                if (this.status() != "NEW") {
274                        var req = this.send("POST");
275                        if (req != null) {
276                                this.updated(req);
277                        } else {
278                                org.isocat.publish("org.isocat.CR.update-failed",this.key());
279                        }
280                } else {
281                        org.isocat.data.CR.log.warn("calling CR["+this.key()+"].update(...) for a NEW CR");
282                }
283                return this.cacheId();
284        };
285       
286        CR_prototype.updated = function(req) {
287                var status = req.getStatus();
288                org.isocat.data.CR.log.info("org.isocat.CR.update service request returned HTTP status["+status+"]");
289               
290        if (status!=200) {
291                        org.isocat.APP.alert("Failed to save the CR","Sorry, an error occurred on the server and the CR couldn't be saved.",null,"OK");
292                        org.isocat.data.CR.log.info("failed to save the CR["+this.id()+"]["+this.key()+"]:status["+status+"]");
293                        org.isocat.publish("org.isocat.CR.update-failed",this.key());
294                } else {
295                        org.isocat.data.CR.log.info("org.isocat.CR.update service request returned response["+req.getResponseText()+"]");
296                        var doc = org.isocat.transformXML(req.getResponseXML(),"CR2CDF_xsl");
297                        var cdf = org.isocat.convertXML2CDF(doc);
298                        this.cdf(cdf);
299                        org.isocat.publish("org.isocat.CR.updated",this.key());
300                        org.isocat.publish("org.isocat.WS.reload",org.isocat.profile.user());
301                }
302        };
303       
304        // DELETE
305});
Note: See TracBrowser for help on using the repository browser.