View Javadoc
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   * Contains utilities used within the Hibernate cartridge.
15   *
16   * @author Chad Brandon
17   * @author Joel Kozikowski
18   * @author Wouter Zoons
19   */
20  public class HibernateUtils
21  {
22      /**
23       * Retrieves all roles from the given <code>services</code> collection.
24       *
25       * @param services the collection services.
26       * @return all roles from the collection.
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       * Stores the version of Hibernate we're generating for.
48       */
49      private String hibernateVersion;
50  
51      /**
52       * Sets the version of Hibernate we're generating for.
53       *
54       * @param hibernateVersion The version to set.
55       */
56      public void setHibernateVersion(final String hibernateVersion)
57      {
58          this.hibernateVersion = hibernateVersion;
59      }
60  
61      /**
62       * Retrieves the appropriate Hibernate package for the given version.
63       *
64       * @return the Hibernate package name.
65       */
66      public String getHibernatePackage()
67      {
68          return this.isVersion3() || this.isVersion4() ? "org.hibernate" : "net.sf.hibernate";
69      }
70  
71      /**
72       * Retrieves the appropriate package for Hibernate user types given
73       * the version defined within this class.
74       *
75       * @return the hibernate user type package.
76       */
77      public String getHibernateUserTypePackage()
78      {
79          return this.isVersion3() || this.isVersion4() ? this.getHibernatePackage() + ".usertype" : this.getHibernatePackage();
80      }
81  
82      /**
83       * Indicates whether or not Hibernate 2 is enabled.
84       *
85       * @return true/false
86       */
87      public boolean isVersion2()
88      {
89          return isVersion2(this.hibernateVersion);
90      }
91  
92      /**
93       * Indicates whether or not Hibernate 3 is enabled.
94       *
95       * @return true/false
96       */
97      public boolean isVersion3()
98      {
99          return isVersion3(this.hibernateVersion);
100     }
101 
102     /**
103      * Indicates whether or not Hibernate 3 is enabled.
104      *
105      * @return true/false
106      */
107     public boolean isVersion4()
108     {
109         return isVersion4(this.hibernateVersion);
110     }
111 
112     /**
113      * Indicates whether or not the given property value is version 3 or not.
114      *
115      * @param hibernateVersionPropertyValue the value of the property
116      * @return true/false
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      * Indicates whether or not the given property value is version 3 or not.
130      *
131      * @param hibernateVersionPropertyValue the value of the property
132      * @return true/false
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      * Indicates whether or not the given property value is version 3 or not.
146      *
147      * @param hibernateVersionPropertyValue the value of the property
148      * @return true/false
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      * Denotes whether or not to make use of Hibernate 3 XML persistence support.
162      */
163     private String hibernateXmlPersistence;
164 
165     /**
166      * @param hibernateXmlPersistence <code>true</code> when you to make use of Hibernate 3 XML persistence support,
167      *      <code>false</code> otherwise
168      */
169     public void setHibernateXMLPersistence(final String hibernateXmlPersistence)
170     {
171         this.hibernateXmlPersistence = hibernateXmlPersistence;
172     }
173 
174     /**
175      * @return isXmlPersistenceActive
176      */
177     public boolean isXmlPersistenceActive()
178     {
179         return isXmlPersistenceActive(
180             this.hibernateVersion,
181             this.hibernateXmlPersistence);
182     }
183 
184     /**
185      * @param hibernateVersionPropertyValue
186      * @param hibernateXMLPersistencePropertyValue
187      * @return isXmlPersistenceActive
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      * @param hibernateMappingStrategy
201      */
202     public void setHibernateMappingStrategy(String hibernateMappingStrategy)
203     {
204         this.hibernateMappingStrategy = hibernateMappingStrategy;
205     }
206 
207     /**
208      * @return mapSubclassesInSeparateFile(this.hibernateMappingStrategy)
209      */
210     public boolean isMapSubclassesInSeparateFile()
211     {
212         return mapSubclassesInSeparateFile(this.hibernateMappingStrategy);
213     }
214 
215     /**
216      * @param hibernateMappingStrategy
217      * @return mapSubclassesInSeparateFile
218      */
219     public static boolean mapSubclassesInSeparateFile(
220         String hibernateMappingStrategy)
221     {
222         // subclass or hierarchy
223         return HibernateGlobals.HIBERNATE_MAPPING_STRATEGY_SUBCLASS.equalsIgnoreCase(hibernateMappingStrategy);
224     }
225 
226     /**
227      * Needed for InheritanceType enumeration in hibernate annotations
228      * @param hibernateMappingStrategy
229      * @return javax.persistence.InheritanceType enum value for template
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 /* if (HibernateGlobals.HIBERNATE_MAPPING_STRATEGY_SUBCLASS.equalsIgnoreCase(hibernateMappingStrategy)) */
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      * Returns the current Date in the specified format. $conversionUtils does not seem to work in vsl.
253      *
254      * @param format The format for the output date
255      * @return the current date in the specified format.
256      */
257     public static String getDate(String format)
258     {
259         /*if (DF == null || !format.equals(DF.toLocalizedPattern()))
260         {
261             DF = new SimpleDateFormat(format);
262         }*/
263         return DF.format(new Date());
264     }
265 
266     /**
267      * Returns the current Date in the specified format.
268      *
269      * @return the current date with the default format .
270      */
271     public static String getDate()
272     {
273         return DF.format(new Date());
274     }
275 }