]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/c-apps/oils_idl-core.c
Add OILS_SIP_MSG_BILL_ERR for when an error occurs getting bills.
[working/Evergreen.git] / Open-ILS / src / c-apps / oils_idl-core.c
1 #include "openils/oils_idl.h"
2 /*
3  * vim:noet:ts=4:
4  */
5
6 #include <stdlib.h>
7 #include <string.h>
8 #include <libxml/globals.h>
9 #include <libxml/xmlerror.h>
10 #include <libxml/parser.h>
11 #include <libxml/tree.h>
12 #include <libxml/debugXML.h>
13 #include <libxml/xmlmemory.h>
14
15 #define PERSIST_NS "http://open-ils.org/spec/opensrf/IDL/persistence/v1"
16 #define OBJECT_NS "http://open-ils.org/spec/opensrf/IDL/objects/v1"
17 #define BASE_NS "http://opensrf.org/spec/IDL/base/v1"
18 #define REPORTER_NS "http://open-ils.org/spec/opensrf/IDL/reporter/v1"
19 #define PERM_NS "http://open-ils.org/spec/opensrf/IDL/permacrud/v1"
20
21 static xmlDocPtr idlDoc = NULL; // parse and store the IDL here
22
23 /* parse and store the IDL here */
24 static osrfHash* idlHash;
25
26 static void add_std_fld( osrfHash* fields_hash, const char* field_name, unsigned pos );
27 osrfHash* oilsIDL(void) { return idlHash; }
28 osrfHash* oilsIDLInit( const char* idl_filename ) {
29
30         if (idlHash) return idlHash;
31
32         char* prop_str = NULL;
33
34         idlHash = osrfNewHash();
35         osrfHash* class_def_hash = NULL;
36
37         osrfLogInfo(OSRF_LOG_MARK, "Parsing the IDL XML...");
38         idlDoc = xmlReadFile( idl_filename, NULL, XML_PARSE_XINCLUDE );
39         
40         if (!idlDoc) {
41                 osrfLogError(OSRF_LOG_MARK, "Could not load or parse the IDL XML file!");
42                 return NULL;
43         }
44
45         osrfLogDebug(OSRF_LOG_MARK, "Initializing the Fieldmapper IDL...");
46
47         xmlNodePtr docRoot = xmlDocGetRootElement(idlDoc);
48         xmlNodePtr kid = docRoot->children;
49         while (kid) {
50                 if (!strcmp( (char*)kid->name, "class" )) {
51
52                         class_def_hash = osrfNewHash();
53                         char* current_class_name = (char*) xmlGetProp(kid, BAD_CAST "id");
54                         
55                         osrfHashSet( class_def_hash, current_class_name, "classname" );
56                         osrfHashSet( class_def_hash, xmlGetNsProp(kid, BAD_CAST "fieldmapper", BAD_CAST OBJECT_NS), "fieldmapper" );
57                         osrfHashSet( class_def_hash, xmlGetNsProp(kid, BAD_CAST "readonly", BAD_CAST PERSIST_NS), "readonly" );
58
59                         osrfHashSet( idlHash, class_def_hash, current_class_name );
60
61                         if ((prop_str = (char*)xmlGetNsProp(kid, BAD_CAST "tablename", BAD_CAST PERSIST_NS))) {
62                                 osrfLogDebug(OSRF_LOG_MARK, "Using table '%s' for class %s", prop_str, current_class_name );
63                                 osrfHashSet(
64                                         class_def_hash,
65                                         prop_str,
66                                         "tablename"
67                                 );
68                         }
69
70                         if ((prop_str = (char*)xmlGetNsProp(kid, BAD_CAST "restrict_primary", BAD_CAST PERSIST_NS))) {
71                                 osrfLogDebug(OSRF_LOG_MARK, "Delete restriction policy set at '%s' for pkey of class %s", prop_str, current_class_name );
72                                 osrfHashSet(
73                                         class_def_hash,
74                                         prop_str,
75                                         "restrict_primary"
76                                 );
77                         }
78
79                         if ((prop_str = (char*)xmlGetNsProp(kid, BAD_CAST "virtual", BAD_CAST PERSIST_NS))) {
80                                 osrfHashSet(
81                                         class_def_hash,
82                                         prop_str,
83                                         "virtual"
84                                 );
85                         }
86
87                         // Tokenize controller attribute into an osrfStringArray
88                         prop_str = (char*) xmlGetProp(kid, BAD_CAST "controller");
89                         if( prop_str )
90                                 osrfLogDebug(OSRF_LOG_MARK, "Controller list is %s", prop_str );
91                         osrfStringArray* controller = osrfStringArrayTokenize( prop_str, ' ' );
92                         xmlFree( prop_str );
93                         osrfHashSet( class_def_hash, controller, "controller");
94
95                         osrfHash* current_links_hash = osrfNewHash();
96                         osrfHash* current_fields_hash = osrfNewHash();
97
98                         osrfHashSet( class_def_hash, current_fields_hash, "fields" );
99                         osrfHashSet( class_def_hash, current_links_hash, "links" );
100
101                         xmlNodePtr _cur = kid->children;
102
103                         while (_cur) {
104
105                                 if (!strcmp( (char*)_cur->name, "fields" )) {
106
107                                         if( (prop_str = (char*)xmlGetNsProp(_cur, BAD_CAST "primary", BAD_CAST PERSIST_NS)) ) {
108                                                 osrfHashSet(
109                                                         class_def_hash,
110                                                         prop_str,
111                                                         "primarykey"
112                                                 );
113                                         }
114
115                                         if( (prop_str = (char*)xmlGetNsProp(_cur, BAD_CAST "sequence", BAD_CAST PERSIST_NS)) ) {
116                                                 osrfHashSet(
117                                                         class_def_hash,
118                                                         prop_str,
119                                                         "sequence"
120                                                 );
121                                         }
122
123                                         unsigned int array_pos = 0;
124                                         char array_pos_buf[ 7 ];  // For up to 1,000,000 fields per class
125
126                                         xmlNodePtr _f = _cur->children;
127                                         while(_f) {
128                                                 if (strcmp( (char*)_f->name, "field" )) {
129                                                         _f = _f->next;
130                                                         continue;
131                                                 }
132
133                                                 // Get the field name.  If it's one of the three standard
134                                                 // fields that we always generate, ignore it.
135                                                 char* field_name = (char*)xmlGetProp(_f, BAD_CAST "name");
136                                                 if( field_name ) {
137                                                         osrfLogDebug(OSRF_LOG_MARK, 
138                                                                         "Found field %s for class %s", field_name, current_class_name );
139                                                         if(    !strcmp( field_name, "isnew" )
140                                                                 || !strcmp( field_name, "ischanged" )
141                                                                 || !strcmp( field_name, "isdeleted" ) ) {
142                                                                 free( field_name );
143                                                                 _f = _f->next;
144                                                                 continue;
145                                                         }
146                                                 } else {
147                                                         osrfLogDebug(OSRF_LOG_MARK,
148                                                                         "Found field with no name for class %s", current_class_name );
149                                                         _f = _f->next;
150                                                         continue;
151                                                 }
152  
153                                                 osrfHash* field_def_hash = osrfNewHash();
154
155                                                 // Insert array_position
156                                                 snprintf( array_pos_buf, sizeof( array_pos_buf ), "%u", array_pos++ );
157                                                 osrfHashSet( field_def_hash, strdup( array_pos_buf ), "array_position" );
158
159                                                 if( (prop_str = (char*)xmlGetNsProp(_f, BAD_CAST "i18n", BAD_CAST PERSIST_NS)) ) {
160                                                         osrfHashSet(
161                                                                 field_def_hash,
162                                                                 prop_str,
163                                                                 "i18n"
164                                                         );
165                                                 }
166
167                                                 if( (prop_str = (char*)xmlGetNsProp(_f, BAD_CAST "virtual", BAD_CAST PERSIST_NS)) ) {
168                                                         osrfHashSet(
169                                                                 field_def_hash,
170                                                                 prop_str,
171                                                                 "virtual"
172                                                         );
173                                                 } else {   // default to virtual
174                                                         osrfHashSet(
175                                                                 field_def_hash,
176                                                                 "false",
177                                                                 "virtual"
178                                                         );
179                                                 }
180
181                                                 if( (prop_str = (char*)xmlGetNsProp(_f, BAD_CAST "primitive", BAD_CAST PERSIST_NS)) ) {
182                                                         osrfHashSet(
183                                                                 field_def_hash,
184                                                                 prop_str,
185                                                                 "primitive"
186                                                         );
187                                                 }
188
189                                                 osrfHashSet( field_def_hash, field_name, "name" );
190                                                 osrfHashSet(
191                                                         current_fields_hash,
192                                                         field_def_hash,
193                                                         field_name
194                                                 );
195                                                 _f = _f->next;
196                                         }
197
198                                         // Create three standard, stereotyped virtual fields for every class
199                                         add_std_fld( current_fields_hash, "isnew",     array_pos++ );
200                                         add_std_fld( current_fields_hash, "ischanged", array_pos++ );
201                                         add_std_fld( current_fields_hash, "isdeleted", array_pos   );
202
203                                 }
204
205                                 if (!strcmp( (char*)_cur->name, "links" )) {
206                                         xmlNodePtr _l = _cur->children;
207
208                                         while(_l) {
209                                                 if (strcmp( (char*)_l->name, "link" )) {
210                                                         _l = _l->next;
211                                                         continue;
212                                                 }
213
214                                                 osrfHash* link_def_hash = osrfNewHash();
215
216                                                 if( (prop_str = (char*)xmlGetProp(_l, BAD_CAST "reltype")) ) {
217                                                         osrfHashSet(
218                                                                 link_def_hash,
219                                                                 prop_str,
220                                                                 "reltype"
221                                                         );
222                                                         osrfLogDebug(OSRF_LOG_MARK, "Adding link with reltype %s", prop_str );
223                                                 } else
224                                                         osrfLogDebug(OSRF_LOG_MARK, "Adding link with no reltype" );
225
226                                                 if( (prop_str = (char*)xmlGetProp(_l, BAD_CAST "key")) ) {
227                                                         osrfHashSet(
228                                                                 link_def_hash,
229                                                                 prop_str,
230                                                                 "key"
231                                                         );
232                                                         osrfLogDebug(OSRF_LOG_MARK, "Link fkey is %s", prop_str );
233                                                 } else
234                                                         osrfLogDebug(OSRF_LOG_MARK, "Link with no fkey" );
235
236                                                 if( (prop_str = (char*)xmlGetProp(_l, BAD_CAST "class")) ) {
237                                                         osrfHashSet(
238                                                                 link_def_hash,
239                                                                 prop_str,
240                                                                 "class"
241                                                         );
242                                                         osrfLogDebug(OSRF_LOG_MARK, "Link fclass is %s", prop_str );
243                                                 } else
244                                                         osrfLogDebug(OSRF_LOG_MARK, "Link with no fclass" );
245
246                                                 // Tokenize map attribute into an osrfStringArray
247                                                 prop_str = (char*) xmlGetProp(_l, BAD_CAST "map");
248                                                 if( prop_str )
249                                                         osrfLogDebug(OSRF_LOG_MARK, "Link mapping list is %s", prop_str );
250                                                 osrfStringArray* map = osrfStringArrayTokenize( prop_str, ' ' );
251                                                 osrfHashSet( link_def_hash, map, "map");
252                                                 xmlFree( prop_str );
253
254                                                 if( (prop_str = (char*)xmlGetProp(_l, BAD_CAST "field")) ) {
255                                                         osrfHashSet(
256                                                                 link_def_hash,
257                                                                 prop_str,
258                                                                 "field"
259                                                         );
260                                                         osrfLogDebug(OSRF_LOG_MARK, "Link fclass is %s", prop_str );
261                                                 } else
262                                                         osrfLogDebug(OSRF_LOG_MARK, "Link with no fclass" );
263
264                                                 osrfHashSet(
265                                                         current_links_hash,
266                                                         link_def_hash,
267                                                         prop_str
268                                                 );
269
270                                                 _l = _l->next;
271                                         }
272                                 }
273 /**** Structure of permacrud in memory ****
274
275 { create :
276     { permission : [ x, y, z ],
277       global_required : "true", -- anything else, or missing, is false
278       local_context : [ f1, f2 ],
279       foreign_context : { class1 : { fkey : local_class_key, field : class1_field, context : [ a, b, c ] }, ...}
280     },
281   retrieve : null, -- no perm check, or structure similar to the others
282   update : -- like create
283     ...
284   delete : -- like create
285     ...
286 }   
287
288 **** Structure of permacrud in memory ****/
289
290                                 if (!strcmp( (char*)_cur->name, "permacrud" )) {
291                                         osrfHash* pcrud = osrfNewHash();
292                                         osrfHashSet( class_def_hash, pcrud, "permacrud" );
293                                         xmlNodePtr _l = _cur->children;
294
295                                         while(_l) {
296                                                 if (strcmp( (char*)_l->name, "actions" )) {
297                                                         _l = _l->next;
298                                                         continue;
299                                                 }
300
301                                                 xmlNodePtr _a = _l->children;
302
303                                                 while(_a) {
304                                                         const char* action_name = (const char*) _a->name;
305                                                         if (
306                                                                 strcmp( action_name, "create" ) &&
307                                                                 strcmp( action_name, "retrieve" ) &&
308                                                                 strcmp( action_name, "update" ) &&
309                                                                 strcmp( action_name, "delete" )
310                                                         ) {
311                                                                 _a = _a->next;
312                                                                 continue;
313                                                         }
314
315                                                         osrfLogDebug(OSRF_LOG_MARK, "Found Permacrud action %s for class %s",
316                                                                 action_name, current_class_name );
317
318                                                         osrfHash* action_def_hash = osrfNewHash();
319                                                         osrfHashSet( pcrud, action_def_hash, action_name );
320
321                                                         // Tokenize permission attribute into an osrfStringArray
322                                                         prop_str = (char*) xmlGetProp(_a, BAD_CAST "permission");
323                                                         if( prop_str )
324                                                                 osrfLogDebug(OSRF_LOG_MARK,
325                                                                         "Permacrud permission list is %s", prop_str );
326                                                         osrfStringArray* map = osrfStringArrayTokenize( prop_str, ' ' );
327                                                         osrfHashSet( action_def_hash, map, "permission");
328                                                         xmlFree( prop_str );
329
330                                                 osrfHashSet( action_def_hash,
331                                                                 (char*)xmlGetNoNsProp(_a, BAD_CAST "global_required"), "global_required");
332
333                                                         // Tokenize context_field attribute into an osrfStringArray
334                                                         prop_str = (char*) xmlGetProp(_a, BAD_CAST "context_field");
335                                                         if( prop_str )
336                                                                 osrfLogDebug(OSRF_LOG_MARK,
337                                                                         "Permacrud context_field list is %s", prop_str );
338                                                         map = osrfStringArrayTokenize( prop_str, ' ' );
339                                                         osrfHashSet( action_def_hash, map, "local_context");
340                                                         xmlFree( prop_str );
341
342                                                         osrfHash* foreign_context = osrfNewHash();
343                                                         osrfHashSet( action_def_hash, foreign_context, "foreign_context");
344
345                                                         xmlNodePtr _f = _a->children;
346
347                                                         while(_f) {
348                                                                 if ( strcmp( (char*)_f->name, "context" ) ) {
349                                                                         _f = _f->next;
350                                                                         continue;
351                                                                 }
352
353                                                                 if( (prop_str = (char*)xmlGetNoNsProp(_f, BAD_CAST "link")) ) {
354                                                                         osrfLogDebug(OSRF_LOG_MARK,
355                                                                                 "Permacrud context link definition is %s", prop_str );
356
357                                                                         osrfHash* _tmp_fcontext = osrfNewHash();
358
359                                                                         // Store pointers to elements already stored
360                                                                         // from the <link> aggregate
361                                                                         osrfHash* _flink = osrfHashGet( current_links_hash, prop_str );
362                                                                         osrfHashSet( _tmp_fcontext, osrfHashGet(_flink, "field"), "fkey" );
363                                                                         osrfHashSet( _tmp_fcontext, osrfHashGet(_flink, "key"), "field" );
364                                                                         xmlFree( prop_str );
365
366                                                                     if( (prop_str = (char*)xmlGetNoNsProp(_f, BAD_CAST "jump")) )
367                                                                             osrfHashSet( _tmp_fcontext, osrfStringArrayTokenize( prop_str, '.' ), "jump" );
368                                                                         xmlFree( prop_str );
369
370                                                                         // Tokenize field attribute into an osrfStringArray
371                                                                         char * field_list = (char*) xmlGetProp(_f, BAD_CAST "field");
372                                                                         if( field_list )
373                                                                                 osrfLogDebug(OSRF_LOG_MARK,
374                                                                                         "Permacrud foreign context field list is %s", field_list );
375                                                                         map = osrfStringArrayTokenize( field_list, ' ' );
376                                                                         osrfHashSet( _tmp_fcontext, map, "context");
377                                                                         xmlFree( field_list );
378
379                                                                         // Insert the new hash into a hash attached to the parent node
380                                                                         osrfHashSet( foreign_context, _tmp_fcontext, osrfHashGet( _flink, "class" ) );
381
382                                                                 } else {
383
384                                                                         if( (prop_str = (char*)xmlGetNoNsProp(_f, BAD_CAST "field") )) {
385                                                                                 char* map_list = prop_str;
386                                                                                 osrfLogDebug(OSRF_LOG_MARK,
387                                                                                         "Permacrud local context field list is %s", prop_str );
388                         
389                                                                                 if (strlen( map_list ) > 0) {
390                                                                                         char* st_tmp = NULL;
391                                                                                         char* _map_class = strtok_r(map_list, " ", &st_tmp);
392                                                                                         osrfStringArrayAdd(
393                                                                                                 osrfHashGet( action_def_hash, "local_context"), _map_class);
394                                                                         
395                                                                                         while ((_map_class = strtok_r(NULL, " ", &st_tmp))) {
396                                                                                                 osrfStringArrayAdd(
397                                                                                                         osrfHashGet( action_def_hash, "local_context"), _map_class);
398                                                                                         }
399                                                                                 }
400                                                                                 xmlFree(map_list);
401                                                                         }
402
403                                                                 }
404                                                                 _f = _f->next;
405                                                         }
406                                                         _a = _a->next;
407                                                 }
408                                                 _l = _l->next;
409                                         }
410                                 }
411
412                                 if (!strcmp( (char*)_cur->name, "source_definition" )) {
413                                         char* content_str;
414                                         if( (content_str = (char*)xmlNodeGetContent(_cur)) ) {
415                                                 osrfLogDebug(OSRF_LOG_MARK, "Using source definition '%s' for class %s",
416                                                         content_str, current_class_name );
417                                                 osrfHashSet(
418                                                         class_def_hash,
419                                                         content_str,
420                                                         "source_definition"
421                                                 );
422                                         }
423
424                                 }
425
426                                 _cur = _cur->next;
427                         } // end while
428                 }
429
430                 kid = kid->next;
431         } // end while
432
433         osrfLogInfo(OSRF_LOG_MARK, "...IDL XML parsed");
434
435         return idlHash;
436 }
437
438 // Adds a standard virtual field to a fields hash
439 static void add_std_fld( osrfHash* fields_hash, const char* field_name, unsigned pos ) {
440         char array_pos_buf[ 7 ];
441         osrfHash* std_fld_hash = osrfNewHash();
442
443         snprintf( array_pos_buf, sizeof( array_pos_buf ), "%u", pos );
444         osrfHashSet( std_fld_hash, strdup( array_pos_buf ), "array_position" );
445         osrfHashSet( std_fld_hash, "true", "virtual" );
446         osrfHashSet( std_fld_hash, strdup( field_name ), "name" );
447         osrfHashSet( fields_hash, std_fld_hash, field_name );
448 }
449
450
451 osrfHash* oilsIDLFindPath( const char* path, ... ) {
452         if(!path || strlen(path) < 1) return NULL;
453
454         osrfHash* obj = idlHash;
455
456         VA_LIST_TO_STRING(path);
457         char* buf = VA_BUF;
458
459         char* token = NULL;
460         char* t = buf;
461         char* tt;
462
463         token = strtok_r(t, "/", &tt);
464         if(!token) return NULL;
465
466         do {
467                 obj = osrfHashGet(obj, token);
468         } while( (token = strtok_r(NULL, "/", &tt)) && obj);
469
470         return obj;
471 }
472
473 static osrfHash* findClassDef( const char* classname ) {
474         if( !classname || !idlHash )
475                 return NULL;
476         else
477                 return osrfHashGet( idlHash, classname );
478 }
479
480 osrfHash* oilsIDL_links( const char* classname ) {
481         osrfHash* classdef = findClassDef( classname );
482         if( classdef )
483                 return osrfHashGet( classdef, "links" );
484         else
485                 return NULL;
486 }
487
488 osrfHash* oilsIDL_fields( const char* classname ) {
489         osrfHash* classdef = findClassDef( classname );
490         if( classdef )
491                 return osrfHashGet( classdef, "fields" );
492         else
493                 return NULL;
494 }
495
496 int oilsIDL_classIsFieldmapper ( const char* classname ) {
497         if( findClassDef( classname ) )
498                 return 1;
499         else
500                 return 0;
501 }
502
503 // For a given class: return the array_position associated with a 
504 // specified field. (or -1 if it doesn't exist)
505 int oilsIDL_ntop (const char* classname, const char* fieldname) {
506         osrfHash* fields_hash = oilsIDL_fields( classname );
507         if( !fields_hash )
508                 return -1;     // No such class, or no fields for it
509
510         osrfHash* field_def_hash = osrfHashGet( fields_hash, fieldname );
511         if( !field_def_hash )
512                 return -1;                      // No such field
513
514         const char* pos_attr = osrfHashGet( field_def_hash, "array_position" );
515         if( !pos_attr )
516                 return -1;                      // No array_position attribute
517
518         return atoi( pos_attr );        // Return position as int
519 }
520
521 // For a given class: return a copy of the name of the field 
522 // at a specified array_position (or NULL if there is none)
523 char * oilsIDL_pton (const char* classname, int pos) {
524         osrfHash* fields_hash = oilsIDL_fields( classname );
525         if( !fields_hash )
526                 return NULL;     // No such class, or no fields for it
527
528         char* ret = NULL;
529         osrfHash* field_def_hash = NULL;
530         osrfHashIterator* iter = osrfNewHashIterator( fields_hash );
531
532         while ( ( field_def_hash = osrfHashIteratorNext( iter ) ) ) {
533                 if ( atoi( osrfHashGet( field_def_hash, "array_position" ) ) == pos ) {
534                         ret = strdup( osrfHashIteratorKey( iter ) );
535                         break;
536                 }
537         }
538
539         osrfHashIteratorFree( iter );
540
541         return ret;
542 }
543