Search in sources :

Example 1 with Property

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

the class ModelHelper method fmtSql.

private static String fmtSql(Class<?> cls, ReflectUtil ru, String alias, Map<String, String> joins, String _sql) {
    ORMConfigBean ormBean = ORMConfigBeanCache.get(cls.getName());
    String sql = _sql;
    if (joins != null && !joins.isEmpty()) {
        for (Iterator<Entry<String, String>> it = joins.entrySet().iterator(); it.hasNext(); ) {
            Entry<String, String> e = it.next();
            String field = e.getKey();
            String _alias = e.getValue();
            sql = fmtSql(ru.getField(field).getType(), null, _alias, null, sql);
        }
    }
    if (alias != null && alias.trim().length() > 0)
        alias = alias + ".";
    sql = sql.replace("#" + alias + "table", ormBean.getTable()).replace("#" + alias + "id", alias + ORMConfigBeanUtil.getIdColumn(cls));
    for (Property p : ormBean.getProperty()) sql = sql.replace("#" + alias + p.getName(), alias + p.getColumn());
    return sql;
}
Also used : Entry(java.util.Map.Entry) ORMConfigBean(org.eweb4j.orm.config.bean.ORMConfigBean) Property(org.eweb4j.orm.config.bean.Property)

Example 2 with Property

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

the class ORMConfigBeanCreator method getORMBean.

public static ORMConfigBean getORMBean() {
    ORMConfigBean result = new ORMConfigBean();
    List<Property> pList = new ArrayList<Property>();
    Property p = new Property();
    pList.add(p);
    result.setProperty(pList);
    return result;
}
Also used : ORMConfigBean(org.eweb4j.orm.config.bean.ORMConfigBean) ArrayList(java.util.ArrayList) Property(org.eweb4j.orm.config.bean.Property)

Example 3 with Property

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

the class ORMConfigBeanUtil method getColumnsOrFields.

/**
	 * get columns through fields
	 * 
	 * @param clazz
	 * @param fields
	 * @param type
	 *            1.getColumns 2.getFields 3.allColumns 4.allFields
	 * 
	 * @return
	 */
private static <T> String[] getColumnsOrFields(T t, String[] strs, 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("columns");
    }
    if (strs == null)
        strs = new String[] { "" };
    String[] result = strs.clone();
    List<String> list = new ArrayList<String>();
    ORMConfigBean ormBean = ORMConfigBeanCache.get(clazz.getName());
    if (ormBean == null) {
        try {
            ReflectUtil ru = new ReflectUtil(clazz);
            // String idColumn = getIdColumn(clazz);
            for (int i = 0; i < strs.length; i++) {
                boolean finished = false;
                Field[] _fields = ru.getFields();
                for (Field _f : _fields) {
                    String pName = _f.getName();
                    JoinColumn jc = _f.getAnnotation(JoinColumn.class);
                    Column c = _f.getAnnotation(Column.class);
                    String col = pName;
                    if (c != null && c.name().trim().length() > 0)
                        col = c.name();
                    if (jc != null && jc.name().trim().length() > 0)
                        col = jc.name();
                    if (finished)
                        break;
                    switch(type) {
                        case 1:
                            int dotIndex = strs[i].indexOf(".");
                            if (dotIndex > 0 && dotIndex < strs[i].length() - 1) {
                                String[] dots = strs[i].split("\\.");
                                StringBuilder builder = new StringBuilder();
                                Class<?> prevCls = null;
                                for (int j = 0; j < dots.length; j++) {
                                    String dot = dots[j];
                                    Field field = ru.getField(dot);
                                    if (field == null && dot.equals(clazz.getSimpleName().toLowerCase())) {
                                        if (builder.length() > 0)
                                            builder.append(".");
                                        builder.append(dot);
                                        prevCls = clazz;
                                    } else if (field != null) {
                                        if (j == dots.length - 1) {
                                            if (builder.length() > 0)
                                                builder.append(".");
                                            builder.append(ORMConfigBeanUtil.getColumn(prevCls, dot));
                                        } else {
                                            Class<?> cls = ClassUtil.getGenericType(field);
                                            if (cls != null) {
                                                if (ORMConfigBeanCache.get(cls.getName()) != null) {
                                                    if (builder.length() > 0)
                                                        builder.append(".");
                                                    builder.append(cls.getSimpleName().toLowerCase());
                                                    prevCls = cls;
                                                    ru = new ReflectUtil(cls);
                                                }
                                            }
                                        }
                                    }
                                }
                                if (builder.length() > 0) {
                                    result[i] = builder.toString();
                                    finished = true;
                                    break;
                                }
                            }
                            if (pName.equals(strs[i])) {
                                result[i] = col;
                                finished = true;
                            }
                            break;
                        case 2:
                            if (col.equals(strs[i])) {
                                result[i] = pName;
                                finished = true;
                            }
                            break;
                        case 3:
                            list.add(col);
                            break;
                        case 4:
                            list.add(pName);
                    }
                }
            }
        } catch (Throwable e) {
        }
    } else {
        try {
            ReflectUtil ru = new ReflectUtil(clazz);
            // String idColumn = getIdColumn(clazz);
            for (int i = 0; i < strs.length; i++) {
                boolean finished = false;
                List<Property> properties = ormBean.getProperty();
                for (Property p : properties) {
                    if (finished)
                        break;
                    switch(type) {
                        case 1:
                            int dotIndex = strs[i].indexOf(".");
                            if (dotIndex > 0 && dotIndex < strs[i].length() - 1) {
                                String[] dots = strs[i].split("\\.");
                                StringBuilder builder = new StringBuilder();
                                Class<?> prevCls = null;
                                for (int j = 0; j < dots.length; j++) {
                                    String dot = dots[j];
                                    Field field = ru.getField(dot);
                                    if (field == null && dot.equals(clazz.getSimpleName().toLowerCase())) {
                                        if (builder.length() > 0)
                                            builder.append(".");
                                        builder.append(dot);
                                        prevCls = clazz;
                                    } else if (field != null) {
                                        if (j == dots.length - 1) {
                                            if (builder.length() > 0)
                                                builder.append(".");
                                            builder.append(ORMConfigBeanUtil.getColumn(prevCls, dot));
                                        } else {
                                            Class<?> cls = ClassUtil.getGenericType(field);
                                            if (cls != null) {
                                                if (ORMConfigBeanCache.get(cls.getName()) != null) {
                                                    if (builder.length() > 0)
                                                        builder.append(".");
                                                    builder.append(cls.getSimpleName().toLowerCase());
                                                    prevCls = cls;
                                                    ru = new ReflectUtil(cls);
                                                }
                                            }
                                        }
                                    }
                                }
                                if (builder.length() > 0) {
                                    result[i] = builder.toString();
                                    finished = true;
                                    break;
                                }
                            }
                            if (p.getName().equals(strs[i])) {
                                result[i] = p.getColumn();
                                finished = true;
                            }
                            break;
                        case 2:
                            if (p.getColumn().equals(strs[i])) {
                                result[i] = p.getName();
                                finished = true;
                            }
                            break;
                        case 3:
                            list.add(p.getColumn());
                            break;
                        case 4:
                            list.add(p.getName());
                    }
                }
            }
        } catch (Exception e) {
        }
    }
    return list.isEmpty() ? result : list.toArray(new String[] {});
}
Also used : HashMap(java.util.HashMap) ORMConfigBean(org.eweb4j.orm.config.bean.ORMConfigBean) ArrayList(java.util.ArrayList) InvocationTargetException(java.lang.reflect.InvocationTargetException) ReflectUtil(org.eweb4j.util.ReflectUtil) Field(java.lang.reflect.Field) JoinColumn(javax.persistence.JoinColumn) JoinColumn(javax.persistence.JoinColumn) Column(javax.persistence.Column) HashMap(java.util.HashMap) Map(java.util.Map) Property(org.eweb4j.orm.config.bean.Property)

Example 4 with Property

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

the class ORMConfigBeanUtil method getToOneField.

public static String[] getToOneField(Class<?> clazz) {
    ORMConfigBean ormBean = ORMConfigBeanCache.get(clazz.getName());
    if (ormBean == null)
        return null;
    List<String> list = new ArrayList<String>();
    for (Property p : ormBean.getProperty()) {
        if (PropType.ONE_ONE.equals(p.getType()) || PropType.MANY_ONE.equals(p.getType()))
            list.add(p.getName());
    }
    return list.toArray(new String[] {});
}
Also used : ORMConfigBean(org.eweb4j.orm.config.bean.ORMConfigBean) ArrayList(java.util.ArrayList) Property(org.eweb4j.orm.config.bean.Property)

Example 5 with Property

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

the class ORMConfigBeanWriter method write.

/**
	 * 将某个Class写入到目标xml文件中
	 * 
	 * @param filePath
	 *            文件相对路径
	 * @param clazzs
	 * @return
	 */
public static String write(String filePath, Class<?>... clazzs) {
    String error = null;
    File file = new File(ConfigConstant.CONFIG_BASE_PATH + filePath);
    if (filePath != null && clazzs != null && clazzs.length > 0) {
        List<ORMConfigBean> ormList = new ArrayList<ORMConfigBean>();
        try {
            for (int i = 0; i < clazzs.length; ++i) {
                Class<?> clazz = clazzs[i];
                String clsName = clazz.getSimpleName();
                Entity entity = clazz.getAnnotation(Entity.class);
                if (entity == null && !clsName.endsWith("PO") && !clsName.endsWith("POJO") && !clsName.endsWith("Entity") && !clsName.endsWith("Model")) {
                    return null;
                }
                Table tableAnn = clazz.getAnnotation(Table.class);
                String table = tableAnn == null ? "" : tableAnn.name();
                table = "".equals(table.trim()) ? clsName : table;
                ORMConfigBean ormBean = new ORMConfigBean();
                ormBean.setClazz(clazz.getName());
                ormBean.setId(clazz.getSimpleName());
                ormBean.setTable(table);
                ReflectUtil ru;
                ru = new ReflectUtil(clazz);
                List<Property> pList = new ArrayList<Property>();
                for (Field f : ru.getFields()) {
                    String name = f.getName();
                    Method getter = ru.getGetter(name);
                    Column colAnn = getter.getAnnotation(Column.class);
                    if (colAnn == null) {
                        colAnn = f.getAnnotation(Column.class);
                        if (colAnn == null)
                            continue;
                    }
                    Id idAnn = f.getAnnotation(Id.class);
                    if (idAnn == null) {
                        idAnn = getter.getAnnotation(Id.class);
                        if (idAnn == null)
                            continue;
                    }
                    Property p = new Property();
                    p.setAutoIncrement("1");
                    p.setPk("1");
                    Pk pkAnn = f.getAnnotation(Pk.class);
                    if (pkAnn != null) {
                        p.setPk("1");
                    }
                    String column = colAnn.name();
                    column = "".equals(column.trim()) ? name : column;
                    p.setName(name);
                    p.setColumn(column);
                    p.setType(f.getType().getName());
                    pList.add(p);
                }
                ormBean.setProperty(pList);
                ormList.add(ormBean);
            }
            BeanXMLUtil.getBeanXMLWriter(file, ormList).write();
        } catch (Exception e) {
            error = e.getMessage();
            e.printStackTrace();
        }
    } else {
        error = "Class参数不能为空";
    }
    return error;
}
Also used : Entity(javax.persistence.Entity) Table(javax.persistence.Table) ORMConfigBean(org.eweb4j.orm.config.bean.ORMConfigBean) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) ReflectUtil(org.eweb4j.util.ReflectUtil) Field(java.lang.reflect.Field) Column(javax.persistence.Column) Id(javax.persistence.Id) Pk(org.eweb4j.orm.annotation.Pk) File(java.io.File) Property(org.eweb4j.orm.config.bean.Property)

Aggregations

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