]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/javascript/opensrf_domain_object.js
added sanity check for app for loop
[OpenSRF.git] / src / javascript / opensrf_domain_object.js
1 // -----------------------------------------------------------------------------
2 // This houses all of the domain object code.
3 // -----------------------------------------------------------------------------
4
5
6
7
8 // -----------------------------------------------------------------------------
9 // DomainObject 
10
11 DomainObject.prototype                                  = new domainObject();
12 DomainObject.prototype.constructor      = DomainObject;
13 DomainObject.prototype.baseClass                = domainObject.prototype.constructor;
14
15 /** Top level DomainObject class.  This most provides convience methods
16   * and a shared superclass
17   */
18 function DomainObject( name ) {
19         if( name ) { this._init_domainObject( name ); }
20 }
21
22 /** Returns the actual element of the given domainObjectAttr. */
23 DomainObject.prototype._findAttr = function ( name ) {
24         
25         var nodes = this.element.childNodes;
26
27         if( ! nodes || nodes.length < 1 ) {
28                 throw new oils_ex_dom( "Invalid xml object in _findAttr: " + this.toString() );
29         }
30
31         var i=0;
32         var node = nodes.item(i);
33
34         while( node != null ) {
35
36                 if( node.nodeName == "oils:domainObjectAttr" &&
37                                 node.getAttribute("name") == name ) {
38                         return node;
39                 }
40
41                 node = nodes.item(++i);
42         }
43
44         return null;
45 }
46
47
48
49
50 /** Returns the value stored in the given attribute */
51 DomainObject.prototype.getAttr = function ( name ) {
52
53         var node = this._findAttr( name );
54         if( node ) { return node.getAttribute( "value" ); }
55         else { 
56                 throw new oils_ex_dom( "getAttr(); Getting nonexistent attribute: " + name ); 
57         }
58 }
59
60 /** Updates the value held by the given attribute */
61 DomainObject.prototype.setAttr = function ( name, value ) {
62
63         var node = this._findAttr( name );
64         if( node ) {
65                 node.setAttribute( "value", value );
66         } else { 
67                 throw new oils_ex_dom( "setAttr(); Setting nonexistent attribute: " + name ); 
68         }
69 }
70
71 /** This takes a raw DOM Element node and creates the DomainObject that the node
72   * embodies and returns the object.  All new DomainObjects should be added to
73   * this list if they require this type of dynamic functionality.
74   * NOTE Much of this will be deprecated as move to a more JSON-centric wire protocol
75   */
76 DomainObject.newFromNode = function( node ) {
77
78         switch( node.getAttribute("name") ) {
79
80                 case "oilsMethod":
81                         return new oilsMethod().replaceNode( node );
82                 
83                 case "oilsMessage":
84                         return new oilsMessage().replaceNode( node );
85
86                 case "oilsResponse":
87                         return new oilsResponse().replaceNode( node );
88
89                 case "oilsResult":
90                         return new oilsResult().replaceNode( node );
91
92                 case "oilsConnectStatus":
93                         return new oilsConnectStatus().replaceNode( node );
94
95                 case "oilsException":
96                         return new oilsException().replaceNode( node );
97
98                 case "oilsMethodException":
99                         return new oilsMethodException().replaceNode( node );
100
101                 case "oilsBrokenSession":
102                         return new oilsBrokenSession().replaceNode( node );
103
104                 case "oilsScalar":
105                         return new oilsScalar().replaceNode( node );
106
107                 case "oilsPair":
108                         return new oilsPair().replaceNode( node );
109
110                 case "oilsArray":
111                         return new oilsArray().replaceNode( node );
112
113                 case "oilsHash":
114                         return new oilsHash().replaceNode( node );
115
116
117         }
118
119 }
120
121
122
123 // -----------------------------------------------------------------------------
124 // oilsMethod
125
126 oilsMethod.prototype = new DomainObject();
127 oilsMethod.prototype.constructor = oilsMethod;
128 oilsMethod.prototype.baseClass = DomainObject.prototype.constructor;
129
130 /**
131   * oilsMethod Constructor
132   * 
133   * @param method_name The name of the method your are sending
134   * to the remote server
135   * @param params_array A Javascript array of the params (any
136   * Javascript data type)
137   */
138
139 function oilsMethod( method_name, params_array ) {
140         this._init_domainObject( "oilsMethod" );
141         this.add( new domainObjectAttr( "method", method_name ) );
142
143         if( params_array ) {
144                 var params = this.doc.createElement( "oils:params" );
145                 this.element.appendChild( params ); 
146                 params.appendChild( this.doc.createTextNode( 
147                                 js2JSON( params_array ) ) ); 
148         }
149 }
150
151
152 /** Locates the params node of this method */
153 oilsMethod.prototype._getParamsNode = function() {
154
155         var nodes = this.element.childNodes;
156         if( ! nodes || nodes.length < 1 ) { return null; }
157         var x=0;
158         var node = nodes.item(x);
159         while( node != null ) {
160                 if( node.nodeName == "oils:params" ) {
161                         return node;
162                 }
163                 node = nodes.item(++x);
164         }
165
166         return null;
167 }
168
169
170 /** Returns an array of param objects  */
171 oilsMethod.prototype.getParams = function() {
172         var node = this._getParamsNode();
173         if(node != null ) { 
174                 return JSON2js( node.textContent ); 
175         }
176 }
177
178
179
180 // -----------------------------------------------------------------------------
181 // oilsMessage
182
183 // -----------------------------------------------------------------------------
184 // oilsMessage message types
185 // -----------------------------------------------------------------------------
186 /** CONNECT Message type */
187 oilsMessage.CONNECT             = 'CONNECT';
188 /** DISCONNECT Message type */
189 oilsMessage.DISCONNECT  = 'DISCONNECT';
190 /** STATUS Message type */
191 oilsMessage.STATUS              = 'STATUS';
192 /** REQUEST Message type */
193 oilsMessage.REQUEST             = 'REQUEST';
194 /** RESULT Message type */
195 oilsMessage.RESULT              = 'RESULT';
196
197
198 oilsMessage.prototype = new DomainObject();
199 oilsMessage.prototype.constructor = oilsMessage;
200 oilsMessage.prototype.baseClass = DomainObject.prototype.constructor;
201
202 /** Core XML object for message passing */
203 function oilsMessage( type, protocol, user_auth ) {
204
205         if( !( type && protocol) ) { 
206                 type = oilsMessage.CONNECT; 
207                 protocol = "1";
208         }
209
210         if( ! ( type == oilsMessage.CONNECT             ||
211                                 type == oilsMessage.DISCONNECT  ||
212                                 type == oilsMessage.STATUS                      ||
213                                 type == oilsMessage.REQUEST             ||
214                                 type == oilsMessage.RESULT      ) ) {
215                 throw new oils_ex_message( "Attempt to create oilsMessage with incorrect type: " + type );
216         }
217
218         this._init_domainObject( "oilsMessage" );
219
220         this.add( new domainObjectAttr( "type", type ) );
221         this.add( new domainObjectAttr( "threadTrace", 0 ) );
222         this.add( new domainObjectAttr( "protocol", protocol ) );       
223
224         if( user_auth ) { this.add( user_auth ); }
225
226 }
227
228 /** Builds a new oilsMessage from raw xml.
229   * Checks are taken to make sure the xml is supposed to be an oilsMessage.  
230   * If not, an oils_ex_dom exception is throw.
231   */
232 oilsMessage.newFromXML = function( xml ) {
233
234         try {
235
236                 if( ! xml || xml == "" ) { throw 1; }
237
238                 var doc = new DOMParser().parseFromString( xml, "text/xml" );
239                 if( ! doc ) { throw 1; }
240
241                 var root = doc.documentElement;
242                 if( ! root ) { throw 1; }
243
244                 // -----------------------------------------------------------------------------
245                 // There are two options here.  One is that we were provided the full message
246                 // xml (i.e. contains the <oils:root> tag.  The other option is that we were
247                 // provided just the message xml portion (top level element is the 
248                 // <oils:domainObject>
249                 // -----------------------------------------------------------------------------
250                 
251                 var element;
252                 if( root.nodeName == "oils:root"  ) { 
253
254                         element = root.firstChild;
255                         if( ! element ) { throw 1; }
256
257                 } else { 
258
259                         if( root.nodeName == "oils:domainObject" ) { 
260                                 element = root;
261
262                         } else { throw 1; }
263
264                 }
265
266
267                 if( element.nodeName != "oils:domainObject" ) { throw 1; } 
268                 if( element.getAttribute( "name" ) != "oilsMessage" ) { throw 1; }
269
270                 return new oilsMessage().replaceNode( element );
271
272         } catch( E ) {
273
274                 if( E && E.message ) {
275                         throw new oils_ex_dom( "Bogus XML for creating oilsMessage: " + E.message + "\n" + xml );
276
277                 } else {
278                         throw new oils_ex_dom( "Bogus XML for creating oilsMessage:\n" + xml );
279                 }
280         }
281
282         return msg;
283 }
284
285
286
287 /** Adds a copy of the given DomainObject to the message */
288 oilsMessage.prototype.addPayload = function( new_DomainObject ) {
289         this.add( new_DomainObject );
290 }
291
292
293 /** Returns the top level DomainObject contained in this message
294   *
295   * Note:  The object retuturned will be the actual object, not just a 
296   * generic DomainObject - e.g. oilsException.
297   */
298 oilsMessage.prototype.getPayload = function() {
299         
300         var nodes = this.element.childNodes;
301         var x=0;
302         var node = nodes.item(0);
303
304         while( node != null ) {
305
306                 if( node.nodeName == "oils:domainObject" ) {
307
308                         new Logger().debug( "Building oilsMessage payload from\n" + 
309                                 new XMLSerializer().serializeToString( node ), Logger.DEBUG );
310                         return DomainObject.newFromNode( node );
311                 }
312                 node = nodes.item(++x);
313         }
314
315         return null;
316 }
317
318 oilsMessage.prototype.getUserAuth = function() {
319
320         var nodes = this.element.getElementsByTagName( "oils:userAuth" );
321         if( ! nodes ) { return null; }
322
323         var auth_elem = nodes.item(0); // should only be one
324         if ( ! auth_elem ) { return null; }
325
326         return new userAuth().replaceNode( auth_elem );
327
328 }
329
330 /** Returns the type of the message */
331 oilsMessage.prototype.getType = function() {
332         return this.getAttr( "type" );
333 }
334
335 /** Returns the message protocol */
336 oilsMessage.prototype.getProtocol = function() {
337         return this.getAttr( "protocol" );
338 }
339
340 /** Returns the threadTrace */
341 oilsMessage.prototype.getThreadTrace = function() {
342         return this.getAttr( "threadTrace" );
343 }
344
345 /** Sets the thread trace - MUST be an integer */
346 oilsMessage.prototype.setThreadTrace = function( trace ) {
347         this.setAttr( "threadTrace", trace );
348 }
349
350 /** Increments the thread trace by 1 */
351 oilsMessage.prototype.updateThreadTrace = function() {
352         var tt = this.getThreadTrace();
353         return this.setThreadTrace( ++tt );
354 }
355
356
357
358
359
360 // -----------------------------------------------------------------------------
361 // oilsResponse
362
363
364 // -----------------------------------------------------------------------------
365 // Response codes
366 // -----------------------------------------------------------------------------
367
368 /**
369   * Below are the various response statuses
370   */
371 oilsResponse.STATUS_CONTINUE                                            = 100
372
373 oilsResponse.STATUS_OK                                                          = 200
374 oilsResponse.STATUS_ACCEPTED                                            = 202
375 oilsResponse.STATUS_COMPLETE                                            = 205
376
377 oilsResponse.STATUS_REDIRECTED                                  = 307
378
379 oilsResponse.STATUS_BADREQUEST                                  = 400
380 oilsResponse.STATUS_UNAUTHORIZED                                        = 401
381 oilsResponse.STATUS_FORBIDDEN                                           = 403
382 oilsResponse.STATUS_NOTFOUND                                            = 404
383 oilsResponse.STATUS_NOTALLOWED                                  = 405
384 oilsResponse.STATUS_TIMEOUT                                             = 408
385 oilsResponse.STATUS_EXPFAILED                                           = 417
386 oilsResponse.STATUS_INTERNALSERVERERROR         = 500
387 oilsResponse.STATUS_NOTIMPLEMENTED                              = 501
388 oilsResponse.STATUS_VERSIONNOTSUPPORTED         = 505
389
390
391 oilsResponse.prototype = new DomainObject();
392 oilsResponse.prototype.constructor = oilsResponse;
393 oilsResponse.prototype.baseClass = DomainObject.prototype.constructor;
394
395 /** Constructor.  status is the text describing the message status and
396   * statusCode must correspond to one of the oilsResponse status code numbers
397   */
398 function oilsResponse( name, status, statusCode ) {
399         if( name && status && statusCode ) {
400                 this._initResponse( name, status, statusCode );
401         }
402 }
403
404 /** Initializes the reponse XML */
405 oilsResponse.prototype._initResponse = function( name, status, statusCode ) {
406
407         this._init_domainObject( name );
408         this.add( new domainObjectAttr( "status", status ));
409         this.add( new domainObjectAttr( "statusCode", statusCode ) );
410 }
411
412
413
414 /** Returns the status of the response */
415 oilsResponse.prototype.getStatus = function() {
416         return this.getAttr( "status" );
417 }
418
419 /** Returns the statusCode of the response */
420 oilsResponse.prototype.getStatusCode = function() {
421         return this.getAttr("statusCode");
422 }
423
424
425 oilsConnectStatus.prototype = new oilsResponse();
426 oilsConnectStatus.prototype.constructor = oilsConnectStatus;
427 oilsConnectStatus.prototype.baseClass = oilsResponse.prototype.constructor;
428
429 /** Constructor.  These give us info on our connection attempts **/
430 function oilsConnectStatus( status, statusCode ) {
431         this._initResponse( "oilsConnectStatus", status, statusCode );
432 }
433
434
435 oilsResult.prototype = new oilsResponse();
436 oilsResult.prototype.constructor = oilsResult;
437 oilsResult.prototype.baseClass = oilsResponse.prototype.constructor;
438
439
440 /** Constructor.  These usually carry REQUEST responses */
441 function oilsResult( status, statusCode ) {
442         if( status && statusCode ) {
443                 this._initResponse( "oilsResult", status, statusCode );
444         }
445 }
446
447 /** Returns the top level DomainObject within this result message.
448   * Note:  The object retuturned will be the actual object, not just a 
449   * generic DomainObject - e.g. oilsException.
450   */
451 oilsResult.prototype.getContent = function() {
452
453         var nodes = this.element.childNodes;
454         if( ! nodes || nodes.length < 1 ) {
455                 throw new oils_ex_dom("Content node for oilsResult is invalid\n" + this.toString() );
456         }
457         var x = 0;
458         var node = nodes.item(x);
459         while( node != null ) {
460
461                 if( node.nodeName == "oils:domainObject"                                        ||
462                                 node.nodeName == "oils:domainObjectCollection" ) {
463
464                         return DomainObject.newFromNode( node );
465                 }
466                 node = nodes.item(++x);
467         }
468
469         throw new oils_ex_dom("Content node for oilsResult is invalid\n" + this.toString() );
470 }
471
472
473 oilsException.prototype = new oilsResponse();
474 oilsException.prototype.constructor = oilsException;
475 oilsException.prototype.baseClass = oilsResponse.prototype.constructor;
476
477 /** Top level exception */
478 function oilsException( status, statusCode ) {
479         if( status && statusCode ) {
480                 this._initResponse( "oilsException", status, statusCode );
481         }
482 }
483
484
485 oilsMethodException.prototype = new oilsException();
486 oilsMethodException.prototype.constructor = oilsMethodException;
487 oilsMethodException.prototype.baseClass = oilsException.prototype.constructor;
488
489 /** Method exception */
490 function oilsMethodException( status, statusCode ) {
491         if( status && statusCode ) {
492                 this._initResponse( "oilsMethodException", status, statusCode );
493         }
494 }
495
496 oilsBrokenSession.prototype = new oilsException();
497 oilsBrokenSession.prototype.constructor = oilsBrokenSession;
498 oilsBrokenSession.prototype.baseClass = oilsException.prototype.constructor;
499
500 /** broken session exception */
501 function oilsBrokenSession( status, statusCode ) {
502         if( status && statusCode ) {
503                 this._initResponse( "oilsBrokenSession", status, statusCode );
504         }
505 }
506
507
508 // -----------------------------------------------------------------------------
509 // oilsScalar
510
511
512 oilsScalar.prototype = new DomainObject();
513 oilsScalar.prototype.constructor = oilsScalar;
514 oilsScalar.prototype.baseClass = DomainObject.prototype.constructor;
515
516 /** Contains a single JSON item as its text content */
517 function oilsScalar( value ) {
518         this._init_domainObject( "oilsScalar" );
519         this.element.appendChild( this.doc.createTextNode( value ) );
520 }
521
522 /** Returns the contained item as a Javascript object */
523 oilsScalar.prototype.getValue = function() {
524         return JSON2js( this.element.textContent );
525 }
526
527
528 // -----------------------------------------------------------------------------
529 // oilsPair
530
531
532 oilsPair.prototype = new DomainObject()
533 oilsPair.prototype.constructor = oilsPair;
534 oilsPair.prototype.baseClass = DomainObject.prototype.constructor;
535
536 function oilsPair( key, value ) {
537
538         this._init_domainObject( "oilsPair" );
539         this.element.appendChild( this.doc.createTextNode( value ) );
540         this.element.setAttribute( "key", key );
541 }
542
543 oilsPair.prototype.getKey = function() {
544         return this.element.getAttribute( "key" );
545 }
546
547 oilsPair.prototype.getValue = function() {
548         return this.element.textContent;
549 }
550
551
552
553 // -----------------------------------------------------------------------------
554 // oilsArray - is a domainObjectCollection that stores a list of domainObject's
555 // or domainObjectCollections.
556 // -----------------------------------------------------------------------------
557
558 oilsArray.prototype = new domainObjectCollection()
559 oilsArray.prototype.constructor = oilsArray;
560 oilsArray.prototype.baseClass = domainObjectCollection.prototype.constructor;
561
562 function oilsArray( obj_list ) {
563         this._initCollection( "oilsArray" );
564         if(obj_list) { this.initArray( obj_list ); }
565 }
566
567 // -----------------------------------------------------------------------------
568 // Adds the array of objects to the array, these should be actual objects, not
569 // DOM nodes/elements, etc.
570 // -----------------------------------------------------------------------------
571 oilsArray.prototype.initArray = function( obj_list ) {
572         if( obj_array != null && obj_array.length > 0 ) {
573                 for( var i= 0; i!= obj_array.length; i++ ) {
574                         this.add( obj_array[i] );
575                 }
576         }
577 }
578
579 // -----------------------------------------------------------------------------
580 // Returns an array of DomainObjects or domainObjectCollections.  The objects
581 // returned will already be 'cast' into the correct object. i.e. you will not
582 // receieve an array of DomainObjects, but instead an array of oilsScalar's or
583 // an array of oilsHashe's, etc.
584 // -----------------------------------------------------------------------------
585 oilsArray.prototype.getObjects = function() {
586
587         var obj_list = new Array();
588         var nodes = this.element.childNodes;
589         var i=0;
590         var node = nodes.item(i);
591
592         while( node != null ) {
593
594                 if( node.nodeName == "oils:domainObject"                                        ||
595                                 node.nodeName == "oils:domainObjectCollection" ) {
596                         obj_list[i++] = DomainObject.newFromNode( node );
597                 }
598                 node = nodes.item(i);
599         }
600
601         return obj_list;
602 }
603
604
605 // -----------------------------------------------------------------------------
606 // oilsHash - an oilsHash is an oilsArray except it only stores oilsPair's
607 // -----------------------------------------------------------------------------
608
609 oilsHash.prototype = new oilsArray();
610 oilsHash.prototype.constructor = oilsHash;
611 oilsHash.prototype.baseClass = oilsArray.prototype.constructor;
612
613 function oilsHash( pair_array ) {
614         this._initCollection("oilsHash");
615         if(pair_array) { this.initArray( pair_array ); }
616 }
617
618 // -----------------------------------------------------------------------------
619 // Returns the array of oilsPairs objects that this hash contains
620 // -----------------------------------------------------------------------------
621 oilsHash.prototype.getPairs = function() {
622         return this.getObjects();
623 }
624
625