001: /*
002: * $Id: GetRelated.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 java.util.List;
027: import java.util.Map;
028:
029: import org.ofbiz.base.util.Debug;
030: import org.ofbiz.entity.GenericEntityException;
031: import org.ofbiz.entity.GenericValue;
032: import org.ofbiz.minilang.SimpleMethod;
033: import org.ofbiz.minilang.method.ContextAccessor;
034: import org.ofbiz.minilang.method.MethodContext;
035: import org.ofbiz.minilang.method.MethodOperation;
036: import org.w3c.dom.Element;
037:
038: /**
039: * Gets a list of related entity instance according to the specified relation-name
040: *
041: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
042: * @version $Revision: 1.1 $
043: * @since 2.0
044: */
045: public class GetRelated extends MethodOperation {
046:
047: public static final String module = GetRelated.class.getName();
048:
049: ContextAccessor valueAcsr;
050: ContextAccessor mapAcsr;
051: ContextAccessor orderByListAcsr;
052: String relationName;
053: String useCacheStr;
054: ContextAccessor listAcsr;
055:
056: public GetRelated(Element element, SimpleMethod simpleMethod) {
057: super (element, simpleMethod);
058: valueAcsr = new ContextAccessor(element
059: .getAttribute("value-name"));
060: relationName = element.getAttribute("relation-name");
061: listAcsr = new ContextAccessor(element
062: .getAttribute("list-name"));
063: mapAcsr = new ContextAccessor(element.getAttribute("map-name"));
064: orderByListAcsr = new ContextAccessor(element
065: .getAttribute("order-by-list-name"));
066:
067: useCacheStr = element.getAttribute("use-cache");
068: }
069:
070: public boolean exec(MethodContext methodContext) {
071: String relationName = methodContext
072: .expandString(this .relationName);
073: String useCacheStr = methodContext
074: .expandString(this .useCacheStr);
075: boolean useCache = "true".equals(useCacheStr);
076:
077: List orderByNames = null;
078: if (!orderByListAcsr.isEmpty()) {
079: orderByNames = (List) orderByListAcsr.get(methodContext);
080: }
081: Map constraintMap = null;
082: if (!mapAcsr.isEmpty()) {
083: constraintMap = (Map) mapAcsr.get(methodContext);
084: }
085:
086: GenericValue value = (GenericValue) valueAcsr
087: .get(methodContext);
088: if (value == null) {
089: Debug.logWarning("Value not found with name: " + valueAcsr
090: + ", not getting related...", module);
091: return true;
092: }
093: try {
094: if (useCache) {
095: listAcsr.put(methodContext, value.getRelatedCache(
096: relationName, constraintMap, orderByNames));
097: } else {
098: listAcsr.put(methodContext, value.getRelated(
099: relationName, constraintMap, orderByNames));
100: }
101: } catch (GenericEntityException e) {
102: String errMsg = "ERROR: Could not complete the "
103: + simpleMethod.getShortDescription()
104: + " process [problem getting related from entity with name "
105: + value.getEntityName()
106: + " for the relation-name: " + relationName + ": "
107: + e.getMessage() + "]";
108: Debug.logError(e, errMsg, module);
109: methodContext.setErrorReturn(errMsg, simpleMethod);
110: return false;
111: }
112: return true;
113: }
114: }
|