Search in sources :

Example 11 with ORMConfigBean

use of org.eweb4j.orm.config.bean.ORMConfigBean in project eweb4j-framework by laiweiwei.

the class PojoAnnotationConfig method handleClass.

/**
	 * 
	 * @param clsName
	 * @throws Exception
	 */
public boolean handleClass(String clsName) {
    Class<?> clazz = getClass(clsName);
    if (clazz == null)
        return false;
    if (clazz.isInterface())
        return false;
    Entity entity = clazz.getAnnotation(Entity.class);
    if (entity == null && !clsName.endsWith("PO") && !clsName.endsWith("POJO") && !clsName.endsWith("Entity") && !clsName.endsWith("Model")) {
        return false;
    }
    Table tableAnn = clazz.getAnnotation(Table.class);
    String table = tableAnn == null ? "" : tableAnn.name();
    table = "".equals(table.trim()) ? clazz.getSimpleName().replace("PO", "").replace("POJO", "").replace("Entity", "").replace("Model", "") : table;
    if (table == null || table.trim().length() == 0)
        return false;
    try {
        List<Property> pList = getProperties(clazz, null, false, log);
        List<Property> superList = new ArrayList<Property>();
        Class<?> superClazz = clazz.getSuperclass();
        for (; superClazz != Object.class && superClazz != null; superClazz = superClazz.getSuperclass()) {
            if (!superClazz.isAnnotationPresent(MappedSuperclass.class))
                continue;
            List<Property> list = getProperties(superClazz, pList, true, log);
            if (list != null)
                superList.addAll(list);
        }
        List<Property> properties = new ArrayList<Property>(superList);
        properties.addAll(pList);
        ORMConfigBean ormBean = new ORMConfigBean();
        ormBean.setClazz(clazz.getName());
        ormBean.setId(clazz.getSimpleName());
        ormBean.setTable(table);
        ormBean.setProperty(properties);
        ORMConfigBeanCache.add(clazz.getName(), ormBean);
    } catch (Throwable e) {
        log.warn("the entity class new instance failued -> " + clsName + " | " + e.toString(), e);
        return false;
    }
    return true;
}
Also used : Entity(javax.persistence.Entity) Table(javax.persistence.Table) ORMConfigBean(org.eweb4j.orm.config.bean.ORMConfigBean) MappedSuperclass(javax.persistence.MappedSuperclass) ArrayList(java.util.ArrayList) Property(org.eweb4j.orm.config.bean.Property)

Example 12 with ORMConfigBean

use of org.eweb4j.orm.config.bean.ORMConfigBean in project eweb4j-framework by laiweiwei.

the class CommonUtil method mappingPojo.

public static <T> List<T> mappingPojo(List<Map<String, Object>> datas, Class<T> cls) throws Exception {
    if (datas == null || datas.isEmpty())
        return null;
    List<String> columns = new ArrayList<String>();
    for (String col : datas.get(0).keySet()) columns.add(col);
    List<T> list = new ArrayList<T>();
    T t = null;
    for (Map<String, Object> data : datas) {
        t = cls.newInstance();
        ReflectUtil ru = new ReflectUtil(t);
        ORMConfigBean ormBean = ORMConfigBeanCache.get(cls.getName());
        for (Iterator<Property> it = ormBean.getProperty().iterator(); it.hasNext(); ) {
            Property p = it.next();
            String type = p.getType();
            if (type == null)
                continue;
            // 如果查询出来的字段名字没有,则不进行值注入
            boolean flag = false;
            for (String col : columns) {
                if (col.equalsIgnoreCase(p.getColumn())) {
                    flag = true;
                    continue;
                }
            }
            if (!flag)
                continue;
            Method m = ru.getSetter(p.getName());
            if (m == null)
                continue;
            Object value = data.get(p.getColumn());
            if (value == null)
                continue;
            String v = String.valueOf(value);
            if (v == null) {
                v = "";
            }
            if ("int".equalsIgnoreCase(type) || "java.lang.Integer".equalsIgnoreCase(type)) {
                if ("".equals(v.trim())) {
                    v = "0";
                }
                if (value instanceof Boolean)
                    v = ((Boolean) value ? "1" : "0");
                m.invoke(t, Integer.parseInt(v));
            } else if ("long".equalsIgnoreCase(type) || "java.lang.Long".equalsIgnoreCase(type)) {
                if ("".equals(v.trim())) {
                    v = "0";
                }
                if (value instanceof Boolean)
                    v = ((Boolean) value ? "1" : "0");
                m.invoke(t, Long.parseLong(v));
            } else if ("float".equalsIgnoreCase(type) || "java.lang.Float".equalsIgnoreCase(type)) {
                if ("".equals(v.trim())) {
                    v = "0.0";
                }
                if (value instanceof Boolean)
                    v = ((Boolean) value ? "1.0" : "0.0");
                m.invoke(t, Float.parseFloat(v));
            } else if ("double".equalsIgnoreCase(type) || "java.lang.Double".equalsIgnoreCase(type)) {
                if ("".equals(v.trim())) {
                    v = "0.0";
                }
                if (value instanceof Boolean)
                    v = ((Boolean) value ? "1.0" : "0.0");
                m.invoke(t, Double.parseDouble(v));
            } else if ("string".equalsIgnoreCase(type) || "java.lang.String".equalsIgnoreCase(type)) {
                m.invoke(t, v);
            } else if ("boolean".equalsIgnoreCase(type) || "java.lang.Boolean".equalsIgnoreCase(type)) {
                if ("1".equals(v.trim()) || "true".equals(v.trim())) {
                    m.invoke(t, true);
                } else if ("0".equals(v.trim()) || "false".equals(v.trim())) {
                    m.invoke(t, false);
                }
            } else if ("date".equalsIgnoreCase(type) || "java.sql.Date".equalsIgnoreCase(type) || "java.util.Date".equalsIgnoreCase(type)) {
                m.invoke(t, value);
            } else if ("timestamp".equalsIgnoreCase(type) || "java.sql.Timestamp".equalsIgnoreCase(type)) {
                m.invoke(t, value);
            } else if ("time".equalsIgnoreCase(type) || "java.sql.Time".equalsIgnoreCase(type)) {
                m.invoke(t, value);
            } else if ("[B".equalsIgnoreCase(type) || "byte[]".equalsIgnoreCase(type) || "[Ljava.lang.Byte;".equalsIgnoreCase(type)) {
                m.invoke(t, value);
            } else if (PropType.ONE_ONE.equalsIgnoreCase(type) || PropType.MANY_ONE.equalsIgnoreCase(type)) {
                if ("".equals(v))
                    continue;
                Field field = ru.getField(p.getName());
                Class<?> tarClass = field.getType();
                String refField = p.getRelProperty();
                Object tarObj = tarClass.newInstance();
                tarObj = ClassUtil.injectFieldValue(tarObj, refField, new String[] { v });
                m.invoke(t, tarObj);
            } else if (PropType.ONE_MANY.equalsIgnoreCase(type)) {
            } else if (PropType.MANY_MANY.equalsIgnoreCase(type)) {
            } else if (!"".equals(type)) {
                m.invoke(t, String.valueOf(value));
            }
        }
        list.add(t);
    }
    return list.isEmpty() ? null : list;
}
Also used : ORMConfigBean(org.eweb4j.orm.config.bean.ORMConfigBean) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) Field(java.lang.reflect.Field) Property(org.eweb4j.orm.config.bean.Property)

Example 13 with ORMConfigBean

use of org.eweb4j.orm.config.bean.ORMConfigBean in project eweb4j-framework by laiweiwei.

the class ORMConfig method check.

