KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: FindByAnd.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.GenericDelegator;
31 import org.ofbiz.entity.GenericEntityException;
32 import org.ofbiz.entity.condition.EntityCondition;
33 import org.ofbiz.entity.condition.EntityFieldMap;
34 import org.ofbiz.entity.condition.EntityOperator;
35 import org.ofbiz.minilang.SimpleMethod;
36 import org.ofbiz.minilang.method.ContextAccessor;
37 import org.ofbiz.minilang.method.MethodContext;
38 import org.ofbiz.minilang.method.MethodOperation;
39 import org.w3c.dom.Element JavaDoc;
40
41 /**
42  * Uses the delegator to find entity values by anding the map fields
43  *
44  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
45  * @version $Rev: 5462 $
46  * @since 2.0
47  */

48 public class FindByAnd extends MethodOperation {
49     
50     public static final String JavaDoc module = FindByAnd.class.getName();
51     
52     ContextAccessor listAcsr;
53     String JavaDoc entityName;
54     ContextAccessor mapAcsr;
55     ContextAccessor orderByListAcsr;
56     String JavaDoc delegatorName;
57     String JavaDoc useCacheStr;
58     String JavaDoc useIteratorStr;
59
60     public FindByAnd(Element JavaDoc element, SimpleMethod simpleMethod) {
61         super(element, simpleMethod);
62         listAcsr = new ContextAccessor(element.getAttribute("list-name"));
63         entityName = element.getAttribute("entity-name");
64         mapAcsr = new ContextAccessor(element.getAttribute("map-name"));
65         orderByListAcsr = new ContextAccessor(element.getAttribute("order-by-list-name"));
66         delegatorName = element.getAttribute("delegator-name");
67
68         useCacheStr = element.getAttribute("use-cache");
69         useIteratorStr = element.getAttribute("use-iterator");
70     }
71
72     public boolean exec(MethodContext methodContext) {
73         String JavaDoc entityName = methodContext.expandString(this.entityName);
74         String JavaDoc delegatorName = methodContext.expandString(this.delegatorName);
75         String JavaDoc useCacheStr = methodContext.expandString(this.useCacheStr);
76         String JavaDoc useIteratorStr = methodContext.expandString(this.useIteratorStr);
77         
78         boolean useCache = "true".equals(useCacheStr);
79         boolean useIterator = "true".equals(useIteratorStr);
80         
81         List JavaDoc orderByNames = null;
82         if (!orderByListAcsr.isEmpty()) {
83             orderByNames = (List JavaDoc) orderByListAcsr.get(methodContext);
84         }
85
86         GenericDelegator delegator = methodContext.getDelegator();
87         if (delegatorName != null && delegatorName.length() > 0) {
88             delegator = GenericDelegator.getGenericDelegator(delegatorName);
89         }
90
91         try {
92             if (useIterator) {
93                 EntityCondition whereCond = null;
94                 if (!mapAcsr.isEmpty()) {
95                     whereCond = new EntityFieldMap((Map JavaDoc) mapAcsr.get(methodContext), EntityOperator.AND);
96                 }
97                 listAcsr.put(methodContext, delegator.findListIteratorByCondition(entityName, whereCond, null, null, orderByNames, null));
98             } else {
99                 if (useCache) {
100                     listAcsr.put(methodContext, delegator.findByAndCache(entityName, (Map JavaDoc) mapAcsr.get(methodContext), orderByNames));
101                 } else {
102                     listAcsr.put(methodContext, delegator.findByAnd(entityName, (Map JavaDoc) mapAcsr.get(methodContext), orderByNames));
103                 }
104             }
105         } catch (GenericEntityException e) {
106             Debug.logError(e, module);
107             String JavaDoc errMsg = "ERROR: Could not complete the " + simpleMethod.getShortDescription() + " process [problem finding the " + entityName + " entity: " + e.getMessage() + "]";
108
109             if (methodContext.getMethodType() == MethodContext.EVENT) {
110                 methodContext.putEnv(simpleMethod.getEventErrorMessageName(), errMsg);
111                 methodContext.putEnv(simpleMethod.getEventResponseCodeName(), simpleMethod.getDefaultErrorCode());
112             } else if (methodContext.getMethodType() == MethodContext.SERVICE) {
113                 methodContext.putEnv(simpleMethod.getServiceErrorMessageName(), errMsg);
114                 methodContext.putEnv(simpleMethod.getServiceResponseMessageName(), simpleMethod.getDefaultErrorCode());
115             }
116             return false;
117         }
118         return true;
119     }
120
121     public String JavaDoc rawString() {
122         // TODO: something more than the empty tag
123
return "<find-by-and/>";
124     }
125     public String JavaDoc expandedString(MethodContext methodContext) {
126         // TODO: something more than a stub/dummy
127
return this.rawString();
128     }
129 }
130
Popular Tags