KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: EntityCount.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2004 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.Map JavaDoc;
27
28 import org.ofbiz.base.util.Debug;
29 import org.ofbiz.base.util.GeneralException;
30 import org.ofbiz.base.util.UtilXml;
31 import org.ofbiz.base.util.collections.FlexibleMapAccessor;
32 import org.ofbiz.base.util.string.FlexibleStringExpander;
33 import org.ofbiz.entity.GenericDelegator;
34 import org.ofbiz.entity.condition.EntityCondition;
35 import org.ofbiz.entity.finder.EntityFinderUtil.Condition;
36 import org.ofbiz.entity.finder.EntityFinderUtil.ConditionExpr;
37 import org.ofbiz.entity.finder.EntityFinderUtil.ConditionList;
38 import org.ofbiz.entity.finder.EntityFinderUtil.ConditionObject;
39 import org.ofbiz.minilang.SimpleMethod;
40 import org.ofbiz.minilang.method.MethodContext;
41 import org.ofbiz.minilang.method.MethodOperation;
42 import org.w3c.dom.Element JavaDoc;
43
44 /**
45  * Uses the delegator to find entity values by a condition
46  *
47  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
48  * @version $Rev: 5462 $
49  * @since 3.1
50  */

51 public class EntityCount extends MethodOperation {
52     
53     public static final String JavaDoc module = EntityCount.class.getName();
54     
55     protected FlexibleStringExpander entityNameExdr;
56     protected FlexibleStringExpander delegatorNameExdr;
57     protected Condition whereCondition;
58     protected Condition havingCondition;
59     protected FlexibleMapAccessor countAcsr;
60
61     public EntityCount(Element JavaDoc element, SimpleMethod simpleMethod) {
62         super(element, simpleMethod);
63         this.entityNameExdr = new FlexibleStringExpander(element.getAttribute("entity-name"));
64         this.delegatorNameExdr = new FlexibleStringExpander(element.getAttribute("delegator-name"));
65         this.countAcsr = new FlexibleMapAccessor(element.getAttribute("count-name"));
66         
67         // process condition-expr | condition-list
68
Element JavaDoc conditionExprElement = UtilXml.firstChildElement(element, "condition-expr");
69         Element JavaDoc conditionListElement = UtilXml.firstChildElement(element, "condition-list");
70         Element JavaDoc conditionObjectElement = UtilXml.firstChildElement(element, "condition-object");
71         if (conditionExprElement != null && conditionListElement != null) {
72             throw new IllegalArgumentException JavaDoc("In entity find by condition element, cannot have condition-expr and condition-list sub-elements");
73         }
74         if (conditionExprElement != null) {
75             this.whereCondition = new ConditionExpr(conditionExprElement);
76         } else if (conditionListElement != null) {
77             this.whereCondition = new ConditionList(conditionListElement);
78         } else if (conditionObjectElement != null) {
79             this.whereCondition = new ConditionObject(conditionObjectElement);
80         }
81         
82         // process having-condition-list
83
Element JavaDoc havingConditionListElement = UtilXml.firstChildElement(element, "having-condition-list");
84         if (havingConditionListElement != null) {
85             this.havingCondition = new ConditionList(havingConditionListElement);
86         }
87     }
88
89     public boolean exec(MethodContext methodContext) {
90         try {
91             Map JavaDoc context = methodContext.getEnvMap();
92             GenericDelegator delegator = methodContext.getDelegator();
93             String JavaDoc entityName = this.entityNameExdr.expandString(context);
94             String JavaDoc delegatorName = this.delegatorNameExdr.expandString(context);
95             
96             if (delegatorName != null && delegatorName.length() > 0) {
97                 delegator = GenericDelegator.getGenericDelegator(delegatorName);
98             }
99
100             // create whereEntityCondition from whereCondition
101
EntityCondition whereEntityCondition = null;
102             if (this.whereCondition != null) {
103                 whereEntityCondition = this.whereCondition.createCondition(context, entityName, delegator);
104             }
105
106             // create havingEntityCondition from havingCondition
107
EntityCondition havingEntityCondition = null;
108             if (this.havingCondition != null) {
109                 havingEntityCondition = this.havingCondition.createCondition(context, entityName, delegator);
110             }
111             
112             long count = delegator.findCountByCondition(entityName, whereEntityCondition, havingEntityCondition);
113             
114             this.countAcsr.put(context, new Long JavaDoc(count));
115         } catch (GeneralException e) {
116             Debug.logError(e, module);
117             String JavaDoc errMsg = "ERROR: Could not complete the " + simpleMethod.getShortDescription() + " process: " + e.getMessage();
118
119             if (methodContext.getMethodType() == MethodContext.EVENT) {
120                 methodContext.putEnv(simpleMethod.getEventErrorMessageName(), errMsg);
121                 methodContext.putEnv(simpleMethod.getEventResponseCodeName(), simpleMethod.getDefaultErrorCode());
122             } else if (methodContext.getMethodType() == MethodContext.SERVICE) {
123                 methodContext.putEnv(simpleMethod.getServiceErrorMessageName(), errMsg);
124                 methodContext.putEnv(simpleMethod.getServiceResponseMessageName(), simpleMethod.getDefaultErrorCode());
125             }
126             return false;
127         }
128         return true;
129     }
130
131     public String JavaDoc rawString() {
132         // TODO: something more than the empty tag
133
return "<entity-count/>";
134     }
135     public String JavaDoc expandedString(MethodContext methodContext) {
136         // TODO: something more than a stub/dummy
137
return this.rawString();
138     }
139 }
140
141
Popular Tags