001: /*
002: * $Id: StoreValue.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 store 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 StoreValue extends MethodOperation {
043:
044: public static final String module = StoreValue.class.getName();
045:
046: ContextAccessor valueAcsr;
047: String doCacheClearStr;
048:
049: public StoreValue(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 store-value a value was not found with the specified valueAcsr: "
064: + valueAcsr + ", not storing";
065:
066: Debug.logWarning(errMsg, module);
067: if (methodContext.getMethodType() == MethodContext.EVENT) {
068: methodContext.putEnv(simpleMethod
069: .getEventErrorMessageName(), errMsg);
070: methodContext.putEnv(simpleMethod
071: .getEventResponseCodeName(), simpleMethod
072: .getDefaultErrorCode());
073: } else if (methodContext.getMethodType() == MethodContext.SERVICE) {
074: methodContext.putEnv(simpleMethod
075: .getServiceErrorMessageName(), errMsg);
076: methodContext.putEnv(simpleMethod
077: .getServiceResponseMessageName(), simpleMethod
078: .getDefaultErrorCode());
079: }
080: return false;
081: }
082:
083: try {
084: methodContext.getDelegator().store(value, doCacheClear);
085: } catch (GenericEntityException e) {
086: Debug.logError(e, module);
087: String errMsg = "ERROR: Could not complete the "
088: + simpleMethod.getShortDescription()
089: + " process [problem storing the " + valueAcsr
090: + " value: " + e.getMessage() + "]";
091:
092: if (methodContext.getMethodType() == MethodContext.EVENT) {
093: methodContext.putEnv(simpleMethod
094: .getEventErrorMessageName(), errMsg);
095: methodContext.putEnv(simpleMethod
096: .getEventResponseCodeName(), simpleMethod
097: .getDefaultErrorCode());
098: } else if (methodContext.getMethodType() == MethodContext.SERVICE) {
099: methodContext.putEnv(simpleMethod
100: .getServiceErrorMessageName(), errMsg);
101: methodContext.putEnv(simpleMethod
102: .getServiceResponseMessageName(), simpleMethod
103: .getDefaultErrorCode());
104: }
105: return false;
106: }
107: return true;
108: }
109: }
|