KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > shark > expression > BaseEntityCondExprBldr


1 /*
2  * $Id: BaseEntityCondExprBldr.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  */

25 package org.ofbiz.shark.expression;
26
27 import java.io.Serializable JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33
34 import org.apache.commons.collections.map.LinkedMap;
35 import org.enhydra.shark.api.common.ExpressionBuilder;
36 import org.ofbiz.base.util.StringUtil;
37 import org.ofbiz.base.util.UtilMisc;
38 import org.ofbiz.base.util.UtilObject;
39 import org.ofbiz.entity.GenericDelegator;
40 import org.ofbiz.entity.GenericEntityException;
41 import org.ofbiz.entity.util.EntityListIterator;
42 import org.ofbiz.entity.condition.EntityCondition;
43 import org.ofbiz.entity.condition.EntityConditionList;
44 import org.ofbiz.entity.condition.EntityOperator;
45 import org.ofbiz.entity.model.DynamicViewEntity;
46 import org.ofbiz.shark.container.SharkContainer;
47
48 /**
49  *
50  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
51  * @version $Rev: 5462 $
52  * @since Apr 17, 2005
53  */

54 public abstract class BaseEntityCondExprBldr implements ExpressionBuilder, Serializable JavaDoc {
55
56     public static final String JavaDoc module = BaseEntityCondExprBldr.class.getName();
57
58     protected EntityCondition condition = null;
59     protected Map JavaDoc entityNames = new LinkedMap();
60     protected Map JavaDoc fieldNames = new LinkedMap();
61     protected List JavaDoc autoFields = new ArrayList JavaDoc();
62     protected List JavaDoc viewLinks = new ArrayList JavaDoc();
63
64     protected boolean isOrSet = false;
65     protected boolean isNotSet = false;
66     protected boolean isComplete = true;
67
68     // ExpressionBuilder Methods
69

70     public boolean isComplete() {
71         return this.isComplete;
72     }
73
74     public String JavaDoc toSQL() {
75         return BaseEntityCondExprBldr.getHexString(this);
76     }
77
78     public String JavaDoc toScript() {
79         return "";
80     }
81
82     public String JavaDoc toExpression() {
83         StringBuffer JavaDoc ret = new StringBuffer JavaDoc();
84         if (!this.isComplete()) {
85             ret.append("/*FORCE*/\n");
86         }
87
88         ret.append(this.toScript());
89         ret.append("\n/*sql ").append(this.toSQL()).append(" sql*/");
90
91         return ret.toString();
92     }
93
94     // Special Purpose Methods
95

96     public EntityCondition getCondition() {
97         return this.condition;
98     }
99
100     public List JavaDoc runQuery() throws GenericEntityException {
101         GenericDelegator delegator = this.getDelegator();
102         DynamicViewEntity view = this.makeView();
103         EntityListIterator eli = null;
104         List JavaDoc result = null;
105         try {
106             eli = delegator.findListIteratorByCondition(view, this.getCondition(), null, null, null, null);
107             result = eli.getCompleteList();
108         } catch (GenericEntityException e) {
109             throw e;
110         } finally {
111             eli.close();
112         }
113         return result;
114     }
115
116     protected void setNot(boolean not) {
117         this.isNotSet = not;
118     }
119
120     protected void setOr(boolean or) {
121         this.isOrSet = or;
122     }
123
124     protected void addCondition(EntityCondition current) {
125         if (condition == null) {
126             condition = current;
127         } else {
128             List JavaDoc condList = UtilMisc.toList(condition, current);
129             if (this.isOrSet) {
130                 condition = new EntityConditionList(condList, EntityOperator.OR);
131             } else {
132                 condition = new EntityConditionList(condList, EntityOperator.AND);
133             }
134         }
135         // reset the NOT value
136
this.setNot(false);
137     }
138
139     protected synchronized void addEntity(String JavaDoc alias, String JavaDoc entity) {
140         entityNames.put(alias, entity);
141     }
142
143     protected synchronized void addField(String JavaDoc entityAlias, String JavaDoc fieldName, String JavaDoc fieldAlias) {
144         Map JavaDoc fieldAliasMap = (Map JavaDoc) fieldNames.get(entityAlias);
145         if (fieldAliasMap == null) {
146             fieldAliasMap = new HashMap JavaDoc();
147             fieldNames.put(entityAlias, fieldAliasMap);
148         }
149         fieldAliasMap.put(fieldName, fieldAlias);
150     }
151
152     protected synchronized void addAllFields(String JavaDoc entityAlias) {
153         autoFields.add(entityAlias);
154     }
155
156     protected synchronized void addLink(String JavaDoc entityAlias, String JavaDoc relEntityAlias, boolean opt, List JavaDoc keyMap) {
157         this.viewLinks.add(new ViewLink(entityAlias, relEntityAlias, opt, keyMap));
158     }
159     
160     protected GenericDelegator getDelegator() {
161         return SharkContainer.getDelegator();
162     }
163
164     private DynamicViewEntity makeView() {
165         DynamicViewEntity view = new DynamicViewEntity();
166
167
168         // create the members
169
Iterator JavaDoc eni = this.entityNames.entrySet().iterator();
170         while (eni.hasNext()) {
171             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) eni.next();
172             view.addMemberEntity((String JavaDoc) entry.getKey(), (String JavaDoc) entry.getValue());
173         }
174
175         // set alias all fields
176
Iterator JavaDoc aai = autoFields.iterator();
177         while (aai.hasNext()) {
178             String JavaDoc alias = (String JavaDoc) aai.next();
179             view.addAliasAll(alias, "");
180         }
181
182         // create the other field aliases
183
Iterator JavaDoc fni = fieldNames.keySet().iterator();
184         while (fni.hasNext()) {
185             String JavaDoc entityAlias = (String JavaDoc) fni.next();
186             Map JavaDoc fieldMap = (Map JavaDoc) fieldNames.get(entityAlias);
187             Iterator JavaDoc fmi = fieldMap.entrySet().iterator();
188             while (fmi.hasNext()) {
189                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) fmi.next();
190                 view.addAlias(entityAlias, (String JavaDoc) entry.getValue(), (String JavaDoc) entry.getKey(), null, null, null, null);
191             }
192         }
193
194         // add the view links
195
Iterator JavaDoc vli = this.viewLinks.iterator();
196         while (vli.hasNext()) {
197             ViewLink vl = (ViewLink) vli.next();
198             view.addViewLink(vl.entityAlias, vl.relEntityAlias, new Boolean JavaDoc(vl.relOptional), vl.keyMaps);
199         }
200
201         return view;
202     }
203
204     // Standard Static Serialization Methods
205

206     public static String JavaDoc getHexString(BaseEntityCondExprBldr builder) {
207         byte[] builderBytes = UtilObject.getBytes(builder);
208         return StringUtil.toHexString(builderBytes);
209     }
210
211     public static BaseEntityCondExprBldr getBuilder(String JavaDoc hexString) {
212         byte[] builderBytes = StringUtil.fromHexString(hexString);
213         return (BaseEntityCondExprBldr) UtilObject.getObject(builderBytes);
214     }
215
216     class ViewLink implements Serializable JavaDoc {
217
218         public String JavaDoc entityAlias;
219         public String JavaDoc relEntityAlias;
220         public boolean relOptional = false;
221         public List JavaDoc keyMaps = new ArrayList JavaDoc();
222
223         public ViewLink(String JavaDoc entityAlias, String JavaDoc relEntityAlias, boolean optional, List JavaDoc keyMaps) {
224             this.entityAlias = entityAlias;
225             this.relEntityAlias = relEntityAlias;
226             this.relOptional = optional;
227             this.keyMaps = keyMaps;
228         }
229     }
230 }
231
Popular Tags