All Downloads are FREE. Search and download functionalities are using the official Maven repository.

templates.v1_2.client-superclass.vm Maven / Gradle / Ivy

There is a newer version: 5.0-M1
Show newest version
##   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 ${rel.Name};
#end
#end
## Create attribute set/get methods
#foreach( $attr in ${object.DeclaredAttributes} )
#if ( $importUtils.isBoolean(${attr.Type}) )

    public boolean is${stringUtils.capitalized($attr.Name)}() {
        if(objectContext != null) {
            objectContext.prepareForAccess(this, "${attr.Name}", false);
        }

        return ${attr.Name};
    }
#else

    public $importUtils.formatJavaType(${attr.Type}) get${stringUtils.capitalized($attr.Name)}() {
        if(objectContext != null) {
            objectContext.prepareForAccess(this, "${attr.Name}", false);
        }

        return ${attr.Name};
    }
#end
#if ("true" != "${object.isReadOnly()}")

    public void set${stringUtils.capitalized($attr.Name)}($importUtils.formatJavaType(${attr.Type}) $stringUtils.formatVariableName(${attr.Name})) {
        if(objectContext != null) {
            objectContext.prepareForAccess(this, "${attr.Name}", false);
            objectContext.propertyChanged(this, "${attr.Name}", this.${stringUtils.formatVariableName($attr.Name)}, $stringUtils.formatVariableName(${attr.Name}));
        }
        
        this.${stringUtils.formatVariableName($attr.Name)} = ${stringUtils.formatVariableName($attr.Name)};
    }
#end
#end
##
##
## Create list add/remove/get methods
#foreach( $rel in ${object.DeclaredRelationships} )
#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.$rel.Name == null) {
#if ( ${rel.CollectionType} == "java.util.Map")
        	throw new RuntimeException("Map relationships cannot be accessed for transient objects");
#else
        	this.$rel.Name = new PersistentObjectList(this, "${rel.Name}");
#end
		}

        return ${rel.Name};
    }
#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.$rel.Name == null) {
        	throw new RuntimeException("Map relationships cannot be accessed for transient objects");        
        }

        this.${rel.Name}.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.$rel.Name == null) {
        	throw new RuntimeException("Map relationships cannot be accessed for transient objects");        
        }

        this.${rel.Name}.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.$rel.Name == null) {
        	this.$rel.Name = new PersistentObjectList(this, "${rel.Name}");
		}

        this.${rel.Name}.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.$rel.Name == null) {
        	this.$rel.Name = new PersistentObjectList(this, "${rel.Name}");
		}

        this.${rel.Name}.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.$rel.Name == null) {
        	this.$rel.Name = new PersistentObjectHolder(this, "$rel.Name");
		}

        return ($importUtils.formatJavaType(${rel.TargetEntity.ClientClassName})) ${rel.Name}.getValue();
    }
#if ( !${object.isReadOnly()} && !$rel.ReadOnly )

    public void set${stringUtils.capitalized($rel.Name)}(${importUtils.formatJavaType($rel.TargetEntity.ClientClassName)} $stringUtils.formatVariableName(${rel.Name})) {
        if(objectContext != null) {
            objectContext.prepareForAccess(this, "${rel.Name}", true);
            objectContext.propertyChanged(this, "$rel.Name", this.${rel.Name}.getValueDirectly(), $stringUtils.formatVariableName(${rel.Name}));
        } else if (this.$rel.Name == null) {
        	this.$rel.Name = new PersistentObjectHolder(this, "$rel.Name");
		}
        
        this.${stringUtils.formatVariableName($rel.Name)}.setValue(${stringUtils.formatVariableName($rel.Name)});
    }
#end
#end
#end
##callback methods
#foreach( $cbname in ${entityUtils.callbackNames})

    protected abstract void ${cbname}();
#end
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy