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

link.jfire.sql.util.DaoFactory Maven / Gradle / Ivy

package link.jfire.sql.util;

import java.lang.reflect.Field;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import link.jfire.baseutil.reflect.ReflectUtil;
import link.jfire.baseutil.verify.Verify;
import link.jfire.sql.annotation.Id;
import link.jfire.sql.annotation.TableEntity;
import link.jfire.sql.field.MapField;
import link.jfire.sql.field.impl.BooleanField;
import link.jfire.sql.field.impl.CalendarField;
import link.jfire.sql.field.impl.DateField;
import link.jfire.sql.field.impl.DoubleField;
import link.jfire.sql.field.impl.FloatField;
import link.jfire.sql.field.impl.IntField;
import link.jfire.sql.field.impl.IntegerField;
import link.jfire.sql.field.impl.LongField;
import link.jfire.sql.field.impl.StringField;
import link.jfire.sql.field.impl.TimeField;
import link.jfire.sql.field.impl.TimestampField;
import link.jfire.sql.field.impl.WBooleanField;
import link.jfire.sql.field.impl.WDoubleField;
import link.jfire.sql.field.impl.WFloatField;
import link.jfire.sql.field.impl.WLongField;
import link.jfire.sql.function.DAOBean;
import link.jfire.sql.function.impl.DAOBeanImpl;

public class DaoFactory
{
    private static Map, DAOBean> daoMap = new HashMap<>();
    
    public static DAOBean getDaoBean(Class entityClass)
    {
        return daoMap.get(entityClass);
    }
    
    public static void buildDaoBean(Set set)
    {
        for (String each : set)
        {
            try
            {
                Class ckass = Class.forName(each);
                if (ckass.isAnnotationPresent(TableEntity.class))
                {
                    if (hasIdField(ckass))
                    {
                        daoMap.put(ckass, new DAOBeanImpl(ckass));
                    }
                }
            }
            catch (ClassNotFoundException e)
            {
                throw new RuntimeException(e);
            }
        }
        
    }
    
    private static boolean hasIdField(Class ckass)
    {
        Field[] fields = ReflectUtil.getAllFields(ckass);
        for (Field each : fields)
        {
            if (each.isAnnotationPresent(Id.class))
            {
                return true;
            }
        }
        return false;
    }
    
    public static MapField buildMapField(Field field)
    {
        Class fieldType = field.getType();
        if (fieldType.equals(Integer.class))
        {
            return new IntegerField(field);
        }
        else if (fieldType.equals(String.class))
        {
            return new StringField(field);
        }
        else if (fieldType.equals(Float.class))
        {
            return new WFloatField(field);
        }
        else if (fieldType.equals(Long.class))
        {
            return new WLongField(field);
        }
        else if (fieldType.equals(Boolean.class))
        {
            return new WBooleanField(field);
        }
        else if (fieldType.equals(Double.class))
        {
            return new WDoubleField(field);
        }
        else if (fieldType.equals(int.class))
        {
            return new IntField(field);
        }
        else if (fieldType.equals(long.class))
        {
            return new LongField(field);
        }
        else if (fieldType.equals(float.class))
        {
            return new FloatField(field);
        }
        else if (fieldType.equals(double.class))
        {
            return new DoubleField(field);
        }
        else if (fieldType.equals(boolean.class))
        {
            return new BooleanField(field);
        }
        else if (fieldType.equals(Date.class))
        {
            return new DateField(field);
        }
        else if (fieldType.equals(java.util.Date.class))
        {
            return new DateField(field);
        }
        else if (fieldType.equals(Time.class))
        {
            return new TimeField(field);
        }
        else if (fieldType.equals(Timestamp.class))
        {
            return new TimestampField(field);
        }
        else if (fieldType.equals(Calendar.class))
        {
            return new CalendarField(field);
        }
        else
        {
            Verify.error("属性{}.{}的类型尚未支持", field.getDeclaringClass(), field.getName());
            return null;
        }
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy