001package org.andromda.cartridges.spring;
002
003import java.util.ArrayList;
004import java.util.Collection;
005import java.util.List;
006import org.andromda.cartridges.spring.metafacades.SpringGlobals;
007import org.andromda.metafacades.uml.Entity;
008
009/**
010 * Contains utilities used within the Spring cartridge
011 * when dealing with Hibernate.
012 *
013 * @author Chad Brandon
014 * @author Joel Kozikowski
015 */
016public class SpringHibernateUtils
017{
018    /**
019     * The version of Hibernate we're generating for.
020     */
021    private String hibernateVersion = SpringGlobals.HIBERNATE_VERSION_3;
022
023    /**
024     * Sets the version of Hibernate we're generating for.
025     *
026     * @param hibernateVersionIn the Hibernate version.
027     */
028    public void setHibernateVersion(final String hibernateVersionIn)
029    {
030        this.hibernateVersion = hibernateVersionIn;
031    }
032
033    /**
034     * Gets the appropriate hibernate package name for the given
035     * <code>version</code>.
036     *
037     * @return the base package name.
038     */
039    public String getBasePackage()
040    {
041        return this.isVersion3() || this.isVersion4() ? "org.hibernate" : "net.sf.hibernate";
042    }
043
044    /**
045     * Gets the appropriate hibernate criterion package name for the given <code>version</code>.
046     *
047     * @return the Hibernate criterion package name.
048     */
049    public String getCriterionPackage()
050    {
051        return this.getBasePackage() + (this.isVersion3() || this.isVersion4() ? ".criterion" : ".expression");
052    }
053
054    /**
055     * Gets the appropriate hibernate criterion package name for the given <code>version</code>.
056     * @param entities 
057     * @return the Hibernate criterion package name.
058     */
059    public String getEntityBasePackage(Collection<Entity> entities)
060    {
061        String base = "";
062        List<String> packages = new ArrayList<String>();
063        // Get unique packages containing entities
064        for (Entity entity : entities)
065        {
066            String packageName = entity.getPackageName();
067            if (!packages.contains(packageName))
068            {
069                // TODO: Allow entities in vastly different packages
070                /*for (String pkgName : packages)
071                {
072                    if (packageName.length() < pkgName.length() &&
073                        pkgName.contains(packageName))
074                    {
075                        // replace the longer contained package name in the List
076                    }
077                }*/
078                packages.add(packageName);
079            }
080        }
081        // Get unique top level packages containing entities
082        /*for (String packageName : packages)
083        {
084
085        }*/
086        // TODO Implement this function. Right now it just returns "" always
087        return base;
088    }
089
090    /**
091     * Gets the appropriate hibernate Restrictions/Expression fully qualified class name for the given <code>version</code>.
092     *
093     * @return the fully qualified Hibernate Restriction/Expression class name.
094     */
095    public String getRestrictionClass()
096    {
097        return getCriterionPackage() + (this.isVersion3() || this.isVersion4() ? ".Restrictions" : ".Expression");
098    }
099
100    /**
101     * Gets the appropriate Spring Hibernate package based on the given
102     * <code>version</code>.
103     *
104     * @return the spring hibernate package.
105     */
106    public String getSpringHibernatePackage()
107    {
108        return this.isVersion3() ? "org.springframework.orm.hibernate3" : this.isVersion4() ? "org.springframework.orm.hibernate4" : "org.springframework.orm.hibernate";
109    }
110
111    /**
112     * Retrieves the appropriate package for Hibernate user types given
113     * the version defined within this class.
114     *
115     * @return the hibernate user type package.
116     */
117    public String getEagerFetchMode()
118    {
119        return this.isVersion3() || this.isVersion4() ? "JOIN" : "EAGER";
120    }
121
122    /**
123     * Retrieves the fully qualified name of the class that retrieves the Hibernate
124     * disjunction instance.
125     * @return the fully qualified class name.
126     */
127    public String getDisjunctionClassName()
128    {
129        return this.getCriterionPackage() + (this.isVersion3() || this.isVersion4() ? ".Restrictions" : ".Expression");
130    }
131
132    /**
133     * Indicates whether or not version 2 is the one that is currently being used.
134     *
135     * @return true/false
136     */
137    public boolean isVersion2()
138    {
139        return isVersion2(hibernateVersion);
140    }
141
142    /**
143     * Indicates whether or not version 3 is the one that is currently being used.
144     *
145     * @return true/false
146     */
147    public boolean isVersion3()
148    {
149        return isVersion3(hibernateVersion);
150    }
151
152    /**
153     * Indicates whether or not version 4 is the one that is currently being used.
154     *
155     * @return true/false
156     */
157    public boolean isVersion4()
158    {
159        return isVersion4(hibernateVersion);
160    }
161
162    /**
163     * @param hibernateVersionPropertyValue
164     * @return hibernateVersionPropertyValue.startsWith(SpringGlobals.HIBERNATE_VERSION_2)
165     */
166    public static boolean isVersion2(String hibernateVersionPropertyValue)
167    {
168        return hibernateVersionPropertyValue.startsWith(SpringGlobals.HIBERNATE_VERSION_2);
169    }
170
171    /**
172     * @param hibernateVersionPropertyValue
173     * @return hibernateVersionPropertyValue.startsWith(SpringGlobals.HIBERNATE_VERSION_3)
174     */
175    public static boolean isVersion3(String hibernateVersionPropertyValue)
176    {
177        return hibernateVersionPropertyValue.startsWith(SpringGlobals.HIBERNATE_VERSION_3);
178    }
179
180    /**
181     * @param hibernateVersionPropertyValue
182     * @return hibernateVersionPropertyValue.startsWith(SpringGlobals.HIBERNATE_VERSION_4)
183     */
184    public static boolean isVersion4(String hibernateVersionPropertyValue)
185    {
186        return hibernateVersionPropertyValue.startsWith(SpringGlobals.HIBERNATE_VERSION_4);
187    }
188
189    /**
190     * Denotes whether or not to make use of Hibernate 3 XML persistence support.
191     */
192    private String hibernateXmlPersistence;
193
194    /**
195     * @param hibernateXmlPersistenceIn <code>true</code> when you to make use of Hibernate 3 XML persistence support,
196     *      <code>false</code> otherwise
197     */
198    public void setHibernateXMLPersistence(final String hibernateXmlPersistenceIn)
199    {
200        this.hibernateXmlPersistence = hibernateXmlPersistenceIn;
201    }
202
203    /**
204     * @return isXmlPersistenceActive(hibernateVersion, hibernateXmlPersistence)
205     */
206    public boolean isXmlPersistenceActive()
207    {
208        return isXmlPersistenceActive(
209            this.hibernateVersion,
210            this.hibernateXmlPersistence);
211    }
212
213    /**
214     * @param hibernateVersionPropertyValue
215     * @param hibernateXMLPersistencePropertyValue
216     * @return isVersion3(hibernateVersionPropertyValue) && "true".equalsIgnoreCase(hibernateXMLPersistencePropertyValue)
217     */
218    public static boolean isXmlPersistenceActive(
219        String hibernateVersionPropertyValue,
220        String hibernateXMLPersistencePropertyValue)
221    {
222        return isVersion3(hibernateVersionPropertyValue) &&
223            "true".equalsIgnoreCase(hibernateXMLPersistencePropertyValue);
224    }
225
226    private String hibernateMappingStrategy;
227
228    /**
229     * @param hibernateMappingStrategyIn
230     */
231    public void setHibernateMappingStrategy(String hibernateMappingStrategyIn)
232    {
233        this.hibernateMappingStrategy = hibernateMappingStrategyIn;
234    }
235
236    /**
237     * @return mapSubclassesInSeparateFile(this.hibernateMappingStrategy)
238     */
239    public boolean isMapSubclassesInSeparateFile()
240    {
241        return mapSubclassesInSeparateFile(this.hibernateMappingStrategy);
242    }
243
244    /**
245     * @param hibernateMappingStrategyIn
246     * @return SpringGlobals.HIBERNATE_MAPPING_STRATEGY_SUBCLASS.equalsIgnoreCase(hibernateMappingStrategy)
247     */
248    public static boolean mapSubclassesInSeparateFile(
249        String hibernateMappingStrategyIn)
250    {
251        // subclass or hierarchy
252        return SpringGlobals.HIBERNATE_MAPPING_STRATEGY_SUBCLASS.equalsIgnoreCase(hibernateMappingStrategyIn);
253    }
254}