KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > minilang > method > entityops > GetRelated


1 /*
2  * $Id: GetRelated.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.minilang.method.entityops;
25
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import org.ofbiz.base.util.Debug;
30 import org.ofbiz.entity.GenericEntityException;
31 import org.ofbiz.entity.GenericValue;
32 import org.ofbiz.minilang.SimpleMethod;
33 import org.ofbiz.minilang.method.ContextAccessor;
34 import org.ofbiz.minilang.method.MethodContext;
35 import org.ofbiz.minilang.method.MethodOperation;
36 import org.w3c.dom.Element JavaDoc;
37
38 /**
39  * Gets a list of related entity instance according to the specified relation-name
40  *
41  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
42  * @version $Rev: 5462 $
43  * @since 2.0
44  */

45 public class GetRelated extends MethodOperation {
46     
47     public static final String JavaDoc module = GetRelated.class.getName();
48     
49     ContextAccessor valueAcsr;
50     ContextAccessor mapAcsr;
51     ContextAccessor orderByListAcsr;
52     String JavaDoc relationName;
53     String JavaDoc useCacheStr;
54     ContextAccessor listAcsr;
55
56     public GetRelated(Element JavaDoc element, SimpleMethod simpleMethod) {
57         super(element, simpleMethod);
58         valueAcsr = new ContextAccessor(element.getAttribute("value-name"));
59         relationName = element.getAttribute("relation-name");
60         listAcsr = new ContextAccessor(element.getAttribute("list-name"));
61         mapAcsr = new ContextAccessor(element.getAttribute("map-name"));
62         orderByListAcsr = new ContextAccessor(element.getAttribute("order-by-list-name"));
63
64         useCacheStr = element.getAttribute("use-cache");
65     }
66
67     public boolean exec(MethodContext methodContext) {
68         String JavaDoc relationName = methodContext.expandString(this.relationName);
69         String JavaDoc useCacheStr = methodContext.expandString(this.useCacheStr);
70         boolean useCache = "true".equals(useCacheStr);
71         
72         List JavaDoc orderByNames = null;
73         if (!orderByListAcsr.isEmpty()) {
74             orderByNames = (List JavaDoc) orderByListAcsr.get(methodContext);
75         }
76         Map JavaDoc constraintMap = null;
77         if (!mapAcsr.isEmpty()) {
78             constraintMap = (Map JavaDoc) mapAcsr.get(methodContext);
79         }
80
81         GenericValue value = (GenericValue) valueAcsr.get(methodContext);
82         if (value == null) {
83             Debug.logWarning("Value not found with name: " + valueAcsr + ", not getting related...", module);
84             return true;
85         }
86         try {
87             if (useCache) {
88                 listAcsr.put(methodContext, value.getRelatedCache(relationName, constraintMap, orderByNames));
89             } else {
90                 listAcsr.put(methodContext, value.getRelated(relationName, constraintMap, orderByNames));
91             }
92         } catch (GenericEntityException e) {
93             String JavaDoc errMsg = "ERROR: Could not complete the " + simpleMethod.getShortDescription() + " process [problem getting related from entity with name " + value.getEntityName() + " for the relation-name: " + relationName + ": " + e.getMessage() + "]";
94             Debug.logError(e, errMsg, module);
95             methodContext.setErrorReturn(errMsg, simpleMethod);
96             return false;
97         }
98         return true;
99     }
100
101     public String JavaDoc rawString() {
102         // TODO: something more than the empty tag
103
return "<get-related/>";
104     }
105     public String JavaDoc expandedString(MethodContext methodContext) {
106         // TODO: something more than a stub/dummy
107
return this.rawString();
108     }
109 }
110
Popular Tags