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 remove entities related to the specified value object from the datasource
031: */
032: public class RemoveRelated extends MethodOperation {
033:
034: public static final String module = RemoveRelated.class.getName();
035:
036: ContextAccessor valueAcsr;
037: String relationName;
038: String doCacheClearStr;
039:
040: public RemoveRelated(Element element, SimpleMethod simpleMethod) {
041: super (element, simpleMethod);
042: valueAcsr = new ContextAccessor(element
043: .getAttribute("value-name"));
044: relationName = element.getAttribute("relation-name");
045: doCacheClearStr = element.getAttribute("do-cache-clear");
046: }
047:
048: public boolean exec(MethodContext methodContext) {
049: boolean doCacheClear = !"false".equals(doCacheClearStr);
050: String relationName = methodContext
051: .expandString(this .relationName);
052:
053: GenericValue value = (GenericValue) valueAcsr
054: .get(methodContext);
055: if (value == null) {
056: String errMsg = "In remove-related a value was not found with the specified valueAcsr: "
057: + valueAcsr + ", not removing related";
058:
059: Debug.logWarning(errMsg, module);
060: if (methodContext.getMethodType() == MethodContext.EVENT) {
061: methodContext.putEnv(simpleMethod
062: .getEventErrorMessageName(), errMsg);
063: methodContext.putEnv(simpleMethod
064: .getEventResponseCodeName(), simpleMethod
065: .getDefaultErrorCode());
066: } else if (methodContext.getMethodType() == MethodContext.SERVICE) {
067: methodContext.putEnv(simpleMethod
068: .getServiceErrorMessageName(), errMsg);
069: methodContext.putEnv(simpleMethod
070: .getServiceResponseMessageName(), simpleMethod
071: .getDefaultErrorCode());
072: }
073: return false;
074: }
075:
076: try {
077: methodContext.getDelegator().removeRelated(relationName,
078: value, doCacheClear);
079: } catch (GenericEntityException e) {
080: Debug.logError(e, module);
081: String errMsg = "ERROR: Could not complete the "
082: + simpleMethod.getShortDescription()
083: + " process [problem removing the relation "
084: + relationName + " of the value " + valueAcsr
085: + " value: " + e.getMessage() + "]";
086:
087: if (methodContext.getMethodType() == MethodContext.EVENT) {
088: methodContext.putEnv(simpleMethod
089: .getEventErrorMessageName(), errMsg);
090: methodContext.putEnv(simpleMethod
091: .getEventResponseCodeName(), simpleMethod
092: .getDefaultErrorCode());
093: } else if (methodContext.getMethodType() == MethodContext.SERVICE) {
094: methodContext.putEnv(simpleMethod
095: .getServiceErrorMessageName(), errMsg);
096: methodContext.putEnv(simpleMethod
097: .getServiceResponseMessageName(), simpleMethod
098: .getDefaultErrorCode());
099: }
100: return false;
101: }
102: return true;
103: }
104:
105: public String rawString() {
106: // TODO: something more than the empty tag
107: return "<remove-related/>";
108: }
109:
110: public String expandedString(MethodContext methodContext) {
111: // TODO: something more than a stub/dummy
112: return this.rawString();
113: }
114: }
|