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

.mardao-maven-plugin.3.0.5.source-code.Mapper.vm Maven / Gradle / Ivy

There is a newer version: 3.0.13
Show newest version
#set( $allFields = $entity.allFields )
package ${daoBasePackage};

import java.util.Collection;
import java.util.Date;

import net.sf.mardao.dao.Mapper;
import net.sf.mardao.dao.Supplier;
import net.sf.mardao.domain.AbstractEntityBuilder;
import ${entity.className};

/**
 * The ${entity.simpleName} domain-object specific mapping methods go here.
 *
 * Generated on ${currentDate}.
 * @author mardao DAO generator (${helper.class.name})
 */
public class ${entity.simpleName}Mapper
  implements Mapper<${entity.simpleName}, ${entity.pk.simpleType}> {

  private final Supplier supplier;

  public enum Field {
#if( $entity.parent )
    ${entity.parent.nameUpper}("${entity.parent.name}"),
#end
    ${entity.pk.nameUpper}("${entity.pk.name}")#foreach( $field in $allFields ),
    ${field.nameUpper}("${field.columnName}")#end;

    private final String fieldName;

    Field(String fieldName) {
      this.fieldName = fieldName;
    }

    public String getFieldName() {
      return fieldName;
    }
  }

  public ${entity.simpleName}Mapper(Supplier supplier) {
    this.supplier = supplier;
  }

  @Override
  public ${entity.pk.simpleType} fromKey(Object key) {
    return supplier.to${entity.pk.simpleType}Key(key);
  }

  @Override
  public ${entity.simpleName} fromReadValue(Object value) {
    final ${entity.simpleName} entity = new ${entity.simpleName}();

    // set primary key:
    final Object key = supplier.getKey(value, Field.${entity.pk.nameUpper}.getFieldName());
    entity.set${entity.pk.nameFirst}(supplier.to${entity.pk.simpleType}Key(key));

#if( $entity.parent )
    // set parent key:
    entity.set${entity.parent.nameFirst}(supplier.getParentKey(value, Field.${entity.parent.nameUpper}.getFieldName()));

#end
    // set all fields:
#foreach( $field in $allFields )
    entity.set${field.nameFirst}(supplier.get${field.simpleType}(value, Field.${field.nameUpper}.getFieldName()));
#end
    return entity;
  }

  @Override
  public String getCreatedByColumnName() {
#if( $entity.createdBy )
    return Field.${entity.createdBy.nameUpper}.getFieldName();
#else
    return null;
#end
  }

  @Override
  public String getCreatedDateColumnName() {
#if( $entity.createdDate )
    return Field.${entity.createdDate.nameUpper}.getFieldName();
#else
    return null;
#end
  }

  @Override
  public String getUpdatedByColumnName() {
#if( $entity.updatedBy )
    return Field.${entity.updatedBy.nameUpper}.getFieldName();
#else
    return null;
#end
  }

  @Override
  public String getUpdatedDateColumnName() {
#if( $entity.updatedDate )
    return Field.${entity.updatedDate.nameUpper}.getFieldName();
#else
    return null;
#end
  }

  @Override
  public ${entity.pk.simpleType} getId(${entity.simpleName} entity) {
    return entity != null ? entity.get${entity.pk.nameFirst}() : null;
  }

  @Override
  public Object getParentKey(${entity.simpleName} entity) {
#if( $entity.parent )
    return null != entity ? entity.get${entity.parent.nameFirst}() : null;
#else
    return null;
#end
  }

  @Override
  public void setParentKey(${entity.simpleName} entity, Object parentKey) {
#if( $entity.parent )
    entity.set${entity.parent.nameFirst}(parentKey);
#else
    // this entity has no parent
#end
  }

  @Override
  public void updateEntityPostWrite(${entity.simpleName} entity, Object key, Object value) {
    entity.set${entity.pk.nameFirst}(supplier.to${entity.pk.simpleType}Key(key));
#if( $entity.createdBy )
    entity.set${entity.createdBy.nameFirst}(supplier.getString(value, Field.${entity.createdBy.nameUpper}.getFieldName()));
#end
#if( $entity.createdDate )
    entity.set${entity.createdDate.nameFirst}(supplier.getDate(value, Field.${entity.createdDate.nameUpper}.getFieldName()));
#end
#if( $entity.updatedBy )
    entity.set${entity.updatedBy.nameFirst}(supplier.getString(value, Field.${entity.updatedBy.nameUpper}.getFieldName()));
#end
#if( $entity.updatedDate )
    entity.set${entity.updatedDate.nameFirst}(supplier.getDate(value, Field.${entity.updatedDate.nameUpper}.getFieldName()));
#end
}

@Override
  public String getKind() {
    return ${entity.simpleName}.class.getSimpleName();
  }

  @Override
  public Object toKey(Object parentKey, ${entity.pk.simpleType} id) {
    return supplier.toKey(parentKey, getKind(), id);
  }

  @Override
  public Object toWriteValue(${entity.simpleName} entity) {
    final ${entity.pk.simpleType} id = getId(entity);
    final Object parentKey = getParentKey(entity);
    final Object value = supplier.createWriteValue(parentKey, getKind(), id);

    // set all fields:
#foreach( $field in $allFields )
    supplier.set${field.simpleType}(value, Field.${field.nameUpper}.getFieldName(), entity.get${field.nameFirst}());
#end
    return value;
  }

  public static ${entity.simpleName}Builder newBuilder() {
    return new ${entity.simpleName}Builder();
  }

  public static class ${entity.simpleName}Builder extends AbstractEntityBuilder<${entity.simpleName}> {

    @Override
    protected ${entity.simpleName} newInstance() {
      return new ${entity.simpleName}();
    }

    public ${entity.simpleName}Builder ${entity.pk.name}(${entity.pk.simpleType} ${entity.pk.name}) {
      entity.set${entity.pk.nameFirst}(${entity.pk.name});
      return this;
    }

#foreach( $field in $allFields )
    public ${entity.simpleName}Builder ${field.name}(${field.simpleType} ${field.name}) {
      entity.set${field.nameFirst}(${field.name});
      return this;
    }
#end
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy