001: package org.andromda.cartridges.hibernate.metafacades;
002:
003: import org.andromda.cartridges.hibernate.HibernateProfile;
004: import org.andromda.cartridges.hibernate.HibernateUtils;
005: import org.andromda.metafacades.uml.ClassifierFacade;
006: import org.andromda.metafacades.uml.EntityMetafacadeUtils;
007: import org.andromda.metafacades.uml.NameMasker;
008: import org.andromda.metafacades.uml.TypeMappings;
009: import org.andromda.metafacades.uml.UMLMetafacadeProperties;
010: import org.apache.commons.lang.StringUtils;
011:
012: /**
013: * MetafacadeLogic implementation for
014: * org.andromda.cartridges.hibernate.metafacades.HibernateEntityAttribute.
015: *
016: * @see org.andromda.cartridges.hibernate.metafacades.HibernateEntityAttribute
017: */
018: public class HibernateEntityAttributeLogicImpl extends
019: HibernateEntityAttributeLogic {
020: // ---------------- constructor -------------------------------
021: public HibernateEntityAttributeLogicImpl(Object metaObject,
022: String context) {
023: super (metaObject, context);
024: }
025:
026: /**
027: * Overridden to provide handling of inheritance.
028: *
029: * @see org.andromda.metafacades.uml.AttributeFacade#isRequired()
030: */
031: public boolean isRequired() {
032: boolean required = super .isRequired();
033: if (this .getOwner() instanceof HibernateEntity) {
034: HibernateEntity entity = (HibernateEntity) this .getOwner();
035: if (entity.isHibernateInheritanceClass()
036: && entity.getGeneralization() != null) {
037: required = false;
038: }
039: }
040: return required;
041: }
042:
043: /**
044: * Override to provide java specific handling of the default value.
045: *
046: * @see org.andromda.metafacades.uml.AttributeFacade#getDefaultValue()
047: */
048: public String getDefaultValue() {
049: String defaultValue = super .getDefaultValue();
050: final ClassifierFacade type = this .getType();
051: if (type != null) {
052: final String fullyQualifiedName = StringUtils
053: .trimToEmpty(type.getFullyQualifiedName());
054: if ("java.lang.String".equals(fullyQualifiedName)) {
055: defaultValue = "\"" + defaultValue + "\"";
056: } else if (fullyQualifiedName.startsWith("java.lang")) {
057: defaultValue = fullyQualifiedName + ".valueOf("
058: + defaultValue + ")";
059: } else if (type.isEnumeration()) {
060: final String mask = (String) this
061: .getConfiguredProperty(UMLMetafacadeProperties.ENUMERATION_LITERAL_NAME_MASK);
062: defaultValue = type.getFullyQualifiedName() + '.'
063: + NameMasker.mask(defaultValue, mask);
064: }
065: }
066: return defaultValue;
067: }
068:
069: /**
070: * @see org.andromda.cartridges.hibernate.metafacades.HibernateEntityAttribute#isContainsEmbeddedObject()
071: */
072: protected boolean handleIsContainsEmbeddedObject() {
073: boolean returnValue = false;
074: if (this .getType() instanceof HibernateEmbeddedValue) {
075: returnValue = true;
076: }
077: return returnValue;
078: }
079:
080: /**
081: * @see org.andromda.cartridges.hibernate.metafacades.HibernateEntityAttribute#concatColumnName(java.lang.String,
082: * java.lang.String)
083: */
084: protected String handleConcatColumnName(java.lang.String prefix,
085: java.lang.String name) {
086: String returnValue = name;
087: if (StringUtils.isNotBlank(prefix)) {
088: returnValue = prefix
089: + this
090: .getConfiguredProperty(UMLMetafacadeProperties.SQL_NAME_SEPARATOR)
091: + name;
092:
093: // handle maxSqlNameLength
094: Short maxSqlNameLength = Short
095: .valueOf((String) this
096: .getConfiguredProperty(UMLMetafacadeProperties.MAX_SQL_NAME_LENGTH));
097: returnValue = EntityMetafacadeUtils
098: .ensureMaximumNameLength(returnValue,
099: maxSqlNameLength);
100: }
101: return returnValue;
102: }
103:
104: /**
105: * @see org.andromda.metafacades.uml.AssociationEndFacade#isLazy()
106: */
107: protected boolean handleIsLazy() {
108: final String value = (String) findTaggedValue(HibernateProfile.TAGGEDVALUE_HIBERNATE_LAZY);
109: return StringUtils.isNotBlank(value) ? Boolean.valueOf(value)
110: .booleanValue() : false;
111: }
112:
113: /**
114: * @see org.andromda.cartridges.hibernate.metafacades.HibernateEntityAttribute#getFormula()
115: */
116: protected String handleGetFormula() {
117: return (String) findTaggedValue(HibernateProfile.TAGGEDVALUE_HIBERNATE_FORMULA);
118: }
119:
120: /**
121: * @see org.andromda.cartridges.hibernate.metafacades.HibernateEntityAttribute#isInsertEnabled()
122: */
123: protected boolean handleIsInsertEnabled() {
124: final String value = (String) findTaggedValue(HibernateProfile.TAGGEDVALUE_HIBERNATE_PROPERTY_INSERT);
125: return StringUtils.isNotBlank(value) ? Boolean.valueOf(value)
126: .booleanValue() : true;
127: }
128:
129: /**
130: * @see org.andromda.cartridges.hibernate.metafacades.HibernateEntityAttribute#isUpdateEnabled()
131: */
132: protected boolean handleIsUpdateEnabled() {
133: final String value = (String) findTaggedValue(HibernateProfile.TAGGEDVALUE_HIBERNATE_PROPERTY_UPDATE);
134: return StringUtils.isNotBlank(value) ? Boolean.valueOf(value)
135: .booleanValue() : true;
136: }
137:
138: private boolean isXmlPersistenceActive() {
139: return HibernateUtils
140: .isXmlPersistenceActive(
141: (String) this
142: .getConfiguredProperty(HibernateGlobals.HIBERNATE_VERSION),
143: (String) this
144: .getConfiguredProperty(HibernateGlobals.HIBERNATE_XML_PERSISTENCE));
145: }
146:
147: private boolean persistIDAsAttribute() {
148: boolean persistAsAttribute = true;
149: String prop = (String) this
150: .getConfiguredProperty(HibernateGlobals.HIBERNATE_XML_PERSISTENCE_ID_AS_ATTRIBUTE);
151: if (prop != null && prop.equalsIgnoreCase("false"))
152: persistAsAttribute = false;
153:
154: return persistAsAttribute;
155: }
156:
157: protected String handleGetXmlTagName() {
158: String tagName = null;
159:
160: if (isXmlPersistenceActive()) {
161: tagName = (String) this
162: .findTaggedValue(HibernateProfile.TAGGEDVALUE_HIBERNATE_XML_TAG_NAME);
163:
164: if (tagName == null) {
165: if (this .isIdentifier() && this .persistIDAsAttribute())
166: tagName = "@" + this .getName();
167: else
168: tagName = this .getName();
169: }
170:
171: }
172: return (StringUtils.isBlank(tagName)) ? null : tagName;
173: }
174:
175: protected String handleGetFullyQualifiedHibernateType() {
176: final String fullyQualifiedName;
177:
178: if (this .getType().isEnumeration()) {
179: fullyQualifiedName = "org.andromda.persistence.hibernate.usertypes.HibernateEnumType";
180: } else {
181: final String hibernateTypeMappingsUri = (String) this
182: .getConfiguredProperty("hibernateTypeMappingsUri");
183: final TypeMappings mappings = TypeMappings
184: .getInstance(hibernateTypeMappingsUri);
185:
186: if (mappings == null) {
187: fullyQualifiedName = this .getType()
188: .getFullyQualifiedName();
189: } else {
190: final String fullyQualifiedModelName = this .getType()
191: .getFullyQualifiedName(true);
192: if (mappings.getMappings().containsFrom(
193: fullyQualifiedModelName)) {
194: fullyQualifiedName = mappings
195: .getTo(fullyQualifiedModelName);
196: } else {
197: fullyQualifiedName = this.getType()
198: .getFullyQualifiedName();
199: }
200: }
201: }
202:
203: return fullyQualifiedName;
204: }
205:
206: }
|