Skip to content

Instantly share code, notes, and snippets.

@hugithordarson
Last active April 14, 2016 22:35
Show Gist options
  • Save hugithordarson/377ed698b6966d25bcd65ccd9970aa7c to your computer and use it in GitHub Desktop.
Save hugithordarson/377ed698b6966d25bcd65ccd9970aa7c to your computer and use it in GitHub Desktop.
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.beans.SimpleBeanInfo;
import java.util.ArrayList;
import java.util.List;
public class ${superClassName}BeanInfo extends SimpleBeanInfo {
#if ( !${object.DeclaredAttributes.isEmpty()} || !${object.DeclaredRelationships.isEmpty()} )
@Override
public PropertyDescriptor[] getPropertyDescriptors() {
try {
List<PropertyDescriptor> list = new ArrayList<>();
#foreach( $attr in ${object.DeclaredAttributes} )
list.add( new PropertyDescriptor( "${attr.Name}", ${superClassName}.class, "${attr.Name}", "set${stringUtils.capitalized($attr.Name)}" ) );
#end
#foreach( $rel in ${object.DeclaredRelationships} )
list.add( new PropertyDescriptor( "${rel.Name}", ${superClassName}.class, "${rel.Name}", "set${stringUtils.capitalized($rel.Name)}" ) );
#end
PropertyDescriptor[] array = new PropertyDescriptor[list.size()];
array = list.toArray( array );
return array;
}
catch( IntrospectionException e ) {
throw new RuntimeException( e );
}
}
#end
}
<plugin>
<groupId>org.apache.cayenne.plugins</groupId>
<artifactId>maven-cayenne-plugin</artifactId>
<version>4.0.M3-SNAPSHOT</version>
<executions>
<execution>
<id>classes</id>
<goals>
<goal>cgen</goal>
</goals>
<configuration>
<destDir>${project.basedir}/src/main/java</destDir>
<superTemplate>${project.basedir}/src/main/resources/wosuperclass.vm</superTemplate>
<map>${project.basedir}/src/main/resources/cayenne/datamap.map.xml</map>
</configuration>
</execution>
<execution>
<id>beaninfo</id>
<goals>
<goal>cgen</goal>
</goals>
<configuration>
<destDir>${project.basedir}/src/main/java</destDir>
<template>${project.basedir}/src/main/resources/beaninfo.vm</template>
<map>${project.basedir}/src/main/resources/cayenne/datamap.map.xml</map>
<outputPattern>/auto/_*BeanInfo.java</outputPattern>
<makePairs>false</makePairs>
<overwrite>true</overwrite>
</configuration>
</execution>
</executions>
</plugin>
## 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.CayenneDataObject 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("${subPackageName}.${subClassName}")}##
${importUtils.addType("${basePackageName}.${baseClassName}")}##
#foreach( $attr in ${object.DeclaredAttributes} )
$importUtils.addType(${attr.Type})##
#end
#foreach( $rel in ${object.DeclaredRelationships} )
$importUtils.addType(${rel.TargetEntity.ClassName})##
#if(${rel.CollectionType})
$importUtils.addType(${rel.CollectionType})##
#end
#end
${importUtils.generate()}
import org.apache.cayenne.ObjectContext;
import org.apache.cayenne.exp.Expression;
import org.apache.cayenne.query.Ordering;
import org.apache.cayenne.query.SelectQuery;
import org.apache.cayenne.exp.Property;
/**
* Class ${superClassName} was generated by Cayenne.
* It is probably a good idea to avoid changing this class manually,
* since it may be overwritten next time code is regenerated.
* If you need to make any customizations, please use subclass.
*/
public abstract class ${superClassName} extends ${baseClassName} {
public static final String ENTITY_NAME = "${object.Name}";
## Create Keys
#foreach( $attr in ${object.DeclaredAttributes} )
public static final Property<${attr.Type}> ${stringUtils.capitalizedAsConstant($attr.Name)} = new Property<>("${attr.Name}");
#end
#foreach( $rel in ${object.DeclaredRelationships} )
public static final Property<${rel.TargetEntity.ClassName}> ${stringUtils.capitalizedAsConstant($rel.Name)} = new Property<>("${rel.Name}");
#end
#if( $object.DbEntity )
#foreach( $idAttr in ${object.DbEntity.PrimaryKeys} )
public static final String ${stringUtils.capitalizedAsConstant($idAttr.Name)}_PK_COLUMN = "${idAttr.Name}";
#end
#end
public static $subClassName create${stringUtils.capitalized($subClassName)}(ObjectContext oc
#foreach( $attr in ${object.DeclaredAttributes} )
#if ($attr.mandatory)
, $importUtils.formatJavaType(${attr.Type}) $attr.name
#end
#end
) {
$subClassName result = new $subClassName();
oc.registerNewObject(result);
#foreach( $attr in ${object.DeclaredAttributes} )
#if ($attr.mandatory)
result.set${stringUtils.capitalized($attr.Name)}($attr.name);
#end
#end
return result;
}
## Create attribute set/get methods
#foreach( $attr in ${object.DeclaredAttributes} )
#if ("true" != "${object.isReadOnly()}")
public void set${stringUtils.capitalized($attr.Name)}($importUtils.formatJavaType(${attr.Type}) $stringUtils.formatVariableName(${attr.Name})) {
writeProperty("${attr.Name}", $stringUtils.formatVariableName(${attr.Name}));
}
#end
#if ( $importUtils.isBoolean(${attr.Type}) )
public boolean is${stringUtils.capitalized($attr.Name)}() {
Boolean value = (Boolean)readProperty("${attr.Name}");
return (value != null) ? value.booleanValue() : false;
}
#elseif ( $importUtils.isNonBooleanPrimitive(${attr.Type}) )
public ${importUtils.formatJavaType($attr.Type)} ${attr.Name}() {
Object value = readProperty("${attr.Name}");
return (value != null) ? ($importUtils.formatJavaTypeAsNonBooleanPrimitive(${attr.Type})) value : 0;
}
#else
public $importUtils.formatJavaType(${attr.Type}) ${attr.Name}() {
return ($importUtils.formatJavaType(${attr.Type}))readProperty("${attr.Name}");
}
#end
#end
##
## Create list add/remove/get/createAnd methods
#foreach( $rel in ${object.DeclaredRelationships} )
#if( $rel.ToMany )
#if ( ! $rel.ReadOnly )
public void addTo${stringUtils.capitalized($rel.Name)}($importUtils.formatJavaType(${rel.TargetEntity.ClassName}) obj) {
addToManyTarget("${rel.name}", obj, true);
}
public void removeFrom${stringUtils.capitalized($rel.Name)}($importUtils.formatJavaType(${rel.TargetEntity.ClassName}) obj) {
removeToManyTarget("${rel.name}", obj, true);
}
public $importUtils.formatJavaType(${rel.TargetEntity.ClassName}) create${stringUtils.capitalized($rel.Name)}Relationship() {
$importUtils.formatJavaType(${rel.TargetEntity.ClassName}) obj = getObjectContext().newObject(${importUtils.formatJavaType(${rel.TargetEntity.ClassName})}.class);
addToManyTarget("${rel.name}", obj, true);
return obj;
}
public void delete${stringUtils.capitalized($rel.Name)}Relationship($importUtils.formatJavaType(${rel.TargetEntity.ClassName}) obj) {
removeToManyTarget("${rel.name}", obj, true);
getObjectContext().deleteObject(obj);
}
#end
@SuppressWarnings("unchecked")
#if ( ${rel.CollectionType} == "java.util.Map")
public $importUtils.formatJavaType($rel.CollectionType)<$importUtils.formatJavaType($entityUtils.getMapKeyType($rel)), $importUtils.formatJavaType($rel.TargetEntity.ClassName)> ${rel.Name}() {
return ($importUtils.formatJavaType($rel.CollectionType)<$importUtils.formatJavaType($entityUtils.getMapKeyType($rel)), $importUtils.formatJavaType($rel.TargetEntity.ClassName)>)readProperty("${rel.name}");
}
#else
public $importUtils.formatJavaType($rel.CollectionType)<$importUtils.formatJavaType($rel.TargetEntity.ClassName)> ${rel.Name}() {
return ($importUtils.formatJavaType($rel.CollectionType)<$importUtils.formatJavaType($rel.TargetEntity.ClassName)>)readProperty("${rel.name}");
}
public $importUtils.formatJavaType($rel.CollectionType)<$importUtils.formatJavaType($rel.TargetEntity.ClassName)> ${rel.Name}(Expression expression) {
return expression.filterObjects(${rel.Name}());
}
#end
#else
#if ( !${object.isReadOnly()} && !$rel.ReadOnly )
public void set${stringUtils.capitalized($rel.Name)}($importUtils.formatJavaType(${rel.TargetEntity.ClassName}) $stringUtils.formatVariableName(${rel.name})) {
setToOneTarget("${rel.name}", $stringUtils.formatVariableName(${rel.name}), true);
}
public $importUtils.formatJavaType(${rel.TargetEntity.ClassName}) create${stringUtils.capitalized($rel.Name)}Relationship() {
$importUtils.formatJavaType(${rel.TargetEntity.ClassName}) obj = getObjectContext().newObject(${importUtils.formatJavaType(${rel.TargetEntity.ClassName})}.class);
setToOneTarget("${rel.name}", obj, true);
return obj;
}
#end
public $importUtils.formatJavaType(${rel.TargetEntity.ClassName}) ${rel.Name}() {
return ($importUtils.formatJavaType(${rel.TargetEntity.ClassName}))readProperty("${rel.name}");
}
#end
#end
##
##callback methods
#foreach($cbname in ${entityUtils.callbackNames})
protected abstract void ${cbname}();
#end
## fetch methods
@SuppressWarnings("unchecked")
public static java.util.List<${subClassName}> fetchAll( ObjectContext oc ) {
SelectQuery<${subClassName}> q = new SelectQuery<>( ${subClassName}.class );
return oc.performQuery( q );
}
@SuppressWarnings("unchecked")
public static java.util.List<${subClassName}> fetch( ObjectContext oc, Expression expression ) {
SelectQuery<${subClassName}> q = new SelectQuery<>( ${subClassName}.class, expression );
return oc.performQuery( q );
}
@SuppressWarnings("unchecked")
public static java.util.List<${subClassName}> fetch( ObjectContext oc, Expression expression, java.util.List<Ordering> orderings ) {
SelectQuery<${subClassName}> q = new SelectQuery<>( ${subClassName}.class, expression );
if ( orderings != null ) {
for( Ordering ordering : orderings ) {
q.addOrdering( ordering );
}
}
return oc.performQuery( q );
}
@SuppressWarnings("unchecked")
public static java.util.List<${subClassName}> fetchAll( ObjectContext oc, java.util.List<Ordering> orderings ) {
SelectQuery<${subClassName}> q = new SelectQuery<>( ${subClassName}.class);
if ( orderings != null ) {
for( Ordering ordering : orderings ) {
q.addOrdering( ordering );
}
}
return oc.performQuery( q );
}
public static ${subClassName} fetchOne(ObjectContext oc, Expression expression) {
java.util.List<${subClassName}> objects = fetch(oc, expression);
${subClassName} obj;
int count = objects.size();
if (count == 0) {
obj = null;
} else if (count == 1) {
obj = objects.get(0);
} else {
throw new IllegalStateException("There was more than one ${subClassName} that matched the qualifier '" + expression + "'.");
}
return obj;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment