Scala _Entity.java Template

Last modified by Ravi Mendis on 2010/09/07 05:06

This is a basic proof-of-concept Scala template.
 Scala is supposed to reduce boilerplate and so potentially allows for a better or improved solution.


// $LastChangedRevision$ DO NOT EDIT.  Make changes to ${entity.classNameWithOptionalPackage}.java instead.
#if ($entity.superclassPackageName)
package $entity.superclassPackageName

#end
import com.webobjects.eocontrol.{EOGenericRecord, EORelationshipManipulation}
import com.webobjects.foundation.{NSArray, NSData}
import org.apache.log4j.Logger

import scala.collection.JavaConversions._
#foreach ($relationship in $entity.sortedClassToOneRelationships)
#if ($relationship.actualDestination.classNameWithDefault != "EOGenericRecord" && $relationship.actualDestination.packageName != $entity.packageName)
import $relationship.actualDestination.classNameWithDefault
#end
#end
#foreach ($relationship in $entity.sortedClassToManyRelationships)
#if ($relationship.actualDestination.classNameWithDefault != "EOGenericRecord" && $relationship.actualDestination.packageName != $entity.packageName)
import $relationship.actualDestination.classNameWithDefault
#end
#end

object ${entity.classNameWithoutPackage} {
#if ($entity.partialEntitySet)
val ENTITY_NAME = "$entity.partialEntity.name"
#else
val ENTITY_NAME = "$entity.name"
#end

/*
* API
*/
    object Keys {
// Attributes
#foreach ($attribute in $entity.sortedClassAttributes)
val ${attribute.uppercaseUnderscoreName} = "$attribute.name"
#end

// Relationships
#foreach ($relationship in $entity.sortedClassRelationships)
val ${relationship.uppercaseUnderscoreName} = "$relationship.name"
#end
}
}

abstract class ${entity.classNameWithoutPackage} extends#if ($entity.parentClassNameSet)${entity.parentClassName}#elseif ($entity.partialEntitySet)er.extensions.partials.ERXPartial<${entity.partialEntity.className}>#elseif ($entity.parentSet)${entity.parent.classNameWithDefault}#elseif ($EOGenericRecord)${EOGenericRecord}#else EOGenericRecord#end {
  private lazy val logger = Logger.getLogger(this.getClass.getName)

#foreach ($attribute in $entity.sortedClassAttributes)
#if (!$attribute.inherited)
#if ($attribute.userInfo.ERXConstantClassName)
 def ${attribute.name}: $attribute.userInfo.ERXConstantClassName = {
    storedValueForKey(${entity.classNameWithoutPackage}.Keys.${attribute.uppercaseUnderscoreName})
  }

 def set${attribute.capitalizedName}(value: $attribute.userInfo.ERXConstantClassName) = {
    takeStoredValueForKey(value, ${entity.classNameWithoutPackage}.Keys.${attribute.uppercaseUnderscoreName})
  }
#else
 def ${attribute.name} = {
    storedValueForKey(${entity.classNameWithoutPackage}.Keys.${attribute.uppercaseUnderscoreName}).asInstanceOf[$attribute.getJavaClassName(true)]
  }

 def set${attribute.capitalizedName}(value: $attribute.javaClassName) {
    logger.debug( "updating $attribute.name from " + ${attribute.name} + " to " + value)
    takeStoredValueForKey(value, ${entity.classNameWithoutPackage}.Keys.${attribute.uppercaseUnderscoreName})
  }
#end

#end
#end
#foreach ($relationship in $entity.sortedClassToOneRelationships)
#if (!$relationship.inherited) 
 def ${relationship.name} = {
    storedValueForKey(${entity.classNameWithoutPackage}.Keys.${relationship.uppercaseUnderscoreName}).asInstanceOf[$relationship.actualDestination.classNameWithoutPackage]
  }

 def set${relationship.capitalizedName}Relationship(value :$relationship.actualDestination.classNameWithoutPackage) = {
    logger.debug("updating $relationship.name from " + ${relationship.name} + " to " + value)
   if (value == null) {
     val oldValue: EORelationshipManipulation = ${relationship.name}.asInstanceOf[EORelationshipManipulation]
     if (oldValue != null) {
     removeObjectFromBothSidesOfRelationshipWithKey(oldValue, ${entity.classNameWithoutPackage}.Keys.${relationship.uppercaseUnderscoreName})
      }
    } else {
     addObjectToBothSidesOfRelationshipWithKey(value, ${entity.classNameWithoutPackage}.Keys.${relationship.uppercaseUnderscoreName})
    }
  }
 
#end
#end
#foreach ($relationship in $entity.sortedClassToManyRelationships)
#if (!$relationship.inherited) 
 def ${relationship.name} = {
    storedValueForKey(${entity.classNameWithoutPackage}.Keys.${relationship.uppercaseUnderscoreName}).asInstanceOf[NSArray[${relationship.actualDestination.classNameWithoutPackage}]]
  }
 
  def ${relationship.name}List = {
   ${relationship.name}.asInstanceOf[java.lang.Iterable[${relationship.actualDestination.classNameWithoutPackage}]].toList
  }
 
 def addTo${relationship.capitalizedName}Relationship(eo: $relationship.actualDestination.classNameWithoutPackage) = {
    logger.debug("adding " + eo + " to ${relationship.name} relationship")
    addObjectToBothSidesOfRelationshipWithKey(eo, ${entity.classNameWithoutPackage}.Keys.${relationship.uppercaseUnderscoreName})
  }

 def removeFrom${relationship.capitalizedName}Relationship(eo: $relationship.actualDestination.classNameWithoutPackage) = {
    logger.debug("removing " + eo + " from ${relationship.name} relationship")
    removeObjectFromBothSidesOfRelationshipWithKey(eo, ${entity.classNameWithoutPackage}.Keys.${relationship.uppercaseUnderscoreName})
  }

 def delete${relationship.capitalizedName}Relationship(eo: $relationship.actualDestination.classNameWithoutPackage) = {
    removeObjectFromBothSidesOfRelationshipWithKey(eo, ${entity.classNameWithoutPackage}.Keys.${relationship.uppercaseUnderscoreName})
#if (!$relationship.ownsDestination)
    editingContext.deleteObject(eo)
#end
  }
#end
#end

}