Package br.net.woodstock.rockframework.domain.persistence.orm.tools

Source Code of br.net.woodstock.rockframework.domain.persistence.orm.tools.DefaultColumnChecker

/*
* This file is part of rockframework.
*
* rockframework is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* rockframework is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>;.
*/
package br.net.woodstock.rockframework.domain.persistence.orm.tools;

import java.lang.annotation.Annotation;
import java.lang.reflect.Proxy;
import java.util.Collection;
import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;

import br.net.woodstock.rockframework.core.reflection.BeanDescriptor;
import br.net.woodstock.rockframework.core.reflection.PropertyDescriptor;
import br.net.woodstock.rockframework.core.utils.Conditions;

public class DefaultColumnChecker implements ColumnChecker {

  public DefaultColumnChecker() {
    super();
  }

  // Types
  @Override
  public void checkBinaryColumn(final String name, final PropertyDescriptor property, final Collection<String> errors) {
    Class<?> clazz = property.getType();
    Class<?> byteClass = byte.class;
    if (!((clazz.isArray()) && (clazz.getComponentType().equals(byteClass)))) {
      errors.add("Invalid @Lob on " + this.getAttributeName(property));
    }
    this.checkBinaryColumnName(this.getColumnName(property), property, errors);
  }

  @Override
  public void checkDateColumn(final String name, final PropertyDescriptor property, final Collection<String> errors) {
    Class<?> clazz = property.getType();
    if (!Date.class.isAssignableFrom(clazz)) {
      errors.add("Invalid @Temporal on " + this.getAttributeName(property));
    }
    this.checkDateColumnName(this.getColumnName(property), property, errors);
  }

  @Override
  public void checkEnumColumn(final String name, final PropertyDescriptor property, final Collection<String> errors) {
    Class<?> clazz = property.getType();
    if (!clazz.isEnum()) {
      errors.add("Invalid @Enumerated on " + this.getAttributeName(property));
    }
    this.checkEnumColumnName(this.getColumnName(property), property, errors);
  }

  @Override
  public void checkIdColumn(final String name, final PropertyDescriptor property, final Collection<String> errors) {
    this.checkIdColumnName(this.getColumnName(property), property, errors);
  }

  @Override
  public void checkNumericColumn(final String name, final PropertyDescriptor property, final Collection<String> errors) {
    this.checkNumericColumnName(this.getColumnName(property), property, errors);
  }

  @Override
  public void checkStringColumn(final String name, final PropertyDescriptor property, final Collection<String> errors) {
    Column column = property.getAnnotation(Column.class);
    int length = column.length();
    if (length == 255) {
      errors.add("Check lenght of " + this.getAttributeName(property));
    }
    this.checkStringColumnName(this.getColumnName(property), property, errors);
  }

  // Relations
  @Override
  public void checkOneToOne(final String name, final PropertyDescriptor property, final Collection<String> errors) {
    OneToOne oneToOne = property.getAnnotation(OneToOne.class);
    CascadeType[] cascades = oneToOne.cascade();
    FetchType fetchType = oneToOne.fetch();
    String mappedBy = oneToOne.mappedBy();

    this.checkCascade(errors, oneToOne, property, cascades);
    this.checkFetch(errors, oneToOne, property, fetchType);

    if (Conditions.isEmpty(mappedBy)) {
      if (!property.isAnnotationPresent(JoinColumn.class)) {
        errors.add("Missing @OneToOne(mappedBy) or @JoinColumn on " + this.getAttributeName(property));
      } else {
        JoinColumn joinColumn = property.getAnnotation(JoinColumn.class);
        if (oneToOne.optional() != joinColumn.nullable()) {
          errors.add("Conflict in @OneToOne(optional) and @JoinColumn(nullable) on " + this.getAttributeName(property));
        }
      }
    }
  }

  @Override
  public void checkOneToMany(final String name, final PropertyDescriptor property, final Collection<String> errors) {
    OneToMany oneToMany = property.getAnnotation(OneToMany.class);
    CascadeType[] cascades = oneToMany.cascade();
    FetchType fetchType = oneToMany.fetch();
    String mappedBy = oneToMany.mappedBy();

    this.checkCascade(errors, oneToMany, property, cascades);
    this.checkFetch(errors, oneToMany, property, fetchType);
    this.checkMappedBy(errors, oneToMany, property, mappedBy);
  }

