]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/javascript/backend/circ/circ_lib.js
added some logging and made the logging a little more clear
[Evergreen.git] / Open-ILS / src / javascript / backend / circ / circ_lib.js
1 load_lib('catalog/record_type.js');
2 load_lib('circ/circ_groups.js');
3
4
5 try {
6         if( environment.copy ) {
7                 environment.copy.fetchBestHold = function() {
8                         var key = scratchKey();
9                         environment.copy.__OILS_FUNC_fetch_best_hold(scratchPad(key));
10                         var val = getScratch(key);
11                         return (val) ? val : null;
12                 }
13         }
14 } catch(e) {}
15
16
17 /* ----------------------------------------------------------------------------- 
18         Collect all of the global variables
19         ----------------------------------------------------------------------------- */
20
21 /* the global result object.  Any data returned to the 
22         caller must be put into this object.  */
23 var result                              = environment.result = {};
24 result.event                    = 'SUCCESS';
25 result.events                   = [];
26 result.fatalEvents      = [];
27 result.infoEvents               = [];
28
29
30 /* Pull in any variables passed in from the calling process */
31 var copy                                        = environment.copy;
32 var volume                              = environment.volume;
33 var title                               = environment.title;
34 var recDescriptor               = environment.titleDescriptor;
35 var patron                              = environment.patron;
36 var patronItemsOut      = environment.patronItemsOut;
37 var patronOverdueCount  = environment.patronOverdueCount;
38 var patronFines         = environment.patronFines;
39 var isRenewal                   = environment.isRenewal;
40 var isPrecat                    = environment.isPrecat;
41 var currentLocation     = environment.location;
42 var holdRequestLib      = environment.requestLib;
43
44
45
46 /* create some new vars based on the data we have wherever possible */
47 var patronProfile;
48 var copyStatus;
49 var marcXMLDoc;
50
51 if( patron && patron.profile ) 
52         patronProfile = patron.profile.name;
53 if( copy && copy.status ) 
54         copyStatus = copy.status.name;
55 if( title && title.marc )
56         marcXMLDoc = new XML(title.marc);
57
58
59
60 /* copy the group tree into some other useful data structures */
61 var groupTree           = environment.groupTree;
62 var groupList           = {};
63 var groupIDList = {};
64 flattenGroupTree(groupTree);
65
66
67
68
69
70
71 /* ----------------------------------------------------------------------------- 
72         Define all of the utility functions
73         ----------------------------------------------------------------------------- */
74
75
76
77 /* useful functions for creating wrappers around system functions */
78 var __scratchKey = 0;
79 var __SCRATCH = {};
80 function scratchKey()           { return '_' + __scratchKey++; };
81 function scratchPad(key)        { return '__SCRATCH.'+ key; }
82 function getScratch(key)        { return __SCRATCH[ key ]; }
83 function scratchClear()         { for( var o in __SCRATCH ) __SCRATCH[o] = null; }
84
85
86 /* note: returns false if the value is 'f' or 'F' ... */
87 function isTrue(d) {
88         if(     d && 
89                         d != "0" && 
90                         d != "f" &&
91                         d != "F" )
92                         return true;
93         return false;
94 }
95
96 /* Utility function for iterating over array */
97 function iterate( arr, callback ) {
98         for( var i = 0; i < arr.length; i++ ) 
99                 callback(arr[i]);
100 }
101
102
103 /**
104   * returns a list of items that when passed to the callback 
105   * 'func' returned true returns null if none were found
106   */
107 function grep( arr, func ) {
108         var results = [];
109         iterate( arr, 
110                 function(d) {
111                         if( func(d) ) 
112                                 results.push(d);
113                 }
114         );
115         if(results.length > 0)  
116                 return results;
117         return null;
118 }
119
120
121
122 function flattenGroupTree(node) {
123         if(!node) return null;
124         groupList[node.name] = node;
125         groupIDList[node.id] = node;
126         iterate( node.children,
127                 function(n) {
128                         flattenGroupTree(n);
129                 }
130         );
131 }
132
133
134
135 /**
136   * Returns true if 'child' is equal or descends from 'parent'
137   * @param parent The name of the parent group
138   * @param child The name of the child group
139   */
140 function isGroupDescendant( parent, child ) {
141         log_debug("checking descendant p="+parent + " c=" + child);
142         return __isGroupDescendant(
143                 groupList[parent],
144                 groupList[child]);
145 }
146
147
148
149 /**
150   * Returns true if 'child' is equal or descends from 'parent'
151   * @param parent The node of the parent group
152   * @param child The node of the child group
153   */
154 function __isGroupDescendant( parent, child ) {
155         if (parent.id == child.id) return true;
156         var node = child;
157         while( (node = groupIDList[node.parent]) ) {
158                 if( node.id == parent.id ) 
159                         return true;
160         }
161         return false;
162 }
163
164
165 function getMARCItemType() {
166
167         if(     copy &&
168                         copy.circ_as_type &&
169                         copy.circ_as_type != 'undef' )
170                 return copy.circ_as_type;
171         
172         return (marcXMLDoc) ? extractFixedField(marcXMLDoc, 'Type') : "";
173 }
174
175
176 function isOrgDescendent( parentName, childId ) {
177         var key = scratchKey();
178         __OILS_FUNC_isOrgDescendent(scratchPad(key), parentName, childId);
179         var val = getScratch(key);
180         if( val == '1' ) return true;
181         return false;
182 }
183
184 function hasCommonAncestor( org1, org2, depth ) {
185         var key = scratchKey();
186         __OILS_FUNC_hasCommonAncestor(scratchPad(key), org1, org2, depth);
187         var val = getScratch(key);
188         if( val == '1' ) return true;
189         return false;
190 }
191
192
193
194 /* useful for testing */
195 function die(msg) {
196         log_error("die(): "+msg);
197         log_stderr("die(): "+msg);
198         var foo = null;
199         var baz = foo.bar;
200 }
201
202
203
204 /*
205 - at some point we should add a library of objects that map 
206 codes to names (item_form, item_type, etc.)
207 load_lib('item_form_map.js');
208 var form_name = item_form_map[env.record_descriptor.item_form];
209 */
210
211
212
213 /* logs a load of info */
214 function log_vars( prefix ) {
215         var str = prefix + ' : ';
216
217         if(patron) {
218                 str += ' Patron=' + patron.id;
219                 str += ', Patron_Username='+ patron.usrname;
220                 str += ', Patron_Profile Group='+ patronProfile;
221                 str += ', Patron_Fines='        + patronFines;
222                 str += ', Patron_OverdueCount=' + patronOverdueCount;
223                 str += ', Patron_Items Out=' + patronItemsOut;
224
225                 try {
226                         str += ', Patron_Barcode='      + patron.card.barcode;
227                         str += ', Patron_Library='      + patron.home_ou.name;
228                 } catch(e) {}
229         }
230
231         if(copy)        {
232                 str += ', Copy=' + copy.id;
233                 str += ', Copy_Barcode=' + copy.barcode;
234                 str += ', Copy_status=' + copyStatus;
235       str += (copy.circ_modifier) ? ', Circ_Mod=' + copy.circ_modifier : '';
236
237                 try {
238                         str += ', Circ_Lib=' + copy.circ_lib.shortname;
239                         str += ', Copy_location=' + copy.location.name;
240                 } catch(e) {}
241         }
242
243         if(volume)                      str += ', Volume='      + volume.id;
244         if(title)                       str += ', Record='      + title.id;
245
246         if(recDescriptor)       {
247                 str += ', Record_Descriptor=' + recDescriptor.id;
248                 str += ', Item_Type='                   + recDescriptor.item_type;
249                 str += ', Item_Form='                   + recDescriptor.item_form;
250                 str += ', Item_Lang='                   + recDescriptor.item_lang;
251                 str += ', Item_Audience='               + recDescriptor.audience;
252         }
253
254         str += ', Is_Renewal: ' + ( (isTrue(isRenewal)) ? "yes" : "no" );
255         str += ', Is_Precat: '  + ( (isTrue(isPrecat)) ? "yes" : "no" );
256         str += (holdRequestLib) ? ', Hold_request_lib=' + holdRequestLib.shortname : '';
257
258         log_info(str);
259 }
260
261