1 package org.andromda.cartridges.hibernate;
2
3 import java.text.SimpleDateFormat;
4 import java.util.Collection;
5 import java.util.Date;
6 import java.util.LinkedHashSet;
7 import org.andromda.cartridges.hibernate.metafacades.HibernateGlobals;
8 import org.andromda.metafacades.uml.Role;
9 import org.andromda.metafacades.uml.Service;
10 import org.apache.commons.collections.Closure;
11 import org.apache.commons.collections.CollectionUtils;
12
13
14
15
16
17
18
19
20 public class HibernateUtils
21 {
22
23
24
25
26
27
28 public Collection<Role> getAllRoles(Collection services)
29 {
30 final Collection<Role> allRoles = new LinkedHashSet<Role>();
31 CollectionUtils.forAllDo(
32 services,
33 new Closure()
34 {
35 public void execute(Object object)
36 {
37 if (object instanceof Service)
38 {
39 allRoles.addAll(((Service)object).getAllRoles());
40 }
41 }
42 });
43 return allRoles;
44 }
45
46
47
48
49 private String hibernateVersion;
50
51
52
53
54
55
56 public void setHibernateVersion(final String hibernateVersion)
57 {
58 this.hibernateVersion = hibernateVersion;
59 }
60
61
62
63
64
65
66 public String getHibernatePackage()
67 {
68 return this.isVersion3() || this.isVersion4() ? "org.hibernate" : "net.sf.hibernate";
69 }
70
71
72
73
74
75
76
77 public String getHibernateUserTypePackage()
78 {
79 return this.isVersion3() || this.isVersion4() ? this.getHibernatePackage() + ".usertype" : this.getHibernatePackage();
80 }
81
82
83
84
85
86
87 public boolean isVersion2()
88 {
89 return isVersion2(this.hibernateVersion);
90 }
91
92
93
94
95
96
97 public boolean isVersion3()
98 {
99 return isVersion3(this.hibernateVersion);
100 }
101
102
103
104
105
106
107 public boolean isVersion4()
108 {
109 return isVersion4(this.hibernateVersion);
110 }
111
112
113
114
115
116
117
118 public static boolean isVersion2(String hibernateVersionPropertyValue)
119 {
120 boolean version2 = false;
121 if (hibernateVersionPropertyValue != null)
122 {
123 version2 = hibernateVersionPropertyValue.startsWith(HibernateGlobals.HIBERNATE_VERSION_2);
124 }
125 return version2;
126 }
127
128
129
130
131
132
133
134 public static boolean isVersion3(String hibernateVersionPropertyValue)
135 {
136 boolean version3 = false;
137 if (hibernateVersionPropertyValue != null)
138 {
139 version3 = hibernateVersionPropertyValue.startsWith(HibernateGlobals.HIBERNATE_VERSION_3);
140 }
141 return version3;
142 }
143
144
145
146
147
148
149
150 public static boolean isVersion4(String hibernateVersionPropertyValue)
151 {
152 boolean version4 = false;
153 if (hibernateVersionPropertyValue != null)
154 {
155 version4 = hibernateVersionPropertyValue.startsWith(HibernateGlobals.HIBERNATE_VERSION_4);
156 }
157 return version4;
158 }
159
160
161
162
163 private String hibernateXmlPersistence;
164
165
166
167
168
169 public void setHibernateXMLPersistence(final String hibernateXmlPersistence)
170 {
171 this.hibernateXmlPersistence = hibernateXmlPersistence;
172 }
173
174
175
176
177 public boolean isXmlPersistenceActive()
178 {
179 return isXmlPersistenceActive(
180 this.hibernateVersion,
181 this.hibernateXmlPersistence);
182 }
183
184
185
186
187
188
189 public static boolean isXmlPersistenceActive(
190 String hibernateVersionPropertyValue,
191 String hibernateXMLPersistencePropertyValue)
192 {
193 return isVersion3(hibernateVersionPropertyValue) &&
194 "true".equalsIgnoreCase(hibernateXMLPersistencePropertyValue);
195 }
196
197 private String hibernateMappingStrategy;
198
199
200
201
202 public void setHibernateMappingStrategy(String hibernateMappingStrategy)
203 {
204 this.hibernateMappingStrategy = hibernateMappingStrategy;
205 }
206
207
208
209
210 public boolean isMapSubclassesInSeparateFile()
211 {
212 return mapSubclassesInSeparateFile(this.hibernateMappingStrategy);
213 }
214
215
216
217
218
219 public static boolean mapSubclassesInSeparateFile(
220 String hibernateMappingStrategy)
221 {
222
223 return HibernateGlobals.HIBERNATE_MAPPING_STRATEGY_SUBCLASS.equalsIgnoreCase(hibernateMappingStrategy);
224 }
225
226
227
228
229
230
231 public static String getInheritanceTypeEnum(
232 String hibernateMappingStrategy)
233 {
234 String inheritanceType = null;
235 if (HibernateGlobals.HIBERNATE_MAPPING_STRATEGY_HIERARCHY.equalsIgnoreCase(hibernateMappingStrategy))
236 {
237 inheritanceType = "JOINED";
238 }
239 else if (HibernateGlobals.HIBERNATE_MAPPING_STRATEGY_CONCRETE.equalsIgnoreCase(hibernateMappingStrategy))
240 {
241 inheritanceType = "TABLE_PER_CLASS";
242 }
243 else
244 {
245 inheritanceType = "SINGLE_TABLE";
246 }
247 return inheritanceType;
248 }
249
250 private static final SimpleDateFormat DF = new SimpleDateFormat("MM/dd/yyyy HH:mm:ssZ");
251
252
253
254
255
256
257 public static String getDate(String format)
258 {
259
260
261
262
263 return DF.format(new Date());
264 }
265
266
267
268
269
270
271 public static String getDate()
272 {
273 return DF.format(new Date());
274 }
275 }