KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > repository > DjOql


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, is permitted provided that the following conditions are met: -
6  * Redistributions of source code must retain the above copyright notice, this
7  * list of conditions and the following disclaimer. - Redistributions in binary
8  * form must reproduce the above copyright notice, this list of conditions and
9  * the following disclaimer in the documentation and/or other materials
10  * provided with the distribution. - All advertising materials mentioning
11  * features or use of this software must display the following acknowledgment:
12  * "This product includes Djeneric." - Products derived from this software may
13  * not be called "Djeneric" nor may "Djeneric" appear in their names without
14  * prior written permission of Genimen BV. - Redistributions of any form
15  * whatsoever must retain the following acknowledgment: "This product includes
16  * Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG, OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.repository;
31
32 import java.math.BigDecimal JavaDoc;
33 import java.util.ArrayList JavaDoc;
34 import java.util.HashMap JavaDoc;
35 import java.util.Iterator JavaDoc;
36
37 import com.genimen.djeneric.language.Messages;
38 import com.genimen.djeneric.repository.exceptions.DjenericException;
39 import com.genimen.djeneric.repository.exceptions.ObjectNotDefinedException;
40 import com.genimen.djeneric.repository.oql.DjOqlWhereTranslator;
41 import com.genimen.djeneric.repository.oql.core.MatchException;
42 import com.genimen.djeneric.repository.oql.core.ParseException;
43 import com.genimen.djeneric.repository.oql.core.SimpleNode;
44 import com.genimen.djeneric.repository.oql.core.nodes.MatchingContext;
45
46 public class DjOql implements DjObjectMatcher
47 {
48   DjExtent _extent;
49   String JavaDoc _alias;
50   ArrayList JavaDoc _parameterNames = null;
51   HashMap JavaDoc _parameters = new HashMap JavaDoc();
52   String JavaDoc _sqlFromWhere;
53   SimpleNode _matchTree = null;
54   DjOqlWhereTranslator _xltr = null;
55   ArrayList JavaDoc _baseRestrictions = new ArrayList JavaDoc();
56   String JavaDoc _expression = null;
57   boolean _dirty = false;
58   boolean _resultIsUnique = true;
59
60   public DjOql(DjExtent extent)
61   {
62     _extent = extent;
63
64     String JavaDoc alias = _extent.getAlias().toLowerCase();
65     if (alias.length() > 4) alias = alias.substring(0, 4);
66     alias += "0";
67
68     setAlias(alias);
69   }
70
71   public DjOql(DjExtent extent, String JavaDoc whereExpression)
72   {
73     this(extent);
74     setWhereExpression(whereExpression);
75   }
76
77   public void clearBaseRestrictions()
78   {
79     _baseRestrictions = new ArrayList JavaDoc();
80     _dirty = true;
81   }
82
83   public void addBaseRestriction(String JavaDoc restriction)
84   {
85     if (restriction != null && restriction.trim().length() > 0)
86     {
87       _baseRestrictions.add(restriction);
88       _dirty = true;
89     }
90   }
91
92   public void setWhereExpression(String JavaDoc expression)
93   {
94     _expression = expression;
95     _dirty = true;
96   }
97
98   public String JavaDoc getWhereExpression()
99   {
100     return _expression;
101   }
102   
103   private void rebuildExpressionTree() throws ParseException
104   {
105     String JavaDoc completeExpr = "";
106
107     Iterator JavaDoc it = _baseRestrictions.iterator();
108     while (it.hasNext())
109     {
110       completeExpr = addAnd(completeExpr);
111       completeExpr += "(" + it.next().toString() + ")";
112     }
113
114     if (_expression != null && _expression.trim().length() > 0)
115     {
116       completeExpr = addAnd(completeExpr);
117       completeExpr += "(" + _expression + ")";
118     }
119
120     if (completeExpr.trim().length() == 0)
121     {
122       _xltr = null;
123       _sqlFromWhere = getFromOnly(_extent);
124     }
125     else
126     {
127       _xltr = new DjOqlWhereTranslator();
128       try
129       {
130         _sqlFromWhere = _xltr.translateWhere(_extent, _alias, completeExpr);
131         _resultIsUnique = _xltr.isResultUnique();
132       }
133       catch (ParseException pe)
134       {
135         pe.setAdditionalInfo(completeExpr);
136         throw pe;
137       }
138       _parameterNames = _xltr.getParameters();
139       _matchTree = _xltr.getTree();
140     }
141
142     removeUnusedParameters();
143
144     _dirty = false;
145
146   }
147
148   protected void removeUnusedParameters()
149   {
150     ArrayList JavaDoc removeThese = new ArrayList JavaDoc();
151     Iterator JavaDoc it = _parameters.keySet().iterator();
152     while (it.hasNext())
153     {
154       String JavaDoc name = it.next().toString();
155       if (!_parameterNames.contains(name)) removeThese.add(name);
156     }
157
158     it = removeThese.iterator();
159     while (it.hasNext())
160       _parameters.remove(it.next());
161   }
162
163   private String JavaDoc addAnd(String JavaDoc completeExpr)
164   {
165     if (completeExpr.trim().length() > 0) completeExpr += " && ";
166     return completeExpr;
167   }
168
169   private String JavaDoc getFromOnly(DjExtent extent)
170   {
171     return "from " + getExtent().getName() + " " + getAlias();
172   }
173
174   public void setBaseClassExtent(DjExtent extent)
175   {
176     _extent = extent;
177     if (_xltr == null) _dirty = true;
178   }
179
180   public String JavaDoc getFromWhereClause() throws ParseException
181   {
182     if (!_dirty && _xltr != null)
183     {
184       // extent might have changed so do a quick translate again
185
// and not a simple string cache
186
_sqlFromWhere = _xltr.translateAgain(_extent);
187     }
188     else if (_dirty) rebuildExpressionTree();
189
190     return _sqlFromWhere;
191   }
192
193   public boolean match(DjObject po) throws DjenericException
194   {
195     try
196     {
197       if (_dirty) rebuildExpressionTree();
198     }
199     catch (ParseException e1)
200     {
201       throw new DjenericException(e1);
202     }
203
204     if (_matchTree == null) return true;
205
206     MatchingContext ctxt = new MatchingContext();
207     ctxt.setObject(po);
208     ctxt.setParameters(_parameters);
209
210     try
211     {
212       return _matchTree.match(ctxt);
213     }
214     catch (MatchException e)
215     {
216       throw new DjenericException(e);
217     }
218   }
219
220   public String JavaDoc getAlias()
221   {
222     return _alias;
223   }
224
225   protected void setAlias(String JavaDoc string)
226   {
227     _alias = string.toLowerCase();
228   }
229
230   public void setParameter(String JavaDoc name, Object JavaDoc value)
231   {
232     if (value instanceof Boolean JavaDoc)
233     {
234       boolean b = ((Boolean JavaDoc) value).booleanValue();
235       value = new Integer JavaDoc(b ? 1 : 0);
236     }
237
238     _parameters.put(name, value);
239   }
240
241   public void setParameter(String JavaDoc name, int value)
242   {
243     setParameter(name, new Integer JavaDoc(value));
244   }
245
246   public void setParameter(String JavaDoc name, long value)
247   {
248     setParameter(name, new Long JavaDoc(value));
249   }
250
251   public void setParameter(String JavaDoc name, double value)
252   {
253     setParameter(name, new BigDecimal JavaDoc(value));
254   }
255
256   public void setParameter(String JavaDoc name, boolean value)
257   {
258     setParameter(name, new Boolean JavaDoc(value));
259   }
260
261   public HashMap JavaDoc getParameters()
262   {
263     return _parameters;
264   }
265
266   public void setParameters(HashMap JavaDoc parameters)
267   {
268     _parameters = parameters;
269   }
270
271   public DjExtent getExtent()
272   {
273     return _extent;
274   }
275
276   public void validateParameters() throws ObjectNotDefinedException, ParseException
277   {
278     if (_dirty) rebuildExpressionTree();
279     Iterator JavaDoc it = _parameters.keySet().iterator();
280
281     while (it.hasNext())
282     {
283       String JavaDoc name = it.next().toString();
284       if (!_parameterNames.contains(name)) throw new ObjectNotDefinedException(Messages
285           .getString("DjOql.ParameterUndefined", name));
286     }
287   }
288
289   public String JavaDoc[] getParameterNames() throws ParseException
290   {
291     if (_parameterNames == null) getFromWhereClause();
292     if (_parameterNames == null) return new String JavaDoc[0];
293     return (String JavaDoc[]) _parameterNames.toArray(new String JavaDoc[_parameterNames.size()]);
294   }
295
296   public boolean isResultUnique()
297   {
298     return _resultIsUnique;
299   }
300 }
Popular Tags