#include "oils_constants.h"
#include "opensrf/osrf_app_session.h"
#include "opensrf/osrf_settings.h"
+#include "opensrf/osrf_application.h"
#ifdef __cplusplus
extern "C" {
jsonObject* oilsUtilsQuickReq( const char* service, const char* method,
const jsonObject* params );
+jsonObject* oilsUtilsQuickReqCtx( osrfMethodContext* ctx, const char* service,
+ const char* method, const jsonObject* params );
+
jsonObject* oilsUtilsStorageReq( const char* method, const jsonObject* params );
+jsonObject* oilsUtilsStorageReqCtx( osrfMethodContext* ctx, const char* method, const jsonObject* params );
jsonObject* oilsUtilsCStoreReq( const char* method, const jsonObject* params );
+jsonObject* oilsUtilsCStoreReqCtx( osrfMethodContext* ctx, const char* method, const jsonObject* params );
/**
* Searches the storage server for a user with the given username
* Caller is responsible for freeing the returned object
*/
-jsonObject* oilsUtilsFetchUserByUsername( const char* name );
+jsonObject* oilsUtilsFetchUserByUsername( osrfMethodContext* ctx, const char* name );
/**
*/
jsonObject* oilsUtilsFetchWorkstation( long id );
-jsonObject* oilsUtilsFetchUserByBarcode(const char* barcode);
+jsonObject* oilsUtilsFetchUserByBarcode(osrfMethodContext* ctx, const char* barcode);
jsonObject* oilsUtilsFetchWorkstationByName( const char* name );
* Creates actor.usr_activity entries
* @return The number of rows created. 0 or 1.
*/
-int oilsUtilsTrackUserActivity( long usr, const char* ewho, const char* ewhat, const char* ehow );
+int oilsUtilsTrackUserActivity( osrfMethodContext* ctx, long usr, const char* ewho, const char* ewhat, const char* ehow );
/**
* Returns the ID of the root org unit (parent_ou = NULL)
int user_id = -1;
jsonObject* resp = NULL; // free
- jsonObject* user_obj = oilsUtilsFetchUserByUsername(username); // free
+ jsonObject* user_obj = oilsUtilsFetchUserByUsername(ctx, username); // free
if (user_obj && user_obj->type != JSON_NULL)
user_id = oilsFMGetObjectId(user_obj);
int user_id = -1;
jsonObject* resp = NULL; // free
- jsonObject* user_obj = oilsUtilsFetchUserByBarcode(barcode); // free
+ jsonObject* user_obj = oilsUtilsFetchUserByBarcode(ctx, barcode); // free
if (user_obj && user_obj->type != JSON_NULL)
user_id = oilsFMGetObjectId(user_obj);
Returns the event that should be returned to the user.
Event must be freed
*/
-static oilsEvent* oilsAuthHandleLoginOK( jsonObject* userObj, const char* uname,
+static oilsEvent* oilsAuthHandleLoginOK( osrfMethodContext* ctx, jsonObject* userObj, const char* uname,
const char* type, int orgloc, const char* workstation ) {
oilsEvent* response = NULL;
if (workstation)
jsonObjectSetKey(params, "workstation", jsonNewObject(workstation));
- jsonObject* authEvt = oilsUtilsQuickReq(
+ jsonObject* authEvt = oilsUtilsQuickReqCtx(
+ ctx,
"open-ils.auth_internal",
"open-ils.auth_internal.session.create", params);
jsonObjectFree(params);
}
jsonObject* param = jsonNewNumberObject(user_id); // free
- userObj = oilsUtilsCStoreReq(
- "open-ils.cstore.direct.actor.user.retrieve", param);
+ userObj = oilsUtilsCStoreReqCtx(
+ ctx, "open-ils.cstore.direct.actor.user.retrieve", param);
jsonObjectFree(param);
char* freeable_uname = NULL;
jsonObjectSetKey(params, "login_type", jsonNewObject(type));
if (barcode) jsonObjectSetKey(params, "barcode", jsonNewObject(barcode));
- jsonObject* authEvt = oilsUtilsQuickReq( // freed after password test
+ jsonObject* authEvt = oilsUtilsQuickReqCtx( // freed after password test
+ ctx
"open-ils.auth_internal",
"open-ils.auth_internal.user.validate", params);
jsonObjectFree(params);
} else {
response = oilsAuthHandleLoginOK(
- userObj, uname, type, orgloc, workstation);
+ ctx, userObj, uname, type, orgloc, workstation);
}
oilsUtilsTrackUserActivity(
+ ctx,
oilsFMGetObjectId(userObj),
ewho, ewhat,
osrfAppSessionGetIngress()
// fetch the user object
jsonObject* idParam = jsonNewNumberStringObject(user_id);
- jsonObject* userObj = oilsUtilsCStoreReq(
- "open-ils.cstore.direct.actor.user.retrieve", idParam);
+ jsonObject* userObj = oilsUtilsCStoreReqCtx(
+ ctx, "open-ils.cstore.direct.actor.user.retrieve", idParam);
jsonObjectFree(idParam);
if (!userObj) {
// Confirm user exists, active=true, barred=false, deleted=false
params = jsonNewNumberStringObject(user_id);
- userObj = oilsUtilsCStoreReq(
- "open-ils.cstore.direct.actor.user.retrieve", params);
+ userObj = oilsUtilsCStoreReqCtx(
+ ctx, "open-ils.cstore.direct.actor.user.retrieve", params);
jsonObjectFree(params);
if (userObj && userObj->type != JSON_NULL) {
int card_ok = 0;
params = jsonParseFmt("{\"barcode\":\"%s\"}", barcode);
- jsonObject* card = oilsUtilsCStoreReq(
- "open-ils.cstore.direct.actor.card.search", params);
+ jsonObject* card = oilsUtilsCStoreReqCtx(
+ ctx, "open-ils.cstore.direct.actor.card.search", params);
jsonObjectFree(params);
if (card && card->type != JSON_NULL) {
return id;
}
-int oilsUtilsTrackUserActivity(long usr, const char* ewho, const char* ewhat, const char* ehow) {
+int oilsUtilsTrackUserActivity(osrfMethodContext* ctx, long usr, const char* ewho, const char* ewhat, const char* ehow) {
if (!usr && !(ewho || ewhat || ehow)) return 0;
int rowcount = 0;
}
/**
+ @brief Perform a remote procedure call, propagating session
+ locale and timezone
+ @param service The name of the service to invoke.
+ @param method The name of the method to call.
+ @param params The parameters to be passed to the method, if any.
+ @return A copy of whatever the method returns as a result, or a JSON_NULL if the method
+ doesn't return anything.
+
+ If the @a params parameter points to a JSON_ARRAY, pass each element of the array
+ as a separate parameter. If it points to any other kind of jsonObject, pass it as a
+ single parameter. If it is NULL, pass no parameters.
+
+ The calling code is responsible for freeing the returned object by calling jsonObjectFree().
+*/
+jsonObject* oilsUtilsQuickReqCtx( osrfMethodContext* ctx, const char* service,
+ const char* method, const jsonObject* params ) {
+ if(!(service && method && ctx)) return NULL;
+
+ osrfLogDebug(OSRF_LOG_MARK, "oilsUtilsQuickReqCtx(): %s - %s (%s)", service, method, ctx->session->session_tz );
+
+ // Open an application session with the service, and send the request
+ osrfAppSession* session = osrfAppSessionClientInit( service );
+ osrf_app_session_set_tz(session, ctx->session->session_tz);
+ int reqid = osrfAppSessionSendRequest( session, params, method, 1 );
+
+ // Get the response
+ osrfMessage* omsg = osrfAppSessionRequestRecv( session, reqid, 60 );
+ jsonObject* result = jsonObjectClone( osrfMessageGetResult(omsg) );
+
+ // Clean up
+ osrfMessageFree(omsg);
+ osrfAppSessionFree(session);
+ return result;
+}
+
+/**
@brief Call a method of the open-ils.storage service.
@param method Name of the method.
@param params Parameters to be passed to the method, if any.
return oilsUtilsQuickReq("open-ils.cstore", method, params);
}
+/**
+ @brief Call a method of the open-ils.cstore service, context aware.
+ @param ctx Method context object.
+ @param method Name of the method.
+ @param params Parameters to be passed to the method, if any.
+ @return A copy of whatever the method returns as a result, or a JSON_NULL if the method
+ doesn't return anything.
+
+ If the @a params parameter points to a JSON_ARRAY, pass each element of the array
+ as a separate parameter. If it points to any other kind of jsonObject, pass it as a
+ single parameter. If it is NULL, pass no parameters.
+
+ The calling code is responsible for freeing the returned object by calling jsonObjectFree().
+*/
+jsonObject* oilsUtilsCStoreReqCtx( osrfMethodContext* ctx, const char* method, const jsonObject* params ) {
+ return oilsUtilsQuickReqCtx(ctx, "open-ils.cstore", method, params);
+}
+
/**
The calling code is responsible for freeing the returned object by calling jsonObjectFree().
*/
-jsonObject* oilsUtilsFetchUserByUsername( const char* name ) {
+jsonObject* oilsUtilsFetchUserByUsername( osrfMethodContext* ctx, const char* name ) {
if(!name) return NULL;
jsonObject* params = jsonParseFmt("{\"usrname\":\"%s\"}", name);
- jsonObject* user = oilsUtilsQuickReq(
- "open-ils.cstore", "open-ils.cstore.direct.actor.user.search", params );
+ jsonObject* user = oilsUtilsQuickReqCtx(
+ ctx, "open-ils.cstore", "open-ils.cstore.direct.actor.user.search", params );
jsonObjectFree(params);
long id = oilsFMGetObjectId(user);
The calling code is responsible for freeing the returned object by calling jsonObjectFree().
*/
-jsonObject* oilsUtilsFetchUserByBarcode(const char* barcode) {
+jsonObject* oilsUtilsFetchUserByBarcode(osrfMethodContext* ctx, const char* barcode) {
if(!barcode) return NULL;
osrfLogInfo(OSRF_LOG_MARK, "Fetching user by barcode %s", barcode);
jsonObject* params = jsonParseFmt("{\"barcode\":\"%s\"}", barcode);
- jsonObject* card = oilsUtilsQuickReq(
- "open-ils.cstore", "open-ils.cstore.direct.actor.card.search", params );
+ jsonObject* card = oilsUtilsQuickReqCtx(
+ ctx, "open-ils.cstore", "open-ils.cstore.direct.actor.card.search", params );
jsonObjectFree(params);
if(!card)
// Look up the user in actor.usr
params = jsonParseFmt("[%f]", iusr);
- jsonObject* user = oilsUtilsQuickReq(
- "open-ils.cstore", "open-ils.cstore.direct.actor.user.retrieve", params);
+ jsonObject* user = oilsUtilsQuickReqCtx(
+ ctx, "open-ils.cstore", "open-ils.cstore.direct.actor.user.retrieve", params);
jsonObjectFree(params);
return user;
char* script = NULL;
char* authtoken = NULL;
+static char* tz = NULL;
static int do_request( char* request );
static char* format_response( const jsonObject* o );
printf("Connected to OpenSRF network...\n");
+ tz = getenv("TZ");
+
if( username && password &&
( authtoken = oilsUtilsLogin(username, password, "staff", -1 )) ) {
printf("Login Session: %s\n", authtoken);
}
osrfAppSession* session = osrfAppSessionClientInit(service);
+ if (tz) osrf_app_session_set_tz(session,tz);
+
int req_id = osrfAppSessionSendRequest( session, params, method, 1 );
osrfMessage* omsg;