001: /*
002: * $Id: GetRelatedOne.java,v 1.2 2003/09/14 05:40:41 jonesde 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: * Gets a list of related entity instance according to the specified relation-name
037: *
038: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
039: * @version $Revision: 1.2 $
040: * @since 2.0
041: */
042: public class GetRelatedOne extends MethodOperation {
043:
044: public static final String module = GetRelatedOne.class.getName();
045:
046: ContextAccessor valueAcsr;
047: ContextAccessor toValueAcsr;
048: String relationName;
049: String useCacheStr;
050:
051: public GetRelatedOne(Element element, SimpleMethod simpleMethod) {
052: super (element, simpleMethod);
053: valueAcsr = new ContextAccessor(element
054: .getAttribute("value-name"));
055: toValueAcsr = new ContextAccessor(element
056: .getAttribute("to-value-name"));
057: relationName = element.getAttribute("relation-name");
058: useCacheStr = element.getAttribute("use-cache");
059: }
060:
061: public boolean exec(MethodContext methodContext) {
062: String relationName = methodContext
063: .expandString(this .relationName);
064: String useCacheStr = methodContext
065: .expandString(this .useCacheStr);
066: boolean useCache = "true".equals(useCacheStr);
067:
068: Object valueObject = valueAcsr.get(methodContext);
069: if (!(valueObject instanceof GenericValue)) {
070: String errMsg = "ERROR: Could not complete the "
071: + simpleMethod.getShortDescription()
072: + " process [env variable for value-name "
073: + valueAcsr.toString()
074: + " is not a GenericValue object; for the relation-name: "
075: + relationName + "]";
076: Debug.logError(errMsg, module);
077: methodContext.setErrorReturn(errMsg, simpleMethod);
078: return false;
079: }
080: GenericValue value = (GenericValue) valueObject;
081: if (value == null) {
082: Debug.logWarning("Value not found with name: " + valueAcsr
083: + ", not getting related...", module);
084: return true;
085: }
086: try {
087: if (useCache) {
088: toValueAcsr.put(methodContext, value
089: .getRelatedOneCache(relationName));
090: } else {
091: toValueAcsr.put(methodContext, value
092: .getRelatedOne(relationName));
093: }
094: } catch (GenericEntityException e) {
095: String errMsg = "ERROR: Could not complete the "
096: + simpleMethod.getShortDescription()
097: + " process [problem getting related one from entity with name "
098: + value.getEntityName()
099: + " for the relation-name: " + relationName + ": "
100: + e.getMessage() + "]";
101: Debug.logError(e, errMsg, module);
102: methodContext.setErrorReturn(errMsg, simpleMethod);
103: return false;
104: }
105: return true;
106: }
107: }
|