KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mdarad > framework > expr > HibernateCriteriaTranslator


1 /*
2     Mdarad-Toolobox is a collection of tools for Architected RAD
3     (Rapid Application Development) based on an MDA approach.
4     The toolbox contains frameworks and generators for many environments
5     (JAVA, J2EE, Hibernate, .NET, C++, etc.) which allow to generate
6     applications from a design Model
7     Copyright (C) 2004-2005 Elapse Technologies Inc.
8
9     This library is free software; you can redistribute it and/or
10     modify it under the terms of the GNU General Public
11     License as published by the Free Software Foundation; either
12     version 2.1 of the License, or (at your option) any later version.
13
14     This library is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17     General Public License for more details.
18
19     You should have received a copy of the GNU General Public
20     License along with this library; if not, write to the Free Software
21     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */

23 package org.mdarad.framework.expr;
24
25 import java.util.Collection JavaDoc;
26 import java.util.Iterator JavaDoc;
27
28 import org.hibernate.criterion.DetachedCriteria;
29 import org.hibernate.criterion.Property;
30 import org.apache.commons.lang.StringUtils;
31
32 /**
33  * This class is used to translate criteria objects from the MDARAD framework
34  * to criteria objects from the Hibernate persistence framework
35  * (http://www.hibernate.org). To use this translator, instanciated from
36  * the class {@link CriteriaTranslator}
37  *
38  * @author Philippe Brouillette
39  * @version 1.0
40  */

