View Javadoc

1   package org.andromda.cartridges.spring;
2   
3   import org.andromda.cartridges.spring.metafacades.SpringGlobals;
4   
5   /***
6    * Contains utilities used within the Spring cartridge
7    * when dealing with Hibernate.
8    *
9    * @author Chad Brandon
10   * @author Joel Kozikowski
11   */
12  public class SpringHibernateUtils
13  {
14      /***
15       * The version of Hibernate we're generating for.
16       */
17      private String hibernateVersion = SpringGlobals.HIBERNATE_VERSION_3;
18  
19      /***
20       * Sets the version of Hibernate we're generating for.
21       *
22       * @param hibernateVersion the Hibernate version.
23       */
24      public void setHibernateVersion(final String hibernateVersion)
25      {
26          this.hibernateVersion = hibernateVersion;
27      }
28  
29      /***
30       * Gets the appropriate hibernate package name for the given
31       * <code>version</code>.
32       *
33       * @return the base package name.
34       */
35      public String getBasePackage()
36      {
37          return this.isVersion3() ? "org.hibernate" : "net.sf.hibernate";
38      }
39  
40      /***
41       * Gets the appropriate hibernate criterion package name for the given <code>version</code>.
42       *
43       * @return the Hibernate criterion package name.
44       */
45      public String getCriterionPackage()
46      {
47          return this.getBasePackage() + (this.isVersion3() ? ".criterion" : ".expression");
48      }
49  
50      /***
51       * Gets the appropriate Spring Hibernate package based on the given
52       * <code>version</code>.
53       *
54       * @return the spring hibernate package.
55       */
56      public String getSpringHibernatePackage()
57      {
58          return this.isVersion3() ? "org.springframework.orm.hibernate3" : "org.springframework.orm.hibernate";
59      }
60  
61      /***
62       * Retrieves the appropriate package for Hibernate user types given
63       * the version defined within this class.
64       *
65       * @return the hibernate user type package.
66       */
67      public String getEagerFetchMode()
68      {
69          return this.isVersion3() ? "JOIN" : "EAGER";
70      }
71  
72      /***
73       * Retrieves the fully qualified name of the class that retrieves the Hibernate
74       * disjunction instance.
75       * @return the fully qualified class name.
76       */
77      public String getDisjunctionClassName()
78      {
79          return this.getCriterionPackage() + (this.isVersion3() ? ".Restrictions" : ".Expression");
80      }
81  
82      /***
83       * Indicates whether or not version 3 is the one that is currently being used.
84       *
85       * @return true/false
86       */
87      public boolean isVersion3()
88      {
89          return isVersion3(hibernateVersion);
90      }
91  
92      public static boolean isVersion3(String hibernateVersionPropertyValue)
93      {
94          return SpringGlobals.HIBERNATE_VERSION_3.equals(hibernateVersionPropertyValue);
95      }
96  
97      /***
98       * Denotes whether or not to make use of Hibernate 3 XML persistence support.
99       */
100     private String hibernateXmlPersistence;
101 
102     /***
103      * @param hibernateXmlPersistence <code>true</code> when you to make use of Hibernate 3 XML persistence support,
104      *      <code>false</code> otherwise
105      */
106     public void setHibernateXMLPersistence(final String hibernateXmlPersistence)
107     {
108         this.hibernateXmlPersistence = hibernateXmlPersistence;
109     }
110 
111     public boolean isXmlPersistenceActive()
112     {
113         return isXmlPersistenceActive(
114             this.hibernateVersion,
115             this.hibernateXmlPersistence);
116     }
117 
118     public static boolean isXmlPersistenceActive(
119         String hibernateVersionPropertyValue,
120         String hibernateXMLPersistencePropertyValue)
121     {
122         return isVersion3(hibernateVersionPropertyValue) &&
123             "true".equalsIgnoreCase(hibernateXMLPersistencePropertyValue);
124     }
125 
126     private String hibernateMappingStrategy;
127 
128     public void setHibernateMappingStrategy(String hibernateMappingStrategy)
129     {
130         this.hibernateMappingStrategy = hibernateMappingStrategy;
131     }
132 
133     public boolean isMapSubclassesInSeparateFile()
134     {
135         return mapSubclassesInSeparateFile(this.hibernateMappingStrategy);
136     }
137 
138     public static boolean mapSubclassesInSeparateFile(
139         String hibernateMappingStrategy)
140     {
141         // subclass or hierarchy
142         return SpringGlobals.HIBERNATE_MAPPING_STRATEGY_SUBCLASS.equalsIgnoreCase(hibernateMappingStrategy);
143     }
144 }