001: /*
002: * $Id: CreateValue.java,v 1.1 2003/08/17 06:06:11 ajzeneski Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: */
024: package org.ofbiz.minilang.method.entityops;
025:
026: import org.ofbiz.base.util.Debug;
027: import org.ofbiz.entity.GenericEntityException;
028: import org.ofbiz.entity.GenericValue;
029: import org.ofbiz.minilang.SimpleMethod;
030: import org.ofbiz.minilang.method.ContextAccessor;
031: import org.ofbiz.minilang.method.MethodContext;
032: import org.ofbiz.minilang.method.MethodOperation;
033: import org.w3c.dom.Element;
034:
035: /**
036: * Uses the delegator to create the specified value object entity in the datasource
037: *
038: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
039: * @version $Revision: 1.1 $
040: * @since 2.0
041: */
042: public class CreateValue extends MethodOperation {
043:
044: public static final String module = CreateValue.class.getName();
045:
046: ContextAccessor valueAcsr;
047: String doCacheClearStr;
048:
049: public CreateValue(Element element, SimpleMethod simpleMethod) {
050: super (element, simpleMethod);
051: valueAcsr = new ContextAccessor(element
052: .getAttribute("value-name"));
053: doCacheClearStr = element.getAttribute("do-cache-clear");
054: }
055:
056: public boolean exec(MethodContext methodContext) {
057: boolean doCacheClear = !"false".equals(methodContext
058: .expandString(doCacheClearStr));
059:
060: GenericValue value = (GenericValue) valueAcsr
061: .get(methodContext);
062: if (value == null) {
063: String errMsg = "In create-value a value was not found with the specified valueAcsr: "
064: + valueAcsr + ", not creating";
065: Debug.logWarning(errMsg, module);
066: if (methodContext.getMethodType() == MethodContext.EVENT) {
067: methodContext.putEnv(simpleMethod
068: .getEventErrorMessageName(), errMsg);
069: methodContext.putEnv(simpleMethod
070: .getEventResponseCodeName(), simpleMethod
071: .getDefaultErrorCode());
072: } else if (methodContext.getMethodType() == MethodContext.SERVICE) {
073: methodContext.putEnv(simpleMethod
074: .getServiceErrorMessageName(), errMsg);
075: methodContext.putEnv(simpleMethod
076: .getServiceResponseMessageName(), simpleMethod
077: .getDefaultErrorCode());
078: }
079: return false;
080: }
081:
082: try {
083: methodContext.getDelegator().create(value, doCacheClear);
084: } catch (GenericEntityException e) {
085: Debug.logError(e, module);
086: String errMsg = "ERROR: Could not complete the "
087: + simpleMethod.getShortDescription()
088: + " process [problem creating the " + valueAcsr
089: + " value: " + e.getMessage() + "]";
090: if (methodContext.getMethodType() == MethodContext.EVENT) {
091: methodContext.putEnv(simpleMethod
092: .getEventErrorMessageName(), errMsg);
093: methodContext.putEnv(simpleMethod
094: .getEventResponseCodeName(), simpleMethod
095: .getDefaultErrorCode());
096: } else if (methodContext.getMethodType() == MethodContext.SERVICE) {
097: methodContext.putEnv(simpleMethod
098: .getServiceErrorMessageName(), errMsg);
099: methodContext.putEnv(simpleMethod
100: .getServiceResponseMessageName(), simpleMethod
101: .getDefaultErrorCode());
102: }
103: return false;
104: }
105: return true;
106: }
107: }
|