public static synchronized String check() {
    String error = null;
    ConfigBean cb = (ConfigBean) SingleBeanCache.get(ConfigBean.class.getName());
    if (cb == null)
        return null;
    List<String> ormXmlFilePaths = cb.getOrm().getOrmXmlFiles().getPath();
    for (String filePath : ormXmlFilePaths) {
        if (filePath == null || filePath.length() == 0)
            continue;
        File configFile = new File(ConfigConstant.CONFIG_BASE_PATH + filePath);
        try {
            XMLReader reader = BeanXMLUtil.getBeanXMLReader(configFile);
            reader.setBeanName("orm");
            reader.setClass("orm", ORMConfigBean.class);
            List<ORMConfigBean> ormList = reader.read();
            if (ormList == null || ormList.isEmpty()) {
                error = rebuildXmlFile(configFile, ConfigInfoCons.CANNOT_READ_ANY_CONFIG_INFO);
            } else {
                for (ORMConfigBean orm : ormList) {
                    String error1 = CheckConfigBean.checkORM(orm, filePath);
                    if (error1 != null)
                        if (error == null)
                            error = error1;
                        else
                            error += error1;
                    String error2 = CheckConfigBean.checkORMProperty(orm.getProperty(), ormList, orm.getId(), filePath);
                    if (error2 != null)
                        if (error == null)
                            error = error2;
                        else
                            error += error2;
                }
                if (error == null) {
                    for (ORMConfigBean orm : ormList) {
                        if (!"".equals(orm.getClazz()))
                            ORMConfigBeanCache.add(orm.getClazz(), orm);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            error = rebuildXmlFile(configFile, CommonUtil.getExceptionString(e));
        }
    }
    if (error != null)
        ORMConfigBeanCache.clear();
    else
        log.debug(ConfigInfoCons.READ_CONFIG_INFO_SUCCESS);
    return error;
}
Also used : ORMConfigBean(org.eweb4j.orm.config.bean.ORMConfigBean) CheckConfigBean(org.eweb4j.config.CheckConfigBean) ORMConfigBean(org.eweb4j.orm.config.bean.ORMConfigBean) ConfigBean(org.eweb4j.config.bean.ConfigBean) File(java.io.File) XMLReader(org.eweb4j.util.xml.XMLReader)

Example 14 with ORMConfigBean

use of org.eweb4j.orm.config.bean.ORMConfigBean in project eweb4j-framework by laiweiwei.

the class ORMConfigBeanUtil method getId.

/**
	 * 获取自增长的主键名字
	 * 
	 * @param clazz
	 * @param type
	 *            1的时候获取的是数据库字段名,2的时候获取的是java类的属性名
	 * @return
	 */
public static <T> String getId(T t, int type) {
    Class<?> clazz;
    if (t instanceof Class) {
        clazz = (Class<?>) t;
    } else {
        clazz = t.getClass();
    }
    if (!(t instanceof Class) && Map.class.isAssignableFrom(clazz)) {
        HashMap<String, Object> map = (HashMap<String, Object>) t;
        return (String) map.get("idColumn");
    }
    ORMConfigBean ormBean = ORMConfigBeanCache.get(clazz.getName());
    if (ormBean == null)
        return null;
    for (Property property : ormBean.getProperty()) {
        if (("true".equals(property.getPk()) || "1".equals(property.getPk())) && ("true".equals(property.getAutoIncrement()) || "1".equals(property.getAutoIncrement()))) {
            if (1 == type)
                return property.getColumn();
            else if (2 == type)
                return property.getName();
            break;
        }
    }
    return null;
}
Also used : HashMap(java.util.HashMap) ORMConfigBean(org.eweb4j.orm.config.bean.ORMConfigBean) HashMap(java.util.HashMap) Map(java.util.Map) Property(org.eweb4j.orm.config.bean.Property)

Example 15 with ORMConfigBean

use of org.eweb4j.orm.config.bean.ORMConfigBean in project eweb4j-framework by laiweiwei.

the class CheckConfigBean method checkORM.

/**
	 * Check the ORM independent components configuration files
	 * 
	 * @param orm
	 * @return
	 */
public static String checkORM(ORMConfigBean orm, String xmlFile) {
    String error = null;
    ConfigBean cb = (ConfigBean) SingleBeanCache.get(ConfigBean.class.getName());
    if ("true".equalsIgnoreCase(cb.getOrm().getOpen()) || "1".equals(cb.getOrm().getOpen())) {
        StringBuilder sb = new StringBuilder();
        if (!"".equals(orm.getClazz())) {
            try {
                if (Thread.currentThread().getContextClassLoader().loadClass(orm.getClazz()) == null) {
                    sb.append("当前您填写的( class=").append(orm.getClazz()).append(" )是错误的!它必须是一个有效的类 ;\n");
                }
            } catch (ClassNotFoundException e) {
                sb.append("当前您填写的( class=").append(orm.getClazz()).append(" )是错误的!它必须是一个有效的类 ;\n");
            }
        }
        if (!"".equals(sb.toString())) {
            error = "\n<br /><b>" + xmlFile + ":[bean name=" + orm.getId() + "]</b>\n" + sb.toString();
        }
    }
    return error;
}
Also used : ORMConfigBean(org.eweb4j.orm.config.bean.ORMConfigBean) FieldConfigBean(org.eweb4j.mvc.config.bean.FieldConfigBean) DBInfoConfigBean(org.eweb4j.orm.dao.config.bean.DBInfoConfigBean) ParamConfigBean(org.eweb4j.mvc.config.bean.ParamConfigBean) ResultConfigBean(org.eweb4j.mvc.config.bean.ResultConfigBean) IOCConfigBean(org.eweb4j.ioc.config.bean.IOCConfigBean) LogConfigBean(org.eweb4j.config.bean.LogConfigBean) ActionConfigBean(org.eweb4j.mvc.config.bean.ActionConfigBean) ConfigBean(org.eweb4j.config.bean.ConfigBean) InterConfigBean(org.eweb4j.mvc.config.bean.InterConfigBean) ValidatorConfigBean(org.eweb4j.mvc.config.bean.ValidatorConfigBean)

Aggregations

ORMConfigBean (org.eweb4j.orm.config.bean.ORMConfigBean)16 Property (org.eweb4j.orm.config.bean.Property)11 Field (java.lang.reflect.Field)6 ArrayList (java.util.ArrayList)6 ConfigBean (org.eweb4j.config.bean.ConfigBean)5 ReflectUtil (org.eweb4j.util.ReflectUtil)5 File (java.io.File)4 Method (java.lang.reflect.Method)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Entry (java.util.Map.Entry)3 JoinColumn (javax.persistence.JoinColumn)3 FileWriter (java.io.FileWriter)2 IOException (java.io.IOException)2 Column (javax.persistence.Column)2 Entity (javax.persistence.Entity)2 JoinTable (javax.persistence.JoinTable)2 ManyToMany (javax.persistence.ManyToMany)2 Table (javax.persistence.Table)2 LogConfigBean (org.eweb4j.config.bean.LogConfigBean)2