]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/c-apps/oils_fetch.c
changed hold creation to make request_lib the ws_ou of the requestor - also added...
[Evergreen.git] / Open-ILS / src / c-apps / oils_fetch.c
1 #include "opensrf/osrf_app_session.h"
2 #include "opensrf/osrf_application.h"
3 #include "opensrf/osrf_settings.h"
4 #include "objson/object.h"
5 #include "opensrf/log.h"
6 #include "oils_utils.h"
7 #include "oils_constants.h"
8 #include "oils_event.h"
9 #include <dbi/dbi.h>
10 #include <openils/fieldmapper_lookup.h>
11
12 #define OILS_AUTH_CACHE_PRFX "oils_fetch_"
13
14 #define MODULENAME "open-ils.fetch"
15 dbi_conn dbhandle; /* our db connection */
16
17 /* handy NULL json object to have around */
18 static jsonObject* oilsFetchNULL = NULL;
19
20 int osrfAppChildInit();
21
22 /* turns a singal db result row into a jsonObject */
23 jsonObject* oilsFetchMakeJSON( dbi_result result, char* hint );
24
25 osrfHash* fmClassMap = NULL;
26
27
28 int osrfAppInitialize() {
29         osrfLogInfo(OSRF_LOG_MARK, "Initializing Fetch Server...");
30
31         oilsFetchNULL = jsonNewObject(NULL);
32         fmClassMap = osrfNewHash();
33
34         int i;
35         char* hint;
36         char* apiname;
37
38         osrfList* keys = fm_classes();
39         if(!keys) return 0;
40
41         /* cycle through all of the classes and register a 
42          * retrieve method for each */
43         for( i = 0; i < keys->size; i++ ) {
44
45                 hint = OSRF_LIST_GET_INDEX(keys, i);    
46                 i++;
47                 apiname = OSRF_LIST_GET_INDEX(keys, i); 
48                 if(!(hint && apiname)) break;
49
50                 osrfHashSet( fmClassMap, hint, apiname );
51
52                 char method[256];
53                 bzero(method, 256);
54                 snprintf(method, 256, "open-ils.fetch.%s.retrieve", apiname);
55
56                 osrfAppRegisterMethod( MODULENAME, 
57                                 method, "oilsFetchDoRetrieve", "", 1, 0 );
58         }
59
60         return 0;
61 }
62
63
64 /**
65  * Connects to the database 
66  */
67 int osrfAppChildInit() {
68
69         dbi_initialize(NULL);
70
71         char* driver = osrf_settings_host_value("/apps/%s/app_settings/databases/driver", MODULENAME);
72         char* user       = osrf_settings_host_value("/apps/%s/app_settings/databases/database/user", MODULENAME);
73         char* host       = osrf_settings_host_value("/apps/%s/app_settings/databases/database/host", MODULENAME);
74         char* port       = osrf_settings_host_value("/apps/%s/app_settings/databases/database/port", MODULENAME);
75         char* db                 = osrf_settings_host_value("/apps/%s/app_settings/databases/database/db", MODULENAME);
76         char* pw                 = osrf_settings_host_value("/apps/%s/app_settings/databases/database/pw", MODULENAME);
77
78         dbhandle = dbi_conn_new(driver);
79
80         if(!dbhandle) {
81                 osrfLogError(OSRF_LOG_MARK, "Error creating database driver %s", driver);
82                 return -1;
83         }
84
85         osrfLogInfo(OSRF_LOG_MARK, "oils_fetch connecting to database.  host=%s, "
86                 "port=%s, user=%s, pw=%s, db=%s", host, port, user, pw, db );
87
88         if(host) dbi_conn_set_option(dbhandle, "host", host );
89         if(port) dbi_conn_set_option_numeric( dbhandle, "port", atoi(port) );
90         if(user) dbi_conn_set_option(dbhandle, "username", user);
91         if(pw) dbi_conn_set_option(dbhandle, "password", pw );
92         if(db) dbi_conn_set_option(dbhandle, "dbname", db );
93
94         free(user);
95         free(host);
96         free(port);
97         free(db);
98         free(pw);
99
100         if (dbi_conn_connect(dbhandle) < 0) {
101                 const char* err;
102                 dbi_conn_error(dbhandle, &err);
103                 osrfLogError( OSRF_LOG_MARK, "Error connecting to database: %s", err);
104                 return -1;
105         }
106
107         osrfLogInfo(OSRF_LOG_MARK, "%s successfully connected to the database", MODULENAME);
108
109         return 0;
110 }
111
112
113
114 int oilsFetchDoRetrieve( osrfMethodContext* ctx ) {
115
116         OSRF_METHOD_VERIFY_CONTEXT(ctx); 
117
118         char* id                = jsonObjectToSimpleString(jsonObjectGetIndex(ctx->params, 0));
119         char* meth      = strdup(ctx->method->name);
120         char* strtk;
121
122         strtok_r(meth, ".", &strtk); /* open-ils */
123         strtok_r(NULL, ".", &strtk); /* fetch */
124         char* schema    = strtok_r(NULL, ".", &strtk); 
125         char* object    = strtok_r(NULL, ".", &strtk);
126
127         osrfLogDebug(OSRF_LOG_MARK, "%s retrieving %s.%s "
128                         "object with id %s", MODULENAME, schema, object, id );
129
130         /* construct the SQL */
131         char sql[256];
132         bzero(sql, 256);
133         snprintf( sql, 255, "select * from %s.%s where id = %s;", schema, object, id );
134
135         /* find the object hint from the api name */
136         char hintbuf[256];
137         bzero(hintbuf,256);
138         snprintf(hintbuf, 255, "%s.%s", schema, object );
139         char* hint = osrfHashGet( fmClassMap, hintbuf );
140
141         osrfLogDebug(OSRF_LOG_MARK, "%s SQL =  %s", MODULENAME, sql);
142
143         dbi_result result = dbi_conn_queryf(dbhandle, sql);
144
145         if(result) {
146
147                 /* there should be one row at the most  */
148                 dbi_result_next_row(result); 
149
150                 /* JSONify the result */
151                 jsonObject* obj = oilsFetchMakeJSON( result, hint );
152
153                 /* clean up the query */
154                 dbi_result_free(result); 
155
156                 osrfAppRespondComplete( ctx, obj ); 
157                 jsonObjectFree(obj);
158
159         } else {
160
161                 osrfLogDebug(OSRF_LOG_MARK, "%s returned no results for query %s", MODULENAME, sql);
162                 osrfAppRespondComplete( ctx, oilsFetchNULL );
163         }
164
165         free(id);
166         free(meth);
167         return 0;
168 }
169
170
171 jsonObject* oilsFetchMakeJSON( dbi_result result, char* hint ) {
172         if(!(result && hint)) return NULL;
173
174         jsonObject* object = jsonParseString("[]");
175         jsonObjectSetClass(object, hint);
176
177         int attr;  
178         int fmIndex; 
179         int columnIndex = 1; 
180         unsigned short type;
181         const char* columnName; 
182
183         /* cycle through the column list */
184         while( (columnName = dbi_result_get_field_name(result, columnIndex++)) ) {
185
186                 /* determine the field type and storage attributes */
187                 type = dbi_result_get_field_type(result, columnName);
188                 attr = dbi_result_get_field_attribs(result, columnName);
189
190                 /* fetch the fieldmapper index */
191                 if( (fmIndex = fm_ntop(hint, (char*) columnName)) < 0 ) continue;
192
193                 switch( type ) {
194
195                         case DBI_TYPE_INTEGER :
196
197                                 if( attr & DBI_INTEGER_SIZE8 ) 
198                                         jsonObjectSetIndex( object, fmIndex, 
199                                                 jsonNewNumberObject(dbi_result_get_longlong(result, columnName)));
200                                 else 
201                                         jsonObjectSetIndex( object, fmIndex, 
202                                                 jsonNewNumberObject(dbi_result_get_long(result, columnName)));
203
204                                 break;
205
206                         case DBI_TYPE_DECIMAL :
207                                 jsonObjectSetIndex( object, fmIndex, 
208                                                 jsonNewNumberObject(dbi_result_get_double(result, columnName)));
209                                 break;
210
211                         case DBI_TYPE_STRING :
212                                 jsonObjectSetIndex( object, fmIndex, 
213                                         jsonNewObject(dbi_result_get_string(result, columnName)));
214                                 break;
215
216                         case DBI_TYPE_DATETIME :
217                                 jsonObjectSetIndex( object, fmIndex, 
218                                         jsonNewNumberObject(dbi_result_get_datetime(result, columnName)));
219                                 break;
220
221                         case DBI_TYPE_BINARY :
222                                 osrfLogError( OSRF_LOG_MARK, 
223                                         "Can't do binary at column %s : index %d", columnName, columnIndex - 1);
224                 }
225         }
226
227         return object;
228 }
229
230
231