Coverage Report - com.buckosoft.PicMan.db.MosaicsDaoJdbc
 
Classes in this File Line Coverage Branch Coverage Complexity
MosaicsDaoJdbc
0%
0/61
0%
0/22
1.875
MosaicsDaoJdbc$1
N/A
N/A
1.875
MosaicsDaoJdbc$KeyValue
0%
0/1
N/A
1.875
MosaicsDaoJdbc$MosaicConfigsDelete
0%
0/7
N/A
1.875
MosaicsDaoJdbc$MosaicConfigsInsert
0%
0/10
N/A
1.875
MosaicsDaoJdbc$MosaicConfigsQuery
0%
0/9
N/A
1.875
MosaicsDaoJdbc$MosaicsCount
0%
0/4
N/A
1.875
MosaicsDaoJdbc$MosaicsInsert
0%
0/20
0%
0/2
1.875
MosaicsDaoJdbc$MosaicsQuery
0%
0/29
N/A
1.875
MosaicsDaoJdbc$MosaicsUpdate
0%
0/19
0%
0/2
1.875
 
 1  
 /******************************************************************************
 2  
  * MosaicsDaoJdbc.java - Implement the Dao interface for the Mosaics
 3  
  * 
 4  
  * PicMan - The BuckoSoft Picture Manager in Java
 5  
  * Copyright(c) 2006 - Dick Balaska
 6  
  * 
 7  
  */ 
 8  
 package com.buckosoft.PicMan.db;
 9  
 
 10  
 import java.sql.ResultSet;
 11  
 import java.sql.SQLException;
 12  
 import java.sql.Types;
 13  
 import java.util.List;
 14  
 
 15  
 import javax.sql.DataSource;
 16  
 
 17  
 import org.apache.commons.logging.Log;
 18  
 import org.apache.commons.logging.LogFactory;
 19  
 import org.springframework.dao.DataAccessException;
 20  
 import org.springframework.jdbc.core.SqlParameter;
 21  
 import org.springframework.jdbc.object.MappingSqlQuery;
 22  
 import org.springframework.jdbc.object.SqlFunction;
 23  
 import org.springframework.jdbc.object.SqlUpdate;
 24  
 
 25  
 import com.buckosoft.PicMan.domain.Mosaic;
 26  
 
 27  
 /** Implement the Dao Interface for the {@link com.buckosoft.PicMan.domain.Mosaic}s.
 28  
  * @author Dick Balaska
 29  
  * @since 2008/02/14
 30  
  * @see <a href="http://cvs.buckosoft.com/Projects/java/PicMan/PicMan/src/com/buckosoft/PicMan/db/MosaicsDaoJdbc.java">MosaicsDaoJdbc.java</a>
 31  
  */
 32  0
 public class MosaicsDaoJdbc implements MosaicsDao {
 33  
         private static final boolean DEBUG = true;
 34  0
         protected final Log logger = LogFactory.getLog(getClass());
 35  
 
 36  
         private DataSource ds;
 37  
         
 38  
         /** Set the reference to the JDBC datasource.
 39  
          * @param ds The datasource as configured by Spring.
 40  
          * @see <a href="http://cvs.buckosoft.com/Projects/java/PicMan/PicMan/WebContent/WEB-INF/picManDatabase/picManDatabase.xml">picManDatabase.xml</a>
 41  
          */ 
 42  
         public void setDataSource(DataSource ds) {
 43  0
                 this.ds = ds;
 44  0
         }
 45  
 
 46  
         /* (non-Javadoc)
 47  
          * @see com.buckosoft.PicMan.db.MosaicsDao#getMosaic(int)
 48  
          */
 49  
         public Mosaic getMosaic(int mid) {
 50  
                 if (DEBUG)
 51  0
                         logger.info("getMosaic: " + mid);
 52  0
                 if (sql_getMosaicQUERY == null)
 53  0
                         sql_getMosaicQUERY = new MosaicsQuery(ds, mid);
 54  0
         Mosaic m = (Mosaic)sql_getMosaicQUERY.findObject(mid);
 55  0
                getMosaicConfig(m);
 56  0
         return(m);
 57  
         }
 58  0
         private        MosaicsQuery        sql_getMosaicQUERY = null;
 59  
 
 60  
         /* (non-Javadoc)
 61  
          * @see com.buckosoft.PicMan.db.MosaicsDao#getMosaic(java.lang.String, java.lang.String, int, int)
 62  
          */
 63  
         public Mosaic getMosaic(String masterPic, String engine, int sid, int tileHeight) {
 64  0
                 if (sql_getMosaicUniQUERY == null)
 65  0
                         sql_getMosaicUniQUERY = new MosaicsQuery(ds, masterPic, engine, sid, tileHeight);
 66  0
                 Mosaic m = (Mosaic)sql_getMosaicUniQUERY.findObject(new Object[] {masterPic, engine, new Integer(sid), new Integer(tileHeight)});
 67  0
                getMosaicConfig(m);
 68  0
         return(m);
 69  
         }
 70  0
         private        MosaicsQuery        sql_getMosaicUniQUERY = null;
 71  
 
 72  
         /* (non-Javadoc)
 73  
          * @see com.buckosoft.PicMan.db.MosaicsDao#getMosaics()
 74  
          */
 75  
         @SuppressWarnings("unchecked")
 76  
         public List<Mosaic> getMosaics() {
 77  
                 if (DEBUG)
 78  0
                         logger.info("GetMosaicsList");
 79  0
                 if (sql_getMosaicsListQUERY == null)
 80  0
                         sql_getMosaicsListQUERY = new MosaicsQuery(ds);
 81  0
                 List<Mosaic> mosaicList = (List<Mosaic>)sql_getMosaicsListQUERY.execute();
 82  0
                 for (Mosaic m : mosaicList)
 83  0
                         this.getMosaicConfig(m);
 84  0
                 return(mosaicList);
 85  
         }
 86  0
         private        MosaicsQuery        sql_getMosaicsListQUERY = null;
 87  
 
 88  
         /* (non-Javadoc)
 89  
          * @see com.buckosoft.PicMan.db.MosaicsDao#deleteMosaic(int)
 90  
          */
 91  
         public void deleteMosaic(int mid) {
 92  
                 if (DEBUG)
 93  0
                         logger.info("deleteMosaic");
 94  
                 // TODO deleteMosaic not implemented
 95  0
                 throw new RuntimeException("Not implemented");
 96  
         }
 97  
 
 98  
         /* (non-Javadoc)
 99  
          * @see com.buckosoft.PicMan.db.MosaicsDao#storeMosaic(com.buckosoft.PicMan.domain.Mosaic)
 100  
          */
 101  
         public void storeMosaic(Mosaic mosaic) {
 102  
                 if (DEBUG)
 103  0
                         logger.info("storeMosaic " + mosaic.getMid());
 104  0
                 if (mosaic.getMid() <= 0) {
 105  0
                         MosaicsInsert mi = new MosaicsInsert(ds);
 106  0
                         mi.insert(mosaic);
 107  0
                         int c = getMosaicCount();
 108  0
                         mosaic.setMid(c);
 109  0
                 } else {
 110  0
                         MosaicsUpdate mu = new MosaicsUpdate(ds);
 111  0
                         mu.update(mosaic);
 112  
                 }
 113  0
                 deleteMosaicConfigs(mosaic.getMid());
 114  0
                 if (sql_storeMosaicConfigsINSERT == null)
 115  0
                         sql_storeMosaicConfigsINSERT = new MosaicConfigsInsert(ds);
 116  0
                 for (String key : mosaic.getEngineConfig().keySet()) {
 117  0
                         sql_storeMosaicConfigsINSERT.insert(mosaic.getMid(), key, mosaic.getEngineConfig().get(key));
 118  0
                 }
 119  0
         }
 120  0
         private        MosaicConfigsInsert        sql_storeMosaicConfigsINSERT = null;
 121  
 
 122  
         /** Get the number of Virgins in the database
 123  
          * @return the count
 124  
          */
 125  
         public int        getMosaicCount() {
 126  
                 if (DEBUG)
 127  0
                         logger.info("getVirginCount:");
 128  0
                 if (sql_getMosaicCountQUERYr == null)
 129  0
                         sql_getMosaicCountQUERYr = new MosaicsCount(ds);
 130  0
                 return(sql_getMosaicCountQUERYr.run());
 131  
         }
 132  
         private MosaicsCount        sql_getMosaicCountQUERYr;
 133  
         
 134  
         @SuppressWarnings("unchecked")
 135  
         private        void getMosaicConfig(Mosaic mosaic) {
 136  0
                 if (mosaic == null)
 137  0
                         return;
 138  0
                 if (sql_getMosaicConfigQUERY == null)
 139  0
                         sql_getMosaicConfigQUERY = new MosaicConfigsQuery(ds, 0);
 140  0
                 List<KeyValue>kvlist = (List<KeyValue>)sql_getMosaicConfigQUERY.execute(mosaic.getMid());
 141  0
                 for (KeyValue kv : kvlist) {
 142  0
                         mosaic.getEngineConfig().put(kv.key, kv.value);
 143  0
                 }
 144  0
         }
 145  
 
 146  0
         private        MosaicConfigsQuery        sql_getMosaicConfigQUERY = null;
 147  
 
 148  
         private void deleteMosaicConfigs(int sid) throws DataAccessException {
 149  0
                 MosaicConfigsDelete msd = new MosaicConfigsDelete(ds);
 150  0
                 msd.delete(sid);
 151  0
         }
 152  
         
 153  
 
 154  
         /**
 155  
          * Object to Query for Mosaics
 156  
          */
 157  
         private class MosaicsQuery extends MappingSqlQuery {
 158  
                 
 159  0
                 MosaicsQuery(DataSource ds) {
 160  0
                         super(ds, "SELECT * from mosaics");
 161  0
                         compile();
 162  0
                 }
 163  
                 
 164  0
                 MosaicsQuery(DataSource ds, int mid) {
 165  0
                         super(ds, "SELECT * from mosaics where mid = ?");
 166  0
                         declareParameter(new SqlParameter(Types.INTEGER));
 167  0
                         compile();
 168  0
                 }
 169  
 
 170  0
                 MosaicsQuery(DataSource ds, String masterPic, String engine, int sid, int tileHeight) {
 171  0
                         super(ds, "SELECT * from mosaics where masterPic = ? AND engine = ? AND sid = ? AND tileHeight = ? LIMIT 1");
 172  0
                         declareParameter(new SqlParameter(Types.VARCHAR));
 173  0
                         declareParameter(new SqlParameter(Types.VARCHAR));
 174  0
                         declareParameter(new SqlParameter(Types.INTEGER));
 175  0
                         declareParameter(new SqlParameter(Types.INTEGER));
 176  0
                         compile();
 177  0
                 }
 178  
 
 179  
                 protected Object mapRow(ResultSet rs, int rowNum) throws SQLException {
 180  0
                         Mosaic m = new Mosaic();
 181  0
                         m.setMid(rs.getInt("mid"));
 182  0
                         m.setBatch(rs.getBoolean("isBatch"));
 183  0
                         m.setName(rs.getString("name"));
 184  0
                         m.setEngine(rs.getString("engine"));
 185  0
                         m.setMasterPic(rs.getString("masterPic"));
 186  0
                         m.setOutPic(rs.getString("outPic"));
 187  0
                         m.setSid(rs.getInt("sid"));
 188  0
                         m.setTileHeight(rs.getInt("tileHeight"));
 189  
                         try {
 190  0
                                 m.setStartTime(rs.getDate("startTime"));
 191  0
                         } catch (Exception e) {logger.info(e);}
 192  0
                         return(m);
 193  
                 }
 194  
         }
 195  
         /**
 196  
          * <code>Virgin</code> Count Object.
 197  
          */
 198  
         class MosaicsCount extends SqlFunction {
 199  
                 
 200  0
                 MosaicsCount(DataSource ds) {
 201  0
                         super(ds, "SELECT COUNT(*) from mosaics");
 202  0
                         compile();
 203  0
                 }                
 204  
         }
 205  
 
 206  
         /**
 207  
          * <code>Mosaic</code> Insert Object.
 208  
          */
 209  
         private class MosaicsInsert extends SqlUpdate {
 210  
                 
 211  
                 /**
 212  
                  * Create a new instance of MosaicsInsert.
 213  
                  * @param ds the DataSource to use for the insert
 214  
                  */
 215  0
                 protected MosaicsInsert(DataSource ds) {
 216  0
                         super(ds, "INSERT INTO mosaics VALUES(?,?,?,?,?,?,?,?,?)");
 217  0
                         declareParameter(new SqlParameter(Types.INTEGER));
 218  0
                         declareParameter(new SqlParameter(Types.TINYINT));
 219  0
                         declareParameter(new SqlParameter(Types.VARCHAR));
 220  0
                         declareParameter(new SqlParameter(Types.VARCHAR));
 221  0
                         declareParameter(new SqlParameter(Types.VARCHAR));
 222  0
                         declareParameter(new SqlParameter(Types.VARCHAR));
 223  0
                         declareParameter(new SqlParameter(Types.INTEGER));
 224  0
                         declareParameter(new SqlParameter(Types.INTEGER));
 225  0
                         declareParameter(new SqlParameter(Types.TIMESTAMP));
 226  0
                         compile();
 227  0
                 }
 228  
 
 229  
                 protected void insert(Mosaic m) {
 230  0
                         Object[] objs = new Object[] {
 231  
                                 null,
 232  0
                                 new Integer(m.isBatch() ? 1 : 0), m.getName(),
 233  0
                                 m.getEngine(), m.getMasterPic(), m.getOutPic(),
 234  0
                                 new Integer(m.getSid()), new Integer(m.getTileHeight()),
 235  0
                                 m.getStartTime()
 236  
                         };
 237  0
                         super.update(objs);
 238  0
                 }
 239  
         }
 240  
         ///////////////////////////////////////////////////////////////////////////////////////////////
 241  
         /**
 242  
          * <code>Mosaic</code> Update Object.
 243  
          */
 244  
         private class MosaicsUpdate extends SqlUpdate {
 245  
                 //private        DataSource        ds;
 246  
                 
 247  
                 /**
 248  
                  * Create a new instance of MosaicsUpdate.
 249  
                  * @param ds the DataSource to use for the update
 250  
                  */
 251  0
                 protected MosaicsUpdate(DataSource ds) {
 252  0
                         super(ds, "UPDATE mosaics SET name=?, isBatch=?, engine=?, masterPic=?, outPic=?, sid=?, tileHeight=?, startTime=? WHERE mid = ? LIMIT 1");
 253  0
                         declareParameter(new SqlParameter(Types.VARCHAR));
 254  0
                         declareParameter(new SqlParameter(Types.TINYINT));
 255  0
                         declareParameter(new SqlParameter(Types.VARCHAR));
 256  0
                         declareParameter(new SqlParameter(Types.VARCHAR));
 257  0
                         declareParameter(new SqlParameter(Types.VARCHAR));
 258  0
                         declareParameter(new SqlParameter(Types.INTEGER));
 259  0
                         declareParameter(new SqlParameter(Types.INTEGER));
 260  0
                         declareParameter(new SqlParameter(Types.TIMESTAMP));
 261  0
                         declareParameter(new SqlParameter(Types.VARCHAR));
 262  0
                         compile();
 263  0
                 }
 264  
                 /**
 265  
                  * Method to update the <code>Set</code>'s data.
 266  
                  * @param List sets The sets that should be stored
 267  
                  * @return 0
 268  
                  */
 269  
 
 270  
                 protected int update(Mosaic m) {
 271  0
                         return this.update(new Object[] {
 272  0
                                         m.getName(),  new Integer(m.isBatch() ? 1 : 0),
 273  0
                                         m.getEngine(), m.getMasterPic(), m.getOutPic(),
 274  0
                                         new Integer(m.getSid()), new Integer(m.getTileHeight()),
 275  0
                                         m.getStartTime(),
 276  0
                                         new Integer(m.getMid())
 277  
                         });
 278  
                 }
 279  
         }
 280  0
         private class KeyValue {
 281  
                 public String        key;
 282  
                 public String        value;
 283  
         }
 284  
         
 285  
         private class MosaicConfigsQuery extends MappingSqlQuery {
 286  
 
 287  0
                 MosaicConfigsQuery(DataSource ds, int mid) {
 288  0
             super(ds, "SELECT * from mosaicConfigs where mid=?");
 289  0
                         declareParameter(new SqlParameter(Types.INTEGER));
 290  0
             compile();
 291  0
         }
 292  
  
 293  
         protected Object mapRow(ResultSet rs, int rowNum) throws SQLException {
 294  0
                 KeyValue k = new KeyValue();
 295  0
                 k.key = rs.getString("key");
 296  0
                 k.value = rs.getString("value");
 297  0
                 return(k);
 298  
         }
 299  
         }
 300  
 
 301  
         /**
 302  
          * <code>MetaSet</code> Delete Object.
 303  
          */
 304  
         protected class MosaicConfigsDelete extends SqlUpdate {
 305  
                 
 306  
                 /**
 307  
                  * Create a new instance of MetaSetRemove.
 308  
                  * @param ds the DataSource to use for the delete
 309  
                  */
 310  0
                 protected MosaicConfigsDelete(DataSource ds) {
 311  0
                         super(ds, "DELETE FROM mosaicConfigs WHERE mid = (?)");
 312  0
                         declareParameter(new SqlParameter(Types.INTEGER));
 313  0
                         compile();
 314  0
                 }
 315  
                 
 316  
                 protected void delete(int sid) {
 317  0
                         super.update(sid);
 318  0
                 }
 319  
         }
 320  
         /**
 321  
          * <code>MosaicConfigs</code> Insert Object.
 322  
          */
 323  
         protected class MosaicConfigsInsert extends SqlUpdate {
 324  
                 
 325  
                 /**
 326  
                  * Create a new instance of MetaSetInsert.
 327  
                  * @param ds the DataSource to use for the insert
 328  
                  */
 329  0
                 protected MosaicConfigsInsert(DataSource ds) {
 330  0
                         super(ds, "INSERT INTO mosaicConfigs VALUES(?,?,?)");
 331  0
                         declareParameter(new SqlParameter(Types.INTEGER));
 332  0
                         declareParameter(new SqlParameter(Types.VARCHAR));
 333  0
                         declareParameter(new SqlParameter(Types.VARCHAR));
 334  0
                         compile();
 335  0
                 }
 336  
 
 337  
                 protected void insert(int sid, String key, String value) {
 338  0
                         Object[] objs = new Object[] {
 339  
                                         new Integer(sid),
 340  
                                         key,
 341  
                                         value,
 342  
                         };
 343  0
                         super.update(objs);
 344  
                         // retrieveIdentity(owner);
 345  0
                 }
 346  
         }
 347  
 }