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 create the specified value object entity in the datasource
031: */
032: public class CreateValue extends MethodOperation {
033:
034: public static final String module = CreateValue.class.getName();
035:
036: ContextAccessor valueAcsr;
037: String doCacheClearStr;
038:
039: public CreateValue(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 create-value a value was not found with the specified valueAcsr: "
054: + valueAcsr + ", not creating";
055: Debug.logWarning(errMsg, module);
056: if (methodContext.getMethodType() == MethodContext.EVENT) {
057: methodContext.putEnv(simpleMethod
058: .getEventErrorMessageName(), errMsg);
059: methodContext.putEnv(simpleMethod
060: .getEventResponseCodeName(), simpleMethod
061: .getDefaultErrorCode());
062: } else if (methodContext.getMethodType() == MethodContext.SERVICE) {
063: methodContext.putEnv(simpleMethod
064: .getServiceErrorMessageName(), errMsg);
065: methodContext.putEnv(simpleMethod
066: .getServiceResponseMessageName(), simpleMethod
067: .getDefaultErrorCode());
068: }
069: return false;
070: }
071:
072: try {
073: methodContext.getDelegator().create(value, doCacheClear);
074: } catch (GenericEntityException e) {
075: Debug.logError(e, module);
076: String errMsg = "ERROR: Could not complete the "
077: + simpleMethod.getShortDescription()
078: + " process [problem creating the " + valueAcsr
079: + " value: " + e.getMessage() + "]";
080: if (methodContext.getMethodType() == MethodContext.EVENT) {
081: methodContext.putEnv(simpleMethod
082: .getEventErrorMessageName(), errMsg);
083: methodContext.putEnv(simpleMethod
084: .getEventResponseCodeName(), simpleMethod
085: .getDefaultErrorCode());
086: } else if (methodContext.getMethodType() == MethodContext.SERVICE) {
087: methodContext.putEnv(simpleMethod
088: .getServiceErrorMessageName(), errMsg);
089: methodContext.putEnv(simpleMethod
090: .getServiceResponseMessageName(), simpleMethod
091: .getDefaultErrorCode());
092: }
093: return false;
094: }
095: return true;
096: }
097:
098: public String rawString() {
099: // TODO: something more than the empty tag
100: return "<create-value/>";
101: }
102:
103: public String expandedString(MethodContext methodContext) {
104: // TODO: something more than a stub/dummy
105: return this.rawString();
106: }
107: }
|