📄 servicemultieventhandler.java
字号:
/* * $Id: ServiceMultiEventHandler.java 6583 2006-01-25 20:24:44Z jonesde $ * * Copyright (c) 2003-2005 The Open For Business Project - www.ofbiz.org * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * */package org.ofbiz.webapp.event;import java.util.Arrays;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Locale;import java.util.Map;import javax.servlet.ServletContext;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import javolution.util.FastList;import javolution.util.FastMap;import org.ofbiz.base.util.Debug;import org.ofbiz.base.util.UtilHttp;import org.ofbiz.base.util.UtilProperties;import org.ofbiz.base.util.UtilValidate;import org.ofbiz.entity.GenericValue;import org.ofbiz.entity.transaction.GenericTransactionException;import org.ofbiz.entity.transaction.TransactionUtil;import org.ofbiz.service.DispatchContext;import org.ofbiz.service.GenericServiceException;import org.ofbiz.service.LocalDispatcher;import org.ofbiz.service.ModelParam;import org.ofbiz.service.ModelService;import org.ofbiz.service.ServiceAuthException;import org.ofbiz.service.ServiceUtil;import org.ofbiz.service.ServiceValidationException;/** * ServiceMultiEventHandler - Event handler for running a service multiple times; for bulk forms * * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a> * @version $Rev: 6583 $ * @since 2.2 */public class ServiceMultiEventHandler implements EventHandler { public static final String module = ServiceMultiEventHandler.class.getName(); public static final String SYNC = "sync"; public static final String ASYNC = "async"; /** * @see org.ofbiz.webapp.event.EventHandler#init(javax.servlet.ServletContext) */ public void init(ServletContext context) throws EventHandlerException { } /** * @see org.ofbiz.webapp.event.EventHandler#invoke(java.lang.String, java.lang.String, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) */ public String invoke(String eventPath, String eventMethod, HttpServletRequest request, HttpServletResponse response) throws EventHandlerException { // TODO: consider changing this to use the new UtilHttp.parseMultiFormData method // make sure we have a valid reference to the Service Engine LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); if (dispatcher == null) { throw new EventHandlerException("The local service dispatcher is null"); } DispatchContext dctx = dispatcher.getDispatchContext(); if (dctx == null) { throw new EventHandlerException("Dispatch context cannot be found"); } // get the details for the service(s) to call String mode = SYNC; String serviceName = null; if (eventPath == null || eventPath.length() == 0) { mode = SYNC; } else { mode = eventPath; } // we only support SYNC mode in this handler if (mode != SYNC) { throw new EventHandlerException("Async mode is not supported"); } // nake sure we have a defined service to call serviceName = eventMethod; if (serviceName == null) { throw new EventHandlerException("Service name (eventMethod) cannot be null"); } if (Debug.verboseOn()) Debug.logVerbose("[Set mode/service]: " + mode + "/" + serviceName, module); // some needed info for when running the service Locale locale = UtilHttp.getLocale(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")); // get the number of rows String rowCountField = request.getParameter("_rowCount"); if (rowCountField == null) { throw new EventHandlerException("Required field _rowCount is missing"); } int rowCount = 0; // parsed int value try { rowCount = Integer.parseInt(rowCountField); } catch (NumberFormatException e) { throw new EventHandlerException("Invalid value for _rowCount"); } 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 list List errorMessages = FastList.newInstance(); // big try/finally to make sure commit or rollback are run boolean beganTrans = false; String returnString = null; try { // start the transaction try { beganTrans = TransactionUtil.begin(); } catch (GenericTransactionException e) { throw new EventHandlerException("Problem starting transaction", e); } // now loop throw the rows and prepare/invoke the service for each for (int i = 0; i < rowCount; i++) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -