1 25 package org.ofbiz.webapp.event; 26 27 import java.util.Arrays ; 28 import java.util.HashMap ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.Locale ; 32 import java.util.Map ; 33 34 import javax.servlet.ServletContext ; 35 import javax.servlet.http.HttpServletRequest ; 36 import javax.servlet.http.HttpServletResponse ; 37 import javax.servlet.http.HttpSession ; 38 39 import javolution.util.FastList; 40 import javolution.util.FastMap; 41 42 import org.ofbiz.base.util.Debug; 43 import org.ofbiz.base.util.UtilHttp; 44 import org.ofbiz.base.util.UtilProperties; 45 import org.ofbiz.base.util.UtilValidate; 46 import org.ofbiz.entity.GenericValue; 47 import org.ofbiz.entity.transaction.GenericTransactionException; 48 import org.ofbiz.entity.transaction.TransactionUtil; 49 import org.ofbiz.service.DispatchContext; 50 import org.ofbiz.service.GenericServiceException; 51 import org.ofbiz.service.LocalDispatcher; 52 import org.ofbiz.service.ModelParam; 53 import org.ofbiz.service.ModelService; 54 import org.ofbiz.service.ServiceAuthException; 55 import org.ofbiz.service.ServiceUtil; 56 import org.ofbiz.service.ServiceValidationException; 57 import org.ofbiz.webapp.control.RequestHandler; 58 import org.ofbiz.webapp.control.RequestManager; 59 60 67 public class ServiceMultiEventHandler implements EventHandler { 68 69 public static final String module = ServiceMultiEventHandler.class.getName(); 70 71 public static final String SYNC = "sync"; 72 public static final String ASYNC = "async"; 73 private RequestManager requestManager; 74 75 78 public void init(ServletContext context) throws EventHandlerException { 79 80 this.requestManager = new RequestManager(context); 82 } 83 84 87 public String invoke(String eventPath, String eventMethod, HttpServletRequest request, HttpServletResponse response) throws EventHandlerException { 88 90 LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); 92 if (dispatcher == null) { 93 throw new EventHandlerException("The local service dispatcher is null"); 94 } 95 DispatchContext dctx = dispatcher.getDispatchContext(); 96 if (dctx == null) { 97 throw new EventHandlerException("Dispatch context cannot be found"); 98 } 99 100 String mode = SYNC; 102 String serviceName = null; 103 104 if (eventPath == null || eventPath.length() == 0) { 105 mode = SYNC; 106 } else { 107 mode = eventPath; 108 } 109 110 if (mode != SYNC) { 112 throw new EventHandlerException("Async mode is not supported"); 113 } 114 115 serviceName = eventMethod; 117 if (serviceName == null) { 118 throw new EventHandlerException("Service name (eventMethod) cannot be null"); 119 } 120 if (Debug.verboseOn()) Debug.logVerbose("[Set mode/service]: " + mode + "/" + serviceName, module); 121 122 Locale locale = UtilHttp.getLocale(request); 124 HttpSession session = request.getSession(); 125 GenericValue userLogin = (GenericValue) session.getAttribute("userLogin"); 126 127 ModelService modelService = null; 129 130 try { 131 modelService = dctx.getModelService(serviceName); 132 } catch (GenericServiceException e) { 133 throw new EventHandlerException("Problems getting the service model", e); 134 } 135 136 if (modelService == null) { 137 throw new EventHandlerException("Problems getting the service model"); 138 } 139 140 if (Debug.verboseOn()) Debug.logVerbose("[Processing]: SERVICE Event", module); 141 if (Debug.verboseOn()) Debug.logVerbose("[Using delegator]: " + dispatcher.getDelegator().getDelegatorName(), module); 142 143 boolean useRowSubmit = request.getParameter("_useRowSubmit") == null ? false : 145 "Y".equalsIgnoreCase(request.getParameter("_useRowSubmit")); 146 147 boolean checkGlobalScope = request.getParameter("_checkGlobalScope") == null ? true : 149 !"N".equalsIgnoreCase(request.getParameter("_checkGlobalScope")); 150 151 String rowCountField = request.getParameter("_rowCount"); 153 if (rowCountField == null) { 154 throw new EventHandlerException("Required field _rowCount is missing"); 155 } 156 157 int rowCount = 0; try { 159 rowCount = Integer.parseInt(rowCountField); 160 } catch (NumberFormatException e) { 161 throw new EventHandlerException("Invalid value for _rowCount"); 162 } 163 if (rowCount < 1) { 164 throw new EventHandlerException("No rows to process"); 165 } 166 167 String errorPrefixStr = UtilProperties.getMessage("DefaultMessages", "service.error.prefix", locale); 169 String errorSuffixStr = UtilProperties.getMessage("DefaultMessages", "service.error.suffix", locale); 170 String messagePrefixStr = UtilProperties.getMessage("DefaultMessages", "service.message.prefix", locale); 171 String messageSuffixStr = UtilProperties.getMessage("DefaultMessages", "service.message.suffix", locale); 172 173 List errorMessages = FastList.newInstance(); 175 176 String requestUri = RequestHandler.getRequestUri(request.getPathInfo()); 179 boolean eventGlobalTransaction = requestManager.getEventGlobalTransaction(requestUri); 180 181 boolean beganTrans = false; 183 String returnString = null; 184 try { 185 186 if (eventGlobalTransaction) { 187 try { 189 beganTrans = TransactionUtil.begin(modelService.transactionTimeout * rowCount); 190 } catch (GenericTransactionException e) { 191 throw new EventHandlerException("Problem starting multi-service global transaction", e); 192 } 193 } 194 195 for (int i = 0; i < rowCount; i++) { 197 String curSuffix = UtilHttp.MULTI_ROW_DELIMITER + i; 198 boolean rowSelected = request.getParameter(UtilHttp.ROW_SUBMIT_PREFIX + i) == null ? false : 199 "Y".equalsIgnoreCase(request.getParameter(UtilHttp.ROW_SUBMIT_PREFIX + i)); 200 201 if (useRowSubmit && !rowSelected) { 203 continue; 204 } 205 206 Map serviceContext = FastMap.newInstance(); 208 List modelParmInList = modelService.getInModelParamList(); 209 Iterator modelParmInIter = modelParmInList.iterator(); 210 while (modelParmInIter.hasNext()) { 211 ModelParam modelParam = (ModelParam) modelParmInIter.next(); 212 String paramName = (String ) modelParam.name; 213 214 216 if ("userLogin".equals(paramName)) continue; 218 if ("locale".equals(paramName)) continue; 220 221 Object value = null; 222 if (modelParam.stringMapPrefix != null && modelParam.stringMapPrefix.length() > 0) { 223 Map paramMap = UtilHttp.makeParamMapWithPrefix(request, modelParam.stringMapPrefix, curSuffix); 224 value = paramMap; 225 } else if (modelParam.stringListSuffix != null && modelParam.stringListSuffix.length() > 0) { 226 List paramList = UtilHttp.makeParamListWithSuffix(request, modelParam.stringListSuffix, null); 227 value = paramList; 228 } else { 229 String [] paramArr = request.getParameterValues(paramName + curSuffix); 231 if (paramArr != null) { 232 if (paramArr.length > 1) { 233 value = Arrays.asList(paramArr); 234 } else { 235 value = paramArr[0]; 236 } 237 } 238 239 if (value == null) { 241 value = request.getAttribute(paramName + curSuffix); 242 } 243 if (value == null) { 244 value = session.getAttribute(paramName + curSuffix); 245 } 246 247 if (value == null) { 249 if (checkGlobalScope) { 250 String [] gParamArr = request.getParameterValues(paramName); 251 if (gParamArr != null) { 252 if (gParamArr.length > 1) { 253 value = Arrays.asList(gParamArr); 254 } else { 255 value = gParamArr[0]; 256 } 257 } 258 if (value == null) { 259 value = request.getAttribute(paramName); 260 } 261 if (value == null) { 262 value = session.getAttribute(paramName); 263 } 264 } 265 } 266 267 if (value == null) { 268 continue; 270 } 271 272 if (value instanceof String && ((String ) value).length() == 0) { 273 value = null; 275 } 276 } 277 serviceContext.put(paramName, value); 279 280 } 282 283 serviceContext = modelService.makeValid(serviceContext, ModelService.IN_PARAM, true, null, locale); 285 286 if (userLogin != null) { 288 serviceContext.put("userLogin", userLogin); 289 } 290 291 if (locale != null) { 293 serviceContext.put("locale", locale); 294 } 295 296 298 Map result = null; 300 try { 301 result = dispatcher.runSync(serviceName, serviceContext); 302 } catch (ServiceAuthException e) { 303 errorMessages.add(messagePrefixStr + "Service invocation error on row (" + i +"): " + e.getNonNestedMessage()); 305 } catch (ServiceValidationException e) { 306 request.setAttribute("serviceValidationException", e); 308 List errors = e.getMessageList(); 309 if (errors != null) { 310 Iterator erri = errors.iterator(); 311 while (erri.hasNext()) { 312 errorMessages.add("Service invocation error on row (" + i + "): " + erri.next()); 313 } 314 } else { 315 errorMessages.add(messagePrefixStr + "Service invocation error on row (" + i +"): " + e.getNonNestedMessage()); 316 } 317 } catch (GenericServiceException e) { 318 Debug.logError(e, "Service invocation error", module); 319 errorMessages.add(messagePrefixStr + "Service invocation error on row (" + i +"): " + e.getNested() + messageSuffixStr); 320 } 321 322 String errorMessage = ServiceUtil.makeErrorMessage(result, messagePrefixStr, messageSuffixStr, "", ""); 324 if (UtilValidate.isNotEmpty(errorMessage)) { 325 errorMessages.add(errorMessage); 326 } 327 328 if ((result != null) && (result.entrySet() != null)) { 330 Iterator rmei = result.entrySet().iterator(); 331 while (rmei.hasNext()) { 332 Map.Entry rme = (Map.Entry ) rmei.next(); 333 String resultKey = (String ) rme.getKey(); 334 Object resultValue = rme.getValue(); 335 336 if (resultKey != null && !ModelService.RESPONSE_MESSAGE.equals(resultKey) && !ModelService.ERROR_MESSAGE.equals(resultKey) && 337 !ModelService.ERROR_MESSAGE_LIST.equals(resultKey) && !ModelService.ERROR_MESSAGE_MAP.equals(resultKey) && 338 !ModelService.SUCCESS_MESSAGE.equals(resultKey) && !ModelService.SUCCESS_MESSAGE_LIST.equals(resultKey)) { 339 request.setAttribute(resultKey, resultValue); 340 } 341 } 342 } 343 } 344 } finally { 345 if (errorMessages.size() > 0) { 346 if (eventGlobalTransaction) { 347 try { 349 TransactionUtil.rollback(beganTrans, "Error in multi-service event handling: " + errorMessages.toString(), null); 350 } catch (GenericTransactionException e) { 351 Debug.logError(e, "Could not rollback multi-service global transaction", module); 352 } 353 } 354 errorMessages.add(0, errorPrefixStr); 355 errorMessages.add(errorSuffixStr); 356 StringBuffer errorBuf = new StringBuffer (); 357 Iterator ei = errorMessages.iterator(); 358 while (ei.hasNext()) { 359 String em = (String ) ei.next(); 360 errorBuf.append(em + "\n"); 361 } 362 request.setAttribute("_ERROR_MESSAGE_", errorBuf.toString()); 363 returnString = "error"; 364 } else { 365 if (eventGlobalTransaction) { 366 try { 368 TransactionUtil.commit(beganTrans); 369 } catch (GenericTransactionException e) { 370 Debug.logError(e, "Could not commit multi-service global transaction", module); 371 throw new EventHandlerException("Commit multi-service global transaction failed"); 372 } 373 } 374 returnString = "success"; 375 } 376 } 377 378 return returnString; 379 } 380 } 381 | Popular Tags |