41 public class HibernateCriteriaTranslator extends CriteriaTranslator {
42
43     /**
44      * Method that returns the expression statement as a string. Usually, it will
45      * returns a SQL statement as a string.
46      *
47      * @param entity persistence type
48      * @param criteria collection of the criteria
49      * @return The string expression representing the statement (SQL, XML, etc)
50      */

51     public String JavaDoc getExpressionStatement(Class JavaDoc entity, Collection JavaDoc criteria) {
52
53         String JavaDoc className = entity.getName().substring(entity.getName().lastIndexOf('.') + 1, entity.getName().length());
54         String JavaDoc lcClassName = StringUtils.uncapitalise(className);
55
56         //FROM CLAUSE
57
StringBuffer JavaDoc hqlStatement = new StringBuffer JavaDoc("from ").append(className).append(" as ").append(lcClassName).append("_alias");
58
59         //If one criterion is of a different entity, there is a need for a localized class join
60
Criterion localizedCriterion = getFirstLocalizedCriterion(criteria, entity);
61         if (localizedCriterion != null) {
62             String JavaDoc criterionClassName = localizedCriterion.getEntity().getName().substring(localizedCriterion.getEntity().getName().lastIndexOf('.') + 1, localizedCriterion.getEntity().getName().length());
63             String JavaDoc criterionLCClassName = StringUtils.uncapitalise(criterionClassName);
64             hqlStatement.append(" join ").append(lcClassName).append("_alias").append(".");
65             hqlStatement.append(lcClassName).append("Localizations as ").append(criterionLCClassName);
66         }
67                 
68         //WHERE CLAUSE
69
Iterator JavaDoc i = criteria.iterator();
70         if (i.hasNext()) {
71             hqlStatement.append(" where ");
72         }
73         while (i.hasNext()) {
74             Criterion criterion = (Criterion) i.next();
75             String JavaDoc criterionClassName = className;
76             String JavaDoc criterionLCClassName = lcClassName + "_alias";
77             
78             //Only use a different class name if the entity is not derived from the criterion's entity class
79
try {
80                 if(!criterion.getEntity().isInstance(entity.newInstance())) {
81                     criterionClassName = criterion.getEntity().getName().substring(criterion.getEntity().getName().lastIndexOf('.') + 1, criterion.getEntity().getName().length());
82                     criterionLCClassName = StringUtils.uncapitalise(criterionClassName);
83                 }
84             } catch (InstantiationException JavaDoc e) {
85                 throw new IllegalArgumentException JavaDoc();
86             } catch (IllegalAccessException JavaDoc e) {
87                 throw new IllegalArgumentException JavaDoc();
88             }
89             
90             if (criterion != null
91                     && criterion.getValue() != null
92                     && criterion.getValue().toString() != "") {
93             }
94             OperatorType operator = criterion.getOperator();
95             String JavaDoc property = criterion.getProperty();
96
97             // add the operator for each type
98
// equal
99
if (operator.equals(OperatorTypes.EQUAL)) {
100                 hqlStatement.append(criterionLCClassName).append(".").append(property).append(" = ").append(criterion.getValue());
101                 // greater than
102
} else if (operator.equals(OperatorTypes.GREATER_THAN)) {
103                 hqlStatement.append(criterionLCClassName).append(".").append(property).append(" > ").append(criterion.getValue());
104                 // greater than or equal
105
} else if (operator.equals(OperatorTypes.GREATER_THAN_OR_EQUAL)) {
106                 hqlStatement.append(criterionLCClassName).append(".").append(property).append(" >= ").append(criterion.getValue());
107                 // lower than
108
} else if (operator.equals(OperatorTypes.LOWER_THAN)) {
109                 hqlStatement.append(criterionLCClassName).append(".").append(property).append(" < ").append(criterion.getValue());
110                 // lower thn or equal
111
} else if (operator.equals(OperatorTypes.LOWER_THAN_OR_EQUAL)) {
112                 hqlStatement.append(criterionLCClassName).append(".").append(property).append(" <= ").append(criterion.getValue());
113                 // in operator
114
} else if (operator.equals(OperatorTypes.IN)) {
115                 hqlStatement.append(criterionLCClassName).append(".").append(property).append(" in '").append(criterion.getValue()).append("'");
116                 // like operator
117
} else if (operator.equals(OperatorTypes.LIKE)) {
118                 hqlStatement.append(criterionLCClassName).append(".").append(property).append(" like '").append(criterion.getValue()).append("'");
119                 // start with (like) operator
120
} else if (operator.equals(OperatorTypes.START_WITH)) {
121                 hqlStatement.append(criterionLCClassName).append(".").append(property).append(" like '").append(criterion.getValue()).append("%'");
122                 // end with (like)operator
123
} else if (operator.equals(OperatorTypes.END_WITH)) {
124                 hqlStatement.append(criterionLCClassName).append(".").append(property).append(" like '%").append(criterion.getValue()).append("'");
125             }
126
127             //Check if it is the last of the list
128
if (i.hasNext()) {
129                 hqlStatement.append(" and ");
130             }
131         }
132
133         //If one criterion is of a different entity, we need to set the index of the locale of the localized entity
134
if (localizedCriterion != null) {
135             String JavaDoc criterionClassName = localizedCriterion.getEntity().getName().substring(localizedCriterion.getEntity().getName().lastIndexOf('.') + 1, localizedCriterion.getEntity().getName().length());
136             String JavaDoc criterionLCClassName = StringUtils.uncapitalise(criterionClassName);
137             hqlStatement.append(" and index(").append(criterionLCClassName).append(") ='").append(localizedCriterion.getLocale()).append("' ");
138         }
139
140         return hqlStatement.toString();
141     }
142
143     private Criterion getFirstLocalizedCriterion(Collection JavaDoc criteria, Class JavaDoc entity) {
144         Iterator JavaDoc i = criteria.iterator();
145         while (i.hasNext()) {
146             Criterion criterion = (Criterion) i.next();
147
148             //Check if this criterion is on an associated class (if so it is for localization)
149
try {
150                 if (!criterion.getEntity().isInstance(entity.newInstance())) {
151                     return criterion;
152                 }
153             } catch (InstantiationException JavaDoc e) {
154                 throw new IllegalArgumentException JavaDoc();
155             } catch (IllegalAccessException JavaDoc e) {
156                 throw new IllegalArgumentException JavaDoc();
157             }
158         }
159         return null;
160     }
161
162
163     /**
164      * @deprecated Do not use for now. The criterion of hibernate are not yet ready for this
165      *
166      * Method that returns the expression statement as an object. Usually, it will
167      * returns a SQL statement as a object from the persistence framework. This method returns
168      * DetachedCriteria object from the Hibernate Framework.
169      *
170      * @param entity persistence type
171      * @param criteria collection of the criteria
172      * @return Return the expression statement as an object.
173      */

174     public Object JavaDoc getCriterionObject(Class JavaDoc entity, Collection JavaDoc criteria) {
175
176         DetachedCriteria query = DetachedCriteria.forClass(entity);
177         
178         // loop for all the criteria in the collection
179
for (Iterator JavaDoc i = criteria.iterator(); i.hasNext();) {
180             Criterion criterion = (Criterion) i.next();
181
182             if (criterion != null
183                     && criterion.getValue() != null
184                     && criterion.getValue().toString() != "") {
185                 OperatorType operator = criterion.getOperator();
186                 Property property = Property.forName(criterion.getProperty());
187                 Object JavaDoc value = criterion.getValue();
188                 // add the operator for each type
189
// equal
190
if (operator.equals(OperatorTypes.EQUAL)) {
191                     query.add(property.eq(value));
192                     // greater than
193
} else if (operator.equals(OperatorTypes.GREATER_THAN)) {
194                     query.add(property.gt(value));
195                     // greater than or equal
196
} else if (operator.equals(OperatorTypes.GREATER_THAN_OR_EQUAL)) {
197                     query.add(property.ge(value));
198                     // lower than
199
} else if (operator.equals(OperatorTypes.LOWER_THAN)) {
200                     query.add(property.lt(value));
201                     // lower thn or equal
202
} else if (operator.equals(OperatorTypes.LOWER_THAN_OR_EQUAL)) {
203                     query.add(property.le(value));
204                     // in operator
205
} else if (operator.equals(OperatorTypes.IN)) {
206                     query.add(property.in((Object JavaDoc[]) value));
207                     // like operator
208
} else if (operator.equals(OperatorTypes.LIKE)) {
209                     query.add(property.like(value));
210                     // start with (like) operator
211
} else if (operator.equals(OperatorTypes.START_WITH)) {
212                     query.add(property.like(value + "%"));
213                     // end with (like)operator
214
} else if (operator.equals(OperatorTypes.END_WITH)) {
215                     query.add(property.like("%" + value));
216                 }
217             }
218         }
219
220         return query;
221     }
222 }
223
Popular Tags