📄 genericwebevent.java
字号:
/* * $Id: GenericWebEvent.java 5462 2005-08-05 18:35:48Z jonesde $ * * Copyright (c) 2001-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.webtools;import java.lang.reflect.Field;import java.lang.reflect.Method;import java.util.Iterator;import java.util.Locale;import java.util.Map;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.ofbiz.base.util.Debug;import org.ofbiz.base.util.UtilHttp;import org.ofbiz.base.util.UtilMisc;import org.ofbiz.base.util.UtilProperties;import org.ofbiz.entity.GenericDelegator;import org.ofbiz.entity.GenericEntityException;import org.ofbiz.entity.GenericValue;import org.ofbiz.entity.model.ModelEntity;import org.ofbiz.entity.model.ModelField;import org.ofbiz.entity.model.ModelFieldType;import org.ofbiz.entity.model.ModelReader;import org.ofbiz.security.Security;/** * Web Event for doing updates on Generic Entities * * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a> * @version $Rev: 5462 $ * @since 2.0 */public class GenericWebEvent { public static final String module = GenericWebEvent.class.getName(); public static final String err_resource = "WebtoolsErrorUiLabels"; /** An HTTP WebEvent handler that updates a Generic entity * * @param request The HTTP request object for the current JSP or Servlet request. * @param response The HTTP response object for the current JSP or Servlet request. * @return Returns a String specifying the outcome state of the event. This is used to decide which event * to run next or which view to display. If null no event is run nor view displayed, allowing the event to * call a forward on a RequestDispatcher. */ public static String updateGeneric(HttpServletRequest request, HttpServletResponse response) { String errMsg = ""; String entityName = request.getParameter("entityName"); Locale locale = UtilHttp.getLocale(request); if (entityName == null || entityName.length() <= 0) { errMsg = UtilProperties.getMessage(GenericWebEvent.err_resource, "genericWebEvent.entity_name_not_specified", locale) + "."; request.setAttribute("_ERROR_MESSAGE_", errMsg); Debug.logWarning("[GenericWebEvent.updateGeneric] The entityName was not specified, but is required.", module); return "error"; } Security security = (Security) request.getAttribute("security"); GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); if (security == null) { errMsg = UtilProperties.getMessage(GenericWebEvent.err_resource,"genericWebEvent.security_object_not_found", locale) + "."; request.setAttribute("_ERROR_MESSAGE_", errMsg); Debug.logWarning("[updateGeneric] The security object was not found in the request, please check the control servlet init.", module); return "error"; } if (delegator == null) { errMsg = UtilProperties.getMessage(GenericWebEvent.err_resource, "genericWebEvent.delegator_object_not_found", locale) + "."; request.setAttribute("_ERROR_MESSAGE_", errMsg); Debug.logWarning("[updateGeneric] The delegator object was not found in the request, please check the control servlet init.", module); return "error"; } ModelReader reader = delegator.getModelReader(); ModelEntity entity = null; try { entity = reader.getModelEntity(entityName); } catch (GenericEntityException e) { Debug.logError(e, module); } String updateMode = request.getParameter("UPDATE_MODE"); if (updateMode == null || updateMode.length() <= 0) { errMsg = UtilProperties.getMessage(GenericWebEvent.err_resource, "genericWebEvent.update_mode_not_specified", locale) + "."; request.setAttribute("_ERROR_MESSAGE_", errMsg); Debug.logWarning("[updateGeneric] Update Mode was not specified, but is required; entityName: " + entityName, module); return "error"; } // check permissions before moving on... if (!security.hasEntityPermission("ENTITY_DATA", "_" + updateMode, request.getSession()) && !security.hasEntityPermission(entity.getPlainTableName(), "_" + updateMode, request.getSession())) { Map messageMap = UtilMisc.toMap("updateMode", updateMode, "entityName", entity.getEntityName(), "entityPlainTableName", entity.getPlainTableName()); errMsg = UtilProperties.getMessage(GenericWebEvent.err_resource, "genericWebEvent.not_sufficient_permissions_01", messageMap, locale); errMsg += UtilProperties.getMessage(GenericWebEvent.err_resource, "genericWebEvent.not_sufficient_permissions_02", messageMap, locale) + "."; request.setAttribute("_ERROR_MESSAGE_", errMsg); // not really successful, but error return through ERROR_MESSAGE, so quietly fail return "error"; } GenericValue findByEntity = delegator.makeValue(entityName, null); // get the primary key parameters... Iterator pksIter = entity.getPksIterator(); while (pksIter.hasNext()) { ModelField field = (ModelField) pksIter.next(); ModelFieldType type = null; try { type = delegator.getEntityFieldType(entity, field.getType()); } catch (GenericEntityException e) { Debug.logWarning(e, module); Map messageMap = UtilMisc.toMap("fieldType", field.getType()); errMsg += UtilProperties.getMessage(GenericWebEvent.err_resource, "genericWebEvent.fatal_error_param", messageMap, locale) + "."; } String fval = request.getParameter(field.getName()); if (fval != null && fval.length() > 0) { try { findByEntity.setString(field.getName(), fval); } catch (Exception e) { Map messageMap = UtilMisc.toMap("fval", fval); errMsg = errMsg + "<li>" + field.getColName() + UtilProperties.getMessage(GenericWebEvent.err_resource, "genericWebEvent.conversion_failed", messageMap, locale) + type.getJavaType() + "."; Debug.logWarning("[updateGeneric] " + field.getColName() + " conversion failed: \"" + fval + "\" is not a valid " + type.getJavaType() + "; entityName: " + entityName, module); } } } // if this is a delete, do that before getting all of the non-pk parameters and validating them if (updateMode.equals("DELETE")) { // Remove associated/dependent entries from other tables here // Delete actual main entity last, just in case database is set up to do a cascading delete, caches won't get cleared try { delegator.removeByPrimaryKey(findByEntity.getPrimaryKey()); } catch (GenericEntityException e) { Debug.logWarning(e, module); errMsg = UtilProperties.getMessage(GenericWebEvent.err_resource, "genericWebEvent.delete_failed", locale); request.setAttribute("_ERROR_MESSAGE_", errMsg);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -