Search in sources :

Example 16 with ReflectUtil

use of org.eweb4j.util.ReflectUtil in project eweb4j-framework by laiweiwei.

the class SelectSqlCreator method selectOne.

public String selectOne() throws SqlCreateException {
    // 反射工具
    ReflectUtil ru = new ReflectUtil(this.t);
    StringBuilder condition = new StringBuilder();
    Method m = ru.getGetter(idField);
    if (m == null) {
        throw new SqlCreateException("can not find id getter");
    }
    try {
        condition.append(idColumn).append(" = '").append(m.invoke(this.t)).append("'");
    } catch (Exception e) {
        throw new SqlCreateException(m + " invoke exception " + e.toString());
    }
    return String.format("SELECT * FROM %s WHERE %s ;", selectAllColumn, table, condition.toString());
}
Also used : ReflectUtil(org.eweb4j.util.ReflectUtil) Method(java.lang.reflect.Method)

Example 17 with ReflectUtil

use of org.eweb4j.util.ReflectUtil in project eweb4j-framework by laiweiwei.

the class SelectSqlCreator method selectWhere.

/**
	 * 各种条件查询
	 * 
	 * @param fields
	 *            构成条件的属性名数组(填写的应该是对象属性名,不是数据库字段名)
	 * @param values
	 *            构成条件的字段值
	 * @param likeType
	 *            匹配类型 -1左匹配 0全匹配 1右匹配
	 * @param isLike
	 *            是否模糊查询
	 * @param orderField
	 *            对哪个字段进行排序
	 * @param oType
	 *            升序还是降序
	 * @return 字符串
	 * @param currentPage
	 *            第几页
	 * @param numPerPage
	 *            每页显示多少条
	 * @return
	 * @throws SqlCreateException
	 */
public String selectWhere(String[] fields, String[] values, int likeType, boolean isLike, boolean isNot, boolean isOR, String orderField, int oType, int currentPage, int numPerPage) throws SqlCreateException {
    String oper = " = ";
    String connector = " AND ";
    String left = " '";
    String right = "' ";
    if (isNot)
        oper = "<>";
    if (isLike) {
        oper = " LIKE ";
        left = " '%";
        right = "%' ";
        if (isNot)
            oper = " NOT LIKE ";
        switch(likeType) {
            case LikeType.LEFT_LIKE:
                left = " '";
                break;
            case LikeType.RIGHT_LIKE:
                right = "' ";
                break;
            case LikeType.ALL_LIKE:
        }
    }
    if (isOR)
        connector = " OR ";
    String[] type = { oper, connector, left, right };
    if (orderField == null) {
        orderField = idColumn;
        orderField = OrderColumnUtil.getOrderColumn(orderField, dbType);
    } else
        orderField = ORMConfigBeanUtil.getColumn(clazz, orderField);
    StringBuilder condition = new StringBuilder();
    String[] columns = ORMConfigBeanUtil.getColumns(clazz, fields);
    for (int i = 0; i < columns.length; ++i) {
        if (values != null)
            if (condition.length() > 0)
                condition.append(type[1]).append(columns[i]).append(" ").append(type[0]).append(type[2]).append(values[i]).append(type[3]);
            else
                condition.append(columns[i]).append(" ").append(type[0]).append(type[2]).append(values[i]).append(type[3]);
        else {
            ReflectUtil ru = new ReflectUtil(t);
            Method getter = ru.getGetter(fields[i]);
            if (getter == null)
                continue;
            try {
                Object _value = getter.invoke(t);
                if (_value == null)
                    continue;
                Object value = null;
                if (ClassUtil.isPojo(_value.getClass())) {
                    Field f = ru.getField(fields[i]);
                    OneToOne oneAnn = getter.getAnnotation(OneToOne.class);
                    if (oneAnn == null)
                        oneAnn = f.getAnnotation(OneToOne.class);
                    ManyToOne manyToOneAnn = null;
                    if (oneAnn == null) {
                        manyToOneAnn = getter.getAnnotation(ManyToOne.class);
                        if (manyToOneAnn == null)
                            manyToOneAnn = f.getAnnotation(ManyToOne.class);
                    }
                    if (oneAnn != null || manyToOneAnn != null) {
                        JoinColumn joinColAnn = getter.getAnnotation(JoinColumn.class);
                        if (joinColAnn == null)
                            joinColAnn = f.getAnnotation(JoinColumn.class);
                        if (joinColAnn != null && joinColAnn.referencedColumnName().trim().length() > 0) {
                            String refCol = joinColAnn.referencedColumnName();
                            String refField = ORMConfigBeanUtil.getField(_value.getClass(), refCol);
                            ReflectUtil tarRu = new ReflectUtil(_value);
                            Method tarFKGetter = tarRu.getGetter(refField);
                            value = tarFKGetter.invoke(_value);
                        } else {
                            ReflectUtil tarRu = new ReflectUtil(_value);
                            String tarFKField = ORMConfigBeanUtil.getIdField(_value.getClass());
                            if (tarFKField != null) {
                                Method tarFKGetter = tarRu.getGetter(tarFKField);
                                value = tarFKGetter.invoke(_value);
                            }
                        }
                    }
                    if (value == null)
                        continue;
                } else
                    value = _value;
                if (condition.length() > 0)
                    condition.append(type[1]).append(columns[i]).append(" ").append(type[0]).append(type[2]).append(value).append(type[3]);
                else
                    condition.append(columns[i]).append(" ").append(type[0]).append(type[2]).append(value).append(type[3]);
            } catch (Exception e) {
                e.printStackTrace();
                throw new SqlCreateException(getter + " invoke exception " + e.toString());
            }
        }
    }
    String orderType = OrderType.ASC_ORDER == oType ? "ASC" : "DESC";
    if (currentPage > 0 && numPerPage > 0)
        return divPage(currentPage, numPerPage, orderField, oType, condition.toString());
    else {
        return String.format("SELECT %s FROM %s WHERE %s ORDER BY %s %s ;", selectAllColumn, table, condition.toString(), orderField, orderType);
    }
}
Also used : Method(java.lang.reflect.Method) ManyToOne(javax.persistence.ManyToOne) ReflectUtil(org.eweb4j.util.ReflectUtil) Field(java.lang.reflect.Field) OneToOne(javax.persistence.OneToOne) JoinColumn(javax.persistence.JoinColumn)

Example 18 with ReflectUtil

use of org.eweb4j.util.ReflectUtil in project eweb4j-framework by laiweiwei.

the class UpdateSqlCreator method makeSQL.

private Sql makeSQL(T t) throws SqlCreateException {
    Sql sql = new Sql();
    Class<?> clazz = t.getClass();
    String table;
    String[] columns;
    String[] fields;
    Object[] values = null;
    String idColumn;
    String idField;
    Object idValue = null;
    HashMap<String, Object> map = null;
    if (Map.class.isAssignableFrom(clazz)) {
        map = (HashMap) t;
        table = (String) map.get("table");
        idColumn = (String) map.get("idColumn");
        idField = idColumn;
        idValue = map.get("idValue");
        columns = (String[]) map.get("columns");
        fields = columns;
        values = (Object[]) map.get("values");
    } else {
        table = ORMConfigBeanUtil.getTable(clazz, false);
        columns = ORMConfigBeanUtil.getColumns(clazz);
        fields = ORMConfigBeanUtil.getFields(clazz);
        idColumn = ORMConfigBeanUtil.getIdColumn(clazz);
        idField = ORMConfigBeanUtil.getIdField(clazz);
    }
    StringBuilder valuesSb = new StringBuilder();
    ReflectUtil ru = new ReflectUtil(t);
    try {
        if (map == null) {
            Method idGetter = ru.getGetter(idField);
            if (idGetter == null)
                throw new SqlCreateException("can not find id getter");
            idValue = idGetter.invoke(t);
        }
        for (int i = 0; i < columns.length; i++) {
            String column = columns[i];
            String field = fields[i];
            Object value = null;
            // id 字段不允许
            if (idColumn != null && idColumn.equalsIgnoreCase(column))
                continue;
            if (map != null && values != null) {
                value = values[i];
            } else {
                Method getter = ru.getGetter(field);
                if (getter == null)
                    continue;
                Object _value = getter.invoke(t);
                if (_value == null)
                    continue;
                if (ClassUtil.isPojo(_value.getClass())) {
                    Field f = ru.getField(field);
                    OneToOne oneAnn = getter.getAnnotation(OneToOne.class);
                    if (oneAnn == null)
                        oneAnn = f.getAnnotation(OneToOne.class);
                    ManyToOne manyToOneAnn = null;
                    if (oneAnn == null) {
                        manyToOneAnn = getter.getAnnotation(ManyToOne.class);
                        if (manyToOneAnn == null)
                            manyToOneAnn = f.getAnnotation(ManyToOne.class);
                    }
                    if (oneAnn != null || manyToOneAnn != null) {
                        JoinColumn joinColAnn = getter.getAnnotation(JoinColumn.class);
                        if (joinColAnn == null)
                            joinColAnn = f.getAnnotation(JoinColumn.class);
                        if (joinColAnn != null && joinColAnn.referencedColumnName().trim().length() > 0) {
                            String refCol = joinColAnn.referencedColumnName();
                            String refField = ORMConfigBeanUtil.getField(_value.getClass(), refCol);
                            ReflectUtil tarRu = new ReflectUtil(_value);
                            Method tarFKGetter = tarRu.getGetter(refField);
                            value = tarFKGetter.invoke(_value);
                        } else {
                            ReflectUtil tarRu = new ReflectUtil(_value);
                            String tarFKField = ORMConfigBeanUtil.getIdField(_value.getClass());
                            if (tarFKField != null) {
                                Method tarFKGetter = tarRu.getGetter(tarFKField);
                                value = tarFKGetter.invoke(_value);
                            }
                        }
                    }
                    if (value == null)
                        continue;
                } else
                    value = _value;
            }
            if (valuesSb.length() > 0)
                valuesSb.append(",");
            //				valuesSb.append(column).append(" = '").append(value).append("'");
            valuesSb.append(column).append(" = ? ");
            sql.args.add(value);
        }
    } catch (Exception e) {
        throw new SqlCreateException("" + e.toString(), e);
    }
    //		String condition = new StringBuilder().append(idColumn).append(" = ").append("'").append(idValue).append("'").toString();
    String condition = new StringBuilder().append(idColumn).append(" = ? ").toString();
    sql.args.add(idValue);
    sql.sql = String.format("UPDATE %s SET %s WHERE %s ;", table, valuesSb, condition);
    return sql;
}
Also used : Method(java.lang.reflect.Method) ManyToOne(javax.persistence.ManyToOne) ReflectUtil(org.eweb4j.util.ReflectUtil) Field(java.lang.reflect.Field) OneToOne(javax.persistence.OneToOne) JoinColumn(javax.persistence.JoinColumn)

Example 19 with ReflectUtil

use of org.eweb4j.util.ReflectUtil in project eweb4j-framework by laiweiwei.

the class DataSourceCreator method create.

public static DataSource create(final DBInfoConfigBean dbInfo) throws Exception {
    ConfigBean cb = (ConfigBean) SingleBeanCache.get(ConfigBean.class.getName());
    Class<?> cls = Thread.currentThread().getContextClassLoader().loadClass(cb.getOrm().getDataSource());
    DataSource ds = (DataSource) cls.newInstance();
    List<Property> properties = dbInfo.getProperty();
    // 通过反射将配置信息注入到数据源对象中
    for (Property property : properties) {
        String name = property.getKey();
        String value = property.getValue();
        if (null == name || name.trim().length() == 0)
            continue;
        if (null == value || value.trim().length() == 0)
            continue;
        ReflectUtil ru2 = new ReflectUtil(ds);
        Method m2 = ru2.getSetter(name);
        if (m2 == null)
            continue;
        Class<?> clazz = m2.getParameterTypes()[0];
        if (int.class.isAssignableFrom(clazz) || Integer.class.isAssignableFrom(clazz) || long.class.isAssignableFrom(clazz) || Long.class.isAssignableFrom(clazz))
            m2.invoke(ds, Integer.parseInt(value));
        else if (float.class.isAssignableFrom(clazz) || Float.class.isAssignableFrom(clazz) || double.class.isAssignableFrom(clazz) || Double.class.isAssignableFrom(clazz))
            m2.invoke(ds, Float.parseFloat(value));
        else if (boolean.class.isAssignableFrom(clazz) || Boolean.class.isAssignableFrom(clazz))
            m2.invoke(ds, Boolean.parseBoolean(value));
        else
            m2.invoke(ds, value);
    }
    return ds;
}
Also used : ReflectUtil(org.eweb4j.util.ReflectUtil) DBInfoConfigBean(org.eweb4j.orm.dao.config.bean.DBInfoConfigBean) ConfigBean(org.eweb4j.config.bean.ConfigBean) Method(java.lang.reflect.Method) Property(org.eweb4j.orm.dao.config.bean.Property) DataSource(javax.sql.DataSource)

