001: /*
002: Mdarad-Toolobox is a collection of tools for Architected RAD
003: (Rapid Application Development) based on an MDA approach.
004: The toolbox contains frameworks and generators for many environments
005: (JAVA, J2EE, Hibernate, .NET, C++, etc.) which allow to generate
006: applications from a design Model
007: Copyright (C) 2004-2005 Elapse Technologies Inc.
008:
009: This library is free software; you can redistribute it and/or
010: modify it under the terms of the GNU General Public
011: License as published by the Free Software Foundation; either
012: version 2.1 of the License, or (at your option) any later version.
013:
014: This library is distributed in the hope that it will be useful,
015: but WITHOUT ANY WARRANTY; without even the implied warranty of
016: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: General Public License for more details.
018:
019: You should have received a copy of the GNU General Public
020: License along with this library; if not, write to the Free Software
021: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
022: */
023: package org.mdarad.framework.expr;
024:
025: import java.util.Collection;
026: import java.util.Hashtable;
027: import java.util.Map;
028:
029: /**
030: * This abstract class is used to translate criteria objects from the MDARAD framework
031: * to criteria objects of the persistence framework used.
032: *
033: * To be used, a class must be derived and the methods {@link getExpressionStatement()}
034: * and {@link getCriterionObject()} must be implemented.
035: *
036: * At this moment, the only supported translator is for the Hibernate framework
037: * (http://www.hibernate.org)
038: *
039: * @author Philippe Brouillette
040: * @version 1.0
041: */
042: public abstract class CriteriaTranslator {
043:
044: /**
045: * Constant needed to instanciate the Hibernate Translator
046: */
047: final static public String HIBERNATE_TRANSLATOR = "HIBERNATE_TRANSLATOR";
048:
049: /**
050: * Property that keeps the instances of the translators
051: */
052: private static Map instances;
053:
054: /**
055: * Default constructor
056: */
057: protected CriteriaTranslator() {
058: super ();
059: }
060:
061: /**
062: * Method that create an instance of the CriteriaTranslator selected by
063: * the <code>translatorType</code> argument.
064: * @param translatorType The translator type to be instanciated, must be one of
065: * the constants of this class
066: * @return an instance of the selected translator
067: * @throws InvalidTranslatorTypeException if the translator type doesn't exist
068: */
069: public static CriteriaTranslator getInstance(String translatorType)
070: throws InvalidTranslatorTypeException {
071:
072: if (translatorType == null) {
073: throw new InvalidTranslatorTypeException(
074: "The translator type should not be null");
075: }
076:
077: // create the map
078: if (instances == null) {
079: instances = new Hashtable();
080: }
081:
082: // get the instance from the cache
083: CriteriaTranslator translator = (CriteriaTranslator) instances
084: .get(translatorType);
085: // instanciate the translator if it is not cached
086: if (translator == null) {
087: if (translatorType.equals(HIBERNATE_TRANSLATOR)) {
088: translator = new HibernateCriteriaTranslator();
089: instances.put(translatorType, translator);
090: } else {
091: throw new InvalidTranslatorTypeException(
092: "The translator type " + translatorType
093: + " is not implemented");
094: }
095: }
096:
097: return translator;
098: }
099:
100: /**
101: * Method that returns the expression statement as a string. Usually, it will
102: * returns a SQL statement as a string.
103: * @param entity persistence type
104: * @param criteria collection of the criteria
105: * @return The string expression representing the statement (SQL, XML, etc)
106: */
107: public abstract String getExpressionStatement(Class entity,
108: Collection criteria);
109:
110: /**
111: * Method that returns the expression statement as an object. Usually, it will
112: * returns a SQL statement as a object from the persistence framework. As an example,
113: * the object returned for the Hibernate framework is a DetachedCriteria object.
114: * @param entity persistence type
115: * @param criteria collection of the criteria
116: * @return Return the expression statement as an object.
117: */
118: public abstract Object getCriterionObject(Class entity,
119: Collection criteria);
120:
121: }
|