KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > entity > finder > ByConditionFinder


1 /*
2  * $Id: ByConditionFinder.java 6197 2005-11-26 06:07:08Z jonesde $
3  *
4  * Copyright (c) 2004-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.entity.finder;
25
26 import java.io.Serializable JavaDoc;
27 import java.sql.ResultSet JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.LinkedList JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.Set JavaDoc;
33
34 import org.ofbiz.base.util.Debug;
35 import org.ofbiz.base.util.GeneralException;
36 import org.ofbiz.base.util.UtilXml;
37 import org.ofbiz.base.util.collections.FlexibleMapAccessor;
38 import org.ofbiz.base.util.string.FlexibleStringExpander;
39 import org.ofbiz.base.util.UtilMisc;
40 import org.ofbiz.entity.GenericDelegator;
41 import org.ofbiz.entity.GenericEntityException;
42 import org.ofbiz.entity.condition.EntityCondition;
43 import org.ofbiz.entity.condition.EntityConditionList;
44 import org.ofbiz.entity.condition.EntityJoinOperator;
45 import org.ofbiz.entity.finder.EntityFinderUtil.Condition;
46 import org.ofbiz.entity.finder.EntityFinderUtil.ConditionExpr;
47 import org.ofbiz.entity.finder.EntityFinderUtil.ConditionList;
48 import org.ofbiz.entity.finder.EntityFinderUtil.ConditionObject;
49 import org.ofbiz.entity.finder.EntityFinderUtil.GetAll;
50 import org.ofbiz.entity.finder.EntityFinderUtil.LimitRange;
51 import org.ofbiz.entity.finder.EntityFinderUtil.LimitView;
52 import org.ofbiz.entity.finder.EntityFinderUtil.OutputHandler;
53 import org.ofbiz.entity.finder.EntityFinderUtil.UseIterator;
54 import org.ofbiz.entity.transaction.TransactionUtil;
55 import org.ofbiz.entity.util.EntityFindOptions;
56 import org.ofbiz.entity.util.EntityListIterator;
57 import org.ofbiz.entity.util.EntityUtil;
58 import org.w3c.dom.Element JavaDoc;
59
60 /**
61  * Uses the delegator to find entity values by a condition
62  *
63  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
64  * @version $Rev: 6197 $
65  * @since 3.1
66  */

