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