00001 package cedar.jetweb.db; 00002 00003 import cedar.jetweb.model.parameters.Param; 00004 import cedar.jetweb.model.parameters.ArrayParam; 00005 import cedar.jetweb.model.parameters.ParamCollection; 00006 import cedar.jetweb.model.parameters.ArrayCollection; 00007 import cedar.jetweb.JetWebException; 00008 00009 import java.util.Vector; 00010 import java.lang.reflect.*; 00011 00012 import java.sql.*; 00013 00014 public abstract class DBParamManager { 00021 private static DBObjectManager objManager = new DBObjectManager(); 00022 00023 private static boolean initialised_ = false; 00024 00025 private static PreparedStatement insertCollection_; 00026 00027 private static PreparedStatement selectById_; 00028 00029 private static void init(){ 00030 00031 if(initialised_) return; 00032 initialised_ = true; 00033 try{ 00034 selectById_ = DBConfig.getConnection().prepareStatement 00035 ("SELECT * FROM ? WHERE param_id=?"); 00036 00037 insertCollection_ = DBConfig.getConnection().prepareStatement 00038 ("INSERT INTO ? (collection_id, param_id) VALUES (?, ?)"); 00039 00040 }catch(SQLException err){ 00041 00042 System.out.println 00043 ("Error: cannot initialise prepared statements for "+ 00044 "inserting and retrieving parameters from the DB"); 00045 System.out.println(err.getMessage()); 00046 err.printStackTrace(); 00047 00048 } 00049 return; 00050 } 00051 00052 public static <T> void toDB(Param<T> param) 00053 throws JetWebException{ 00054 00055 String tableName = tableName(param); 00056 00057 if(param.getParamId()<0){ 00058 param.setParamId(DBUtil.getNextIndex("param_id", tableName)); 00059 } 00060 00061 objManager.writeObject(param, tableName, "jetweb"); 00062 00063 return; 00064 } 00065 00066 public static <T> void toDB(ParamCollection<T> pc) 00067 throws JetWebException{ 00068 00069 if(pc.size()==0) return; 00070 00071 Vector<Param<T> > params = pc.getParams(); 00072 00073 String tableName = tableName(params.get(0)); 00074 tableName = tableName.substring(0, tableName.lastIndexOf("_")); 00075 tableName = tableName + "collection"; 00076 00077 Integer id = pc.getCollectionId(); 00078 00079 if(id < 0){ 00080 id = DBUtil.getNextIndex("collection_id", tableName); 00081 pc.setCollectionId(id); 00082 } 00083 00084 try{ 00085 insertCollection_.setString(1,tableName); 00086 insertCollection_.setInt(2, pc.getCollectionId()); 00087 00088 for(Param param: pc.getParams()){ 00089 insertCollection_.setInt(3,param.getParamId()); 00090 insertCollection_.executeUpdate(); 00091 } 00092 00093 }catch(SQLException err){ 00094 throw new JetWebException(err); 00095 } 00096 00097 00098 00099 return; 00100 } 00101 00102 00108 public static <T> Integer matchId(Param<T> param) 00109 throws JetWebException{ 00110 00111 Integer id = -1; 00112 00113 Vector<Method> methods = DBUtil.availableMethods(param, "get"); 00114 00115 String select = "SELECT param_id FROM " + tableName(param) + " WHERE "; 00116 Integer numCols = methods.size(); 00117 Integer stop = numCols - 1; 00118 for(int ii=0; ii!= numCols; ++ii){ 00119 try{ 00120 select = select + 00121 DBUtil.unCamelMe(methods.get(ii).getName().substring(3)) + 00122 "='" + methods.get(ii).invoke(param); 00123 if(ii!=stop){ 00124 select = select + " AND "; 00125 } 00126 }catch(Exception err){ 00127 throw new JetWebException(err); 00128 } 00129 } 00130 00131 try{ 00132 Statement stmt = DBConfig.getConnection().createStatement(); 00133 ResultSet rs = stmt.executeQuery(select); 00134 00135 if(rs!=null && rs.next()){ 00136 id = rs.getInt(1); 00137 } 00138 }catch(SQLException err){ 00139 throw new JetWebException(err); 00140 } 00141 return id; 00142 } 00143 00144 public static boolean selectById(Param param) 00145 throws JetWebException{ 00146 boolean success = false; 00147 String select = "SELECT * FROM " + tableName(param) + 00148 " WHERE param_id="+param.getParamId(); 00149 00150 int numFilled = 0; 00151 00152 try{ 00153 selectById_.setString(1, tableName(param)); 00154 selectById_.setInt(2, param.getParamId()); 00155 //Statement stmt = DBConfig.getConnection().createStatement(); 00156 //ResultSet rs = stmt.executeQuery(select); 00157 ResultSet rs = selectById_.executeQuery(); 00158 00159 numFilled = objManager.fillObject(param, rs); 00160 }catch(Exception err){ 00161 throw new JetWebException(err); 00162 } 00163 00164 if(numFilled >=3) success = true; 00165 00166 return success; 00167 }; 00168 00169 private static <T> String tableName(Param<T> param){ 00170 00171 T val = param.getValue(); 00172 00173 String TName = param.getValue().getClass().getName(); 00174 00175 TName = TName.substring(TName.lastIndexOf(".") + 1); 00176 00177 String paramName = param.getClass().getName(); 00178 paramName = paramName.substring(paramName.lastIndexOf(".") + 1); 00179 00180 String fullName = DBUtil.unCamelMe(TName) + "_" + 00181 DBUtil.unCamelMe(paramName); 00182 00183 return fullName; 00184 } 00185 00186 }
Generated Wed Jan 17 09:14:27 GMT 2007