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

DBGeneratorManager.java

Go to the documentation of this file.
00001 package cedar.jetweb.db;
00002 
00003 import java.util.HashMap;
00004 import java.util.Map;
00005 import java.util.Map.Entry;
00006 import java.util.HashSet;
00007 import java.util.Set;
00008 
00009 import java.sql.*;
00010 
00011 import cedar.jetweb.*;
00012 import cedar.jetweb.generator.*;
00013 import cedar.jetweb.model.Model;
00014 import cedar.jetweb.model.RunSeries;
00015 import cedar.jetweb.model.ResultSearchPattern;
00016 
00021 public abstract class DBGeneratorManager {
00022 
00026     public static void selectGeneratorParameters(RunSeries runseries)
00027     throws JetWebException {
00028 
00029     Generator gen = runseries.getGenerator();
00030 
00031     if(gen==null){
00032         try{
00033         gen = makeGen(runseries);
00034         }catch (JetWebDBException err){
00035         System.out.println(err.getMessage());
00036         }
00037     }
00038     String findAll = 
00039         "SELECT *  FROM generator_runseries WHERE runseries_id="+runseries.getId();
00040     try {
00041         selectParameters(gen,true,findAll);
00042     } catch (JetWebException e) {
00043         throw e;
00044     }
00045     }
00046 
00050     public static int getGeneratorId(String generatorName, String generatorVersion)
00051     throws JetWebException {
00052 
00053     String find = 
00054         "SELECT generator_id  FROM generators WHERE name='"+generatorName
00055         +"' AND version='"+generatorVersion+"'";
00056 
00057     try {
00058         Statement stmt = DBConfig.getConnection().createStatement();
00059             ResultSet rs = stmt.executeQuery(find);
00060 
00061             if (rs.next()) {
00062             int id = rs.getInt("generator_id");
00063         rs.close();
00064         stmt.close();
00065         return id;
00066             } else {
00067 
00068         find = "SELECT MAX(generator_id) from generators";
00069         rs = stmt.executeQuery(find);
00070 
00071         if(rs.next()){
00072             Integer id = rs.getInt(1) + 1;
00073             rs.close();
00074             stmt.close();
00075             if(id==null) id = 1;
00076             return id;
00077         }
00078         }
00079     } catch (SQLException E){
00080         throw new JetWebDBException(E,find," Failed ");
00081     }
00082     return -1;
00083     }
00084 
00085 
00092     public static void selectGeneratorParameters(Model model)
00093     throws JetWebException {
00094 
00095     Generator gen = model.getGenerator();
00096 
00097     String findAll = 
00098         "SELECT *  FROM generator_model WHERE mdl_id="+model.getId();
00099     try {
00100         selectParameters(gen,true,findAll);
00101     } catch (JetWebException e) {
00102         throw e;
00103     }
00104     }
00105 
00106 
00113     public static void selectDefaultParameters(Generator gen, boolean hard) 
00114     throws JetWebException {
00115 
00116     String tableName = stripDots
00117         (gen.getName()+gen.getVersion()+"_defaults");
00118     String findAll = "SELECT *  FROM "+tableName;
00119     selectParameters(gen,hard,findAll);
00120 
00121     }
00122     
00123     private static synchronized void selectParameters(Generator gen, 
00124                               boolean hard,
00125                               String findAll)
00126     throws JetWebException {
00127 
00128     HashMap<String,HashMap<Integer,Integer>> intArrays = gen.getIntParameterArrays();
00129     HashMap<String,HashMap<Integer,Double>> realArrays = gen.getRealParameterArrays();
00130     HashMap<String,Integer> intParameters  = gen.getIntParameters();
00131     HashMap<String,Double>  realParameters = gen.getRealParameters();
00132 
00133     try {
00134             Statement stmt = DBConfig.getConnection().createStatement();
00135         ResultSet rs = stmt.executeQuery(findAll);
00136             
00137         while (rs.next()){
00138         boolean isArrayElement;
00139         boolean isInt;
00140             String name = rs.getString("name");         
00141             Integer index = rs.getInt("aindices");
00142         Integer iValue;
00143         Double rValue;
00144         if (rs.wasNull()){
00145             // This is a parameter.
00146             isArrayElement = false;
00147         } else {
00148             // This is an array element.
00149             isArrayElement = true;
00150         }
00151             iValue = rs.getInt("ivalue");
00152         if (rs.wasNull()){
00153             // This is a real value
00154             isInt = false;
00155             rValue = new Double(rs.getFloat("rvalue"));
00156         } else {
00157             // This is an integer value
00158             isInt = true;
00159             rValue = null;
00160         }
00161 
00162         if (isArrayElement) {
00163 
00164             if (isInt) {
00165             // Fill Integer arrays
00166             // If the array doesn't exists, create it.
00167             HashMap<Integer,Integer> map;
00168             if (!intArrays.containsKey(name)){
00169                 map = new HashMap<Integer,Integer>();
00170                 intArrays.put(name,map);
00171             }
00172             map = intArrays.get(name);
00173             
00174             if (map.containsKey(index)){
00175                 if (hard) {
00176                 map.remove(index);
00177                 map.put(index,iValue);
00178                 }
00179             } else {
00180                 map.put(index,iValue);
00181             }
00182 
00183             } else {
00184             // Fill Real arrays
00185             // If the array doesn't exists, create it.
00186             HashMap<Integer,Double> map;
00187             if (!realArrays.containsKey(name)){
00188                 map = new HashMap<Integer,Double>();
00189                 realArrays.put(name,map);
00190             }
00191             map = realArrays.get(name);
00192             
00193             if (map.containsKey(index)){
00194                 if (hard) {
00195                 map.remove(index);
00196                 map.put(index,rValue);
00197                 }
00198             } else {
00199                 map.put(index,rValue);
00200             }
00201             }
00202 
00203         } else {
00204 
00205             if (isInt) {
00206             // Fill Integer parameters
00207             if (intParameters.containsKey(name)){
00208                 if (hard) {
00209                 intParameters.remove(name);
00210                 intParameters.put(name,iValue);
00211                 }   
00212             } else {                    
00213                 intParameters.put(name,iValue);
00214             }
00215             } else {
00216             // Fill Real parameters
00217             if (realParameters.containsKey(name)){
00218                 if (hard) {
00219                 realParameters.remove(name);
00220                 realParameters.put(name,rValue);
00221                 }
00222             } else {
00223                 realParameters.put(name,rValue);
00224             }
00225             }
00226         }
00227 
00228         }
00229         rs.close();
00230         stmt.close();
00231     } catch (SQLException E){
00232         System.out.println("DBManager: Error reading parameters");
00233         System.out.println("SQLException: " + E.getMessage());
00234         System.out.println("SQLState:     " + E.getSQLState());
00235         System.out.println("VendorError:  " + E.getErrorCode());
00236         throw new JetWebDBException(E,findAll," Failed ");
00237     }
00238     
00239     }
00240 
00241     public static void addGeneratorParameters(String insert, Generator gen) 
00242     throws SQLException {
00243     
00244     Set<Map.Entry<String,Integer>> intParameters;
00245     Set<Map.Entry<String,Double>>  realParameters;
00246     Set<Map.Entry<String,HashMap<Integer,Integer>>> intArrays;
00247     Set<Map.Entry<String,HashMap<Integer,Double>>> realArrays;
00248 
00249     String values;
00250 
00251     Statement stmt = DBConfig.getConnection().createStatement();
00252     
00253     // First the Integer parameters
00254     intParameters  = gen.getIntParameters().entrySet(); 
00255     for (Map.Entry<String,Integer> entry : intParameters){
00256         if(gen.inModel(entry.getKey())){
00257         values = "'"+entry.getKey()+
00258             "',NULL,'"+entry.getValue()+"',NULL)";
00259         //System.out.println(insert+values);
00260         int rowsChanged = stmt.executeUpdate(insert+values);
00261         }
00262     }   
00263     // Now the real parameters
00264     realParameters = gen.getRealParameters().entrySet(); 
00265     for (Map.Entry<String,Double> entry : realParameters){
00266         if(gen.inModel(entry.getKey())){
00267         values = "'"+entry.getKey()+"',NULL,NULL,'" + 
00268             entry.getValue()+"')";      
00269         int rowsChanged = stmt.executeUpdate(insert+values);
00270         }       
00271     }   
00272     // Now the integer arrays.
00273     intArrays = gen.getIntParameterArrays().entrySet(); 
00274     for (Map.Entry<String,HashMap<Integer,Integer>> entry : intArrays){
00275         
00276         String arrayName = entry.getKey(); 
00277         if(gen.inModel(entry.getKey())){
00278         Set<Map.Entry<Integer,Integer>> array = 
00279             entry.getValue().entrySet();
00280         for (Map.Entry<Integer,Integer> arrayEntry : array){
00281             if(gen.inModel(entry.getKey(), arrayEntry.getKey())){
00282             Integer value = arrayEntry.getValue();
00283             Integer index = arrayEntry.getKey();            
00284             values = "'"+arrayName+"','"+
00285                 index+"','"+value+"',NULL)";
00286             int rowsChanged = stmt.executeUpdate(insert+values);
00287             }
00288         }
00289         }
00290     }
00291     // Now the real arrays.
00292     realArrays = gen.getRealParameterArrays().entrySet(); 
00293     for (Map.Entry<String,HashMap<Integer,Double>> entry : realArrays){
00294         
00295         String arrayName = entry.getKey(); 
00296         if(gen.inModel(arrayName)){
00297         Set<Map.Entry<Integer,Double>> array = 
00298             entry.getValue().entrySet();
00299         for (Map.Entry<Integer,Double> arrayEntry : array){
00300             
00301             if(gen.inModel(arrayName, arrayEntry.getKey())){
00302 
00303             Double value = arrayEntry.getValue();
00304             Integer index = arrayEntry.getKey();            
00305             values = "'"+arrayName+"','"+index+
00306                 "',NULL,'"+value+"')";
00307             int rowsChanged = stmt.executeUpdate(insert+values);
00308             }
00309         }
00310         }
00311     }
00312     stmt.close();
00313     }
00314 
00319     public static int getMaxIndex(String gname, String gversion, String pname) 
00320     throws JetWebException {
00321 
00322     String query=null;
00323 
00324     try {
00325         Statement stmt = DBConfig.getConnection().createStatement();
00326         String tableName = stripDots(gname+gversion+"_defaults");
00327 
00328             query = "SELECT MAX(aindices) max FROM "+tableName+" where name='"+pname+"'";
00329             ResultSet rs = stmt.executeQuery(query);
00330 
00331             if (rs.next()) {
00332         int max = rs.getInt("max");
00333         stmt.close();
00334         rs.close();
00335         return max;
00336             } else {
00337         rs.close();
00338         stmt.close();
00339         throw new JetWebException("Parameter "+pname+" in "+tableName,"Not found");
00340         }
00341     } catch (SQLException E){
00342         throw new JetWebDBException(E,query," Failed ");
00343     }
00344     }
00345 
00346 
00351     public static int getMinIndex(String gname, String gversion, String pname) 
00352     throws JetWebException {
00353 
00354     String query=null;
00355 
00356     try {
00357         Statement stmt = DBConfig.getConnection().createStatement();
00358         String tableName = stripDots(gname+gversion+"_defaults");
00359 
00360             query = "SELECT MIN(aindices) min FROM "+tableName+" where name='"+pname+"'";
00361             ResultSet rs = stmt.executeQuery(query);
00362 
00363             if (rs.next()) {
00364             int min = rs.getInt("min");
00365         rs.close();
00366         stmt.close();
00367         return min;
00368             } else {
00369         rs.close();
00370         stmt.close();
00371         throw new JetWebException("Parameter "+pname+" in "+tableName,"Not found");
00372         }
00373     } catch (SQLException E){
00374         throw new JetWebDBException(E,query," Failed ");
00375     }
00376     }
00377 
00382     public static Set<String> selectIntNames(String gname, String gversion) 
00383     throws JetWebException {
00384 
00385     String query=null;
00386     Set<String> names = new HashSet<String>();
00387     try {
00388         Statement stmt = DBConfig.getConnection().createStatement();
00389         String tableName = stripDots(gname+gversion+"_defaults");
00390 
00391             query = "SELECT name FROM "+tableName+" WHERE aindices IS NULL AND ivalue IS NOT NULL";
00392             ResultSet rs = stmt.executeQuery(query);
00393 
00394             while (rs.next()) {
00395         names.add(rs.getString("name"));
00396         }
00397         rs.close();
00398         stmt.close();
00399 
00400     } catch (SQLException E){
00401         throw new JetWebDBException(E,query," Failed ");
00402     }
00403     return names;   
00404     }
00409     public static Set<String> selectRealNames(String gname, String gversion) 
00410     throws JetWebException {
00411 
00412     String query=null;
00413     Set<String> names = new HashSet<String>();
00414     try {
00415         Statement stmt = DBConfig.getConnection().createStatement();
00416         String tableName = stripDots(gname+gversion+"_defaults");
00417 
00418             query = "SELECT name FROM "+tableName+" WHERE aindices IS NULL AND rvalue IS NOT NULL";
00419             ResultSet rs = stmt.executeQuery(query);
00420 
00421             while (rs.next()) {
00422         names.add(rs.getString("name"));
00423         }
00424         rs.close();
00425         stmt.close();
00426 
00427     } catch (SQLException E){
00428         throw new JetWebDBException(E,query," Failed ");
00429     }
00430     return names;   
00431     
00432     }
00437     public static Set<String> selectIntArrayNames(String gname, String gversion) 
00438     throws JetWebException {
00439 
00440     String query=null;
00441     Set<String> names = new HashSet<String>();
00442     try {
00443         Statement stmt = DBConfig.getConnection().createStatement();
00444         String tableName = stripDots(gname+gversion+"_defaults");
00445 
00446             query = "SELECT name FROM "+tableName+" WHERE aindices IS NOT NULL AND rvalue IS NULL";
00447             ResultSet rs = stmt.executeQuery(query);
00448 
00449             while (rs.next()) {
00450         names.add(rs.getString("name"));
00451         }
00452         rs.close();
00453         stmt.close();
00454 
00455     } catch (SQLException E){
00456         throw new JetWebDBException(E,query," Failed ");
00457     }
00458     return names;   
00459     
00460     }
00465     public static Set<String> selectRealArrayNames(String gname, String gversion) 
00466     throws JetWebException {
00467 
00468     String query=null;
00469     Set<String> names = new HashSet<String>();
00470     try {
00471         Statement stmt = DBConfig.getConnection().createStatement();
00472         String tableName = stripDots(gname+gversion+"_defaults");
00473 
00474             query = "SELECT name FROM "+tableName+" WHERE aindices IS NOT NULL AND rvalue IS NOT NULL";
00475             ResultSet rs = stmt.executeQuery(query);
00476 
00477             while (rs.next()) {
00478         names.add(rs.getString("name"));
00479         }
00480         rs.close();
00481         stmt.close();
00482 
00483     } catch (SQLException E){
00484         throw new JetWebDBException(E,query," Failed ");
00485     }
00486     return names;       
00487     }
00488 
00489 
00490     private static Generator makeGen(RunSeries runSeries)
00491     throws JetWebDBException{
00492 
00493     Generator gen=null;
00494 
00495     String query = 
00496         "select name, version from generators, runseries where runseries.generator_id=generators.generator_id and runseries_id="+
00497         runSeries.getId();
00498 
00499     try{
00500         Statement stmt = DBConfig.getConnection().createStatement();
00501         ResultSet rs = stmt.executeQuery(query);
00502 
00503         while(rs.next()){
00504         String name = rs.getString("name");
00505         String ver = rs.getString("version");
00506         try{
00507             gen = Generator.Maker(name, ver);
00508         }catch(JetWebException err){
00509             System.out.println("unable to make generator");
00510             System.out.println(err.getMessage());
00511         }
00512         }
00513     }catch (SQLException E){
00514         throw new JetWebDBException(E,query," Failed ");
00515     }
00516 
00517     return gen;
00518     }
00519 
00523     public static String[] getNameAndVersion(int id) 
00524     throws JetWebException {
00525 
00526     String query=null;
00527     String[] nameAndVersion = new String[2];
00528     try {
00529         Statement stmt = DBConfig.getConnection().createStatement();
00530         String tableName = "generators";
00531 
00532             query = "SELECT name,version FROM "+tableName+" WHERE generator_id='"+id+"'";
00533             ResultSet rs = stmt.executeQuery(query);
00534 
00535             while (rs.next()) {
00536         nameAndVersion[0] = rs.getString("name");
00537         nameAndVersion[1] = rs.getString("version");
00538         }
00539         rs.close();
00540         stmt.close();
00541 
00542     } catch (SQLException E){
00543         throw new JetWebDBException(E,query," Failed ");
00544     }
00545     return nameAndVersion;  
00546     }
00547 
00548 
00552     public static Set<String> getVersions(String gname) 
00553     throws JetWebException {
00554 
00555     String query=null;
00556     Set<String> versions = new HashSet<String>();
00557     try {
00558         Statement stmt = DBConfig.getConnection().createStatement();
00559         String tableName = "generators";
00560 
00561             query = "SELECT version FROM "+tableName+" WHERE name='"+gname+"'";
00562             ResultSet rs = stmt.executeQuery(query);
00563 
00564             while (rs.next()) {
00565         versions.add(rs.getString("version"));
00566         }
00567         rs.close();
00568         stmt.close();
00569 
00570     } catch (SQLException E){
00571         throw new JetWebDBException(E,query," Failed ");
00572     }
00573     return versions;    
00574     }
00575 
00576 
00577 
00581     public static Set<String> allNames() 
00582     throws JetWebException {
00583 
00584     String query=null;
00585     Set<String> names = new HashSet<String>();
00586     try {
00587         Statement stmt = DBConfig.getConnection().createStatement();
00588         String tableName = "generators";
00589 
00590             query = "SELECT name FROM "+tableName;
00591             ResultSet rs = stmt.executeQuery(query);
00592 
00593             while (rs.next()) {
00594         String name = rs.getString("name");
00595         if (!names.contains(name)) {
00596             names.add(name);
00597         }
00598         }
00599         rs.close();
00600         stmt.close();
00601 
00602     } catch (SQLException E){
00603         throw new JetWebDBException(E,query," Failed ");
00604     }
00605     return names;   
00606     }
00607 
00611     public static String getDefaultVersion(String gname) 
00612     throws JetWebException {
00613 
00614     String query = null;
00615     String version = null;
00616     try {
00617         Statement stmt = DBConfig.getConnection().createStatement();
00618         String tableName = "generators";
00619 
00620             query = "SELECT version FROM "+tableName+" WHERE name='"
00621         +gname+"'"+" AND (is_default_version='1' OR is_default_version='2')";
00622             ResultSet rs = stmt.executeQuery(query);
00623 
00624             while (rs.next()) {
00625         version = rs.getString("version");
00626         }
00627         rs.close();
00628         stmt.close();
00629 
00630     } catch (SQLException E){
00631         throw new JetWebDBException(E,query," Failed ");
00632     }
00633     if (version==null) {
00634         throw new JetWebException("No default version found.",gname);
00635     }
00636     return version; 
00637     }
00638 
00642     public static void add(Generator gen) throws JetWebDBException {
00643 
00644     try {
00645         Statement stmt = DBConfig.getConnection().createStatement();
00646         String ins = "";
00647         try{
00648         ins = 
00649         "INSERT INTO generators (generator_id, name,version) VALUES('"
00650         + gen.getGeneratorId() + "', '" + 
00651         gen.getName()+"','"+gen.getVersion()+"')";
00652         }catch(Throwable err){
00653         System.out.println(err.getMessage());
00654         }
00655         int result = stmt.executeUpdate(ins);
00656         
00657         stmt.close();
00658 
00659     } catch (SQLException E){
00660         throw new JetWebDBException(E,gen.toString()," Failed ");       
00661     }
00662     }
00663 
00664     private static String stripDots(String word){
00665     int ii=word.indexOf(".");
00666     
00667     while(ii>-1){
00668         word = word.substring(0,ii)+word.substring(ii+1);
00669         ii=word.indexOf(".");
00670     }
00671     return word;
00672     }
00673 
00674 
00683     public static boolean checkProtonRelevance(Generator gen,
00684                            String name,Integer aindex) 
00685     throws JetWebException {
00686 
00687     return checkRelevance(gen,name,aindex,"rel_proton");
00688 
00689     }
00694     public static boolean checkPhotonRelevance(Generator gen,
00695                            String name,Integer aindex) 
00696     throws JetWebException {
00697 
00698     return checkRelevance(gen,name,aindex,"rel_photon");
00699 
00700     }
00701 
00702 
00707     private static boolean checkRelevance(Generator gen,String pName,
00708                     Integer aIndex, String columnName) 
00709     throws JetWebException {
00710 
00711     StringBuffer query=null;
00712     int rel=1;
00713     try {
00714         String tableName = gen.getName()+gen.getVersion()+"_defaults";
00715         DatabaseMetaData metaData = DBConfig.getConnection().getMetaData();
00716         ResultSet existingTables = metaData.getTables
00717         (DBConfig.getConnection().getCatalog(), null, "", null);
00718         
00719         boolean found=false;
00720         while(existingTables.next()&&!found){
00721         found = 
00722             (existingTables.getString("TABLE_NAME").
00723              equals(tableName))? true: false;
00724         }
00725 
00726 
00727         // if we can find no info in the DB about this parameter then
00728         // have to assume it is relevant
00729         if(!found)return true;
00730 
00731         Statement stmt = DBConfig.getConnection().createStatement();
00732 
00733             query = new StringBuffer("SELECT "+columnName+" FROM "+tableName+" WHERE name='"+pName+"'");
00734         if (aIndex!=null){
00735         //query.append(" AND aindex='"+aIndex.toString()+"'");
00736         query.append(" AND aindices='"+aIndex.toString()+"'");
00737         }
00738             ResultSet rs = stmt.executeQuery(query.toString());
00739 
00740             while (rs.next()) {
00741         rel = rs.getInt(columnName);
00742         //System.out.println(query+ " : "+rel);
00743         }
00744         rs.close();
00745         stmt.close();
00746 
00747     } catch (SQLException E){
00748         throw new JetWebDBException(E,query.toString()," Failed ");
00749     }
00750     //System.out.println(rel==1);
00751     return (rel==1);
00752 
00753     }
00754 
00755 
00756 }
00757 

Generated Wed Jan 17 09:14:27 GMT 2007