/*
* 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.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.Column;
import javax.persistence.Enumerated;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Temporal;
import br.net.woodstock.rockframework.core.RockFrameworkVersion;
import br.net.woodstock.rockframework.core.reflection.BeanDescriptor;
import br.net.woodstock.rockframework.core.reflection.ClassFinder;
import br.net.woodstock.rockframework.core.reflection.PropertyDescriptor;
import br.net.woodstock.rockframework.core.reflection.impl.AssignableClassFilter;
import br.net.woodstock.rockframework.core.reflection.impl.BeanDescriptorBuilder;
import br.net.woodstock.rockframework.core.reflection.impl.ClassFinderImpl;
import br.net.woodstock.rockframework.core.util.Assert;
import br.net.woodstock.rockframework.domain.Entity;
public class MappingChecker implements Serializable {
private static final long serialVersionUID = RockFrameworkVersion.VERSION;
private ColumnChecker columnChecker;
public MappingChecker(final ColumnChecker columnChecker) {
super();
this.columnChecker = columnChecker;
}
public Collection<String> getErrors(final String baseName) {
Assert.notEmpty(baseName, "baseName");
Collection<String> collection = new ArrayList<String>();
ClassFinder classFinder = new ClassFinderImpl(baseName, new AssignableClassFilter(Entity.class));
for (Class<?> clazz : classFinder.getClasses()) {
BeanDescriptor beanDescriptor = new BeanDescriptorBuilder(clazz).getBeanDescriptor();
for (PropertyDescriptor propertyDescriptor : beanDescriptor.getProperties()) {
String name = propertyDescriptor.getName();
if (propertyDescriptor.isAnnotationPresent(Column.class)) {
if (propertyDescriptor.isAnnotationPresent(Enumerated.class)) {
this.columnChecker.checkEnumColumn(name, propertyDescriptor, collection);
}
if (propertyDescriptor.isAnnotationPresent(Id.class)) {
this.columnChecker.checkIdColumn(name, propertyDescriptor, collection);
}
if (propertyDescriptor.isAnnotationPresent(Lob.class)) {
this.columnChecker.checkBinaryColumn(name, propertyDescriptor, collection);
}
if (propertyDescriptor.isAnnotationPresent(Temporal.class)) {
this.columnChecker.checkDateColumn(name, propertyDescriptor, collection);
}
if (propertyDescriptor.getType().equals(String.class)) {
this.columnChecker.checkStringColumn(name, propertyDescriptor, collection);
}
if (Number.class.isAssignableFrom(propertyDescriptor.getType())) {
this.columnChecker.checkNumericColumn(name, propertyDescriptor, collection);
}
}
if (propertyDescriptor.isAnnotationPresent(ManyToMany.class)) {
this.columnChecker.checkManyToMany(name, propertyDescriptor, collection);
}
if (propertyDescriptor.isAnnotationPresent(ManyToOne.class)) {
this.columnChecker.checkManyToOne(name, propertyDescriptor, collection);
}
if (propertyDescriptor.isAnnotationPresent(OneToMany.class)) {
this.columnChecker.checkOneToMany(name, propertyDescriptor, collection);
}
if (propertyDescriptor.isAnnotationPresent(OneToOne.class)) {
this.columnChecker.checkOneToOne(name, propertyDescriptor, collection);
}
}
}
return collection;
}
}