
templates.v4_1.client-superclass.vm Maven / Gradle / Ivy
## Licensed to the Apache Software Foundation (ASF) under one
## or more contributor license agreements. See the NOTICE file
## distributed with this work for additional information
## regarding copyright ownership. The ASF licenses this file
## to you under the Apache License, Version 2.0 (the
## "License"); you may not use this file except in compliance
## with the License. You may obtain a copy of the License at
##
## http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing,
## software distributed under the License is distributed on an
## "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
## KIND, either express or implied. See the License for the
## specific language governing permissions and limitations
## under the License.
##
##Terminology:
## Base class - super superclass of entity, ie, org.apache.cayenne.PersistentObject or MyBaseClass
## Super class - superclass of entity, ie, org.apache.cayenne.art.auto._Artist
## Sub class - class of entity, ie, org.apache.cayenne.art.Artist
##
## Classes available in template
## object (duplicated as 'objEntity') - the ObjEntity class: See org.apache.cayenne.map.ObjectEntity
## stringUtils - class for string "helper" functions: See org.apache.cayenne.gen.StringUtils
## entityUtils - class for entity "helper" functions: See org.apache.cayenne.gen.EntityUtils
## importUtils - class for import statement management: See org.apache.cayenne.gen.ImportUtils
## superClassName
## superPackageName
## subClassName
## subPackageName
## baseClassName
## basePackageName
##
##
${importUtils.setPackage($superPackageName)}##
${importUtils.addReservedType("${$superPackageName}.${superClassName}")}##
${importUtils.addType("${basePackageName}.${baseClassName}")}##
#if((${object.DeclaredAttributes} && !${object.DeclaredAttributes.isEmpty()}) || (${object.DeclaredRelationships} && !${object.DeclaredRelationships.isEmpty()}))
${importUtils.addType('org.apache.cayenne.exp.Property')}##
#end
#foreach( $attr in ${object.DeclaredAttributes} )
$importUtils.addType(${attr.Type})##
#end
#foreach( $rel in ${object.DeclaredRelationships} )
$importUtils.addType(${rel.TargetEntity.ClientClassName})##
#if(${rel.CollectionType})
$importUtils.addType(${rel.CollectionType})##
#end
#end
#if( ${entityUtils.hasToOneDeclaredRelationships()} )
${importUtils.addType('org.apache.cayenne.ValueHolder')}##
${importUtils.addType('org.apache.cayenne.util.PersistentObjectHolder')}##
#end
#if( ${entityUtils.hasToManyDeclaredRelationships()} )
${importUtils.addType('org.apache.cayenne.util.PersistentObjectList')}##
#end
${importUtils.generate()}
/**
* A generated persistent class mapped as "${object.name}" Cayenne entity. It is a good idea to
* avoid changing this class manually, since it will be overwritten next time code is
* regenerated. If you need to make any customizations, put them in a subclass.
*/
public abstract class ${superClassName} extends ${baseClassName} {
## Create ivars names
#if( $createPropertyNames )
#foreach( $attr in ${object.DeclaredAttributes} )
public static final String ${stringUtils.capitalizedAsConstant($attr.Name)}_PROPERTY = "${attr.Name}";
#end
#foreach( $rel in ${object.DeclaredRelationships} )
public static final String ${stringUtils.capitalizedAsConstant($rel.Name)}_PROPERTY = "${rel.Name}";
#end
#end
## Create Properties
#foreach( $attr in ${object.DeclaredAttributes} )
#set ( $type = "$importUtils.formatJavaType(${attr.Type}, false)" )
public static final Property<$type> ${stringUtils.capitalizedAsConstant($attr.Name)} = Property.create("${attr.Name}", ${stringUtils.stripGeneric($type)}.class);
#end
#foreach( $rel in ${object.DeclaredRelationships} )
#if( $rel.ToMany )
#if ( ${rel.CollectionType} == "java.util.Map")
#set( $type = "$importUtils.formatJavaType($rel.CollectionType)<$importUtils.formatJavaType($entityUtils.getMapKeyType($rel)), $importUtils.formatJavaType($rel.TargetEntity.ClientClassName)>" )
public static final Property<$type> ${stringUtils.capitalizedAsConstant($rel.Name)} = Property.create("${rel.Name}", ${stringUtils.stripGeneric($type)}.class);
#else
#set( $type = "$importUtils.formatJavaType($rel.CollectionType)<$importUtils.formatJavaType($rel.TargetEntity.ClientClassName)>" )
public static final Property<$type> ${stringUtils.capitalizedAsConstant($rel.Name)} = Property.create("${rel.Name}", ${stringUtils.stripGeneric($type)}.class);
#end
#else
#set( $type = "$importUtils.formatJavaType(${rel.TargetEntity.ClientClassName})" )
public static final Property<$type> ${stringUtils.capitalizedAsConstant($rel.Name)} = Property.create("${rel.Name}", ${stringUtils.stripGeneric($type)}.class);
#end
#end
## Create ivars
#foreach( $attr in ${object.DeclaredAttributes} )
protected $importUtils.formatJavaType(${attr.Type}) ${attr.Name};
#end
#foreach( $rel in ${object.DeclaredRelationships} )
#if( $rel.ToMany )
#if ( ${rel.CollectionType} == "java.util.Map")
protected $importUtils.formatJavaType($rel.CollectionType)<$importUtils.formatJavaType($entityUtils.getMapKeyType($rel)), $importUtils.formatJavaType($rel.TargetEntity.ClientClassName)> ${rel.Name};
#else
protected $importUtils.formatJavaType($rel.CollectionType)<$importUtils.formatJavaType($rel.TargetEntity.ClientClassName)> ${rel.Name};
#end
#else
protected ValueHolder<$importUtils.formatJavaType($rel.TargetEntity.ClientClassName)> ${rel.Name};
#end
#end
## Create attribute set/get methods
#foreach( $attr in ${object.DeclaredAttributes} )
#set( $attrName = $stringUtils.formatVariableName(${attr.Name}))
#if ( $importUtils.isBoolean(${attr.Type}) )
public boolean is${stringUtils.capitalized($attr.Name)}() {
if(objectContext != null) {
objectContext.prepareForAccess(this, "${attr.Name}", false);
}
return $attrName;
}
#else
public $importUtils.formatJavaType(${attr.Type}) get${stringUtils.capitalized($attr.Name)}() {
if(objectContext != null) {
objectContext.prepareForAccess(this, "${attr.Name}", false);
}
return $attrName;
}
#end
#if ("true" != "${object.isReadOnly()}")
public void set${stringUtils.capitalized($attr.Name)}($importUtils.formatJavaType(${attr.Type}) $attrName) {
if(objectContext != null) {
objectContext.prepareForAccess(this, "${attr.Name}", false);
objectContext.propertyChanged(this, "${attr.Name}", this.$attrName, $attrName);
}
this.$attrName = $attrName;
}
#end
#end
##
##
## Create list add/remove/get methods
#foreach( $rel in ${object.DeclaredRelationships} )
#set( $relName = $stringUtils.formatVariableName(${rel.Name}))
#if( $rel.ToMany )
#if ( ${rel.CollectionType} == "java.util.Map")
public $importUtils.formatJavaType($rel.CollectionType)<$importUtils.formatJavaType($entityUtils.getMapKeyType($rel)), $importUtils.formatJavaType($rel.TargetEntity.ClientClassName)> get${stringUtils.capitalized($rel.Name)}() {
#else
public $importUtils.formatJavaType($rel.CollectionType)<$importUtils.formatJavaType($rel.TargetEntity.ClientClassName)> get${stringUtils.capitalized($rel.Name)}() {
#end
if(objectContext != null) {
objectContext.prepareForAccess(this, "${rel.Name}", true);
} else if (this.$relName == null) {
#if ( ${rel.CollectionType} == "java.util.Map")
throw new RuntimeException("Map relationships cannot be accessed for transient objects");
#else
this.$relName = new PersistentObjectList<>(this, "${rel.Name}");
#end
}
return $relName;
}
#if ( ! $rel.ReadOnly )
#if ( ${rel.CollectionType} == "java.util.Map")
public void addTo${stringUtils.capitalized($rel.Name)}($importUtils.formatJavaType(${rel.TargetEntity.ClientClassName}) object) {
if(objectContext != null) {
objectContext.prepareForAccess(this, "${rel.Name}", true);
} else if (this.$relName == null) {
throw new RuntimeException("Map relationships cannot be accessed for transient objects");
}
this.${relName}.put(getMapKey("${rel.Name}", object), object);
}
public void removeFrom${stringUtils.capitalized($rel.Name)}($importUtils.formatJavaType(${rel.TargetEntity.ClientClassName}) object) {
if(objectContext != null) {
objectContext.prepareForAccess(this, "${rel.Name}", true);
} else if (this.$relName == null) {
throw new RuntimeException("Map relationships cannot be accessed for transient objects");
}
this.${relName}.remove(getMapKey("${rel.Name}", object));
}
#else
public void addTo${stringUtils.capitalized($rel.Name)}($importUtils.formatJavaType(${rel.TargetEntity.ClientClassName}) object) {
if(objectContext != null) {
objectContext.prepareForAccess(this, "${rel.Name}", true);
} else if (this.$relName == null) {
this.$relName = new PersistentObjectList<>(this, "${rel.Name}");
}
this.${relName}.add(object);
}
public void removeFrom${stringUtils.capitalized($rel.Name)}($importUtils.formatJavaType(${rel.TargetEntity.ClientClassName}) object) {
if(objectContext != null) {
objectContext.prepareForAccess(this, "${rel.Name}", true);
} else if (this.$relName == null) {
this.$relName = new PersistentObjectList<>(this, "${rel.Name}");
}
this.${relName}.remove(object);
}
#end
#end
#else
public $importUtils.formatJavaType(${rel.TargetEntity.ClientClassName}) get${stringUtils.capitalized($rel.Name)}() {
if(objectContext != null) {
objectContext.prepareForAccess(this, "$rel.Name", true);
} else if (this.$relName == null) {
this.$relName = new PersistentObjectHolder<>(this, "$rel.Name");
}
return ${relName}.getValue();
}
#if ( !${object.isReadOnly()} && !$rel.ReadOnly )
public void set${stringUtils.capitalized($rel.Name)}(${importUtils.formatJavaType($rel.TargetEntity.ClientClassName)} $relName) {
if(objectContext != null) {
objectContext.prepareForAccess(this, "$rel.Name", true);
objectContext.propertyChanged(this, "$rel.Name", this.${relName}.getValueDirectly(), $relName);
} else if (this.$relName == null) {
this.$relName = new PersistentObjectHolder<>(this, "$rel.Name");
}
this.${relName}.setValue($relName);
}
#end
#end
#end
##callback methods
#foreach( $cbname in ${entityUtils.callbackNames})
protected abstract void ${cbname}();
#end
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy