KickJava   Java API By Example, From Geeks To Geeks.

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


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.Hashtable JavaDoc;
27 import java.util.Map JavaDoc;
28
29 /**
30  * This abstract class is used to translate criteria objects from the MDARAD framework
31  * to criteria objects of the persistence framework used.
32  *
33  * To be used, a class must be derived and the methods {@link getExpressionStatement()}
34  * and {@link getCriterionObject()} must be implemented.
35  *
36  * At this moment, the only supported translator is for the Hibernate framework
37  * (http://www.hibernate.org)
38  *
39  * @author Philippe Brouillette
40  * @version 1.0
41  */

42 public abstract class CriteriaTranslator {
43
44     /**
45      * Constant needed to instanciate the Hibernate Translator
46      */

47     final static public String JavaDoc HIBERNATE_TRANSLATOR = "HIBERNATE_TRANSLATOR";
48
49     /**
50      * Property that keeps the instances of the translators
51      */

52     private static Map JavaDoc instances;
53
54     /**
55      * Default constructor
56      */

57     protected CriteriaTranslator() {
58         super();
59     }
60
61     /**
62      * Method that create an instance of the CriteriaTranslator selected by
63      * the <code>translatorType</code> argument.
64      * @param translatorType The translator type to be instanciated, must be one of
65      * the constants of this class
66      * @return an instance of the selected translator
67      * @throws InvalidTranslatorTypeException if the translator type doesn't exist
68      */

69     public static CriteriaTranslator getInstance(String JavaDoc translatorType) throws InvalidTranslatorTypeException {
70         
71         if (translatorType == null) {
72             throw new InvalidTranslatorTypeException("The translator type should not be null");
73         }
74         
75         // create the map
76
if (instances == null) {
77             instances = new Hashtable JavaDoc();
78         }
79
80         // get the instance from the cache
81
CriteriaTranslator translator = (CriteriaTranslator) instances.get(translatorType);
82         // instanciate the translator if it is not cached
83
if (translator == null) {
84             if (translatorType.equals(HIBERNATE_TRANSLATOR)) {
85                 translator = new HibernateCriteriaTranslator();
86                 instances.put(translatorType, translator);
87             }
88             else {
89                 throw new InvalidTranslatorTypeException("The translator type " + translatorType + " is not implemented");
90             }
91         }
92         
93         return translator;
94     }
95
96     /**
97      * Method that returns the expression statement as a string. Usually, it will
98      * returns a SQL statement as a string.
99      * @param entity persistence type
100      * @param criteria collection of the criteria
101      * @return The string expression representing the statement (SQL, XML, etc)
102      */

103     public abstract String JavaDoc getExpressionStatement(Class JavaDoc entity, Collection JavaDoc criteria);
104     
105     /**
106      * Method that returns the expression statement as an object. Usually, it will
107      * returns a SQL statement as a object from the persistence framework. As an example,
108      * the object returned for the Hibernate framework is a DetachedCriteria object.
109      * @param entity persistence type
110      * @param criteria collection of the criteria
111      * @return Return the expression statement as an object.
112      */

113     public abstract Object JavaDoc getCriterionObject(Class JavaDoc entity, Collection JavaDoc criteria);
114     
115 }
116
Popular Tags