TimeZone timeZone = UtilHttp.getTimeZone(request);
HttpSession session = request.getSession();
GenericValue userLogin = (GenericValue) session.getAttribute("userLogin");
// get the service model to generate context(s)
ModelService modelService = null;
try {
modelService = dctx.getModelService(serviceName);
} catch (GenericServiceException e) {
throw new EventHandlerException("Problems getting the service model", e);
}
if (modelService == null) {
throw new EventHandlerException("Problems getting the service model");
}
if (Debug.verboseOn()) Debug.logVerbose("[Processing]: SERVICE Event", module);
if (Debug.verboseOn()) Debug.logVerbose("[Using delegator]: " + dispatcher.getDelegator().getDelegatorName(), module);
// check if we are using per row submit
boolean useRowSubmit = request.getParameter("_useRowSubmit") == null ? false :
"Y".equalsIgnoreCase(request.getParameter("_useRowSubmit"));
// check if we are to also look in a global scope (no delimiter)
boolean checkGlobalScope = request.getParameter("_checkGlobalScope") == null ? true :
!"N".equalsIgnoreCase(request.getParameter("_checkGlobalScope"));
// The number of multi form rows is retrieved
int rowCount = UtilHttp.getMultiFormRowCount(request);
if (rowCount < 1) {
throw new EventHandlerException("No rows to process");
}
// some default message settings
String errorPrefixStr = UtilProperties.getMessage("DefaultMessages", "service.error.prefix", locale);
String errorSuffixStr = UtilProperties.getMessage("DefaultMessages", "service.error.suffix", locale);
String messagePrefixStr = UtilProperties.getMessage("DefaultMessages", "service.message.prefix", locale);
String messageSuffixStr = UtilProperties.getMessage("DefaultMessages", "service.message.suffix", locale);
// prepare the error message and success message lists
List<Object> errorMessages = FastList.newInstance();
List<String> successMessages = FastList.newInstance();
// Check the global-transaction attribute of the event from the controller to see if the
// event should be wrapped in a transaction
String requestUri = RequestHandler.getRequestUri(request.getPathInfo());
ConfigXMLReader.ControllerConfig controllerConfig = ConfigXMLReader.getControllerConfig(ConfigXMLReader.getControllerConfigURL(servletContext));
boolean eventGlobalTransaction = controllerConfig.requestMapMap.get(requestUri).event.globalTransaction;
Set<String> urlOnlyParameterNames = UtilHttp.getUrlOnlyParameterMap(request).keySet();
// big try/finally to make sure commit or rollback are run
boolean beganTrans = false;
String returnString = null;
try {
if (eventGlobalTransaction) {
// start the global transaction
try {
beganTrans = TransactionUtil.begin(modelService.transactionTimeout * rowCount);
} catch (GenericTransactionException e) {
throw new EventHandlerException("Problem starting multi-service global transaction", e);
}
}
// now loop throw the rows and prepare/invoke the service for each
for (int i = 0; i < rowCount; i++) {
String curSuffix = UtilHttp.MULTI_ROW_DELIMITER + i;
boolean rowSelected = false;
if (UtilValidate.isNotEmpty(request.getAttribute(UtilHttp.ROW_SUBMIT_PREFIX + i))) {
rowSelected = request.getAttribute(UtilHttp.ROW_SUBMIT_PREFIX + i) == null ? false :
"Y".equalsIgnoreCase((String)request.getAttribute(UtilHttp.ROW_SUBMIT_PREFIX + i));
} else {
rowSelected = request.getParameter(UtilHttp.ROW_SUBMIT_PREFIX + i) == null ? false :
"Y".equalsIgnoreCase(request.getParameter(UtilHttp.ROW_SUBMIT_PREFIX + i));
}
// make sure we are to process this row
if (useRowSubmit && !rowSelected) {
continue;
}
// build the context
Map<String, Object> serviceContext = FastMap.newInstance();
for (ModelParam modelParam: modelService.getInModelParamList()) {
String paramName = modelParam.name;
// Debug.logInfo("In ServiceMultiEventHandler processing input parameter [" + modelParam.name + (modelParam.optional?"(optional):":"(required):") + modelParam.mode + "] for service [" + serviceName + "]", module);
// don't include userLogin, that's taken care of below
if ("userLogin".equals(paramName)) continue;
// don't include locale, that is also taken care of below
if ("locale".equals(paramName)) continue;
// don't include timeZone, that is also taken care of below
if ("timeZone".equals(paramName)) continue;
Object value = null;
if (modelParam.stringMapPrefix != null && modelParam.stringMapPrefix.length() > 0) {
Map<String, Object> paramMap = UtilHttp.makeParamMapWithPrefix(request, modelParam.stringMapPrefix, curSuffix);
value = paramMap;
} else if (modelParam.stringListSuffix != null && modelParam.stringListSuffix.length() > 0) {
List<Object> paramList = UtilHttp.makeParamListWithSuffix(request, modelParam.stringListSuffix, null);
value = paramList;
} else {
// check attributes; do this before parameters so that attribute which can be changed by code can override parameters which can't
value = request.getAttribute(paramName + curSuffix);
// first check for request parameters
if (value == null) {
String name = paramName + curSuffix;
ServiceEventHandler.checkSecureParameter(requestMap, urlOnlyParameterNames, name, session, serviceName);
String[] paramArr = request.getParameterValues(name);
if (paramArr != null) {
if (paramArr.length > 1) {
value = Arrays.asList(paramArr);
} else {
value = paramArr[0];
}
}
}
// if the parameter wasn't passed and no other value found, check the session
if (value == null) {
value = session.getAttribute(paramName + curSuffix);
}
// now check global scope
if (value == null) {
if (checkGlobalScope) {
String[] gParamArr = request.getParameterValues(paramName);
if (gParamArr != null) {
if (gParamArr.length > 1) {
value = Arrays.asList(gParamArr);
} else {
value = gParamArr[0];
}
}
if (value == null) {
value = request.getAttribute(paramName);
}
if (value == null) {
value = session.getAttribute(paramName);
}
}
}
if (value == null) {
// still null, give up for this one
continue;
}
if (value instanceof String && ((String) value).length() == 0) {
// interpreting empty fields as null values for each in back end handling...
value = null;
}
}
// set even if null so that values will get nulled in the db later on
serviceContext.put(paramName, value);
// Debug.logInfo("In ServiceMultiEventHandler got value [" + value + "] for input parameter [" + paramName + "] for service [" + serviceName + "]", module);
}
// get only the parameters for this service - converted to proper type
serviceContext = modelService.makeValid(serviceContext, ModelService.IN_PARAM, true, null, timeZone, locale);
// include the UserLogin value object
if (userLogin != null) {
serviceContext.put("userLogin", userLogin);
}