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

com.mycomm.dao.dao4comm.util.ClassAnalysis Maven / Gradle / Ivy

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycomm.dao.dao4comm.util;

import com.mycomm.IProtocol.beans.JDataTypes;
import com.mycomm.IProtocol.sql.annotation.MyColumn;
import com.mycomm.IProtocol.sql.annotation.MyId;
import com.mycomm.dao.dao4comm.annotation.dialect.FieldMetaData;
import com.mycomm.dao.dao4comm.annotation.dialect.FieldTypeDetector;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author jw362j
 */
public class ClassAnalysis {

    private static final Logger log = LoggerFactory.getLogger(ClassAnalysis.class);

    public static Map buildFieldsMap(Field[] fields, Object entity, boolean needRecordId, String[] colums) {
        if (fields == null || fields.length <= 0 || entity == null) {
            throw new RuntimeException("Can not build the FieldMetaData,nullpointer exception!");
        }
        final Map metas = new HashMap();
        long recordId = -1;
        if (colums == null || colums.length <= 0) {
            //load full field into the map

            int firtIndex = 1;
            for (Field field : fields) {
                field.setAccessible(true);
                if (field.isAnnotationPresent(MyId.class)) {
                    if (needRecordId) {
                        try {
                            recordId = field.getLong(entity);
                        } catch (IllegalArgumentException ex) {
                            log.error(ex.getMessage());
                        } catch (IllegalAccessException ex) {
                            log.error(ex.getMessage());
                        }
                    }
                    continue;
                }
                if (field.isAnnotationPresent(MyColumn.class)) {
                    MyColumn myColumn = (MyColumn) field.getAnnotation(MyColumn.class);
                    String columName = myColumn.ColumnName();
                    Object columValue = null;
                    if ("".equals(columName) || columName == null) {
                        columName = field.getName();
                    }

                    try {
                        if (field.get(entity) == null) {
                            if (!myColumn.isColumnNullable()) {
                                throw new IllegalArgumentException("the field is null!the theColum:" + columName + "  should not be null!");
                            }
                        }

                        JDataTypes myType = FieldTypeDetector.getDataType(field);
//                        if (JDataTypes.JNull.equals(myType)) {
//                            continue;
//                        }
                        log.info("the dataType is:" + myType);
                        if (JDataTypes.JBoolean.equals(myType)) {
                            columValue = field.getBoolean(entity);
                        }
                        if (JDataTypes.JByte.equals(myType)) {
                            columValue = field.getByte(entity);
                        }
                        if (JDataTypes.JChar1.equals(myType)) {
                            columValue = field.getChar(entity);
                        }

                        if (JDataTypes.JDate.equals(myType)) {
                            if (field.get(entity) == null) {
                                columValue = "";
                            } else {
                                columValue = field.get(entity);
                            }
                        }

                        if (JDataTypes.JDouble.equals(myType)) {
                            columValue = field.getDouble(entity);
                        }
                        if (JDataTypes.JFloat.equals(myType)) {
                            columValue = field.getFloat(entity);
                        }

                        if (JDataTypes.JInt.equals(myType)) {
                            columValue = field.getInt(entity);
                        }

                        if (JDataTypes.JLong.equals(myType)) {
                            columValue = field.getLong(entity);
                        }

                        if (JDataTypes.JShort.equals(myType)) {
                            columValue = field.getShort(entity);
                        }

                        if (JDataTypes.JString.equals(myType)) {
                            if (field.get(entity) == null) {
                                columValue = "";
                            } else {
                                columValue = field.get(entity);
                            }
                        }else{
                             columValue = field.get(entity);
                        }
                        metas.put(firtIndex++, new FieldMetaData(myType, columValue, columName));
                    } catch (IllegalArgumentException ex) {
                        log.error(ex.getMessage());
                    } catch (IllegalAccessException ex) {
                        log.error(ex.getMessage());
                    }
                }
            }
            if (needRecordId) {
                String tableIdColumName = AnnotationParser.LoadAnnotationStructureStrategy(entity.getClass()).getIdColumName();
                metas.put(firtIndex++, new FieldMetaData(JDataTypes.JLong, recordId, tableIdColumName));
            }
            return metas;
        } else {
            for (Field field : fields) {
                field.setAccessible(true);
                if (field.isAnnotationPresent(MyId.class)) {
                    if (needRecordId) {
                        try {
                            recordId = field.getLong(entity);
                        } catch (IllegalArgumentException ex) {
                            log.error(ex.getMessage());
                        } catch (IllegalAccessException ex) {
                            log.error(ex.getMessage());
                        }
                    }
                }

                if (!field.isAnnotationPresent(MyColumn.class)) {
                    continue;
                }
                MyColumn myColumn = (MyColumn) field.getAnnotation(MyColumn.class);
                Object columValue = null;
                String columName = myColumn.ColumnName();
                JDataTypes myType = FieldTypeDetector.getDataType(field);
//                if (JavaDataTypes.JNull.equals(myType)) {
//                    continue;
//                }
                if ("".equals(columName) || columName == null) {
                    columName = field.getName();
                }
                int position = checkExist(colums, columName);
                if (position >= 0) {

                    try {
                        if (field.get(entity) == null) {
                            if (!myColumn.isColumnNullable()) {
                                throw new IllegalArgumentException("the field is null!the theColum:" + columName + "  should not be null!");
                            }
                        }

                        if (JDataTypes.JBoolean.equals(myType)) {
                            columValue = field.getBoolean(entity);
                        }
                        if (JDataTypes.JByte.equals(myType)) {
                            columValue = field.getByte(entity);
                        }
                        if (JDataTypes.JChar1.equals(myType)) {
                            columValue = field.getChar(entity);
                        }

                        if (JDataTypes.JDate.equals(myType)) {
                            if (field.get(entity) == null) {
                                columValue = "";
                            } else {
                                columValue = field.get(entity);
                            }
                        }

                        if (JDataTypes.JDouble.equals(myType)) {
                            columValue = field.getDouble(entity);
                        }
                        if (JDataTypes.JFloat.equals(myType)) {
                            columValue = field.getFloat(entity);
                        }

                        if (JDataTypes.JInt.equals(myType)) {
                            columValue = field.getInt(entity);
                        }

                        if (JDataTypes.JLong.equals(myType)) {
                            columValue = field.getLong(entity);
                        }

                        if (JDataTypes.JShort.equals(myType)) {
                            columValue = field.getShort(entity);
                        }

                        if (JDataTypes.JString.equals(myType)) {
                            if (field.get(entity) == null) {
                                columValue = "";
                            } else {
                                columValue = field.get(entity);
                            }
                        }
                    } catch (IllegalArgumentException ex) {
                        log.error(ex.getMessage());
                    } catch (IllegalAccessException ex) {
                        log.error(ex.getMessage());
                    }
                    
                    metas.put(position+1, new FieldMetaData(myType, columValue, columName));
                    log.info("the position===11>" + position+",metas size:"+metas.size());
                }

            }
            if (needRecordId) {
                String tableIdColumName = AnnotationParser.LoadAnnotationStructureStrategy(entity.getClass()).getIdColumName();
                metas.put(colums.length+1, new FieldMetaData(JDataTypes.JLong, recordId, tableIdColumName));
            }
            log.info( "metas before returing:"+metas.size());
            return metas;
        }
    }
    public static Map buildFieldsMap(Field[] fields, Object entity){
        return buildFieldsMap(fields, entity,false);
    }

    public static Map buildFieldsMap(Field[] fields, Object entity, boolean needRecordId) {
        log.info("in buildFieldsMap ================>" + entity);
        if (fields == null || fields.length <= 0 || entity == null) {
            throw new RuntimeException("Can not build the FieldMetaData,nullpointer exception!");
        }
        return buildFieldsMap(fields, entity, needRecordId, null);
    }

    private static int checkExist(String[] colums, String it) {
        int position = -1;
        if (it == null || "".equals(it)) {
            return position;
        }
        for (int x = 0; x < colums.length; x++) {
            if (it.equals(colums[x])) {
                return x;
            }
        }

        return position;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy