use of org.eweb4j.util.ReflectUtil in project eweb4j-framework by laiweiwei.
the class ValidatorUtil method readValidator.
// /**
// * 读取注解中验证器部分
// *
// * @param actionIndex
// * @param validatorAnn
// * @param fieldAnn
// * @param paramAnn
// * @return
// */
// public static List<ValidatorConfigBean> readValidator() {
// List<ValidatorConfigBean> vList = new ArrayList<ValidatorConfigBean>();
//// String[] name = validatorAnn.value();
//// String[] clsName = validatorAnn.clazz();
//
// for (int a = 0; a < name.length; ++a) {
// ValidatorConfigBean v = new ValidatorConfigBean();
// if (name != null && name.length > a)
// v.setName(StringUtil.parsePropValue(name[a]));
//
// if (clsName != null && clsName.length > a)
// v.setClazz(StringUtil.parsePropValue(clsName[a]));
//
// if (valMessAnn == null || valFieldAnn == null)
// continue;
//
// // 验证器数组下标
// int[] valIndex = valMessAnn.validator();
// // 需要验证的属性域数组下标
// int[] fieldIndex = valMessAnn.field();
//
// String[] valField = valFieldAnn.value();
// String[] mess = valMessAnn.value();
//
// List<String> fnamelist = new ArrayList<String>();
// for (int in : fieldIndex)
// fnamelist.add(StringUtil.parsePropValue(valField[in]));
//
// String[] fname = fnamelist.toArray(new String[] {});
//
// List<FieldConfigBean> fList = new ArrayList<FieldConfigBean>();
// for (int b = 0; b < valIndex.length; ++b) {
// if (valIndex[b] == a) {
// FieldConfigBean f = new FieldConfigBean();
// fList.add(f);
// f.setName(StringUtil.parsePropValue(fname[b]));
// f.setMessage(StringUtil.parsePropValue(mess[b]));
//
// if (paramAnn == null || paramName == null)
// continue;
//
// int[] pindex = paramAnn.valMess();
// int[] pnameIndex = paramAnn.name();
// String[] pnames = paramName.value();
//
// List<String> pnamelist = new ArrayList<String>();
// for (int in : pnameIndex)
// pnamelist.add(StringUtil.parsePropValue(pnames[in]));
//
// String[] pname = pnamelist.toArray(new String[] {});
// String[] pvalue = paramAnn.value();
//
// List<ParamConfigBean> pList = new ArrayList<ParamConfigBean>();
// for (int c = 0; c < pindex.length; ++c) {
// if (pindex[c] == b) {
// ParamConfigBean p = new ParamConfigBean();
// p.setName(StringUtil.parsePropValue(pname[c]));
// p.setValue(StringUtil.parsePropValue(pvalue[c]));
// pList.add(p);
// }
// }
//
// f.setParam(pList);
//
// }
// }
//
// v.setField(fList);
// vList.add(v);
// }
//
// return vList;
// }
/**
* 从Action属性中读取验证器配置
* @param <T>
* @param params
* @param scopeName
* @param ru
* @param vList
* @param hasCls
* @return
*/
public static <T> List<ValidatorConfigBean> readValidator(final String[] params, final String[] excepts, String scopeName, ReflectUtil ru, List<ValidatorConfigBean> vList, Set<Class<?>> hasCls) {
if (params == null || params.length == 0)
return null;
if (ru == null)
return null;
Field[] fs = ru.getFields();
if (fs == null)
return null;
if (vList == null)
vList = new ArrayList<ValidatorConfigBean>();
ValidatorConfigBean val = null;
for (Field f : fs) {
Skip iv = f.getAnnotation(Skip.class);
if (iv != null)
continue;
if (ClassUtil.isPojo(f.getType()) && !UploadFile.class.isAssignableFrom(f.getType())) {
// 解决无限递归问题
if (hasCls == null)
hasCls = new HashSet<Class<?>>();
if (!hasCls.contains(f.getType())) {
hasCls.add(f.getType());
if (scopeName != null && scopeName.length() > 0)
scopeName = scopeName + "." + f.getName();
else
scopeName = f.getName();
try {
readValidator(params, excepts, scopeName, new ReflectUtil(f.getType()), vList, hasCls);
scopeName = null;
} catch (Exception e) {
continue;
}
}
continue;
}
for (Annotation ann : f.getAnnotations()) {
ValidatorCreator valCreator = ValidatorFactory.get(ann);
if (valCreator == null)
continue;
String name = f.getName();
if (scopeName != null && scopeName.length() > 0)
name = scopeName + "." + name;
for (String param : params) {
if (Arrays.asList(excepts).contains(name))
continue;
boolean flag = false;
if (!param.equals("*") && param.endsWith("*") && name.startsWith(param.replace("*", "")))
flag = true;
if (!param.equals("*") && param.startsWith("*") && name.endsWith(param.replace("*", "")))
flag = true;
if (name.equals(param) || param.equals("*"))
flag = true;
if (flag) {
val = valCreator.create(name, val);
if (val != null)
vList.add(val);
break;
}
}
}
}
scopeName = null;
if (vList.size() > 0)
return vList;
return null;
}
use of org.eweb4j.util.ReflectUtil in project eweb4j-framework by laiweiwei.
the class DataAssemUtil method getData.
private static <T> TRData getData(ListPage listPage, Map<String, String> prop, TRData data, String scope, T pojo, boolean flag) throws IllegalAccessException, InvocationTargetException, Exception {
ReflectUtil ru = new ReflectUtil(pojo);
Field[] fields = ru.getFields();
if (data == null)
data = new TRData();
for (Field field : fields) {
String _name = field.getName();
Method getter = ru.getGetter(_name);
if (getter == null)
continue;
Object getterVal = getter.invoke(pojo);
String name = null;
if (scope != null)
name = scope + "." + _name;
else
name = _name;
//如果是POJO类型
if (getterVal != null && ClassUtil.isPojo(field.getType())) {
//进入递归
getData(listPage, prop, data, name, getterVal, flag);
}
if (getterVal == null) {
getterVal = " ";
}
String property = prop.get(name);
// property);
if (property == null) {
continue;
}
// System.out.println(name + "-->" + getterVal);
data.getDatas().add(String.valueOf(getterVal));
// 获取属性名字一次即可
if (flag)
continue;
String[] info = property.split(",");
THeadCell cell = new THeadCell();
cell.setLabel(info[0]);
if (info.length > 2)
cell.setWidth(info[1]);
else
cell.setWidth("");
listPage.getThead().add(cell);
// System.out.println(">>>>>>>cell>>>>" + cell);
}
// System.out.println("trdata-->" + data);
return data;
}
use of org.eweb4j.util.ReflectUtil in project eweb4j-framework by laiweiwei.
the class PojoAnnotationConfig method onOk.
@Override
protected void onOk() throws Exception {
for (Iterator<Entry<Object, ORMConfigBean>> it = ORMConfigBeanCache.entrySet().iterator(); it.hasNext(); ) {
Entry<Object, ORMConfigBean> e = it.next();
ORMConfigBean orm = e.getValue();
Class<?> clazz = null;
ReflectUtil ru = null;
for (Property p : orm.getProperty()) {
String type = p.getType();
if (!PropType.ONE_ONE.equals(type) && !PropType.MANY_ONE.equals(type))
continue;
if (p.getRelProperty() != null && p.getRelProperty().trim().length() > 0)
continue;
if (clazz == null)
clazz = Thread.currentThread().getContextClassLoader().loadClass(orm.getClazz());
if (ru == null)
ru = new ReflectUtil(clazz);
Field f = ru.getField(p.getName());
String refCol = ORMConfigBeanUtil.getIdColumn(f.getType());
p.setRelProperty(ORMConfigBeanUtil.getField(f.getType(), refCol));
}
}
}
use of org.eweb4j.util.ReflectUtil in project eweb4j-framework by laiweiwei.
the class DAOImpl method queryBySql.
public <T> Collection<T> queryBySql(final String sql) {
Class<T> mappingCls = null;
if (this.targetEntity == null)
mappingCls = (Class<T>) this.clazz;
else
mappingCls = (Class<T>) this.targetEntity;
List<T> result = null;
try {
if (Map.class.isAssignableFrom(mappingCls)) {
Connection con = ds.getConnection();
if (args != null && args.size() > 0) {
result = (List<T>) JdbcUtil.getListWithArgs(con, mappingCls, sql, args.toArray(new Object[] {}));
} else {
result = (List<T>) JdbcUtil.getList(con, mappingCls, sql);
}
} else {
if (args != null && args.size() > 0) {
result = (List<T>) DAOFactory.getSelectDAO(dsName).selectBySQL(mappingCls, sql, args.toArray(new Object[] {}));
} else {
result = (List<T>) DAOFactory.getSelectDAO(dsName).selectBySQL(mappingCls, sql);
}
}
//this.clear();
if (result != null && result.size() > 0) {
for (T t : result) {
// ToOne relation class cascade select
ReflectUtil ru = new ReflectUtil(t);
for (Field field : ru.getFields()) {
String f = field.getName();
OneToOne o2o = field.getAnnotation(OneToOne.class);
ManyToOne m2o = field.getAnnotation(ManyToOne.class);
OneToMany o2m = field.getAnnotation(OneToMany.class);
ManyToMany m2m = field.getAnnotation(ManyToMany.class);
FetchType fetchType = null;
boolean is2One = false;
if (o2o != null) {
fetchType = o2o.fetch();
is2One = true;
}
if (m2o != null) {
fetchType = m2o.fetch();
is2One = true;
}
if (o2m != null) {
fetchType = o2m.fetch();
}
if (m2m != null) {
fetchType = m2m.fetch();
}
if (fetchType == null)
continue;
String beanId = field.getType().getName();
if (!is2One)
beanId = ClassUtil.getGenericType(field).getName();
boolean isEntity = ORMConfigBeanCache.containsKey(beanId);
if (!isEntity)
continue;
if (unFetch.contains(f))
continue;
if (fetch.contains(f)) {
log.debug("cascade select -> " + t.getClass().getName() + "." + f);
DAOFactory.getCascadeDAO(dsName).select(t, f);
continue;
}
if (FetchType.LAZY.equals(fetchType))
continue;
log.debug("cascade select -> " + t.getClass().getName() + "." + f);
DAOFactory.getCascadeDAO(dsName).select(t, f);
}
}
}
return result;
} catch (Exception e) {
log.error("sql-->" + sql, e);
throw new DAOException(sql + " execute exception", e);
}
}
use of org.eweb4j.util.ReflectUtil in project eweb4j-framework by laiweiwei.
the class CascadeDAO method cascade.
/**
* 级联
*
* @param <T>
* @param t
* @param fieldNames
* 对指定属性名进行级联操作,当为null时,对所有属性进行级联操作
* @param type
* 1表示级联查询,2表示级联删除,3表示级联插入,4表示级联更新
* @param delType
* @
*/
public <T> void cascade(T[] ts, String[] fieldNames, int type, long newIdVal) {
// 首先判断给定的pojo是否是一个持久化对象
if (ts == null || ts.length == 0) {
return;
}
for (T t : ts) {
if (t == null)
continue;
Class<?> clazz = t.getClass();
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;
}
ReflectUtil ru = new ReflectUtil(t);
Field[] fields = null;
if (fieldNames == null || fieldNames.length == 0) {
fields = ru.getFields();
} else {
List<Field> fieldList = new ArrayList<Field>();
for (String n : fieldNames) {
if (n != null && !"".equals(n.trim())) {
Field f = ru.getField(n);
if (f != null) {
fieldList.add(f);
}
}
}
if (fieldList.size() > 0) {
fields = fieldList.toArray(new Field[] {});
}
}
if (fields != null) {
List<Field> oneList = new ArrayList<Field>();
List<Field> manyList = new ArrayList<Field>();
List<Field> manyManyList = new ArrayList<Field>();
for (Field f : fields) {
Method getter = ru.getGetter(f.getName());
if (f.isAnnotationPresent(OneToMany.class) || (getter != null && getter.isAnnotationPresent(OneToMany.class))) {
manyList.add(f);
} else if (f.isAnnotationPresent(ManyToOne.class) || (getter != null && getter.isAnnotationPresent(ManyToOne.class))) {
oneList.add(f);
} else if (f.isAnnotationPresent(OneToOne.class) || (getter != null && getter.isAnnotationPresent(OneToOne.class))) {
oneList.add(f);
} else if (f.isAnnotationPresent(ManyToMany.class) || (getter != null && getter.isAnnotationPresent(ManyToMany.class))) {
manyManyList.add(f);
}
}
this.manyToMany.init(t, manyManyList);
this.oneToMany.init(t, manyList);
this.oneToOne.init(t, oneList);
switch(type) {
case CascadeType.SELECT:
this.manyToMany.select();
this.oneToMany.select();
this.oneToOne.select();
break;
case CascadeType.DELETE:
this.manyToMany.delete();
this.oneToMany.delete();
break;
case CascadeType.INSERT:
this.manyToMany.insert();
this.oneToMany.insert();
break;
case CascadeType.UPDATE:
this.oneToMany.update(newIdVal);
this.manyToMany.update(newIdVal);
break;
}
}
}
}
Aggregations