From: Vance K. <va...@us...> - 2006-06-01 14:40:52
|
User: vancek Date: 06/06/01 07:40:47 Modified: andromda-ejb3/src/main/resources/templates/ejb3 GlobalMacros.vm EntityEmbeddable.vsl Log: added equals, hashCode and toString to entities Revision Changes Path 1.2 +67 -30 cartridges/andromda-ejb3/src/main/resources/templates/ejb3/GlobalMacros.vm Index: GlobalMacros.vm =================================================================== RCS file: /cvsroot/andromdaplugins/cartridges/andromda-ejb3/src/main/resources/templates/ejb3/GlobalMacros.vm,v retrieving revision 1.1 retrieving revision 1.2 diff -u -w -r1.1 -r1.2 --- GlobalMacros.vm 24 Apr 2006 02:28:00 -0000 1.1 +++ GlobalMacros.vm 1 Jun 2006 14:40:46 -0000 1.2 @@ -3,12 +3,15 @@ ## If an entity is rendered, the attributeSet consists of the identifiers, ## if an value type is renderer, the attributeSet consists of all attributes ## +## If an entity contains a composite primary key consisting of more than 1 +## identifier, must call accessor on PK object. +## #macro (renderEqualsMethod $class $className $attributeSet) public boolean equals(Object object) { -#if ($attributeSet.empty) +#**##if ($attributeSet.empty) return super.equals(object); -#else +#**##else if (this == object) { return true; @@ -18,27 +21,27 @@ return false; } final $className that = ($className)object; -#foreach ($attribute in $attributeSet) -#set ($idType = $attribute.type) -#if ($idType.primitive) - if (this.$attribute.name != that.${attribute.getterName}()) +#* *##foreach ($attribute in $attributeSet) +#* *##set ($idType = $attribute.type) +#* *##if ($idType.primitive) + if (this.#if ($class.compositePrimaryKeyPresent)getPk().#end${attribute.getterName}() != that.#if ($class.compositePrimaryKeyPresent)getPk().#end${attribute.getterName}()) { return false; } -#elseif ($idType.arrayType) - if (!java.util.Arrays.equals(this.$attribute.name, that.${attribute.getterName}())) +#* *##elseif ($idType.arrayType) + if (!java.util.Arrays.equals(this.#if ($class.compositePrimaryKeyPresent)getPk().#end${attribute.getterName}(), that.#if ($class.compositePrimaryKeyPresent)getPk().#end${attribute.getterName}())) { return false; } -#else - if (this.$attribute.name == null || that.${attribute.getterName}() == null || !this.${attribute.name}.equals(that.${attribute.getterName}())) +#* *##else + if (this.#if ($class.compositePrimaryKeyPresent)getPk().#end${attribute.getterName}() == null || that.#if ($class.compositePrimaryKeyPresent)getPk().#end${attribute.getterName}() == null || !this.#if ($class.compositePrimaryKeyPresent)getPk().#end${attribute.getterName}().equals(that.#if ($class.compositePrimaryKeyPresent)getPk().#end${attribute.getterName}())) { return false; } -#end -#end +#* *##end +#* *##end return true; -#end +#**##end } #end ## @@ -46,31 +49,65 @@ ## If an entity is rendered, the attributeSet consists of the identifiers, ## if an value type is renderer, the attributeSet consists of all attributes ## +## If an entity contains a composite primary key consisting of more than 1 +## identifier, must call accessor on PK object. +## #macro (renderHashCodeMethod $class $attributeSet) public int hashCode() { -#if ($attributeSet.empty) +#**##if ($attributeSet.empty) return super.hashCode(); -#else -#if ($class.generalization) +#**##else +#* *##if ($class.generalization) int hashCode = super.hashCode(); -#else +#* *##else int hashCode = 0; -#end -#foreach ($attribute in $attributeSet) -#set ($attrType = $attribute.type) -#if ($attribute.getterSetterTypeName == "boolean") - hashCode = 29 * hashCode + (${attribute.name} ? 1 : 0); -#elseif ($attrType.arrayType) +#* *##end +#* *##foreach ($attribute in $attributeSet) +#* *##set ($attrType = $attribute.type) +#* *##if ($attribute.getterSetterTypeName == "boolean") + hashCode = 29 * hashCode + (#if ($class.compositePrimaryKeyPresent)this.getPk().#end${attribute.getterName}() ? 1 : 0); +#* *##elseif ($attrType.arrayType) // arrays are not part of the hashCode calculation -#elseif ($attrType.primitive) - hashCode = 29 * hashCode + (int)${attribute.name}; -#else - hashCode = 29 * hashCode + (${attribute.name} == null ? 0 : ${attribute.name}.hashCode()); -#end## if -#end## foreach +#* *##elseif ($attrType.primitive) + hashCode = 29 * hashCode + (int)#if ($class.compositePrimaryKeyPresent)this.getPk().#end${attribute.getterName}(); +#* *##else + hashCode = 29 * hashCode + (#if ($class.compositePrimaryKeyPresent)this.getPk().#end${attribute.getterName}() == null ? 0 : #if ($class.compositePrimaryKeyPresent)this.getPk().#end${attribute.getterName}().hashCode()); +#* *##end## if +#* *##end## foreach return hashCode; -#end## $attributeSet.empty +#**##end## $attributeSet.empty + } +#end +## +## This macro will render the toString() method +## If an entity is rendered, the attributeSet consists of the identifiers, +## if an value type is renderer, the attributeSet consists of all attributes +## +## If an entity contains a composite primary key consisting of more than 1 +## identifier, must call accessor on PK object. +## +#macro (renderToStringMethod $class $className $attributeSet) + public String toString() + { +#**##if ($attributeSet.empty) + return super.toString(); +#**##else + StringBuilder sb = new StringBuilder(); + sb.append("${className}(="); +#* *##if ($class.generalization) + sb.append(super.toString()); +#* *##end +#* *##set( $sep = "" ) +#* *##foreach ($attribute in $attributeSet) +#* *##set ($attrType = $attribute.type) + sb.append("${sep}#if ($class.compositePrimaryKeyPresent && $attribute.identifier)pk.#end${attribute.name}: "); + sb.append(#if ($class.compositePrimaryKeyPresent && $attribute.identifier)this.getPk().#end${attribute.getterName}()); +#* *##set( $sep = ", " ) +#* *##end## foreach + sb.append(")"); + return sb.toString(); +#**##end## $attributeSet.empty } #end 1.30 +29 -0 cartridges/andromda-ejb3/src/main/resources/templates/ejb3/EntityEmbeddable.vsl Index: EntityEmbeddable.vsl =================================================================== RCS file: /cvsroot/andromdaplugins/cartridges/andromda-ejb3/src/main/resources/templates/ejb3/EntityEmbeddable.vsl,v retrieving revision 1.29 retrieving revision 1.30 diff -u -w -r1.29 -r1.30 --- EntityEmbeddable.vsl 29 May 2006 01:37:57 -0000 1.29 +++ EntityEmbeddable.vsl 1 Jun 2006 14:40:47 -0000 1.30 @@ -715,4 +715,33 @@ #* *##end #**##end #end + // -------- Common Methods ----------- + +#if ($entity.entityImplementationRequired || $entity.embeddableSuperclass) +#**##set ($entityName = $entity.entityEmbeddableName) +#else +#**##set ($entityName = $entity.entityName) +#end + /** + * Indicates if the argument is of the same type and all values are equal. + * + * @param object The target object to compare with + * @return boolean True if both objects a 'equal' + */ +#renderEqualsMethod($entity $entityName $entity.identifiers) + + /** + * Returns a hash code value for the object + * + * @return int The hash code value + */ +#renderHashCodeMethod($entity $entity.identifiers) + + /** + * Returns a String representation of the object + * + * @return String Textual representation of the object displaying name/value pairs for all attributes + */ +#renderToStringMethod($entity $entityName $entity.attributes) + } \ No newline at end of file |