001: package org.andromda.cartridges.hibernate;
002:
003: import java.util.Collection;
004: import java.util.LinkedHashSet;
005:
006: import org.andromda.cartridges.hibernate.metafacades.HibernateGlobals;
007: import org.andromda.metafacades.uml.Service;
008: import org.apache.commons.collections.Closure;
009: import org.apache.commons.collections.CollectionUtils;
010:
011: /**
012: * Contains utilities used within the Hibernate cartridge.
013: *
014: * @author Chad Brandon
015: * @author Joel Kozikowski
016: * @author Wouter Zoons
017: */
018: public class HibernateUtils {
019: /**
020: * Retrieves all roles from the given <code>services</code> collection.
021: *
022: * @param services the collection services.
023: * @return all roles from the collection.
024: */
025: public Collection getAllRoles(Collection services) {
026: final Collection allRoles = new LinkedHashSet();
027: CollectionUtils.forAllDo(services, new Closure() {
028: public void execute(Object object) {
029: if (object instanceof Service) {
030: allRoles.addAll(((Service) object).getAllRoles());
031: }
032: }
033: });
034: return allRoles;
035: }
036:
037: /**
038: * Stores the version of Hibernate we're generating for.
039: */
040: private String hibernateVersion;
041:
042: /**
043: * Sets the version of Hibernate we're generating for.
044: *
045: * @param hibernateVersion The version to set.
046: */
047: public void setHibernateVersion(final String hibernateVersion) {
048: this .hibernateVersion = hibernateVersion;
049: }
050:
051: /**
052: * Retrieves the appropriate Hibernate package for the given version.
053: *
054: * @return the Hibernate package name.
055: */
056: public String getHibernatePackage() {
057: return this .isVersion3() ? "org.hibernate" : "net.sf.hibernate";
058: }
059:
060: /**
061: * Retrieves the appropriate package for Hibernate user types given
062: * the version defined within this class.
063: *
064: * @return the hibernate user type package.
065: */
066: public String getHibernateUserTypePackage() {
067: return isVersion3() ? this .getHibernatePackage() + ".usertype"
068: : this .getHibernatePackage();
069: }
070:
071: /**
072: * Indicates whether or not Hibernate 3 is enabled.
073: *
074: * @return true/false
075: */
076: public boolean isVersion3() {
077: return isVersion3(this .hibernateVersion);
078: }
079:
080: /**
081: * Indicates whether or not the given property value is version 3 or not.
082: *
083: * @param hibernateVersionPropertyValue the value of the property
084: * @return true/false
085: */
086: public static boolean isVersion3(
087: String hibernateVersionPropertyValue) {
088: boolean version3 = false;
089: if (hibernateVersionPropertyValue != null) {
090: version3 = hibernateVersionPropertyValue
091: .equals(HibernateGlobals.HIBERNATE_VERSION_3);
092: }
093: return version3;
094: }
095:
096: /**
097: * Denotes whether or not to make use of Hibernate 3 XML persistence support.
098: */
099: private String hibernateXmlPersistence;
100:
101: /**
102: * @param hibernateXmlPersistence <code>true</code> when you to make use of Hibernate 3 XML persistence support,
103: * <code>false</code> otherwise
104: */
105: public void setHibernateXMLPersistence(
106: final String hibernateXmlPersistence) {
107: this .hibernateXmlPersistence = hibernateXmlPersistence;
108: }
109:
110: public boolean isXmlPersistenceActive() {
111: return isXmlPersistenceActive(this .hibernateVersion,
112: this .hibernateXmlPersistence);
113: }
114:
115: public static boolean isXmlPersistenceActive(
116: String hibernateVersionPropertyValue,
117: String hibernateXMLPersistencePropertyValue) {
118: return isVersion3(hibernateVersionPropertyValue)
119: && "true"
120: .equalsIgnoreCase(hibernateXMLPersistencePropertyValue);
121: }
122:
123: private String hibernateMappingStrategy;
124:
125: public void setHibernateMappingStrategy(
126: String hibernateMappingStrategy) {
127: this .hibernateMappingStrategy = hibernateMappingStrategy;
128: }
129:
130: public boolean isMapSubclassesInSeparateFile() {
131: return mapSubclassesInSeparateFile(this .hibernateMappingStrategy);
132: }
133:
134: public static boolean mapSubclassesInSeparateFile(
135: String hibernateMappingStrategy) {
136: // subclass or hierarchy
137: return HibernateGlobals.HIBERNATE_MAPPING_STRATEGY_SUBCLASS
138: .equalsIgnoreCase(hibernateMappingStrategy);
139: }
140: }
|