Java tutorial
/** * Copyright (C) 2012 Picon software * * This program 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 2 of the License, or * (at your option) any later version. * * This program 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, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ package fr.eoit.db; import fr.eo.exception.PopulatorException; import fr.eoit.db.annotation.DTO; import fr.eoit.db.annotation.DTOField; import fr.eoit.db.annotation.FieldDefinition; import org.apache.commons.beanutils.PropertyUtils; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.Hashtable; import java.util.List; import java.util.Map; /** * @author picon.software * */ public abstract class AbstractDTOPopulator<D, E> { private static Map<Class<?>, List<FieldDefinition>> beanClassCache = new Hashtable<Class<?>, List<FieldDefinition>>(); public String currentQuery; public E populateBean(D data, Class<E> beanClass) throws PopulatorException { E bean = null; try { bean = beanClass.newInstance(); for (FieldDefinition fieldDefinition : getFieldDefinition(beanClass)) { initField(bean, data, fieldDefinition); } } catch (InstantiationException e) { throw new PopulatorException(e); } catch (IllegalAccessException e) { throw new PopulatorException(e); } return bean; } public List<FieldDefinition> getFieldDefinition(Class<?> beanClass) { if (beanClassCache.containsKey(beanClass)) { return beanClassCache.get(beanClass); } else if (beanClass.getAnnotation(DTO.class) != null) { List<FieldDefinition> fieldDefinitions = new ArrayList<FieldDefinition>(); for (Field field : beanClass.getDeclaredFields()) { DTOField fieldAnnotation = field.getAnnotation(DTOField.class); if (fieldAnnotation != null) { FieldDefinition fieldDefinition = new FieldDefinition(fieldAnnotation); if (fieldAnnotation.name().equals("")) { fieldDefinition.columnName = field.getName(); } fieldDefinition.fieldName = field.getName(); fieldDefinitions.add(fieldDefinition); } } fieldDefinitions.addAll(getFieldDefinition(beanClass.getSuperclass())); beanClassCache.put(beanClass, fieldDefinitions); return fieldDefinitions; } else { return new ArrayList<FieldDefinition>(); } } private void initField(E bean, D data, FieldDefinition fieldDefinition) throws PopulatorException { switch (fieldDefinition.type) { case INTEGER: initIntegerField(bean, data, fieldDefinition); break; case LONG: initLongField(bean, data, fieldDefinition); break; case STRING: initStringField(bean, data, fieldDefinition); break; case DOUBLE: initDoubleField(bean, data, fieldDefinition); break; default: break; } } protected void initFieldProperty(E bean, Object obj, FieldDefinition fieldDefinition) throws PopulatorException { try { PropertyUtils.setProperty(bean, fieldDefinition.fieldName, obj); } catch (IllegalAccessException e) { throw new PopulatorException(e); } catch (InvocationTargetException e) { throw new PopulatorException(e); } catch (NoSuchMethodException e) { throw new PopulatorException(e); } } protected abstract void initIntegerField(E bean, D data, FieldDefinition fieldDefinition) throws PopulatorException; protected abstract void initLongField(E bean, D data, FieldDefinition fieldDefinition) throws PopulatorException; protected abstract void initStringField(E bean, D data, FieldDefinition fieldDefinition) throws PopulatorException; protected abstract void initDoubleField(E bean, D data, FieldDefinition fieldDefinition) throws PopulatorException; }