  @Override
  public void checkManyToOne(final String name, final PropertyDescriptor property, final Collection<String> errors) {
    ManyToOne manyToOne = property.getAnnotation(ManyToOne.class);
    CascadeType[] cascades = manyToOne.cascade();
    FetchType fetchType = manyToOne.fetch();

    this.checkCascade(errors, manyToOne, property, cascades);
    this.checkFetch(errors, manyToOne, property, fetchType);

    if (!property.isAnnotationPresent(JoinColumn.class)) {
      errors.add("Missing @JoinColumn on " + this.getAttributeName(property));
    } else {
      JoinColumn joinColumn = property.getAnnotation(JoinColumn.class);
      if (manyToOne.optional() != joinColumn.nullable()) {
        errors.add("Conflict in @ManyToOne(optional) and @JoinColumn(nullable) on " + this.getAttributeName(property));
      }
    }
  }

  @Override
  public void checkManyToMany(final String name, final PropertyDescriptor property, final Collection<String> errors) {
    ManyToMany manyToMany = property.getAnnotation(ManyToMany.class);
    CascadeType[] cascades = manyToMany.cascade();
    FetchType fetchType = manyToMany.fetch();

    this.checkCascade(errors, manyToMany, property, cascades);
    this.checkFetch(errors, manyToMany, property, fetchType);

    if (!property.isAnnotationPresent(JoinTable.class)) {
      errors.add("Missing @JoinTable on " + this.getAttributeName(property));
    }
  }

  // Aux
  private void checkCascade(final Collection<String> errors, final Annotation annotation, final PropertyDescriptor property, final CascadeType[] cascades) {
    if (Conditions.isEmpty(cascades)) {
      errors.add("Missing value @" + this.getAnnotationName(annotation) + "(cascade) on " + this.getAttributeName(property));
    }
  }

  private void checkFetch(final Collection<String> errors, final Annotation annotation, final PropertyDescriptor property, final FetchType fetchType) {
    if (fetchType != FetchType.LAZY) {
      errors.add("Illegal FetchType @" + this.getAnnotationName(annotation) + "(fetch) on " + this.getAttributeName(property));
    }
  }

  private void checkMappedBy(final Collection<String> errors, final Annotation annotation, final PropertyDescriptor property, final String mappedBy) {
    if (Conditions.isEmpty(mappedBy)) {
      errors.add("Missing value @" + this.getAnnotationName(annotation) + "(mappedBy) on " + this.getAttributeName(property));
    }
  }

  private String getAnnotationName(final Annotation annotation) {
    if (Proxy.isProxyClass(annotation.getClass())) {
      String str = annotation.toString();
      str = str.substring(1);
      if (str.indexOf('.') != -1) {
        str = str.substring(str.lastIndexOf('.') + 1);
      }
      if (str.indexOf('(') != -1) {
        str = str.substring(0, str.indexOf('('));
      }
      return str;
    }
    return annotation.getClass().getCanonicalName();
  }

  protected String getAttributeName(final PropertyDescriptor property) {
    BeanDescriptor bean = property.getBeanDescriptor();
    return bean.getType().getCanonicalName() + "." + property.getName();
  }

  protected String getColumnName(final PropertyDescriptor property) {
    Column column = property.getAnnotation(Column.class);
    String columnName = column.name();

    if (Conditions.isEmpty(columnName)) {
      columnName = "";
    }
    return columnName;
  }

  // Names
  @SuppressWarnings("unused")
  public void checkBinaryColumnName(final String name, final PropertyDescriptor property, final Collection<String> errors) {
    //
  }

  @SuppressWarnings("unused")
  public void checkDateColumnName(final String name, final PropertyDescriptor property, final Collection<String> errors) {
    //
  }

  @SuppressWarnings("unused")
  public void checkEnumColumnName(final String name, final PropertyDescriptor property, final Collection<String> errors) {
    //
  }

  @SuppressWarnings("unused")
  public void checkIdColumnName(final String name, final PropertyDescriptor property, final Collection<String> errors) {
    //
  }

  @SuppressWarnings("unused")
  public void checkNumericColumnName(final String name, final PropertyDescriptor property, final Collection<String> errors) {
    //
  }

  @SuppressWarnings("unused")
  public void checkStringColumnName(final String name, final PropertyDescriptor property, final Collection<String> errors) {
    //
  }

}
TOP

Related Classes of br.net.woodstock.rockframework.domain.persistence.orm.tools.DefaultColumnChecker

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.