001package org.andromda.cartridges.hibernate; 002 003import java.text.SimpleDateFormat; 004import java.util.Collection; 005import java.util.Date; 006import java.util.LinkedHashSet; 007import org.andromda.cartridges.hibernate.metafacades.HibernateGlobals; 008import org.andromda.metafacades.uml.Role; 009import org.andromda.metafacades.uml.Service; 010import org.apache.commons.collections.Closure; 011import org.apache.commons.collections.CollectionUtils; 012 013/** 014 * Contains utilities used within the Hibernate cartridge. 015 * 016 * @author Chad Brandon 017 * @author Joel Kozikowski 018 * @author Wouter Zoons 019 */ 020public class HibernateUtils 021{ 022 /** 023 * Retrieves all roles from the given <code>services</code> collection. 024 * 025 * @param services the collection services. 026 * @return all roles from the collection. 027 */ 028 public Collection<Role> getAllRoles(Collection services) 029 { 030 final Collection<Role> allRoles = new LinkedHashSet<Role>(); 031 CollectionUtils.forAllDo( 032 services, 033 new Closure() 034 { 035 public void execute(Object object) 036 { 037 if (object instanceof Service) 038 { 039 allRoles.addAll(((Service)object).getAllRoles()); 040 } 041 } 042 }); 043 return allRoles; 044 } 045 046 /** 047 * Stores the version of Hibernate we're generating for. 048 */ 049 private String hibernateVersion; 050 051 /** 052 * Sets the version of Hibernate we're generating for. 053 * 054 * @param hibernateVersion The version to set. 055 */ 056 public void setHibernateVersion(final String hibernateVersion) 057 { 058 this.hibernateVersion = hibernateVersion; 059 } 060 061 /** 062 * Retrieves the appropriate Hibernate package for the given version. 063 * 064 * @return the Hibernate package name. 065 */ 066 public String getHibernatePackage() 067 { 068 return this.isVersion3() || this.isVersion4() ? "org.hibernate" : "net.sf.hibernate"; 069 } 070 071 /** 072 * Retrieves the appropriate package for Hibernate user types given 073 * the version defined within this class. 074 * 075 * @return the hibernate user type package. 076 */ 077 public String getHibernateUserTypePackage() 078 { 079 return this.isVersion3() || this.isVersion4() ? this.getHibernatePackage() + ".usertype" : this.getHibernatePackage(); 080 } 081 082 /** 083 * Indicates whether or not Hibernate 2 is enabled. 084 * 085 * @return true/false 086 */ 087 public boolean isVersion2() 088 { 089 return isVersion2(this.hibernateVersion); 090 } 091 092 /** 093 * Indicates whether or not Hibernate 3 is enabled. 094 * 095 * @return true/false 096 */ 097 public boolean isVersion3() 098 { 099 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}