67 public class ByConditionFinder implements Serializable JavaDoc {
68     public static final String JavaDoc module = ByConditionFinder.class.getName();
69     
70     protected FlexibleStringExpander entityNameExdr;
71     protected FlexibleStringExpander useCacheStrExdr;
72     protected FlexibleStringExpander filterByDateStrExdr;
73     protected FlexibleStringExpander distinctStrExdr;
74     protected FlexibleStringExpander delegatorNameExdr;
75     protected FlexibleMapAccessor listAcsr;
76     protected FlexibleStringExpander resultSetTypeExdr;
77     
78     protected Condition whereCondition;
79     protected Condition havingCondition;
80     protected List JavaDoc selectFieldExpanderList;
81     protected List JavaDoc orderByExpanderList;
82     protected OutputHandler outputHandler;
83
84     public ByConditionFinder(Element JavaDoc element) {
85         this.entityNameExdr = new FlexibleStringExpander(element.getAttribute("entity-name"));
86         this.useCacheStrExdr = new FlexibleStringExpander(element.getAttribute("use-cache"));
87         this.filterByDateStrExdr = new FlexibleStringExpander(element.getAttribute("filter-by-date"));
88         this.distinctStrExdr = new FlexibleStringExpander(element.getAttribute("distinct"));
89         this.delegatorNameExdr = new FlexibleStringExpander(element.getAttribute("delegator-name"));
90         this.listAcsr = new FlexibleMapAccessor(element.getAttribute("list-name"));
91         this.resultSetTypeExdr = new FlexibleStringExpander(element.getAttribute("result-set-type"));
92
93         // NOTE: the whereCondition can be null, ie (condition-expr | condition-list) is optional; if left out, means find all, or with no condition in essense
94
// process condition-expr | condition-list
95
Element JavaDoc conditionExprElement = UtilXml.firstChildElement(element, "condition-expr");
96         Element JavaDoc conditionListElement = UtilXml.firstChildElement(element, "condition-list");
97         Element JavaDoc conditionObjectElement = UtilXml.firstChildElement(element, "condition-object");
98         if (conditionExprElement != null) {
99             this.whereCondition = new ConditionExpr(conditionExprElement);
100         } else if (conditionListElement != null) {
101             this.whereCondition = new ConditionList(conditionListElement);
102         } else if (conditionObjectElement != null) {
103             this.whereCondition = new ConditionObject(conditionObjectElement);
104         }
105         
106         // process having-condition-list
107
Element JavaDoc havingConditionListElement = UtilXml.firstChildElement(element, "having-condition-list");
108         if (havingConditionListElement != null) {
109             this.havingCondition = new ConditionList(havingConditionListElement);
110         }
111
112         // process select-field
113
selectFieldExpanderList = EntityFinderUtil.makeSelectFieldExpanderList(element);
114         
115         // process order-by
116
List JavaDoc orderByElementList = UtilXml.childElementList(element, "order-by");
117         if (orderByElementList.size() > 0) {
118             orderByExpanderList = new LinkedList JavaDoc();
119             Iterator JavaDoc orderByElementIter = orderByElementList.iterator();
120             while (orderByElementIter.hasNext()) {
121                 Element JavaDoc orderByElement = (Element JavaDoc) orderByElementIter.next();
122                 orderByExpanderList.add(new FlexibleStringExpander(orderByElement.getAttribute("field-name")));
123             }
124         }
125
126         // process limit-range | limit-view | use-iterator
127
Element JavaDoc limitRangeElement = UtilXml.firstChildElement(element, "limit-range");
128         Element JavaDoc limitViewElement = UtilXml.firstChildElement(element, "limit-view");
129         Element JavaDoc useIteratorElement = UtilXml.firstChildElement(element, "use-iterator");
130         if ((limitRangeElement != null && limitViewElement != null) || (limitRangeElement != null && useIteratorElement != null) || (limitViewElement != null && useIteratorElement != null)) {
131             throw new IllegalArgumentException JavaDoc("In entity find by condition element, cannot have more than one of the following: limit-range, limit-view, and use-iterator");
132         }
133         if (limitRangeElement != null) {
134             outputHandler = new LimitRange(limitRangeElement);
135         } else if (limitViewElement != null) {
136             outputHandler = new LimitView(limitViewElement);
137         } else if (useIteratorElement != null) {
138             outputHandler = new UseIterator(useIteratorElement);
139         } else {
140             // default to get all
141
outputHandler = new GetAll();
142         }
143     }
144
145     public void runFind(Map JavaDoc context, GenericDelegator delegator) throws GeneralException {
146         String JavaDoc entityName = this.entityNameExdr.expandString(context);
147         String JavaDoc useCacheStr = this.useCacheStrExdr.expandString(context);
148         String JavaDoc filterByDateStr = this.filterByDateStrExdr.expandString(context);
149         String JavaDoc distinctStr = this.distinctStrExdr.expandString(context);
150         String JavaDoc delegatorName = this.delegatorNameExdr.expandString(context);
151         String JavaDoc resultSetTypeString = this.resultSetTypeExdr.expandString(context);
152         
153         boolean useCache = "true".equals(useCacheStr);
154         boolean filterByDate = "true".equals(filterByDateStr);
155         boolean distinct = "true".equals(distinctStr);
156         int resultSetType = ResultSet.TYPE_SCROLL_INSENSITIVE;
157         if ("forward".equals(resultSetTypeString))
158             resultSetType = ResultSet.TYPE_FORWARD_ONLY;
159         
160         if (delegatorName != null && delegatorName.length() > 0) {
161             delegator = GenericDelegator.getGenericDelegator(delegatorName);
162         }
163
164         // create whereEntityCondition from whereCondition
165
EntityCondition whereEntityCondition = null;
166         if (this.whereCondition != null) {
167             whereEntityCondition = this.whereCondition.createCondition(context, entityName, delegator);
168         }
169
170         // create havingEntityCondition from havingCondition
171
EntityCondition havingEntityCondition = null;
172         if (this.havingCondition != null) {
173             havingEntityCondition = this.havingCondition.createCondition(context, entityName, delegator);
174         }
175
176         if (useCache) {
177             // if useCache == true && outputHandler instanceof UseIterator, throw exception; not a valid combination
178
if (outputHandler instanceof UseIterator) {
179                 throw new IllegalArgumentException JavaDoc("In find entity by condition cannot have use-cache set to true and select use-iterator for the output type.");
180             }
181             if (distinct) {
182                 throw new IllegalArgumentException JavaDoc("In find entity by condition cannot have use-cache set to true and set distinct to true.");
183             }
184             if (havingEntityCondition != null) {
185                 throw new IllegalArgumentException JavaDoc("In find entity by condition cannot have use-cache set to true and specify a having-condition-list (can only use a where condition with condition-expr or condition-list).");
186             }
187         }
188         
189         // get the list of fieldsToSelect from selectFieldExpanderList
190
Set JavaDoc fieldsToSelect = EntityFinderUtil.makeFieldsToSelect(selectFieldExpanderList, context);
191
192         //if fieldsToSelect != null and useCacheBool is true, throw an error
193
if (fieldsToSelect != null && useCache) {
194             throw new IllegalArgumentException JavaDoc("Error in entity query by condition definition, cannot specify select-field elements when use-cache is set to true");
195         }
196         
197         // get the list of orderByFields from orderByExpanderList
198
List JavaDoc orderByFields = EntityFinderUtil.makeOrderByFieldList(this.orderByExpanderList, context);
199         
200         try {
201             // if filterByDate, do a date filter on the results based on the now-timestamp
202
if (filterByDate) {
203                 EntityCondition filterByDateCondition = EntityUtil.getFilterByDateExpr();
204                 if (whereEntityCondition != null) {
205                     whereEntityCondition = new EntityConditionList(UtilMisc.toList(whereEntityCondition, filterByDateCondition), EntityJoinOperator.AND);
206                 } else {
207                     whereEntityCondition = filterByDateCondition;
208                 }
209             }
210             
211             if (useCache) {
212                 List JavaDoc results = delegator.findByConditionCache(entityName, whereEntityCondition, fieldsToSelect, orderByFields);
213                 this.outputHandler.handleOutput(results, context, listAcsr);
214             } else {
215                 boolean useTransaction = true;
216                 if (this.outputHandler instanceof UseIterator && !TransactionUtil.isTransactionInPlace()) {
217                     Exception JavaDoc newE = new Exception JavaDoc("Stack Trace");
218                     Debug.logError(newE, "ERROR: Cannot do a by condition find that returns an EntityListIterator with no transaction in place. Wrap this call in a transaction.", module);
219                     useTransaction = false;
220                 }
221                 
222                 EntityFindOptions options = new EntityFindOptions();
223                 options.setDistinct(distinct);
224                 options.setResultSetType(resultSetType);
225                 boolean beganTransaction = false;
226                 try {
227                     if (useTransaction) {
228                         beganTransaction = TransactionUtil.begin();
229                     }
230
231                     EntityListIterator eli = delegator.findListIteratorByCondition(entityName, whereEntityCondition, havingEntityCondition, fieldsToSelect, orderByFields, options);
232                     this.outputHandler.handleOutput(eli, context, listAcsr);
233                 } catch (GenericEntityException e) {
234                     String JavaDoc errMsg = "Failure in by condition find operation, rolling back transaction";
235                     Debug.logError(e, errMsg, module);
236                     try {
237                         // only rollback the transaction if we started one...
238
TransactionUtil.rollback(beganTransaction, errMsg, e);
239                     } catch (GenericEntityException e2) {
240                         Debug.logError(e2, "Could not rollback transaction: " + e2.toString(), module);
241                     }
242                     // after rolling back, rethrow the exception
243
throw e;
244                 } finally {
245                     // only commit the transaction if we started one... this will throw an exception if it fails
246
TransactionUtil.commit(beganTransaction);
247                 }
248             }
249         } catch (GenericEntityException e) {
250             String JavaDoc errMsg = "Error doing find by condition: " + e.toString();
251             Debug.logError(e, module);
252             throw new GeneralException(errMsg, e);
253         }
254     }
255 }
256
257
Popular Tags