1 package org.andromda.cartridges.spring;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.List;
6 import org.andromda.cartridges.spring.metafacades.SpringGlobals;
7 import org.andromda.metafacades.uml.Entity;
8
9 /**
10 * Contains utilities used within the Spring cartridge
11 * when dealing with Hibernate.
12 *
13 * @author Chad Brandon
14 * @author Joel Kozikowski
15 */
16 public class SpringHibernateUtils
17 {
18 /**
19 * The version of Hibernate we're generating for.
20 */
21 private String hibernateVersion = SpringGlobals.HIBERNATE_VERSION_3;
22
23 /**
24 * Sets the version of Hibernate we're generating for.
25 *
26 * @param hibernateVersionIn the Hibernate version.
27 */
28 public void setHibernateVersion(final String hibernateVersionIn)
29 {
30 this.hibernateVersion = hibernateVersionIn;
31 }
32
33 /**
34 * Gets the appropriate hibernate package name for the given
35 * <code>version</code>.
36 *
37 * @return the base package name.
38 */
39 public String getBasePackage()
40 {
41 return this.isVersion3() || this.isVersion4() ? "org.hibernate" : "net.sf.hibernate";
42 }
43
44 /**
45 * Gets the appropriate hibernate criterion package name for the given <code>version</code>.
46 *
47 * @return the Hibernate criterion package name.
48 */
49 public String getCriterionPackage()
50 {
51 return this.getBasePackage() + (this.isVersion3() || this.isVersion4() ? ".criterion" : ".expression");
52 }
53
54 /**
55 * Gets the appropriate hibernate criterion package name for the given <code>version</code>.
56 * @param entities
57 * @return the Hibernate criterion package name.
58 */
59 public String getEntityBasePackage(Collection<Entity> entities)
60 {
61 String base = "";
62 List<String> packages = new ArrayList<String>();
63 // Get unique packages containing entities
64 for (Entity entity : entities)
65 {
66 String packageName = entity.getPackageName();
67 if (!packages.contains(packageName))
68 {
69 // TODO: Allow entities in vastly different packages
70 /*for (String pkgName : packages)
71 {
72 if (packageName.length() < pkgName.length() &&
73 pkgName.contains(packageName))
74 {
75 // replace the longer contained package name in the List
76 }
77 }*/
78 packages.add(packageName);
79 }
80 }
81 // Get unique top level packages containing entities
82 /*for (String packageName : packages)
83 {
84
85 }*/
86 // TODO Implement this function. Right now it just returns "" always
87 return base;
88 }
89
90 /**
91 * Gets the appropriate hibernate Restrictions/Expression fully qualified class name for the given <code>version</code>.
92 *
93 * @return the fully qualified Hibernate Restriction/Expression class name.
94 */
95 public String getRestrictionClass()
96 {
97 return getCriterionPackage() + (this.isVersion3() || this.isVersion4() ? ".Restrictions" : ".Expression");
98 }
99
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 }