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

DBUtil.java

Go to the documentation of this file.
00001 package cedar.jetweb.db;
00002 
00003 import java.sql.*;
00004 import cedar.jetweb.JetWebException;
00005 
00006 import java.lang.reflect.*;
00007 import java.util.Vector;
00008 import java.util.HashMap;
00009 
00010 public abstract class DBUtil{
00011 
00012     private static HashMap<String, HashMap<String, Vector<Method> > >
00013         availableMethodsMap_ = 
00014         new HashMap<String, HashMap<String, Vector<Method> > >();
00015 
00016     public static Vector<Method> availableMethods(Object obj, String prefix){
00017 
00018     if(obj==null) return new Vector<Method>(0);
00019 
00020         Vector<Method> methods;
00021         HashMap<String, Vector<Method> > foundMethods = 
00022             availableMethodsMap_.get(obj.getClass().toString());
00023         
00024         boolean findMethods = false;
00025         boolean foundObject = false;
00026         
00027         if(foundMethods==null){
00028             findMethods = true;
00029         }else if(!foundMethods.containsKey(prefix)){
00030             findMethods = true;
00031             foundObject = true;
00032         }else{
00033         
00034             methods = foundMethods.get(prefix);
00035             return(methods);
00036         }
00037     
00038     methods = new Vector<Method>();
00039     
00040         if(findMethods){
00041             Class objClass = obj.getClass();
00042             Method[] objMethods = objClass.getMethods();
00043             for(int ii=0; ii!= objMethods.length; ++ii){
00044                 if(objMethods[ii].
00045                    getName().
00046                    substring(0,prefix.length()).
00047                    equals(prefix)){
00048                     methods.add(objMethods[ii]);
00049                 }
00050             }
00051             
00052             if(!foundObject){
00053                 foundMethods = new HashMap<String, Vector<Method> >();
00054             }
00055         
00056             foundMethods.put(prefix, methods);
00057         }
00058     
00059         availableMethodsMap_.put(obj.getClass().toString(), foundMethods);
00060         
00061         return(methods);
00062     }
00063 
00064 
00065     public static Integer getNextIndex(String col, String table)
00066     throws JetWebException{
00067 
00068     String select = "SELECT MAX(" + col + ") FROM " + table;
00069 
00070     Integer index = 1;
00071 
00072     try{
00073         Statement stmt = DBConfig.getConnection().createStatement();
00074         ResultSet rs = stmt.executeQuery(select);
00075             
00076         if(rs!=null && rs.next()){
00077         index = rs.getInt(1);
00078         ++index;
00079         }
00080     }catch(SQLException err){
00081         throw new JetWebException(err);
00082     }
00083     return index;
00084     }
00085 
00086     public static String unCamelMe(String input){
00087 
00088     String word = input;
00089 
00090         for(int ii=0; ii< word.length()-1; ++ii){
00091             char letter = word.charAt(ii);
00092             if(Character.isUpperCase(letter)){
00093                 String decapitalised = new String();
00094                 decapitalised += Character.toLowerCase(letter);
00095                 if(ii!=0) decapitalised = "_"+decapitalised;
00096                 word = word.substring(0,ii) + 
00097                     decapitalised+ 
00098                     word.substring(ii+1);
00099 
00100             }
00101         }
00102         return(word);
00103     }
00104 
00105 }

Generated Wed Jan 17 09:14:27 GMT 2007