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