001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.minilang.method.entityops;
019:
020: import org.ofbiz.base.util.Debug;
021: import org.ofbiz.entity.GenericEntityException;
022: import org.ofbiz.entity.GenericValue;
023: import org.ofbiz.minilang.SimpleMethod;
024: import org.ofbiz.minilang.method.ContextAccessor;
025: import org.ofbiz.minilang.method.MethodContext;
026: import org.ofbiz.minilang.method.MethodOperation;
027: import org.w3c.dom.Element;
028:
029: /**
030: * Uses the delegator to store the specified value object entity in the datasource
031: */
032: public class StoreValue extends MethodOperation {
033:
034: public static final String module = StoreValue.class.getName();
035:
036: ContextAccessor valueAcsr;
037: String doCacheClearStr;
038:
039: public StoreValue(Element element, SimpleMethod simpleMethod) {
040: super (element, simpleMethod);
041: valueAcsr = new ContextAccessor(element
042: .getAttribute("value-name"));
043: doCacheClearStr = element.getAttribute("do-cache-clear");
044: }
045:
046: public boolean exec(MethodContext methodContext) {
047: boolean doCacheClear = !"false".equals(methodContext
048: .expandString(doCacheClearStr));
049:
050: GenericValue value = (GenericValue) valueAcsr
051: .get(methodContext);
052: if (value == null) {
053: String errMsg = "In store-value a value was not found with the specified valueAcsr: "
054: + valueAcsr + ", not storing";
055:
056: Debug.logWarning(errMsg, module);
057: if (methodContext.getMethodType() == MethodContext.EVENT) {
058: methodContext.putEnv(simpleMethod
059: .getEventErrorMessageName(), errMsg);
060: methodContext.putEnv(simpleMethod
061: .getEventResponseCodeName(), simpleMethod
062: .getDefaultErrorCode());
063: } else if (methodContext.getMethodType() == MethodContext.SERVICE) {
064: methodContext.putEnv(simpleMethod
065: .getServiceErrorMessageName(), errMsg);
066: methodContext.putEnv(simpleMethod
067: .getServiceResponseMessageName(), simpleMethod
068: .getDefaultErrorCode());
069: }
070: return false;
071: }
072:
073: try {
074: methodContext.getDelegator().store(value, doCacheClear);
075: } catch (GenericEntityException e) {
076: Debug.logError(e, module);
077: String errMsg = "ERROR: Could not complete the "
078: + simpleMethod.getShortDescription()
079: + " process [problem storing the " + valueAcsr
080: + " value: " + e.getMessage() + "]";
081:
082: if (methodContext.getMethodType() == MethodContext.EVENT) {
083: methodContext.putEnv(simpleMethod
084: .getEventErrorMessageName(), errMsg);
085: methodContext.putEnv(simpleMethod
086: .getEventResponseCodeName(), simpleMethod
087: .getDefaultErrorCode());
088: } else if (methodContext.getMethodType() == MethodContext.SERVICE) {
089: methodContext.putEnv(simpleMethod
090: .getServiceErrorMessageName(), errMsg);
091: methodContext.putEnv(simpleMethod
092: .getServiceResponseMessageName(), simpleMethod
093: .getDefaultErrorCode());
094: }
095: return false;
096: }
097: return true;
098: }
099:
100: public String rawString() {
101: // TODO: something more than the empty tag
102: return "<store-value/>";
103: }
104:
105: public String expandedString(MethodContext methodContext) {
106: // TODO: something more than a stub/dummy
107: return this.rawString();
108: }
109: }
|