use of org.eweb4j.orm.config.bean.Property 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.orm.config.bean.Property in project eweb4j-framework by laiweiwei.
the class Model2Table method generateOne.
public static String generateOne(final Class<?> entityClass, final boolean isDrop, boolean isCreateFile) {
StringBuilder sql = new StringBuilder();
StringBuilder manyMany = new StringBuilder();
final ORMConfigBean ocb = ORMConfigBeanCache.get(entityClass.getName());
final String table = ocb.getTable();
List<Property> properties = ocb.getProperty();
StringBuilder sb = new StringBuilder();
StringBuilder fkSb = new StringBuilder();
final String fk = ", \n\tKEY %s (%s), \n\tCONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s)";
StringBuilder pkSb = new StringBuilder();
final String pk = ", \n\tPRIMARY KEY (%s)";
StringBuilder uniqueSb = new StringBuilder();
String unique = ", \n\tUNIQUE KEY %s (%s)";
final String idCol = ORMConfigBeanUtil.getIdColumn(properties);
for (Property p : properties) {
if (sb.length() > 0)
sb.append(", \n");
String col = p.getColumn();
String type = p.getType();
String size = null;
if (p.getSize().trim().length() > 0 && CommonUtil.isNumeric(p.getSize().trim()))
size = String.format(" (%s) ", p.getSize());
if ("1".equals(p.getUnique()) || "true".equalsIgnoreCase(p.getUnique()))
uniqueSb.append(String.format(unique, col, col));
String notNull = "";
if ("1".equals(p.getNotNull()) || "true".equalsIgnoreCase(p.getNotNull()))
notNull = " NOT NULL ";
if ("1".equals(p.getPk()) || "true".equalsIgnoreCase(p.getPk())) {
if (pkSb.length() > 0)
pkSb.append(",");
pkSb.append(col);
}
String auto = "";
if ("1".equals(p.getAutoIncrement()) || "true".equalsIgnoreCase(p.getAutoIncrement()))
auto = " AUTO_INCREMENT ";
if (PropType.ONE_ONE.equals(p.getType()) || PropType.MANY_ONE.equals(p.getType())) {
final String relTable = ORMConfigBeanUtil.getTable(p.getRelClass(), false);
if (p.getRelProperty() == null || p.getRelProperty().trim().length() == 0)
p.setRelProperty(ORMConfigBeanUtil.getIdField(p.getRelClass()));
final String relColumn = ORMConfigBeanUtil.getColumn(p.getRelClass(), p.getRelProperty());
fkSb.append(String.format(fk, col, col, table + "_" + col, col, relTable, relColumn));
}
sb.append("\t").append(col).append(" ").append(getType(type)).append(size == null ? "" : size).append(notNull).append(auto);
}
sb.append(uniqueSb.toString());
if (pkSb.length() > 0)
sb.append(String.format(pk, pkSb.toString()));
sb.append(fkSb.toString());
String _sql = String.format(create_table_script, table, sb.toString());
if (isDrop) {
_sql = String.format(drop_table_script, table) + _sql;
;
}
sql.append(_sql);
try {
ReflectUtil ru = new ReflectUtil(entityClass);
for (Field f : ru.getFields()) {
if (!ClassUtil.isListClass(f))
continue;
String name = f.getName();
Method getter = ru.getGetter(name);
if (getter == null)
continue;
ManyToMany mmAnn = getter.getAnnotation(ManyToMany.class);
if (mmAnn == null)
mmAnn = f.getAnnotation(ManyToMany.class);
if (mmAnn != null) {
JoinTable join = getter.getAnnotation(JoinTable.class);
if (join == null) {
join = f.getAnnotation(JoinTable.class);
if (join == null)
continue;
}
JoinColumn[] froms = join.joinColumns();
if (froms == null || froms.length == 0)
continue;
JoinColumn[] tos = join.inverseJoinColumns();
if (tos == null || tos.length == 0)
continue;
final String relTable = join.name();
final String relFrom = froms[0].name();
final String relTo = tos[0].name();
final Class<?> targetClass = ClassUtil.getGenericType(f);
StringBuilder manyManyField = new StringBuilder();
//handle the many to many
manyManyField.append("\tid ").append(getType("long")).append(" (20) NOT NULL AUTO_INCREMENT,");
manyManyField.append("\n\t").append(relFrom).append(" ").append(getType("long")).append(" (20) ,");
manyManyField.append("\n\t").append(relTo).append(" ").append(getType("long")).append(" (20) ,");
manyManyField.append("\n\t").append("PRIMARY KEY (id)");
String tarTable = ORMConfigBeanUtil.getTable(targetClass, false);
String tarIdCol = ORMConfigBeanUtil.getIdColumn(targetClass);
String fk1 = String.format(fk, relFrom, relFrom, relTable + "_" + relFrom, relFrom, table, idCol);
manyManyField.append(fk1);
String fk2 = String.format(fk, relTo, relTo, relTable + "_" + relTo, relTo, tarTable, tarIdCol);
manyManyField.append(fk2);
manyMany.append(String.format(create_table_script, relTable, relTable, manyManyField.toString()));
}
}
} catch (Exception e1) {
e1.printStackTrace();
}
sql.append(manyMany.toString());
String script = String.format("SET FOREIGN_KEY_CHECKS=0;\n%s", sql.toString());
if (!isCreateFile)
return script;
try {
ConfigBean cb = (ConfigBean) SingleBeanCache.get(ConfigBean.class.getName());
File file = new File(ConfigConstant.CONFIG_BASE_PATH() + cb.getOrm().getDdl().getDs() + "-create.sql");
FileWriter writer = new FileWriter(file);
writer.write(script);
writer.flush();
writer.close();
LogFactory.getORMLogger(Model2Table.class).debug("create models sql script file success -> " + file.getAbsoluteFile());
return script;
} catch (IOException e2) {
e2.printStackTrace();
return null;
}
}
use of org.eweb4j.orm.config.bean.Property in project eweb4j-framework by laiweiwei.
the class Model2Table method generateAll.
public static String generateAll(final boolean isDrop, final boolean isCreateFile) {
StringBuilder sql = new StringBuilder();
StringBuilder manyMany = new StringBuilder();
for (Iterator<Entry<Object, ORMConfigBean>> it = ORMConfigBeanCache.entrySet().iterator(); it.hasNext(); ) {
final Entry<Object, ORMConfigBean> e = it.next();
final ORMConfigBean ocb = e.getValue();
final String table = ocb.getTable();
List<Property> properties = ocb.getProperty();
StringBuilder sb = new StringBuilder();
StringBuilder fkSb = new StringBuilder();
final String fk = ", \n\tKEY %s (%s), \n\tCONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s)";
StringBuilder pkSb = new StringBuilder();
final String pk = ", \n\tPRIMARY KEY (%s)";
StringBuilder uniqueSb = new StringBuilder();
String unique = ", \n\tUNIQUE KEY %s (%s)";
final String idCol = ORMConfigBeanUtil.getIdColumn(properties);
for (Property p : properties) {
if (sb.length() > 0)
sb.append(", \n");
String col = p.getColumn();
String type = p.getType();
String size = null;
if (p.getSize().trim().length() > 0 && CommonUtil.isNumeric(p.getSize().trim()))
size = String.format(" (%s) ", p.getSize());
if ("1".equals(p.getUnique()) || "true".equalsIgnoreCase(p.getUnique()))
uniqueSb.append(String.format(unique, col, col));
String notNull = "";
if ("1".equals(p.getNotNull()) || "true".equalsIgnoreCase(p.getNotNull()))
notNull = " NOT NULL ";
if ("1".equals(p.getPk()) || "true".equalsIgnoreCase(p.getPk())) {
if (pkSb.length() > 0)
pkSb.append(",");
pkSb.append(col);
}
String auto = "";
if ("1".equals(p.getAutoIncrement()) || "true".equalsIgnoreCase(p.getAutoIncrement()))
auto = " AUTO_INCREMENT ";
if (PropType.ONE_ONE.equals(p.getType()) || PropType.MANY_ONE.equals(p.getType())) {
final String relTable = ORMConfigBeanUtil.getTable(p.getRelClass(), false);
if (p.getRelProperty() == null || p.getRelProperty().trim().length() == 0)
p.setRelProperty(ORMConfigBeanUtil.getIdField(p.getRelClass()));
final String relColumn = ORMConfigBeanUtil.getColumn(p.getRelClass(), p.getRelProperty());
fkSb.append(String.format(fk, col, col, table + "_" + col, col, relTable, relColumn));
}
sb.append("\t").append(col).append(" ").append(getType(type)).append(size == null ? "" : size).append(notNull).append(auto);
}
sb.append(uniqueSb.toString());
if (pkSb.length() > 0)
sb.append(String.format(pk, pkSb.toString()));
sb.append(fkSb.toString());
String _sql = String.format(create_table_script, table, sb.toString());
if (isDrop) {
_sql = String.format(drop_table_script, table) + _sql;
;
}
sql.append(_sql);
try {
Class<?> clazz = Thread.currentThread().getContextClassLoader().loadClass((String) e.getKey());
ReflectUtil ru = new ReflectUtil(clazz);
for (Field f : ru.getFields()) {
if (!ClassUtil.isListClass(f))
continue;
String name = f.getName();
Method getter = ru.getGetter(name);
if (getter == null)
continue;
ManyToMany mmAnn = getter.getAnnotation(ManyToMany.class);
if (mmAnn == null)
mmAnn = f.getAnnotation(ManyToMany.class);
if (mmAnn != null) {
JoinTable join = getter.getAnnotation(JoinTable.class);
if (join == null) {
join = f.getAnnotation(JoinTable.class);
if (join == null)
continue;
}
JoinColumn[] froms = join.joinColumns();
if (froms == null || froms.length == 0)
continue;
JoinColumn[] tos = join.inverseJoinColumns();
if (tos == null || tos.length == 0)
continue;
final String relTable = join.name();
final String relFrom = froms[0].name();
final String relTo = tos[0].name();
final Class<?> targetClass = ClassUtil.getGenericType(f);
StringBuilder manyManyField = new StringBuilder();
//handle the many to many
manyManyField.append("\tid ").append(getType("long")).append(" (20) NOT NULL AUTO_INCREMENT,");
manyManyField.append("\n\t").append(relFrom).append(" ").append(getType("long")).append(" (20) ,");
manyManyField.append("\n\t").append(relTo).append(" ").append(getType("long")).append(" (20) ,");
manyManyField.append("\n\t").append("PRIMARY KEY (id)");
String tarTable = ORMConfigBeanUtil.getTable(targetClass, false);
String tarIdCol = ORMConfigBeanUtil.getIdColumn(targetClass);
String fk1 = String.format(fk, relFrom, relFrom, relTable + "_" + relFrom, relFrom, table, idCol);
manyManyField.append(fk1);
String fk2 = String.format(fk, relTo, relTo, relTable + "_" + relTo, relTo, tarTable, tarIdCol);
manyManyField.append(fk2);
manyMany.append(String.format(create_table_script, relTable, relTable, manyManyField.toString()));
}
}
} catch (Exception e1) {
continue;
}
}
sql.append(manyMany.toString());
String script = String.format("SET FOREIGN_KEY_CHECKS=0;\n%s", sql.toString());
if (!isCreateFile)
return script;
try {
ConfigBean cb = (ConfigBean) SingleBeanCache.get(ConfigBean.class.getName());
File file = new File(ConfigConstant.CONFIG_BASE_PATH() + cb.getOrm().getDdl().getDs() + "-create.sql");
FileWriter writer = new FileWriter(file);
writer.write(script);
writer.flush();
writer.close();
LogFactory.getORMLogger(Model2Table.class).debug("create models sql script file success -> " + file.getAbsoluteFile());
return script;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
use of org.eweb4j.orm.config.bean.Property 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.Property in project eweb4j-framework by laiweiwei.
the class PojoAnnotationConfig method getProperties.
private static List<Property> getProperties(Class<?> clazz, final List<Property> pList, final boolean requireSuper, Log log) throws Throwable {
List<Property> result = new ArrayList<Property>();
ReflectUtil ru;
try {
ru = new ReflectUtil(clazz);
ru.setRequiredSuper(requireSuper);
} catch (Throwable e) {
log.warn(e.toString(), e);
throw e;
}
for (Field f : ru.getFields()) {
if (Collection.class.isAssignableFrom(f.getType()))
continue;
String name = f.getName();
Method getter = ru.getGetter(name);
if (getter == null)
continue;
Ignore igAnn = f.getAnnotation(Ignore.class);
if (igAnn == null)
igAnn = getter.getAnnotation(Ignore.class);
if (igAnn != null)
continue;
Transient trans = f.getAnnotation(Transient.class);
if (trans == null)
trans = getter.getAnnotation(Transient.class);
if (trans != null)
continue;
OneToMany manyAnn = getter.getAnnotation(OneToMany.class);
if (manyAnn != null)
continue;
else {
manyAnn = f.getAnnotation(OneToMany.class);
if (manyAnn != null)
continue;
}
ManyToMany manyManyAnn = getter.getAnnotation(ManyToMany.class);
if (manyManyAnn != null)
continue;
else {
manyManyAnn = f.getAnnotation(ManyToMany.class);
if (manyManyAnn != null)
continue;
}
Property p = new Property();
if (Long.class.isAssignableFrom(f.getType()) || long.class.isAssignableFrom(f.getType()))
p.setSize("20");
else if (Integer.class.isAssignableFrom(f.getType()) || int.class.isAssignableFrom(f.getType()))
p.setSize("8");
else if (String.class.isAssignableFrom(f.getType()))
p.setSize("255");
else if (Boolean.class.isAssignableFrom(f.getType()) || boolean.class.isAssignableFrom(f.getType()))
p.setSize("");
else if (Float.class.isAssignableFrom(f.getType()) || float.class.isAssignableFrom(f.getType()))
p.setSize("8");
Id idAnn = getter.getAnnotation(Id.class);
if (idAnn == null)
idAnn = f.getAnnotation(Id.class);
if (idAnn != null) {
if (pList != null && hasIdProperty(pList))
continue;
p.setAutoIncrement("1");
p.setPk("1");
p.setSize("20");
}
Column colAnn = getter.getAnnotation(Column.class);
if (colAnn == null) {
colAnn = f.getAnnotation(Column.class);
}
String column = colAnn == null ? "" : colAnn.name();
column = "".equals(column.trim()) ? name : column;
p.setName(name);
p.setColumn(column);
p.setType(f.getType().getName());
p.setNotNull("false");
if (colAnn != null) {
// int size = colAnn.length();
p.setNotNull(String.valueOf(!colAnn.nullable()));
p.setUnique(String.valueOf(colAnn.unique()));
}
if (ClassUtil.isPojo(f.getType())) {
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) {
if (oneAnn != null)
p.setType(PropType.ONE_ONE);
else
p.setType(PropType.MANY_ONE);
JoinColumn joinColumn = getter.getAnnotation(JoinColumn.class);
if (joinColumn == null)
joinColumn = f.getAnnotation(JoinColumn.class);
if (joinColumn != null && joinColumn.name().trim().length() > 0)
p.setColumn(joinColumn.name());
else
p.setColumn(f.getName() + "_id");
p.setRelProperty(null);
String refCol = null;
if (joinColumn != null && joinColumn.referencedColumnName().trim().length() > 0) {
refCol = joinColumn.referencedColumnName();
if (refCol != null && refCol.trim().length() > 0) {
String relField = ORMConfigBeanUtil.getField(f.getType(), refCol);
if (relField != null && relField.trim().length() > 0)
p.setRelProperty(relField);
}
}
p.setRelClass(f.getType());
p.setSize("20");
}
}
result.add(p);
}
return result;
}
Aggregations