Coverage Report - com.buckosoft.PicMan.db.SetsDaoJdbc
 
Classes in this File Line Coverage Branch Coverage Complexity
SetsDaoJdbc
0%
0/84
0%
0/30
2.292
SetsDaoJdbc$SetDelete
0%
0/9
N/A
2.292
SetsDaoJdbc$SetInsert
0%
0/22
0%
0/8
2.292
SetsDaoJdbc$SetsQuery
0%
0/17
N/A
2.292
SetsDaoJdbc$SetsUpdate
0%
0/33
0%
0/10
2.292
 
 1  
 /******************************************************************************
 2  
  * SetsDaoJdbc.java - Implement the Dao interface for the Sets
 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.sql.ResultSet;
 11  
 import java.sql.SQLException;
 12  
 import java.sql.Types;
 13  
 import java.util.Date;
 14  
 import java.util.Iterator;
 15  
 import java.util.List;
 16  
 
 17  
 import javax.sql.DataSource;
 18  
 
 19  
 import org.apache.commons.logging.Log;
 20  
 import org.apache.commons.logging.LogFactory;
 21  
 import org.springframework.dao.DataAccessException;
 22  
 import org.springframework.jdbc.core.SqlParameter;
 23  
 import org.springframework.jdbc.object.MappingSqlQuery;
 24  
 import org.springframework.jdbc.object.SqlUpdate;
 25  
 
 26  
 import com.buckosoft.PicMan.domain.Set;
 27  
 
 28  
 /** Implement the Dao Interface for the {@link com.buckosoft.PicMan.domain.Set}s.
 29  
  * @author Dick Balaska
 30  
  * @since 2005/07/30
 31  
  * @version $Revision: 1.1 $ <br> $Date: 2013/12/26 01:25:41 $
 32  
  * @see <a href="http://cvs.buckosoft.com/Projects/java/PicMan/PicMan/src/com/buckosoft/PicMan/db/SetsDaoJdbc.java">SetsDaoJdbc.java</a>
 33  
  */
 34  0
 public class SetsDaoJdbc implements SetsDao {
 35  
 
 36  
         /** Logger for this class and subclasses */
 37  
         private static final boolean DEBUG = false;
 38  0
         protected final Log logger = LogFactory.getLog(getClass());
 39  
 
 40  
         private DataSource ds;
 41  
 
 42  0
         private        List<Set>        setsCache = null;
 43  
         
 44  
         /** Set the reference to the JDBC datasource.
 45  
          * @param ds The datasource as configured by Spring.
 46  
          * @see <a href="http://cvs.buckosoft.com/Projects/java/PicMan/PicMan/WebContent/WEB-INF/picManDatabase/picManDatabase.xml">picManDatabase.xml</a>
 47  
          */ 
 48  
         public void setDataSource(DataSource ds) {
 49  
                 if (DEBUG)
 50  
                         logger.info("Set Datasource.");
 51  0
         this.ds = ds;
 52  0
     }
 53  
 
 54  
         /* (non-Javadoc)
 55  
          * @see com.buckosoft.PicMan.db.SetsDao#getSets()
 56  
          */
 57  
         public List<Set> getSets() throws DataAccessException {
 58  0
                 if (setsCache != null)
 59  0
                         return(setsCache);
 60  
                 if (DEBUG)
 61  
                         logger.info("getSets()");
 62  0
         SetsQuery sq = new SetsQuery(ds);
 63  
             @SuppressWarnings("unchecked")
 64  0
         List<Set> l = sq.execute();
 65  0
                 l.add(0, new Set(Set.FUNC_SID_DATE, Set.FUNC_NAME_DATE));
 66  0
                 l.add(0, new Set(Set.FUNC_SID_ROOT, Set.FUNC_NAME_ROOT));
 67  0
             setSetsCache(l);
 68  0
         return(l);
 69  
         }
 70  
 
 71  
         private synchronized void setSetsCache(List<Set> list) {
 72  0
                 this.setsCache = list;
 73  0
         }
 74  
 
 75  
         /* (non-Javadoc)
 76  
          * @see com.buckosoft.PicMan.db.SetsDao#getSetsClone()
 77  
          */
 78  
         public List<Set> getSetsClone() {
 79  0
         SetsQuery sq = new SetsQuery(ds);
 80  
             @SuppressWarnings("unchecked")
 81  0
         List<Set> l = sq.execute();
 82  0
                 l.add(0, new Set(Set.FUNC_SID_DATE, Set.FUNC_NAME_DATE));
 83  0
                 l.add(0, new Set(Set.FUNC_SID_ROOT, Set.FUNC_NAME_ROOT));
 84  0
         return(l);
 85  
         }
 86  
 
 87  
         /* (non-Javadoc)
 88  
          * @see com.buckosoft.PicMan.db.SetsDao#getInactiveSets()
 89  
          */
 90  
         public List<Set> getInactiveSets() throws DataAccessException {
 91  0
         SetsQuery sq = new SetsQuery(ds);
 92  
             @SuppressWarnings("unchecked")
 93  0
         List<Set> inactiveSets = sq.execute();
 94  0
                 Iterator<Set> siter = inactiveSets.iterator();
 95  0
                 while (siter.hasNext()) {
 96  0
                         Set set = siter.next();
 97  0
                         if (set.isActive())
 98  0
                                 siter.remove();
 99  0
                 }
 100  0
                 return(inactiveSets);
 101  
         }
 102  
 
 103  
         /* (non-Javadoc)
 104  
          * @see com.buckosoft.PicMan.db.SetsDao#getActiveSets()
 105  
          */
 106  
         public List<Set> getActiveSets() throws DataAccessException {
 107  0
         SetsQuery sq = new SetsQuery(ds);
 108  
             @SuppressWarnings("unchecked")
 109  0
         List<Set> activeSets = sq.execute();
 110  0
                 Iterator<Set> siter = activeSets.iterator();
 111  0
                 while (siter.hasNext()) {
 112  0
                         Set set = siter.next();
 113  0
                         if (!set.isActive())
 114  0
                                 siter.remove();
 115  0
                 }
 116  0
                 return(activeSets);
 117  
         }
 118  
 
 119  
         /* (non-Javadoc)
 120  
          * @see com.buckosoft.PicMan.db.SetsDao#getSetCount()
 121  
          */
 122  
         public int        getSetCount() throws DataAccessException {
 123  0
                 if (setsCache == null)
 124  0
                         getSets();
 125  0
                 return(setsCache.size());
 126  
         }
 127  
 
 128  
         /* (non-Javadoc)
 129  
          * @see com.buckosoft.PicMan.db.SetsDao#getSet(int)
 130  
          */
 131  
         public        Set        getSet(int sid) throws DataAccessException {
 132  0
                 if (setsCache == null)
 133  0
                         getSets();
 134  0
                 Iterator<Set> iter = setsCache.iterator();
 135  0
                 while (iter.hasNext()) {
 136  0
                         Set set = iter.next();
 137  0
                         if (set.getSid() == sid)
 138  0
                                 return(set);
 139  0
                 }
 140  0
                 return(null);
 141  
         }
 142  
 
 143  
         /* (non-Javadoc)
 144  
          * @see com.buckosoft.PicMan.db.SetsDao#getSet(java.lang.String)
 145  
          */
 146  
         public        Set        getSet(String setName) throws DataAccessException {
 147  0
                 if (setsCache == null)
 148  0
                         getSets();
 149  0
                 Iterator<Set> iter = setsCache.iterator();
 150  0
                 while (iter.hasNext()) {
 151  0
                         Set set = iter.next();
 152  0
                         if (set.getName().equals(setName))
 153  0
                                 return(set);
 154  0
                 }
 155  0
                 return(null);
 156  
         }
 157  
         /** Update this <code>Set</code> in the database
 158  
          * @param set The Set to store
 159  
          */
 160  
         public void storeSet(Set set) throws DataAccessException {
 161  0
                 if (set.getSid() <= 0)
 162  0
                         return;                        // throw up!
 163  0
                 setSetsCache(null);
 164  0
                 SetsUpdate su = new SetsUpdate(ds, set);
 165  0
                 su.update(set);                        
 166  
 //                SetInsert si = new SetInsert(ds);
 167  
 //                si.insert(set);
 168  0
         }
 169  
 
 170  
         /* (non-Javadoc)
 171  
          * @see com.buckosoft.PicMan.db.SetsDao#setSets(java.util.List)
 172  
          */
 173  
         public void setSets(List<Set> sets) throws DataAccessException {
 174  0
                 setSetsCache(null);
 175  0
                 SetsUpdate su = new SetsUpdate(ds);
 176  0
                 su.update(sets);
 177  0
         }
 178  
 
 179  
         /* (non-Javadoc)
 180  
          * @see com.buckosoft.PicMan.db.SetsDao#addSet(com.buckosoft.PicMan.domain.Set)
 181  
          */
 182  
         public void addSet(Set set) throws DataAccessException {
 183  0
                 set.setSid(getNextAvailableSetNumber());
 184  0
                 setSetsCache(null);
 185  0
                 SetInsert si = new SetInsert(ds);
 186  0
                 si.insert(set);
 187  0
         }
 188  
 
 189  
         /* (non-Javadoc)
 190  
          * @see com.buckosoft.PicMan.db.SetsDao#deleteSet(com.buckosoft.PicMan.domain.Set)
 191  
          */
 192  
         public        void        deleteSet(Set s) throws DataAccessException {
 193  0
                 setSetsCache(null);
 194  0
                 SetDelete sd = new SetDelete(ds);
 195  0
                 sd.delete(s);
 196  0
         }
 197  
 
 198  
         private        int        getNextAvailableSetNumber() {
 199  0
                 int high = 1;
 200  0
                 for (Set set : getSets()) {
 201  0
                         if (set.getSid() > high)
 202  0
                                 high = set.getSid();
 203  0
                 }
 204  0
                 return(high+1);
 205  
         }
 206  
         
 207  
         /**
 208  
          * <code>Set</code> Query object.
 209  
          */
 210  
         class SetsQuery extends MappingSqlQuery {
 211  
 
 212  0
                 SetsQuery(DataSource ds) {
 213  0
             super(ds, "SELECT * from sets ORDER BY name");
 214  0
             compile();
 215  0
         }
 216  
  
 217  
         protected Object mapRow(ResultSet rs, int rowNum) throws SQLException {
 218  0
                 Set s = new Set();
 219  0
                 s.setSid(rs.getInt("sid"));
 220  0
                 s.setName(rs.getString("name"));
 221  0
                 s.setDescription(rs.getString("description"));
 222  0
                 s.setActive(rs.getBoolean("active"));
 223  0
                 s.setMetaSet(rs.getBoolean("metaSet"));
 224  0
                 s.setMicroSet(rs.getBoolean("microSet"));
 225  0
                 s.setNanoSet(rs.getBoolean("nanoSet"));
 226  
                 try {
 227  0
                                 s.getEditDate().setTime(rs.getTimestamp("editDate").getTime());
 228  0
                         } catch (java.sql.SQLException e) {
 229  0
                                 s.setEditDate(new Date(0));
 230  0
                         }
 231  0
                 return(s);
 232  
         }
 233  
         }
 234  
 
 235  
         /**
 236  
          * <code>Set</code> Update Object.
 237  
          */
 238  
         protected class SetsUpdate extends SqlUpdate {
 239  
                 private        DataSource        ds;
 240  
                 
 241  
                 /**
 242  
                  * Create a new instance of OwnerUpdate.
 243  
                  * @param ds the DataSource to use for the update
 244  
                  */
 245  0
                 protected SetsUpdate(DataSource ds) {
 246  0
                         this.ds = ds;
 247  0
                 }
 248  
 
 249  0
                 protected SetsUpdate(DataSource ds, Set set) {
 250  0
                         super(ds, "UPDATE sets SET name=?, description=?, active=?, metaSet=?, microSet=?, nanoSet=?, editDate=? WHERE sid = ? LIMIT 1");
 251  0
                         declareParameter(new SqlParameter(Types.VARCHAR));
 252  0
                         declareParameter(new SqlParameter(Types.VARCHAR));
 253  0
                         declareParameter(new SqlParameter(Types.TINYINT));
 254  0
                         declareParameter(new SqlParameter(Types.TINYINT));
 255  0
                         declareParameter(new SqlParameter(Types.TINYINT));
 256  0
                         declareParameter(new SqlParameter(Types.TINYINT));
 257  0
                         declareParameter(new SqlParameter(Types.TIMESTAMP));
 258  0
                         declareParameter(new SqlParameter(Types.INTEGER));
 259  0
                         compile();
 260  0
                 }
 261  
                 /**
 262  
                  * Method to update the <code>Set</code>'s data.
 263  
                  * @param sets The sets that should be stored
 264  
                  * @return 0
 265  
                  */
 266  
                 protected int update(List<Set> sets) {
 267  
                         // Empty the existing table
 268  0
                         SqlUpdate sf = new SqlUpdate(ds, "TRUNCATE TABLE sets");
 269  0
                         sf.compile();
 270  0
                         int ret = sf.update();
 271  0
                         logger.info("sf.empty() returned" + ret);
 272  
 
 273  
                         // Save each of our attributes
 274  0
                         SetInsert si = new SetInsert(ds);
 275  0
                         Iterator<Set> it = sets.iterator();
 276  0
                         while (it.hasNext())
 277  0
                                 si.insert(it.next());
 278  0
                         return(0);
 279  
                 }
 280  
 
 281  
                 protected int update(Set set) {
 282  0
                         return this.update(new Object[] {
 283  0
                                         set.getName(),
 284  0
                                         set.getDescription(), 
 285  0
                                         new Integer(set.isActive() ? 1 : 0),
 286  0
                                         new Integer(set.isMetaSet() ? 1 : 0),
 287  0
                                         new Integer(set.isMicroSet() ? 1 : 0),
 288  0
                                         new Integer(set.isNanoSet() ? 1 : 0),
 289  0
                                         set.getEditDate(),
 290  0
                                         new Integer(set.getSid()),
 291  
                         });
 292  
                 }
 293  
         }
 294  
 
 295  
 
 296  
         /**
 297  
          * <code>Set</code> Insert Object.
 298  
          */
 299  
         protected class SetInsert extends SqlUpdate {
 300  
 
 301  
                 /**
 302  
                  * Create a new instance of SetInsert.
 303  
                  * @param ds the DataSource to use for the insert
 304  
                  */
 305  0
                 protected SetInsert(DataSource ds) {
 306  0
                         super(ds, "REPLACE INTO sets VALUES(?,?,?,?,?,?,?,?)");
 307  0
                         declareParameter(new SqlParameter(Types.INTEGER));
 308  0
                         declareParameter(new SqlParameter(Types.VARCHAR));
 309  0
                         declareParameter(new SqlParameter(Types.VARCHAR));
 310  0
                         declareParameter(new SqlParameter(Types.TINYINT));
 311  0
                         declareParameter(new SqlParameter(Types.TINYINT));
 312  0
                         declareParameter(new SqlParameter(Types.TINYINT));
 313  0
                         declareParameter(new SqlParameter(Types.TINYINT));
 314  0
                         declareParameter(new SqlParameter(Types.TIMESTAMP));
 315  0
                         compile();
 316  0
                 }
 317  
 
 318  
                 protected void insert(Set s) {
 319  0
                         Object[] objs = new Object[] {
 320  0
                                 new Integer(s.getSid()),
 321  0
                                 s.getName(), s.getDescription(),
 322  0
                                 new Integer(s.isActive() ? 1 : 0),
 323  0
                                 new Integer(s.isMetaSet() ? 1 : 0),
 324  0
                                 new Integer(s.isMicroSet() ? 1 : 0),
 325  0
                                 new Integer(s.isNanoSet() ? 1 : 0),
 326  0
                                 s.getEditDate(),
 327  
                                 };
 328  0
                         super.update(objs);
 329  0
                 }
 330  
         }
 331  
         /**
 332  
          * <code>Set</code> Delete Object.
 333  
          */
 334  
         protected class SetDelete extends SqlUpdate {
 335  
                 
 336  
                 /**
 337  
                  * Create a new instance of SetDelete.
 338  
                  * @param ds the DataSource to use for the delete
 339  
                  */
 340  0
                 protected SetDelete(DataSource ds) {
 341  0
                         super(ds, "DELETE FROM sets WHERE name = (?)");
 342  0
                         declareParameter(new SqlParameter(Types.VARCHAR));
 343  0
                         compile();
 344  0
                 }
 345  
                 
 346  
                 protected void delete(Set set) {
 347  0
                         Object[] objs = new Object[] {
 348  0
                                         set.getName()};
 349  0
                         super.update(objs);
 350  0
                 }
 351  
         }
 352  
 }