package pwv.spring.edbutil; import java.beans.BeanInfo; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.sql.Timestamp; import java.sql.Types; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.StringTokenizer; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; public class EDbHelper { private static Log log = LogFactory.getLog( EDbHelper.class ); private String clsName; // クラスパス private String objLabel; // クラスパスの最後の名前 private List propList; private String insertSql = null; private String deleteSql = null; private int id = 0; public EDbHelper(Class cls) { clsName = cls.getName(); try { StringTokenizer tk = new StringTokenizer(clsName, "."); while (tk.hasMoreTokens()) { objLabel = tk.nextToken(); } BeanInfo info = Introspector.getBeanInfo(cls); PropertyDescriptor[] objProps = info.getPropertyDescriptors(); /** * getter, setterが揃っているものだけをDB登録用候補とする */ propList = new ArrayList(); for (int i = 0; i < objProps.length; i++) { Method getMethod = objProps[i].getReadMethod(); Method setMethod = objProps[i].getWriteMethod(); if (getMethod != null && setMethod != null) { propList.add(objProps[i]); } } } catch (Exception e) { } } public EDbHelper(Object obj) { this(obj.getClass()); } public String toString(Object obj) { if (!obj.getClass().getName().equals(clsName)) return (null); StringBuffer buf = new StringBuffer(); buf.append(objLabel.toUpperCase() + ":"); for (int i = 0; i < propList.size(); i++) { PropertyDescriptor objProp = (PropertyDescriptor)propList.get(i); String propName = objProp.getName().toUpperCase(); Class propType = objProp.getPropertyType(); Method getMethod = objProp.getReadMethod(); if (propName.equals("CLASS")) // オブジェクト自身を除く continue; Object propValue = null; try { propValue = getMethod.invoke(obj, null); } catch (Exception e) { propValue = null; } if (propValue != null) { if (Integer.class.equals(propType) || Double.class.equals(propType) || Boolean.class.equals(propType) || Timestamp.class.equals(propType)) { buf.append(" "); buf.append(propName); buf.append("="); buf.append(propValue.toString()); } else if (Date.class.equals(propType)) { SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd"); buf.append(" "); buf.append(propName); buf.append("='"); buf.append(fmt.format(propValue)); buf.append("'"); } else if (String.class.equals(propType)) { buf.append(" "); buf.append(propName); buf.append("='"); buf.append(propValue.toString()); buf.append("'"); } } } return (buf.toString()); } public String getTableName() { return ("T_" + objLabel.toUpperCase()); } public String getCreateTableStmt() { StringBuffer buf = new StringBuffer(); buf.append("CREATE TABLE " + getTableName()); buf.append( "(ID INTEGER NOT NULL PRIMARY KEY"); for (int i = 0; i < propList.size(); i++) { PropertyDescriptor objProp = (PropertyDescriptor)propList.get(i); String propName = objProp.getName().toUpperCase(); Class propType = objProp.getPropertyType(); Method getMethod = objProp.getReadMethod(); Method setMethod = objProp.getWriteMethod(); if (propName.equals("ID")) // IDは固定とする continue; if (propName.equals("CLASS")) // オブジェクト自身を除く continue; // getter, setterが揃っているものだけ使用する if (setMethod == null || getMethod == null) continue; // volatile宣言された変数はDBには、含めない if (Modifier.isVolatile(propType.getModifiers())) continue; if (Integer.class.equals(propType)) { buf.append("," + propName + " INTEGER"); } else if (Double.class.equals(propType)) { buf.append("," + propName + " DOUBLE"); } else if (String.class.equals(propType)) { buf.append("," + propName + " VARCHAR"); } else if (Boolean.class.equals(propType)) { buf.append("," + propName + " BIT"); } else if (Date.class.equals(propType)) { buf.append("," + propName + " DATE"); } else if (Timestamp.class.equals(propType)) { buf.append("," + propName + " TIMESTAMP"); } } buf.append(")"); return (buf.toString()); } public Object[] getObjectArray(Object obj) { if (!obj.getClass().getName().equals(clsName)) return (null); List list = new ArrayList(); Object propValue = null; // カラムをセット for (int i = 0; i < propList.size(); i++) { PropertyDescriptor objProp = (PropertyDescriptor)propList.get(i); String propName = objProp.getName().toUpperCase(); Class propType = objProp.getPropertyType(); Method getMethod = objProp.getReadMethod(); Method setMethod = objProp.getWriteMethod(); if (propName.equals("CLASS")) // オブジェクト自身を除く continue; // getter, setterが揃っているものだけ使用する if (setMethod == null || getMethod == null) continue; // volatile宣言された変数はDBには、含めない if (Modifier.isVolatile(propType.getModifiers())) continue; if (Integer.class.equals(propType) || Double.class.equals(propType) || String.class.equals(propType) || Boolean.class.equals(propType) || Date.class.equals(propType) || Timestamp.class.equals(propType)) { try { propValue = getMethod.invoke(obj, null); if (propName.equals("ID")) { // IDがnullの場合にはidをセットする if (propValue == null) { propValue = new Integer(id++); ((IEBase)obj).setId((Integer)propValue); } } } catch (Exception e) { propValue = null; } list.add(propValue); } } return (list.toArray()); } public int[] getTypeArray(Object obj) { if (!obj.getClass().getName().equals(clsName)) return (null); List list = new ArrayList(); // カラムをセット for (int i = 0; i < propList.size(); i++) { PropertyDescriptor objProp = (PropertyDescriptor)propList.get(i); String propName = objProp.getName().toUpperCase(); Class propType = objProp.getPropertyType(); Method getMethod = objProp.getReadMethod(); Method setMethod = objProp.getWriteMethod(); if (propName.equals("CLASS")) // オブジェクト自身を除く continue; // getter, setterが揃っているものだけ使用する if (setMethod == null || getMethod == null) continue; // volatile宣言された変数はDBには、含めない if (Modifier.isVolatile(propType.getModifiers())) continue; if (Integer.class.equals(propType)) { list.add(new Integer(Types.INTEGER)); } else if(Double.class.equals(propType)) { list.add(new Integer(Types.DOUBLE)); } else if (String.class.equals(propType)) { list.add(new Integer(Types.VARCHAR)); } else if (Boolean.class.equals(propType)) { list.add(new Integer(Types.BIT)); } else if (Date.class.equals(propType)) { list.add(new Integer(Types.DATE)); } else if (Timestamp.class.equals(propType)) { list.add(new Integer(Types.TIMESTAMP)); } } int[] ret = new int[list.size()]; for (int i = 0; i < ret.length; i++) { ret[i] = ((Integer)list.get(i)).intValue(); } return (ret); } public String getInsertStmt(Object obj) { if (!obj.getClass().getName().equals(clsName)) return (null); if (insertSql == null) { StringBuffer buf = new StringBuffer(); buf.append("INSERT INTO " + getTableName() +" ("); int insCount = 0; // カラムをセット for (int i = 0; i < propList.size(); i++) { PropertyDescriptor objProp = (PropertyDescriptor)propList.get(i); String propName = objProp.getName().toUpperCase(); Class propType = objProp.getPropertyType(); Method getMethod = objProp.getReadMethod(); Method setMethod = objProp.getWriteMethod(); if (propName.equals("CLASS")) // オブジェクト自身を除く continue; // getter, setterが揃っているものだけ使用する if (setMethod == null || getMethod == null) continue; // volatile宣言された変数はDBには、含めない if (Modifier.isVolatile(propType.getModifiers())) continue; if (Integer.class.equals(propType) || Double.class.equals(propType) || String.class.equals(propType) || Boolean.class.equals(propType) || Date.class.equals(propType) || Timestamp.class.equals(propType)) { if (insCount++ != 0) { buf.append(", "); } buf.append(propName); } } buf.append(")" +"VALUES ("); insCount = 0; // VALUESをセット for (int i = 0; i < propList.size(); i++) { PropertyDescriptor objProp = (PropertyDescriptor)propList.get(i); String propName = objProp.getName().toUpperCase(); Class propType = objProp.getPropertyType(); Method getMethod = objProp.getReadMethod(); Method setMethod = objProp.getWriteMethod(); Object propValue = null; if (propName.equals("CLASS")) // オブジェクト自身を除く continue; // getter, setterが揃っているものだけ使用する if (setMethod == null || getMethod == null) continue; // volatile宣言された変数はDBには、含めない if (Modifier.isVolatile(propType.getModifiers())) continue; if (Integer.class.equals(propType) || Double.class.equals(propType) || String.class.equals(propType) || Boolean.class.equals(propType) || Date.class.equals(propType) || Timestamp.class.equals(propType)) { if (insCount++ != 0) { buf.append(", ?"); } else { buf.append("? "); } } } buf.append(")"); insertSql = buf.toString(); } return (insertSql); } public String getUpdateStmt(Object obj) { if (!obj.getClass().getName().equals(clsName)) return (null); StringBuffer buf = new StringBuffer(); int insCount = 0; buf.append("UPDATE " + getTableName() +" SET "); for (int i = 0; i < propList.size(); i++) { PropertyDescriptor objProp = (PropertyDescriptor)propList.get(i); String propName = objProp.getName().toUpperCase(); Class propType = objProp.getPropertyType(); Method getMethod = objProp.getReadMethod(); Method setMethod = objProp.getWriteMethod(); Object propValue = null; if (propName.equals("CLASS")) // オブジェクト自身を除く continue; // getter, setterが揃っているものだけ使用する if (setMethod == null || getMethod == null) continue; // volatile宣言された変数はDBには、含めない if (Modifier.isVolatile(propType.getModifiers())) continue; if (Integer.class.equals(propType) || Double.class.equals(propType) || String.class.equals(propType) || Boolean.class.equals(propType) || Date.class.equals(propType) || Timestamp.class.equals(propType)) { if (insCount++ != 0) buf.append(","); buf.append(propName + "=? "); } } Integer id = ((IEBase)obj).getId(); if (id != null) { buf.append(" WHERE ID=" + id); } else { buf.append(" WHERE ID=NULL"); } return (buf.toString()); } public String getDeleteStmt(Object obj) { if (!obj.getClass().getName().equals(clsName)) return (null); if (deleteSql == null) { StringBuffer buf = new StringBuffer(); Integer id = null; buf.append("DELETE FROM " + getTableName()); for (int i = 0; i < propList.size(); i++) { PropertyDescriptor objProp = (PropertyDescriptor)propList.get(i); String propName = objProp.getName().toUpperCase(); Class propType = objProp.getPropertyType(); Method getMethod = objProp.getReadMethod(); Object propValue = null; if (!propName.equals("ID")) continue; try { propValue = getMethod.invoke(obj, null); } catch (Exception e) { propValue = "NULL"; } id = (Integer)propValue; } if (id == null) return (null); buf.append(" WHERE ID=" + id); deleteSql = buf.toString(); } return (deleteSql); } public String getHibernateXml() { StringBuffer buf = new StringBuffer(); buf.append("\n"); buf.append("\n"); buf.append("\n"); buf.append("\t\n"); buf.append("\t\t\n"); buf.append("\t\t\t\n"); buf.append("\t\t\n"); Object propValue = null; // カラムをセット for (int i = 0; i < propList.size(); i++) { PropertyDescriptor objProp = (PropertyDescriptor)propList.get(i); String propName = objProp.getName(); Class propType = objProp.getPropertyType(); Method getMethod = objProp.getReadMethod(); Method setMethod = objProp.getWriteMethod(); if (propName.toUpperCase().equals("ID")) // IDは固定とする continue; if (propName.toUpperCase().equals("CLASS")) // オブジェクト自身を除く continue; // getter, setterが揃っているものだけ使用する if (setMethod == null || getMethod == null) continue; // volatile宣言された変数はDBには、含めない if (Modifier.isVolatile(propType.getModifiers())) continue; if (Integer.class.equals(propType) || Double.class.equals(propType) || String.class.equals(propType) || Boolean.class.equals(propType) || Date.class.equals(propType) || Timestamp.class.equals(propType)) { buf.append("\t\t\n"); } } buf.append("\t\n"); buf.append("\n"); return (buf.toString()); } public List getPropList() { return propList; } public String getObjLabel() { return objLabel; } public String getClsName() { return clsName; } public int getId() { return id; } public void setId(int i) { id = i; } public static void main(String[] args) { for (int i = 0; i < args.length; i++) { try { EDbHelper helper = new EDbHelper(Class.forName(args[0])); System.out.println(""); System.out.println(helper.getCreateTableStmt()); System.out.println(""); System.out.println(helper.getHibernateXml()); } catch (Exception e) { System.err.println("Class not found"); } } } }