]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/c-apps/oils_event.c
Patch from Scott McKellar:
[Evergreen.git] / Open-ILS / src / c-apps / oils_event.c
1 #include "openils/oils_event.h"
2 #include <libxml/parser.h>
3 #include <libxml/tree.h>
4 #include "opensrf/osrf_settings.h"
5
6 static void _oilsEventParseEvents();
7
8 // The following two osrfHashes are created when we
9 // create the first osrfEvent, and are never freed.
10
11 static osrfHash* _oilsEventEvents = NULL;
12 static osrfHash* _oilsEventDescriptions = NULL;
13
14 oilsEvent* oilsNewEvent( const char* file, int line, const char* event ) {
15         if(!event) return NULL;
16         osrfLogInfo(OSRF_LOG_MARK, "Creating new event: %s", event);
17         if(!_oilsEventEvents) _oilsEventParseEvents();
18         oilsEvent* evt = safe_malloc(sizeof(oilsEvent));
19         evt->event = strdup(event);
20         evt->perm = NULL;
21         evt->permloc = -1;
22         evt->payload = NULL;
23         evt->json = NULL;
24         if(file) evt->file = strdup(file);
25         else evt->file = NULL;
26         evt->line = line;
27         return evt;
28 }
29
30 oilsEvent* oilsNewEvent2( const char* file, int line, const char* event,
31                 const jsonObject* payload ) {
32         oilsEvent* evt = oilsNewEvent(file, line, event);
33         if(payload) evt->payload = jsonObjectClone(payload);
34         return evt;
35 }
36
37 oilsEvent* oilsNewEvent3( const char* file, int line, const char* event,
38                 const char* perm, int permloc ) {
39         oilsEvent* evt = oilsNewEvent(file, line, event);
40         if(perm) {
41                 evt->perm = strdup(perm);
42                 evt->permloc = permloc;
43         }
44         return evt;
45 }
46
47 oilsEvent* oilsNewEvent4( const char* file, int line, const char* event,
48                 const char* perm, int permloc, const jsonObject* payload ) {
49         oilsEvent* evt = oilsNewEvent3( file, line, event, perm, permloc );
50         if(payload) evt->payload = jsonObjectClone(payload);
51         return evt;
52 }
53
54 void oilsEventSetPermission( oilsEvent* event, const char* perm, int permloc ) {
55         if(!(event && perm)) return;
56         if(event->perm) free(event->perm);
57         event->perm = strdup(perm);
58         event->permloc = permloc;
59 }
60
61 void oilsEventSetPayload( oilsEvent* event, const jsonObject* payload ) {
62         if(!(event && payload)) return;
63         if(event->payload) jsonObjectFree(event->payload);
64         event->payload = jsonObjectClone(payload);
65 }
66
67
68 void oilsEventFree( oilsEvent* event ) {
69         if(!event) return;
70         free(event->event);
71         free(event->perm);
72         free(event->file);
73         if(event->json) jsonObjectFree(event->json);
74         if(event->payload) jsonObjectFree(event->payload);
75         free(event);
76 }
77
78
79 jsonObject* oilsEventToJSON( oilsEvent* event ) {
80         if(!event) return NULL;
81         char* code = osrfHashGet( _oilsEventEvents, event->event );
82
83         if(!code) {
84                 osrfLogError(OSRF_LOG_MARK,  "No such event name: %s", event->event );
85                 return NULL;
86         }
87
88
89         char* lang = "en-US"; /* assume this for now */
90         char* desc = NULL;
91         osrfHash* h = osrfHashGet(_oilsEventDescriptions, lang);
92         if(h) {
93                 osrfLogDebug(OSRF_LOG_MARK, "Loaded event lang hash for %s",lang);
94                 desc = osrfHashGet(h, code);
95                 osrfLogDebug(OSRF_LOG_MARK, "Found event description %s", desc);
96         }
97         if(!desc) desc = "";
98
99         jsonObject* json = jsonNewObject(NULL);
100         jsonObjectSetKey( json, "ilsevent", jsonNewNumberObject(atoi(code)) );
101         jsonObjectSetKey( json, "textcode", jsonNewObject(event->event) );
102         jsonObjectSetKey( json, "desc", jsonNewObject(desc) );
103         jsonObjectSetKey( json, "pid", jsonNewNumberObject(getpid()) );
104
105         char buf[256];
106         memset(buf, '\0', sizeof(buf));
107         snprintf(buf, sizeof(buf), "%s:%d", event->file, event->line);
108         jsonObjectSetKey( json, "stacktrace", jsonNewObject(buf) );
109
110         if(event->perm) jsonObjectSetKey( json, "ilsperm", jsonNewObject(event->perm) );
111         if(event->permloc != -1) jsonObjectSetKey( json, "ilspermloc", jsonNewNumberObject(event->permloc) );
112         if(event->payload) jsonObjectSetKey( json, "payload", event->payload );
113         
114         if(event->json) jsonObjectFree(event->json);
115         event->json = json;
116         return json;
117 }
118
119 /* Parses the events file */
120 static void _oilsEventParseEvents() {
121         
122         char* xml = osrf_settings_host_value("/ils_events");
123
124         if(!xml) {
125                 osrfLogError(OSRF_LOG_MARK, "Unable to find ILS Events file: %s", xml);
126                 return;
127         }
128
129         xmlDocPtr doc = xmlParseFile(xml);
130         free(xml);
131         int success = 0;
132         _oilsEventEvents = osrfNewHash();
133         _oilsEventDescriptions = osrfNewHash();
134
135         if( doc ) {
136                 xmlNodePtr root = xmlDocGetRootElement(doc);
137                 if( root ) {
138                         xmlNodePtr child = root->children;
139                         while( child ) {
140                                 if( !strcmp((char*) child->name, "event") ) {
141                                         xmlChar* code = xmlGetProp( child, BAD_CAST "code");
142                                         xmlChar* textcode = xmlGetProp( child, BAD_CAST "textcode");
143                                         if( code && textcode ) {
144                                                 osrfHashSet( _oilsEventEvents, code, (char*) textcode );
145                                                 success = 1;
146                                         }
147
148                                         /* here we collect all of the <desc> nodes on the event
149                                          * element and store them based on the xml:lang attribute
150                                          */
151                                         xmlNodePtr desc = child->children;
152                                         while(desc) {
153                                                 if( !strcmp((char*) desc->name, "desc") ) {
154                                                         xmlChar* lang = xmlGetProp( desc, BAD_CAST "lang");     
155                                                         if(lang) {
156                                                                 osrfLogDebug(OSRF_LOG_MARK, "Loaded event lang: %s", (char*) lang);
157                                                                 osrfHash* langHash = osrfHashGet(
158                                                                         _oilsEventDescriptions, (char*) lang);
159                                                                 if(!langHash) {
160                                                                         langHash = osrfNewHash();
161                                                                         osrfHashSet(_oilsEventDescriptions, langHash, (char*) lang);
162                                                                 }
163                                                                 char* content;
164                                                                 if( desc->children && (content = (char*) desc->children->content) ) {
165                                                                         osrfLogDebug(OSRF_LOG_MARK, "Loaded event desc: %s", content);
166                                                                         osrfHashSet( langHash, content, (char*) code );
167                                                                 }
168                                                         }
169                                                 }
170                                                 desc = desc->next;
171                                         }
172                                 }
173                                 child = child->next;
174                         }
175                 }
176         }
177
178         if(!success) osrfLogError(OSRF_LOG_MARK,  " ! Unable to parse events file: %s", xml );
179 }
180
181