1 #include <opensrf/osrf_app_session.h>
2 #include <opensrf/osrf_system.h>
3 #include <opensrf/osrfConfig.h>
4 #include <opensrf/utils.h>
5 #include <opensrf/osrf_hash.h>
10 #include "oils_utils.h"
12 #define CSTORE "open-ils.cstore"
13 #define APPNAME "oils_dataloader"
15 int sendCommand ( char* );
16 int startTransaction ( );
17 int endTransaction ( );
20 osrfHash* mnames = NULL;
21 osrfAppSession* session = NULL;
22 char* trans_id = NULL;
24 int main (int argc, char **argv) {
26 fprintf( stderr, "Usage: %s <path_to_config_file> <config_context> <create|update|delete>\n", argv[0] );
30 mnames = osrfNewHash();
32 char* config = strdup( argv[1] );
33 char* context = strdup( argv[2] );
34 char* method = strdup( argv[3] );
36 if (strcmp(method, "create") && strcmp(method, "update") && strcmp(method, "delete")) {
37 osrfLogError(OSRF_LOG_MARK, "Bad method name! Use create, update, or delete.");
41 // connect to the network
42 osrfLogInfo(OSRF_LOG_MARK, "Launching data loader with config %s and config context %s", config, context );
43 if (!osrfSystemBootstrapClientResc( config, context, APPNAME )) {
44 osrfLogError(OSRF_LOG_MARK, "Unable to bootstrap data loader!");
50 char* idl_filename = osrfConfigGetValue(NULL, "/IDL");
52 if (!(idl = oilsIDLInit( idl_filename ))) {
53 osrfLogError(OSRF_LOG_MARK, "Unable to load IDL!");
57 // Generate "create" method name for each
58 osrfStringArray* classes = osrfHashKeys(idl);
63 while ((classname = osrfStringArrayGetString(classes, c_index++))) {
64 osrfHash* idlClass = oilsIDLFindPath("/%s", classname);
66 char* _fm = strdup( (char*)osrfHashGet(idlClass, "fieldmapper") );
67 char* part = strtok_r(_fm, ":", &st_tmp);
69 growing_buffer* _method_name = buffer_init(64);
70 buffer_fadd(_method_name, "%s.direct.%s", CSTORE, part);
72 while ((part = strtok_r(NULL, ":", &st_tmp))) {
73 buffer_fadd(_method_name, ".%s", part);
75 buffer_fadd(_method_name, ".%s", method);
77 char* m = buffer_data(_method_name);
78 osrfHashSet( mnames, m, classname );
80 osrfLogDebug(OSRF_LOG_MARK, "Constructed %s method named %s for %s", method, m, classname);
82 buffer_free(_method_name);
89 // Connect to open-ils.cstore
90 session = osrf_app_client_session_init(CSTORE);
91 osrfAppSessionConnect(session);
93 // Start a transaction
94 if (!startTransaction()) {
95 osrfLogError(OSRF_LOG_MARK, "An error occured while attempting to start a transaction");
98 growing_buffer* json = buffer_init(128);
102 while ((c = (char)getchar())) {
107 json_string = buffer_data(json);
110 if (!sendCommand(json_string)) {
113 "An error occured while attempting to %s an object: [%s]",
126 buffer_add_char( json, c );
131 // clean up, commit, go away
132 if (!endTransaction()) {
133 osrfLogError(OSRF_LOG_MARK, "An error occured while attempting to complete a transaction");
136 osrfAppSessionFree(session);
141 int endTransaction () {
144 int req_id = osrfAppSessionMakeRequest( session, NULL, "open-ils.cstore.transaction.commit", 1, NULL );
145 osrf_message* res = osrfAppSessionRequestRecv( session, req_id, 5 );
146 if ( (data = jsonObjectClone(osrfMessageGetResult(res))) ) {
147 if(!(trans_id = jsonObjectGetString(data))) {
150 jsonObjectFree(data);
154 osrfMessageFree(res);
159 int startTransaction () {
162 int req_id = osrfAppSessionMakeRequest( session, NULL, "open-ils.cstore.transaction.begin", 1, NULL );
163 osrf_message* res = osrfAppSessionRequestRecv( session, req_id, 5 );
164 if ( (data = osrfMessageGetResult(res)) ) {
165 if(!(trans_id = jsonObjectToSimpleString(data))) {
171 osrfMessageFree(res);
176 int sendCommand ( char* json ) {
178 jsonObject* item = jsonParseString(json);
180 if (!item->classname) {
181 osrfLogError(OSRF_LOG_MARK, "Data loader cannot handle unclassed objects. Skipping [%s]!", json);
182 jsonObjectFree(item);
186 // Get the method name...
187 char* method_name = osrfHashGet( mnames, item->classname );
188 osrfLogDebug(OSRF_LOG_MARK, "Calling %s -> %s for %s", CSTORE, method_name, item->classname);
190 // make the param array
191 jsonObject* params = jsonParseString("[]");
192 jsonObjectSetIndex( params, 0, item );
193 jsonObjectSetIndex( params, 1, jsonParseString("{\"quiet\":\"true\"}") );
196 int req_id = osrfAppSessionMakeRequest( session, params, method_name, 1, NULL );
197 jsonObjectFree(params);
199 osrf_message* res = osrfAppSessionRequestRecv( session, req_id, 5 );
202 if ( !(data = osrfMessageGetResult(res)) ) {
205 osrfMessageFree(res);