Example 20 with ReflectUtil

use of org.eweb4j.util.ReflectUtil in project eweb4j-framework by laiweiwei.

the class BeanXMLWriter method writeRecursion.

private <T> void writeRecursion(final Element bean, T t) throws Exception {
    ReflectUtil ru = new ReflectUtil(t);
    Field[] fields = ru.getFields();
    for (Field f : fields) {
        String n = f.getName();
        Method m = ru.getGetter(n);
        if (m == null)
            continue;
        if ("clazz".equals(n))
            n = "class";
        Object obj = m.invoke(t);
        Skip skip = f.getAnnotation(Skip.class);
        if (skip != null)
            continue;
        AttrTag attrTag = f.getAnnotation(AttrTag.class);
        Readonly readonly = f.getAnnotation(Readonly.class);
        if (readonly != null)
            continue;
        if (ClassUtil.isPojo(f.getType())) {
            // 属性为class,进入递归
            Class<?> cls = this.classes.get(n);
            if (cls == null)
                continue;
            if (obj == null)
                obj = cls.newInstance();
            writeRecursion(bean.addElement(n), cls.cast(obj));
        } else if (attrTag != null) {
            if (obj == null)
                obj = "";
            bean.addAttribute(n, String.valueOf(obj));
        } else if (ClassUtil.isListClass(f)) {
            Collection<?> list = (Collection<?>) obj;
            Class<?> cls = this.classes.get(n);
            if (list.size() == 0 && cls != null)
                writeRecursion(bean.addElement(n), cls.newInstance());
            else
                for (Iterator<?> it = list.iterator(); it.hasNext(); ) writeRecursion(bean.addElement(n), it.next());
        } else if (ClassUtil.isListString(f)) {
            Collection<?> list = (Collection<?>) obj;
            for (Iterator<?> it = list.iterator(); it.hasNext(); ) {
                Object v2 = it.next();
                if (v2 == null)
                    v2 = "";
                bean.addElement(n).addText(String.valueOf(v2));
            }
        } else {
            if (obj == null)
                obj = "";
            bean.addElement(n).addText(String.valueOf(obj));
        }
    }
}
Also used : Method(java.lang.reflect.Method) ReflectUtil(org.eweb4j.util.ReflectUtil) Field(java.lang.reflect.Field) Iterator(java.util.Iterator) Collection(java.util.Collection)

Aggregations

ReflectUtil (org.eweb4j.util.ReflectUtil)49 Method (java.lang.reflect.Method)37 Field (java.lang.reflect.Field)26 JoinColumn (javax.persistence.JoinColumn)14 ManyToOne (javax.persistence.ManyToOne)10 ArrayList (java.util.ArrayList)9 OneToOne (javax.persistence.OneToOne)9 DAOException (org.eweb4j.orm.dao.DAOException)9 JoinTable (javax.persistence.JoinTable)7 HashMap (java.util.HashMap)6 ManyToMany (javax.persistence.ManyToMany)6 OneToMany (javax.persistence.OneToMany)6 ORMConfigBean (org.eweb4j.orm.config.bean.ORMConfigBean)6 Property (org.eweb4j.orm.config.bean.Property)6 Entry (java.util.Map.Entry)5 File (java.io.File)4 List (java.util.List)4 Map (java.util.Map)4 ConfigBean (org.eweb4j.config.bean.ConfigBean)4 Hashtable (java.util.Hashtable)3