use of org.apache.ofbiz.service.GenericServiceException in project ofbiz-framework by apache.
the class EntitySyncContext method runPullStartOrRestoreSavedResults.
// ======================== PULL Methods ========================
public void runPullStartOrRestoreSavedResults() throws SyncDataErrorException, SyncServiceErrorException, SyncAbortException {
// if EntitySync.statusId is ESR_RUNNING, make sure startDate matches EntitySync.lastHistoryStartDate; or return error
if (isEntitySyncRunning() && this.startDate == null) {
throw new SyncAbortException("Not running EntitySync [" + entitySyncId + "], an instance is already running and no startDate for the current run was passed.");
}
if (this.startDate == null) {
// get it started!
String markErrorMsg = "Could not start Entity Sync service, could not mark as running";
try {
// not running, get started NOW
// set running status on entity sync, run in its own tx
Map<String, Object> startEntitySyncRes = dispatcher.runSync("updateEntitySyncRunning", UtilMisc.toMap("entitySyncId", entitySyncId, "runStatusId", "ESR_RUNNING", "userLogin", userLogin));
if (ModelService.RESPOND_ERROR.equals(startEntitySyncRes.get(ModelService.RESPONSE_MESSAGE))) {
throw new SyncDataErrorException(markErrorMsg, null, null, startEntitySyncRes, null);
}
} catch (GenericServiceException e) {
throw new SyncServiceErrorException(markErrorMsg, e);
}
// finally create the initial history record
this.createInitialHistory();
this.setSplitStartTime();
} else {
try {
// set the latest values from the EntitySyncHistory, based on the values on the EntitySync
GenericValue entitySyncHistory = EntityQuery.use(delegator).from("EntitySyncHistory").where("entitySyncId", entitySyncId, "startDate", startDate).queryOne();
this.toCreateInserted = UtilMisc.toLong(entitySyncHistory.getLong("toCreateInserted"));
this.toCreateUpdated = UtilMisc.toLong(entitySyncHistory.getLong("toCreateUpdated"));
this.toCreateNotUpdated = UtilMisc.toLong(entitySyncHistory.getLong("toCreateNotUpdated"));
this.toStoreInserted = UtilMisc.toLong(entitySyncHistory.getLong("toStoreInserted"));
this.toStoreUpdated = UtilMisc.toLong(entitySyncHistory.getLong("toStoreUpdated"));
this.toStoreNotUpdated = UtilMisc.toLong(entitySyncHistory.getLong("toStoreNotUpdated"));
this.toRemoveDeleted = UtilMisc.toLong(entitySyncHistory.getLong("toRemoveDeleted"));
this.toRemoveAlreadyDeleted = UtilMisc.toLong(entitySyncHistory.getLong("toRemoveAlreadyDeleted"));
this.totalStoreCalls = UtilMisc.toLong(entitySyncHistory.getLong("totalStoreCalls"));
this.totalSplits = UtilMisc.toLong(entitySyncHistory.getLong("totalSplits"));
this.totalRowsToCreate = UtilMisc.toLong(entitySyncHistory.getLong("totalRowsToCreate"));
this.totalRowsToStore = UtilMisc.toLong(entitySyncHistory.getLong("totalRowsToStore"));
this.totalRowsToRemove = UtilMisc.toLong(entitySyncHistory.getLong("totalRowsToRemove"));
this.perSplitMinMillis = UtilMisc.toLong(entitySyncHistory.getLong("perSplitMinMillis"));
this.perSplitMaxMillis = UtilMisc.toLong(entitySyncHistory.getLong("perSplitMaxMillis"));
this.perSplitMinItems = UtilMisc.toLong(entitySyncHistory.getLong("perSplitMinItems"));
this.perSplitMaxItems = UtilMisc.toLong(entitySyncHistory.getLong("perSplitMaxItems"));
this.splitStartTime = UtilMisc.toLong(entitySyncHistory.getLong("lastSplitStartTime"));
} catch (GenericEntityException e) {
throw new SyncDataErrorException("Error getting existing EntitySyncHistory values", e);
}
// got the previous values, now add to them with the values from the context...
this.toCreateInserted += UtilMisc.toLong(this.context.get("toCreateInserted"));
this.toCreateUpdated += UtilMisc.toLong(this.context.get("toCreateUpdated"));
this.toCreateNotUpdated += UtilMisc.toLong(this.context.get("toCreateNotUpdated"));
this.toStoreInserted += UtilMisc.toLong(this.context.get("toStoreInserted"));
this.toStoreUpdated += UtilMisc.toLong(this.context.get("toStoreUpdated"));
this.toStoreNotUpdated += UtilMisc.toLong(this.context.get("toStoreNotUpdated"));
this.toRemoveDeleted += UtilMisc.toLong(this.context.get("toRemoveDeleted"));
this.toRemoveAlreadyDeleted += UtilMisc.toLong(this.context.get("toRemoveAlreadyDeleted"));
this.totalStoreCalls++;
this.saveResultsReportedFromDataStore();
}
}
use of org.apache.ofbiz.service.GenericServiceException in project ofbiz-framework by apache.
the class CallService method exec.
@Override
public boolean exec(MethodContext methodContext) throws MiniLangException {
if (methodContext.isTraceOn()) {
outputTraceMessage(methodContext, "Begin call-service.");
}
String serviceName = serviceNameFse.expandString(methodContext.getEnvMap());
String errorCode = this.errorCode;
if (errorCode.isEmpty()) {
errorCode = simpleMethod.getDefaultErrorCode();
}
String successCode = this.successCode;
if (successCode.isEmpty()) {
successCode = simpleMethod.getDefaultSuccessCode();
}
Map<String, Object> inMap = inMapFma.get(methodContext.getEnvMap());
if (inMap == null) {
inMap = new HashMap<String, Object>();
}
// before invoking the service, clear messages
if (methodContext.getMethodType() == MethodContext.EVENT) {
methodContext.removeEnv(simpleMethod.getEventErrorMessageName());
methodContext.removeEnv(simpleMethod.getEventEventMessageName());
methodContext.removeEnv(simpleMethod.getEventResponseCodeName());
} else {
methodContext.removeEnv(simpleMethod.getServiceErrorMessageName());
methodContext.removeEnv(simpleMethod.getServiceSuccessMessageName());
methodContext.removeEnv(simpleMethod.getServiceResponseMessageName());
}
// add UserLogin to context if expected
if (includeUserLogin) {
GenericValue userLogin = methodContext.getUserLogin();
if (userLogin != null && inMap.get("userLogin") == null) {
inMap.put("userLogin", userLogin);
}
}
// always add Locale to context unless null
Locale locale = methodContext.getLocale();
if (locale != null) {
inMap.put("locale", locale);
}
// invoke the service
Map<String, Object> result = null;
try {
ModelService modelService = methodContext.getDispatcher().getDispatchContext().getModelService(serviceName);
int timeout = modelService.transactionTimeout;
if (this.transactionTimeout >= 0) {
timeout = this.transactionTimeout;
}
if (methodContext.isTraceOn()) {
outputTraceMessage(methodContext, "Invoking service \"" + serviceName + "\", require-new-transaction = " + requireNewTransaction + ", transaction-timeout = " + timeout + ", IN attributes:", inMap.toString());
}
result = methodContext.getDispatcher().runSync(serviceName, inMap, timeout, requireNewTransaction);
} catch (GenericServiceException e) {
if (methodContext.isTraceOn()) {
outputTraceMessage(methodContext, "Service engine threw an exception: " + e.getMessage());
}
String errMsg = "ERROR: Could not complete the " + simpleMethod.getShortDescription() + " process [problem invoking the [" + serviceName + "] service with the map named [" + inMapFma + "] containing [" + inMap + "]: " + e.getMessage() + "]";
Debug.logError(e, errMsg, module);
if (breakOnError) {
if (methodContext.getMethodType() == MethodContext.EVENT) {
methodContext.putEnv(simpleMethod.getEventErrorMessageName(), errMsg);
methodContext.putEnv(simpleMethod.getEventResponseCodeName(), errorCode);
} else {
methodContext.putEnv(simpleMethod.getServiceErrorMessageName(), errMsg);
methodContext.putEnv(simpleMethod.getServiceResponseMessageName(), errorCode);
}
if (methodContext.isTraceOn()) {
outputTraceMessage(methodContext, "break-on-error set to \"true\", halting script execution. End call-service.");
}
return false;
} else {
if (methodContext.isTraceOn()) {
outputTraceMessage(methodContext, "End call-service.");
}
return true;
}
}
if (resultsToMapList != null) {
if (methodContext.isTraceOn()) {
outputTraceMessage(methodContext, "Processing " + resultsToMapList.size() + " <results-to-map> elements.");
}
for (String mapName : resultsToMapList) {
methodContext.putEnv(mapName, UtilMisc.makeMapWritable(result));
}
}
if (resultToFieldList != null) {
if (methodContext.isTraceOn()) {
outputTraceMessage(methodContext, "Processing " + resultToFieldList.size() + " <result-to-field> elements.");
}
for (ResultToField rtfDef : resultToFieldList) {
rtfDef.exec(methodContext, result);
}
}
if (resultToResultList != null) {
if (methodContext.isTraceOn()) {
outputTraceMessage(methodContext, "Processing " + resultToResultList.size() + " <result-to-result> elements.");
}
for (ResultToResult rtrDef : resultToResultList) {
rtrDef.exec(methodContext, result);
}
}
if (methodContext.getMethodType() == MethodContext.EVENT) {
if (resultToRequestList != null) {
if (methodContext.isTraceOn()) {
outputTraceMessage(methodContext, "Processing " + resultToRequestList.size() + " <result-to-request> elements.");
}
for (ResultToRequest rtrDef : resultToRequestList) {
rtrDef.exec(methodContext, result);
}
}
if (resultToSessionList != null) {
if (methodContext.isTraceOn()) {
outputTraceMessage(methodContext, "Processing " + resultToSessionList.size() + " <result-to-session> elements.");
}
for (ResultToSession rtsDef : resultToSessionList) {
rtsDef.exec(methodContext, result);
}
}
}
String errorPrefixStr = errorPrefix.getMessage(methodContext.getLoader(), methodContext);
String errorSuffixStr = errorSuffix.getMessage(methodContext.getLoader(), methodContext);
String successPrefixStr = successPrefix.getMessage(methodContext.getLoader(), methodContext);
String successSuffixStr = successSuffix.getMessage(methodContext.getLoader(), methodContext);
String messagePrefixStr = messagePrefix.getMessage(methodContext.getLoader(), methodContext);
String messageSuffixStr = messageSuffix.getMessage(methodContext.getLoader(), methodContext);
String errorMessage = null;
List<String> errorMessageList = null;
// See if there is a single message
if (result.containsKey(ModelService.ERROR_MESSAGE)) {
errorMessage = ServiceUtil.makeErrorMessage(result, messagePrefixStr, messageSuffixStr, errorPrefixStr, errorSuffixStr);
} else if (result.containsKey(ModelService.ERROR_MESSAGE_LIST)) {
errorMessageList = UtilGenerics.checkList(result.get(ModelService.ERROR_MESSAGE_LIST));
}
if ((UtilValidate.isNotEmpty(errorMessage) || UtilValidate.isNotEmpty(errorMessageList)) && breakOnError) {
if (methodContext.getMethodType() == MethodContext.EVENT) {
if (UtilValidate.isNotEmpty(errorMessage)) {
if (Debug.verboseOn()) {
errorMessage += UtilProperties.getMessage(resource, "simpleMethod.error_show_service_name", UtilMisc.toMap("serviceName", serviceName, "methodName", simpleMethod.getMethodName()), locale);
}
methodContext.putEnv(simpleMethod.getEventErrorMessageName(), errorMessage);
} else {
if (Debug.verboseOn()) {
errorMessageList.add(UtilProperties.getMessage(resource, "simpleMethod.error_show_service_name", UtilMisc.toMap("serviceName", serviceName, "methodName", simpleMethod.getMethodName()), locale));
}
methodContext.putEnv(simpleMethod.getEventErrorMessageListName(), errorMessageList);
}
} else {
ServiceUtil.addErrors(UtilMisc.<String, String>getListFromMap(methodContext.getEnvMap(), this.simpleMethod.getServiceErrorMessageListName()), UtilMisc.<String, String, Object>getMapFromMap(methodContext.getEnvMap(), this.simpleMethod.getServiceErrorMessageMapName()), result);
Debug.logError(new Exception(errorMessage), module);
}
}
String successMessage = ServiceUtil.makeSuccessMessage(result, messagePrefixStr, messageSuffixStr, successPrefixStr, successSuffixStr);
if (UtilValidate.isNotEmpty(successMessage)) {
if (methodContext.getMethodType() == MethodContext.EVENT) {
methodContext.putEnv(simpleMethod.getEventEventMessageName(), successMessage);
} else {
methodContext.putEnv(simpleMethod.getServiceSuccessMessageName(), successMessage);
}
}
String defaultMessageStr = defaultMessage.getMessage(methodContext.getLoader(), methodContext);
if (UtilValidate.isEmpty(errorMessage) && UtilValidate.isEmpty(errorMessageList) && UtilValidate.isEmpty(successMessage) && UtilValidate.isNotEmpty(defaultMessageStr)) {
if (methodContext.getMethodType() == MethodContext.EVENT) {
methodContext.putEnv(simpleMethod.getEventEventMessageName(), defaultMessageStr);
} else {
methodContext.putEnv(simpleMethod.getServiceSuccessMessageName(), defaultMessageStr);
}
}
String responseCode = result.containsKey(ModelService.RESPONSE_MESSAGE) ? (String) result.get(ModelService.RESPONSE_MESSAGE) : successCode;
if (errorCode.equals(responseCode)) {
if (methodContext.isTraceOn()) {
outputTraceMessage(methodContext, "Service returned an error.");
}
if (breakOnError) {
if (methodContext.getMethodType() == MethodContext.EVENT) {
methodContext.putEnv(simpleMethod.getEventResponseCodeName(), responseCode);
} else {
methodContext.putEnv(simpleMethod.getServiceResponseMessageName(), responseCode);
}
if (methodContext.isTraceOn()) {
outputTraceMessage(methodContext, "break-on-error set to \"true\", halting script execution. End call-service.");
}
return false;
} else {
if (methodContext.isTraceOn()) {
outputTraceMessage(methodContext, "End call-service.");
}
return true;
}
} else {
if (methodContext.isTraceOn()) {
outputTraceMessage(methodContext, "Service ran successfully. End call-service.");
}
if (methodContext.getMethodType() == MethodContext.EVENT) {
methodContext.putEnv(simpleMethod.getEventResponseCodeName(), responseCode);
} else {
methodContext.putEnv(simpleMethod.getServiceResponseMessageName(), responseCode);
}
return true;
}
}
use of org.apache.ofbiz.service.GenericServiceException in project ofbiz-framework by apache.
the class SetServiceFields method exec.
@Override
public boolean exec(MethodContext methodContext) throws MiniLangException {
Map<String, ? extends Object> fromMap = mapFma.get(methodContext.getEnvMap());
if (fromMap == null) {
if (Debug.verboseOn()) {
Debug.logVerbose("The from map in set-service-field was not found with name: " + mapFma, module);
}
return true;
}
String serviceName = serviceNameFse.expandString(methodContext.getEnvMap());
ModelService modelService = null;
try {
modelService = methodContext.getDispatcher().getDispatchContext().getModelService(serviceName);
} catch (GenericServiceException e) {
throw new MiniLangRuntimeException("Could not get service definition for service name \"" + serviceName + "\": " + e.getMessage(), this);
}
Map<String, Object> toMap = toMapFma.get(methodContext.getEnvMap());
if (toMap == null) {
toMap = new HashMap<String, Object>();
toMapFma.put(methodContext.getEnvMap(), toMap);
}
List<Object> errorMessages = new LinkedList<Object>();
Map<String, Object> validAttributes = modelService.makeValid(fromMap, mode, true, errorMessages, methodContext.getTimeZone(), methodContext.getLocale());
if (errorMessages.size() > 0) {
for (Object obj : errorMessages) {
simpleMethod.addErrorMessage(methodContext, (String) obj);
}
throw new MiniLangRuntimeException("Errors encountered while setting service attributes for service name \"" + serviceName + "\"", this);
}
toMap.putAll(validAttributes);
return true;
}
use of org.apache.ofbiz.service.GenericServiceException in project ofbiz-framework by apache.
the class CallServiceAsynch method exec.
@Override
public boolean exec(MethodContext methodContext) throws MiniLangException {
if (methodContext.isTraceOn()) {
outputTraceMessage(methodContext, "Begin call-service-asynch.");
}
String serviceName = serviceNameFse.expandString(methodContext.getEnvMap());
Map<String, Object> inMap = inMapFma.get(methodContext.getEnvMap());
if (inMap == null) {
inMap = new HashMap<String, Object>();
}
if (includeUserLogin) {
GenericValue userLogin = methodContext.getUserLogin();
if (userLogin != null && inMap.get("userLogin") == null) {
inMap.put("userLogin", userLogin);
}
}
Locale locale = methodContext.getLocale();
if (locale != null) {
inMap.put("locale", locale);
}
try {
if (methodContext.isTraceOn()) {
outputTraceMessage(methodContext, "Invoking service \"" + serviceName + "\", IN attributes:", inMap.toString());
}
methodContext.getDispatcher().runAsync(serviceName, inMap);
} catch (GenericServiceException e) {
if (methodContext.isTraceOn()) {
outputTraceMessage(methodContext, "Service engine threw an exception: " + e.getMessage() + ", halting script execution. End call-service-asynch.");
}
Debug.logError(e, module);
String errMsg = "ERROR: Could not complete the " + simpleMethod.getShortDescription() + " process [problem invoking the " + serviceName + " service: " + e.getMessage() + "]";
if (methodContext.getMethodType() == MethodContext.EVENT) {
methodContext.putEnv(simpleMethod.getEventErrorMessageName(), errMsg);
methodContext.putEnv(simpleMethod.getEventResponseCodeName(), simpleMethod.getDefaultErrorCode());
} else {
methodContext.putEnv(simpleMethod.getServiceErrorMessageName(), errMsg);
methodContext.putEnv(simpleMethod.getServiceResponseMessageName(), simpleMethod.getDefaultErrorCode());
}
return false;
}
if (methodContext.isTraceOn()) {
outputTraceMessage(methodContext, "End call-service-asynch.");
}
return true;
}
use of org.apache.ofbiz.service.GenericServiceException in project ofbiz-framework by apache.
the class GenericAsyncEngine method runAsync.
/**
* @see org.apache.ofbiz.service.engine.GenericEngine#runAsync(java.lang.String, org.apache.ofbiz.service.ModelService, java.util.Map, org.apache.ofbiz.service.GenericRequester, boolean)
*/
public void runAsync(String localName, ModelService modelService, Map<String, Object> context, GenericRequester requester, boolean persist) throws GenericServiceException {
DispatchContext dctx = dispatcher.getLocalContext(localName);
Job job = null;
if (persist) {
// check for a delegator
if (dispatcher.getDelegator() == null) {
throw new GenericServiceException("No reference to delegator; cannot run persisted services.");
}
GenericValue jobV = null;
// Build the value object(s).
try {
// Create the runtime data
String dataId = dispatcher.getDelegator().getNextSeqId("RuntimeData");
GenericValue runtimeData = dispatcher.getDelegator().makeValue("RuntimeData", "runtimeDataId", dataId);
runtimeData.set("runtimeInfo", XmlSerializer.serialize(context));
runtimeData.create();
// Get the userLoginId out of the context
String authUserLoginId = null;
if (context.get("userLogin") != null) {
GenericValue userLogin = (GenericValue) context.get("userLogin");
authUserLoginId = userLogin.getString("userLoginId");
}
// Create the job info
String jobId = dispatcher.getDelegator().getNextSeqId("JobSandbox");
String jobName = Long.toString(System.currentTimeMillis());
Map<String, Object> jFields = UtilMisc.toMap("jobId", jobId, "jobName", jobName, "runTime", UtilDateTime.nowTimestamp());
jFields.put("poolId", ServiceConfigUtil.getServiceEngine().getThreadPool().getSendToPool());
jFields.put("statusId", "SERVICE_PENDING");
jFields.put("serviceName", modelService.name);
jFields.put("loaderName", localName);
jFields.put("maxRetry", Long.valueOf(modelService.maxRetry));
jFields.put("runtimeDataId", dataId);
if (UtilValidate.isNotEmpty(authUserLoginId)) {
jFields.put("authUserLoginId", authUserLoginId);
}
jobV = dispatcher.getDelegator().makeValue("JobSandbox", jFields);
jobV.create();
} catch (GenericEntityException e) {
throw new GenericServiceException("Unable to create persisted job", e);
} catch (SerializeException e) {
throw new GenericServiceException("Problem serializing service attributes", e);
} catch (FileNotFoundException e) {
throw new GenericServiceException("Problem serializing service attributes", e);
} catch (IOException e) {
throw new GenericServiceException("Problem serializing service attributes", e);
} catch (GenericConfigException e) {
throw new GenericServiceException("Problem serializing service attributes", e);
}
Debug.logInfo("Persisted job queued : " + jobV.getString("jobName"), module);
} else {
JobManager jMgr = dispatcher.getJobManager();
if (jMgr != null) {
String name = Long.toString(System.currentTimeMillis());
String jobId = modelService.name + "." + name;
job = new GenericServiceJob(dctx, jobId, name, modelService.name, context, requester);
try {
dispatcher.getJobManager().runJob(job);
} catch (JobManagerException jse) {
throw new GenericServiceException("Cannot run job.", jse);
}
} else {
throw new GenericServiceException("Cannot get JobManager instance to invoke the job");
}
}
}
Aggregations