Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

DBParamManager Class Reference

Collaboration diagram for DBParamManager:

Collaboration graph
[legend]
List of all members.

Static Public Member Functions

< T > void toDB (Param< T > param) throws JetWebException
< T > void toDB (ParamCollection< T > pc) throws JetWebException
< T > Integer matchId (Param< T > param) throws JetWebException
boolean selectById (Param param) throws JetWebException

Static Private Member Functions

void init ()
< T > String tableName (Param< T > param)

Static Private Attributes

DBObjectManager objManager = new DBObjectManager()
boolean initialised_ = false
PreparedStatement insertCollection_
PreparedStatement selectById_

Member Function Documentation

void init  )  [static, private]
 

Definition at line 29 of file DBParamManager.java.

00029                               {
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     }

<T> Integer matchId Param< T >  param  )  throws JetWebException [static]
 

Matches the Id to a parameter using the value, name plus any other attribute (e.g. index if it is an array)

Definition at line 108 of file DBParamManager.java.

References DBObjectManager.availableMethods(), and DBObjectManager.unCamelMe().

00109                           {
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     }

boolean selectById Param  param  )  throws JetWebException [static]
 

Definition at line 144 of file DBParamManager.java.

References DBObjectManager.fillObject().

00145                           {
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     };

<T> String tableName Param< T >  param  )  [static, private]
 

Definition at line 169 of file DBParamManager.java.

References DBObjectManager.unCamelMe().

00169                                                        {
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     }

<T> void toDB ParamCollection< T >  pc  )  throws JetWebException [static]
 

Definition at line 66 of file DBParamManager.java.

00067                           {
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     }

<T> void toDB Param< T >  param  )  throws JetWebException [static]
 

Definition at line 52 of file DBParamManager.java.

References DBObjectManager.writeObject().

00053                           {
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     }


Member Data Documentation

boolean initialised_ = false [static, private]
 

Definition at line 23 of file DBParamManager.java.

PreparedStatement insertCollection_ [static, private]
 

Definition at line 25 of file DBParamManager.java.

DBObjectManager objManager = new DBObjectManager() [static, private]
 

writes a parameter to the data base. Selects the table to write to according to the param type works for anything that inherits from Param (e.g. also ArrayParam) If the param has no id set then it sets the next highest available one

Definition at line 21 of file DBParamManager.java.

PreparedStatement selectById_ [static, private]
 

Definition at line 27 of file DBParamManager.java.


The documentation for this class was generated from the following file:

Generated Wed Jan 17 09:14:27 GMT 2007