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
11
12
13
14
15
16 public class SpringHibernateUtils
17 {
18
19
20
21 private String hibernateVersion = SpringGlobals.HIBERNATE_VERSION_3;
22
23
24
25
26
27
28 public void setHibernateVersion(final String hibernateVersionIn)
29 {
30 this.hibernateVersion = hibernateVersionIn;
31 }
32
33
34
35
36
37
38
39 public String getBasePackage()
40 {
41 return this.isVersion3() || this.isVersion4() ? "org.hibernate" : "net.sf.hibernate";
42 }
43
44
45
46
47
48
49 public String getCriterionPackage()
50 {
51 return this.getBasePackage() + (this.isVersion3() || this.isVersion4() ? ".criterion" : ".expression");
52 }
53
54
55
56
57
58
59 public String getEntityBasePackage(Collection<Entity> entities)
60 {
61 String base = "";
62 List<String> packages = new ArrayList<String>();
63
64 for (Entity entity : entities)
65 {
66 String packageName = entity.getPackageName();
67 if (!packages.contains(packageName))
68 {
69
70
71
72
73
74
75
76
77
78 packages.add(packageName);
79 }
80 }
81
82
83
84
85
86
87 return base;
88 }
89
90
91
92
93
94
95 public String getRestrictionClass()
96 {
97 return getCriterionPackage() + (this.isVersion3() || this.isVersion4() ? ".Restrictions" : ".Expression");
98 }
99
100
101
102
103
104
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
113
114
115
116
117 public String getEagerFetchMode()
118 {
119 return this.isVersion3() || this.isVersion4() ? "JOIN" : "EAGER";
120 }
121
122
123
124
125
126
127 public String getDisjunctionClassName()
128 {
129 return this.getCriterionPackage() + (this.isVersion3() || this.isVersion4() ? ".Restrictions" : ".Expression");
130 }
131
132
133
134
135
136
137 public boolean isVersion2()
138 {
139 return isVersion2(hibernateVersion);
140 }
141
142
143
144
145
146
147 public boolean isVersion3()
148 {
149 return isVersion3(hibernateVersion);
150 }
151
152
153
154
155
156
157 public boolean isVersion4()
158 {
159 return isVersion4(hibernateVersion);
160 }
161
162
163
164
165
166 public static boolean isVersion2(String hibernateVersionPropertyValue)
167 {
168 return hibernateVersionPropertyValue.startsWith(SpringGlobals.HIBERNATE_VERSION_2);
169 }
170
171
172
173
174
175 public static boolean isVersion3(String hibernateVersionPropertyValue)
176 {
177 return hibernateVersionPropertyValue.startsWith(SpringGlobals.HIBERNATE_VERSION_3);
178 }
179
180
181
182
183
184 public static boolean isVersion4(String hibernateVersionPropertyValue)
185 {
186 return hibernateVersionPropertyValue.startsWith(SpringGlobals.HIBERNATE_VERSION_4);
187 }
188
189
190
191
192 private String hibernateXmlPersistence;
193
194
195
196
197
198 public void setHibernateXMLPersistence(final String hibernateXmlPersistenceIn)
199 {
200 this.hibernateXmlPersistence = hibernateXmlPersistenceIn;
201 }
202
203
204
205
206 public boolean isXmlPersistenceActive()
207 {
208 return isXmlPersistenceActive(
209 this.hibernateVersion,
210 this.hibernateXmlPersistence);
211 }
212
213
214
215
216
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
230
231 public void setHibernateMappingStrategy(String hibernateMappingStrategyIn)
232 {
233 this.hibernateMappingStrategy = hibernateMappingStrategyIn;
234 }
235
236
237
238
239 public boolean isMapSubclassesInSeparateFile()
240 {
241 return mapSubclassesInSeparateFile(this.hibernateMappingStrategy);
242 }
243
244
245
246
247
248 public static boolean mapSubclassesInSeparateFile(
249 String hibernateMappingStrategyIn)
250 {
251
252 return SpringGlobals.HIBERNATE_MAPPING_STRATEGY_SUBCLASS.equalsIgnoreCase(hibernateMappingStrategyIn);
253 }
254 }