Coverage Report - com.buckosoft.PicMan.db.SystemDaoJdbc
 
Classes in this File Line Coverage Branch Coverage Complexity
SystemDaoJdbc
0%
0/60
0%
0/46
4.111
SystemDaoJdbc$1
N/A
N/A
4.111
SystemDaoJdbc$KeyValue
0%
0/1
N/A
4.111
SystemDaoJdbc$SystemInsert
0%
0/9
N/A
4.111
SystemDaoJdbc$SystemQuery
0%
0/8
N/A
4.111
SystemDaoJdbc$SystemUpdate
0%
0/74
0%
0/2
4.111
 
 1  
 /******************************************************************************
 2  
  * SystemDaoJdbc.java - Implement the Dao interface for the System bean
 3  
  * 
 4  
  * PicMan - The BuckoSoft Picture Manager in Java
 5  
  * Copyright(c) 2005 - Dick Balaska
 6  
  * 
 7  
  */
 8  
 package com.buckosoft.PicMan.db;
 9  
 
 10  
 import java.util.List;
 11  
 import java.util.Iterator;
 12  
 
 13  
 import javax.sql.DataSource;
 14  
 
 15  
 import java.sql.ResultSet;
 16  
 import java.sql.SQLException;
 17  
 import java.sql.Types;
 18  
 
 19  
 import org.apache.commons.logging.Log;
 20  
 import org.apache.commons.logging.LogFactory;
 21  
 
 22  
 import org.springframework.dao.DataAccessException;
 23  
 import org.springframework.jdbc.core.SqlParameter;
 24  
 import org.springframework.jdbc.object.SqlUpdate;
 25  
 import org.springframework.jdbc.object.MappingSqlQuery;
 26  
 
 27  
 import com.buckosoft.PicMan.domain.System;
 28  
 import com.buckosoft.PicMan.db.SystemDao;
 29  
 
 30  
 /** Implement the Dao Interface for the {@link com.buckosoft.PicMan.domain.System}.
 31  
  * This table is key / value pairs so that {@link com.buckosoft.PicMan.domain.System} can be extended without
 32  
  * altering the table. 
 33  
  * @author Dick Balaska
 34  
  * @since 2005/07/25
 35  
  * @see <a href="http://cvs.buckosoft.com/Projects/java/PicMan/PicMan/src/com/buckosoft/PicMan/db/SystemDaoJdbc.java">SystemDaoJdbc.java</a>
 36  
  */
 37  0
 public class SystemDaoJdbc implements SystemDao {
 38  
 
 39  
         private final static boolean DEBUG = false;
 40  0
     protected final Log logger = LogFactory.getLog(getClass());
 41  
 
 42  
     private DataSource ds;
 43  
     private        System        systemCache;
 44  
     
 45  
     /** Set the database reference
 46  
      * @param ds The DataSource as generated by the Spring config/setup.
 47  
      */
 48  
     public void setDataSource(DataSource ds) {
 49  
             if (DEBUG)
 50  
                     logger.info("Set Datasource.");
 51  0
         this.ds = ds;
 52  0
     }
 53  
 
 54  
     /** Return a copy of the PicMan System attributes.
 55  
      * @return The System
 56  
      */
 57  
         public System getSystem() throws DataAccessException {
 58  
                 if (DEBUG)
 59  
                         logger.info("getSystem()");
 60  0
                 if (systemCache != null)
 61  0
                         return(systemCache);
 62  0
                 SystemQuery sq = new SystemQuery(ds);
 63  
                 @SuppressWarnings("unchecked")
 64  0
                 List<KeyValue> l = sq.execute();
 65  0
                 systemCache = new System();
 66  0
                 Iterator<KeyValue> it = l.iterator();
 67  0
                 while (it.hasNext()) {
 68  0
                 KeyValue k = (KeyValue)it.next();
 69  
                     //logger.info("getSystem() k=" + k.key + " v=" + k.value );
 70  
 //                if (k.key.equals("root"))
 71  
  //                       systemCache.addRoot(k.value);
 72  0
                 if (k.key.equals("picExtension"))
 73  0
                         systemCache.addPicExtension(k.value);
 74  0
                 else if (k.key.equals("thumbHeight"))
 75  0
                         systemCache.setThumbHeight(new Integer(k.value).intValue());
 76  0
                 else if (k.key.equals("mosaicDepth"))
 77  0
                         systemCache.setMosaicDepth(new Integer(k.value).intValue());
 78  0
                 else if (k.key.equals("mosaicThumbHeight"))
 79  0
                         systemCache.setMosaicThumbHeight(new Integer(k.value).intValue());
 80  0
                 else if (k.key.equals("mosaicPicsPerPage"))
 81  0
                         systemCache.setMosaicPicsPerPage(new Integer(k.value).intValue());
 82  0
                 else if (k.key.equals("useThumbCache"))
 83  0
                         systemCache.setUseThumbCache(new Boolean(k.value).booleanValue());
 84  0
                 else if (k.key.equals("thumbCacheDirectory"))
 85  0
                         systemCache.setThumbCacheDirectory(k.value);
 86  0
                 else if (k.key.equals("thumbCacheSize"))
 87  0
                         systemCache.setThumbCacheSize(new Integer(k.value).intValue());
 88  0
                 else if (k.key.equals("importDirectory"))
 89  0
                         systemCache.setImportDirectory(k.value);
 90  0
                 else if (k.key.equals("importsToDo"))
 91  0
                         systemCache.setImportsToDo(new Integer(k.value).intValue());
 92  0
                 else if (k.key.equals("virginsToDo"))
 93  0
                         systemCache.setVirginsToDo(new Integer(k.value).intValue());
 94  0
                 else if (k.key.equals("engineOn"))
 95  0
                         systemCache.setEngineOn(new Boolean(k.value).booleanValue());
 96  0
                 else if (k.key.equals("engineRunOnce"))
 97  0
                         systemCache.setEngineRunOnce(new Boolean(k.value).booleanValue());
 98  0
                 else if (k.key.equals("skipXThumbs"))
 99  0
                         systemCache.setSkipXThumbs(new Boolean(k.value).booleanValue());
 100  0
                 else if (k.key.equals("showRateOnPics"))
 101  0
                         systemCache.setShowRateOnPics(new Boolean(k.value).booleanValue());
 102  0
                 else if (k.key.equals("jobLogSummaryOnly"))
 103  0
                         systemCache.setJobLogSummaryOnly(new Boolean(k.value).booleanValue());
 104  0
                 else if (k.key.equals("engineCronExpression"))
 105  0
                         systemCache.setEngineCronExpression(k.value);
 106  0
                 else if (k.key.equals("mosaicThumbCacheDir"))
 107  0
                         systemCache.setMosaicThumbCacheDir(k.value);
 108  0
                 else if (k.key.equals("mosThumbCacheHeight"))
 109  0
                         systemCache.setMosaicThumbCacheHeight(new Integer(k.value).intValue());
 110  0
                 else if (k.key.equals("syncEnable"))
 111  0
                         systemCache.setSyncEnable(new Boolean(k.value).booleanValue());
 112  0
                 else if (k.key.equals("syncUrl"))
 113  0
                         systemCache.setSyncUrl(k.value);
 114  0
         }
 115  0
         return (systemCache);
 116  
         }
 117  
 
 118  
         /** Save the System to the database
 119  
          * @param system The System to write
 120  
          * @throws DataAccessException whatever.
 121  
          */
 122  
         public void setSystem(System system) throws DataAccessException {
 123  0
                 SystemUpdate au = new SystemUpdate(ds);
 124  0
                 au.update(system);
 125  0
                 systemCache = null;
 126  0
         }
 127  
 
 128  0
         private class KeyValue {
 129  
                 public String        key;
 130  
                 public String        value;
 131  
         }
 132  
         
 133  
         private class SystemQuery extends MappingSqlQuery {
 134  
 
 135  0
                 SystemQuery(DataSource ds) {
 136  0
             super(ds, "SELECT * from system");
 137  0
             compile();
 138  0
         }
 139  
  
 140  
         protected Object mapRow(ResultSet rs, int rowNum) throws SQLException {
 141  0
                 KeyValue k = new KeyValue();
 142  0
                 k.key = rs.getString("key");
 143  0
                 k.value = rs.getString("value");
 144  0
                 return(k);
 145  
         }
 146  
         }
 147  
 
 148  
         /**
 149  
          * <code>System</code> Update Object.
 150  
          */
 151  
         private class SystemUpdate extends SqlUpdate {
 152  
                 private        DataSource        ds;
 153  
                 
 154  
                 /**
 155  
                  * Create a new instance of OwnerUpdate.
 156  
                  * @param ds the DataSource to use for the update
 157  
                  */
 158  0
                 protected SystemUpdate(DataSource ds) {
 159  0
                         this.ds = ds;
 160  0
                 }
 161  
 
 162  
                 /**
 163  
                  * Method to update the <code>System</code>'s data.
 164  
                  * @param System
 165  
                  * @return 0
 166  
                  */
 167  
                 protected int update(System sys) {
 168  
                         // Empty the existing table
 169  0
                         SqlUpdate sf = new SqlUpdate(ds, "TRUNCATE TABLE system");
 170  0
                         sf.compile();
 171  0
                         int ret = sf.update();
 172  
                         if (DEBUG)
 173  
                                 logger.info("sf.empty() returned" + ret);
 174  
                         
 175  
                         // Save each of our attributes
 176  0
                         SystemInsert si = new SystemInsert(ds);
 177  
                         Iterator<String> it;
 178  0
                         KeyValue kv = new KeyValue();
 179  0
                         it = sys.getPicExtensions().iterator();
 180  0
                         kv.key = "picExtension";
 181  0
                         while (it.hasNext())
 182  
                         {
 183  0
                                 kv.value = (String)it.next();
 184  0
                                 si.insert(kv);
 185  
                         }
 186  
                         
 187  0
                         kv.key = "thumbHeight";
 188  0
                         kv.value = String.valueOf(sys.getThumbHeight());
 189  0
                         si.insert(kv);
 190  
 
 191  0
                         kv.key = "mosaicDepth";
 192  0
                         kv.value = String.valueOf(sys.getMosaicDepth());
 193  0
                         si.insert(kv);
 194  
 
 195  0
                         kv.key = "mosaicThumbHeight";
 196  0
                         kv.value = String.valueOf(sys.getMosaicThumbHeight());
 197  0
                         si.insert(kv);
 198  
 
 199  0
                         kv.key = "mosaicPicsPerPage";
 200  0
                         kv.value = String.valueOf(sys.getMosaicPicsPerPage());
 201  0
                         si.insert(kv);
 202  
 
 203  0
                         kv.key = "mosThumbCacheHeight";
 204  0
                         kv.value = String.valueOf(sys.getMosaicThumbCacheHeight());
 205  0
                         si.insert(kv);
 206  
 
 207  0
                         kv.key = "useThumbCache";
 208  0
                         kv.value = String.valueOf(sys.isUseThumbCache());
 209  0
                         si.insert(kv);
 210  
 
 211  0
                         kv.key = "thumbCacheDirectory";
 212  0
                         kv.value = sys.getThumbCacheDirectory();
 213  0
                         si.insert(kv);
 214  
                         
 215  0
                         kv.key = "mosaicThumbCacheDir";
 216  0
                         kv.value = sys.getMosaicThumbCacheDir();
 217  0
                         si.insert(kv);
 218  
                         
 219  0
                         kv.key = "thumbCacheSize";
 220  0
                         kv.value = String.valueOf(sys.getThumbCacheSize());
 221  0
                         si.insert(kv);
 222  
                         
 223  0
                         kv.key = "importDirectory";
 224  0
                         kv.value = sys.getImportDirectory();
 225  0
                         si.insert(kv);
 226  
                         
 227  0
                         kv.key = "importsToDo";
 228  0
                         kv.value = String.valueOf(sys.getImportsToDo());
 229  0
                         si.insert(kv);
 230  
 
 231  0
                         kv.key = "virginsToDo";
 232  0
                         kv.value = String.valueOf(sys.getVirginsToDo());
 233  0
                         si.insert(kv);
 234  
 
 235  0
                         kv.key = "engineOn";
 236  0
                         kv.value = String.valueOf(sys.isEngineOn());
 237  0
                         si.insert(kv);
 238  
 
 239  0
                         kv.key = "engineRunOnce";
 240  0
                         kv.value = String.valueOf(sys.isEngineRunOnce());
 241  0
                         si.insert(kv);
 242  
 
 243  0
                         kv.key = "skipXThumbs";
 244  0
                         kv.value = String.valueOf(sys.isSkipXThumbs());
 245  0
                         si.insert(kv);
 246  
 
 247  0
                         kv.key = "showRateOnPics";
 248  0
                         kv.value = String.valueOf(sys.isShowRateOnPics());
 249  0
                         si.insert(kv);
 250  
                                                 
 251  0
                         kv.key = "jobLogSummaryOnly";
 252  0
                         kv.value = String.valueOf(sys.isJobLogSummaryOnly());
 253  0
                         si.insert(kv);
 254  
 
 255  0
                         kv.key = "engineCronExpression";
 256  0
                         kv.value = sys.getEngineCronExpression();
 257  0
                         si.insert(kv);
 258  
                         
 259  0
                         kv.key = "syncEnable";
 260  0
                         kv.value = String.valueOf(sys.isSyncEnable());
 261  0
                         si.insert(kv);
 262  
 
 263  0
                         kv.key = "syncUrl";
 264  0
                         kv.value = sys.getSyncUrl();
 265  0
                         si.insert(kv);
 266  
                         
 267  0
                         return(0);
 268  
                 }
 269  
         }
 270  
 
 271  
 
 272  
         /**
 273  
          * <code>System</code> Insert Object.
 274  
          */
 275  
         private class SystemInsert extends SqlUpdate {
 276  
 
 277  
                 /**
 278  
                  * Create a new instance of SystemInsert.
 279  
                  * @param ds the DataSource to use for the insert
 280  
                  */
 281  0
                 protected SystemInsert(DataSource ds) {
 282  0
                         super(ds, "INSERT INTO system VALUES(?,?)");
 283  0
                         declareParameter(new SqlParameter(Types.VARCHAR));
 284  0
                         declareParameter(new SqlParameter(Types.VARCHAR));
 285  0
                         compile();
 286  0
                 }
 287  
 
 288  
                 protected void insert(KeyValue kv) {
 289  0
                         Object[] objs = new Object[] {
 290  
                                 kv.key, kv.value};
 291  0
                         super.update(objs);
 292  0
                 }
 293  
         }
 294  
 }