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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations