Coverage Report - com.buckosoft.PicMan.db.RootsDaoJdbc
 
Classes in this File Line Coverage Branch Coverage Complexity
RootsDaoJdbc
0%
0/68
0%
0/26
2.048
RootsDaoJdbc$RootDelete
0%
0/9
N/A
2.048
RootsDaoJdbc$RootInsert
0%
0/15
0%
0/2
2.048
RootsDaoJdbc$RootUpdate
0%
0/27
0%
0/4
2.048
RootsDaoJdbc$RootsQuery
0%
0/11
N/A
2.048
 
 1  
 /******************************************************************************
 2  
  * RootsDaoJdbc.java - Implement the Dao interface for the Roots
 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.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.SqlFunction;
 25  
 import org.springframework.jdbc.object.SqlUpdate;
 26  
 import org.springframework.jdbc.object.MappingSqlQuery;
 27  
 
 28  
 import com.buckosoft.PicMan.domain.Root;
 29  
 import com.buckosoft.PicMan.db.RootsDao;
 30  
 
 31  
 /** Implement the Dao Interface for the {@link com.buckosoft.PicMan.domain.Root}s.
 32  
  * @author Dick Balaska
 33  
  * @since 2006/09/30
 34  
  * @see <a href="http://cvs.buckosoft.com/Projects/java/PicMan/PicMan/src/com/buckosoft/PicMan/db/RootsDaoJdbc.java">RootsDaoJdbc.java</a>
 35  
  */
 36  0
 public class RootsDaoJdbc implements RootsDao {
 37  
 
 38  
         /** Logger for this class and subclasses */
 39  
         private static final boolean DEBUG = false;
 40  0
         protected final Log logger = LogFactory.getLog(getClass());
 41  
 
 42  
         private DataSource ds;
 43  
 
 44  0
         private        List<Root>        rootsCache = null;
 45  
         
 46  
         /** Set the reference to the JDBC datasource.
 47  
          * @param ds The datasource as configured by Spring.
 48  
          * @see <a href="http://cvs.buckosoft.com/Projects/java/PicMan/PicMan/WebContent/WEB-INF/picManDatabase/picManDatabase.xml">picManDatabase.xml</a>
 49  
          */ 
 50  
         public void setDataSource(DataSource ds) {
 51  
                 if (DEBUG)
 52  
                         logger.info("Set Datasource.");
 53  0
         this.ds = ds;
 54  0
     }
 55  
 
 56  
         /* (non-Javadoc)
 57  
          * @see com.buckosoft.PicMan.db.RootsDao#getRoots()
 58  
          */
 59  
         @SuppressWarnings("unchecked")
 60  
         public List<Root> getRoots() throws DataAccessException {
 61  0
                 if (rootsCache != null)
 62  0
                         return(rootsCache);
 63  
                 if (DEBUG)
 64  
                         logger.info("getSets()");
 65  0
         RootsQuery rq = new RootsQuery(ds);
 66  0
         List<Root> l = rq.execute();
 67  0
         rootsCache = l;
 68  0
         return(l);
 69  
         }
 70  
 
 71  
         /* (non-Javadoc)
 72  
          * @see com.buckosoft.PicMan.db.RootsDao#getInactiveRoots()
 73  
          */
 74  
         public List<Root> getInactiveRoots() throws DataAccessException {
 75  0
                 RootsQuery rq = new RootsQuery(ds);
 76  
                 @SuppressWarnings("unchecked")
 77  0
         List<Root> inactiveRoots = rq.execute();
 78  0
                 Iterator<Root> siter = inactiveRoots.iterator();
 79  0
                 while (siter.hasNext()) {
 80  0
                         Root root = (Root)siter.next();
 81  0
                         if (root.isActive())
 82  0
                                 siter.remove();
 83  0
                 }
 84  0
                 return(inactiveRoots);
 85  
         }
 86  
 
 87  
         /* (non-Javadoc)
 88  
          * @see com.buckosoft.PicMan.db.RootsDao#getActiveRoots()
 89  
          */
 90  
         @SuppressWarnings("unchecked")
 91  
         public List<Root> getActiveRoots() throws DataAccessException {
 92  0
                 RootsQuery rq = new RootsQuery(ds);
 93  0
         List<Root> activeRoots = rq.execute();
 94  0
                 Iterator<Root> siter = activeRoots.iterator();
 95  0
                 while (siter.hasNext()) {
 96  0
                         Root root = siter.next();
 97  0
                         if (!root.isActive())
 98  0
                                 siter.remove();
 99  0
                 }
 100  0
                 return(activeRoots);
 101  
         }
 102  
 
 103  
                 /* (non-Javadoc)
 104  
          * @see com.buckosoft.PicMan.db.RootsDao#getRootCount()
 105  
          */
 106  
         public int        getRootCount() throws DataAccessException {
 107  0
                 if (rootsCache == null)
 108  0
                         getRoots();
 109  0
                 return(rootsCache.size());
 110  
         }
 111  
 
 112  
         /* (non-Javadoc)
 113  
          * @see com.buckosoft.PicMan.db.RootsDao#getRoot(int)
 114  
          */
 115  
         public        Root        getRoot(int rid) throws DataAccessException {
 116  0
                 if (rootsCache == null)
 117  0
                         getRoots();
 118  0
                 Iterator<Root> iter = rootsCache.iterator();
 119  0
                 while (iter.hasNext()) {
 120  0
                         Root root = (Root)iter.next();
 121  0
                         if (root.getRid() == rid)
 122  0
                                 return(root);
 123  0
                 }
 124  0
                 return(null);
 125  
         }
 126  
 
 127  
         /* (non-Javadoc)
 128  
          * @see com.buckosoft.PicMan.db.RootsDao#getRoot(java.lang.String)
 129  
          */
 130  
         public        Root        getRoot(String name) throws DataAccessException {
 131  0
                 if (rootsCache == null)
 132  0
                         getRoots();
 133  0
                 Iterator<Root> iter = rootsCache.iterator();
 134  0
                 while (iter.hasNext()) {
 135  0
                         Root root = (Root)iter.next();
 136  0
                         if (root.getName().equals(name))
 137  0
                                 return(root);
 138  0
                 }
 139  0
                 return(null);
 140  
         }
 141  
 
 142  
         /* (non-Javadoc)
 143  
          * @see com.buckosoft.PicMan.db.RootsDao#storeRoot(com.buckosoft.PicMan.domain.Root)
 144  
          */
 145  
         public void storeRoot(Root root) throws DataAccessException {
 146  0
                 if (root.getRid() <= 0)
 147  0
                         throw(new RuntimeException("Can't store Root with rid=" + root.getRid()));
 148  0
                 rootsCache = null;
 149  0
                 RootUpdate ru = new RootUpdate(ds, root);
 150  0
                 ru.update(root);                        
 151  0
         }
 152  
 
 153  
 
 154  
         /* (non-Javadoc)
 155  
          * @see com.buckosoft.PicMan.db.RootsDao#setRoots(java.util.List)
 156  
          */
 157  
         public void setRoots(List<Root> roots) throws DataAccessException {
 158  0
                 rootsCache = null;
 159  0
                 RootUpdate ru = new RootUpdate(ds);
 160  0
                 ru.update(roots);
 161  0
         }
 162  
 
 163  
         /* (non-Javadoc)
 164  
          * @see com.buckosoft.PicMan.db.RootsDao#addRoot(com.buckosoft.PicMan.domain.Root)
 165  
          */
 166  
         public void addRoot(Root r) throws DataAccessException {
 167  0
                 rootsCache = null;
 168  0
                 RootInsert ri = new RootInsert(ds);
 169  0
                 ri.insert(r);
 170  0
         }
 171  
 
 172  
         /* (non-Javadoc)
 173  
          * @see com.buckosoft.PicMan.db.RootsDao#deleteRoot(com.buckosoft.PicMan.domain.Root)
 174  
          */
 175  
         public        void        deleteRoot(Root r) throws DataAccessException {
 176  0
                 rootsCache = null;
 177  0
                 RootDelete rd = new RootDelete(ds);
 178  0
                 rd.delete(r);
 179  0
         }
 180  
 
 181  
         /**
 182  
          * <code>Root</code> Query object.
 183  
          */
 184  
         class RootsQuery extends MappingSqlQuery {
 185  
 
 186  0
                 RootsQuery(DataSource ds) {
 187  0
             super(ds, "SELECT * from roots");
 188  0
             compile();
 189  0
         }
 190  
  
 191  
         protected Object mapRow(ResultSet rs, int rowNum) throws SQLException {
 192  0
                 Root r = new Root();
 193  0
                 r.setRid(rs.getInt("rid"));
 194  0
                 r.setName(rs.getString("name"));
 195  0
                 r.setPath(rs.getString("path"));
 196  0
                 r.setFilePrefix(rs.getString("filePrefix"));
 197  0
                 r.setActive(rs.getBoolean("active"));
 198  0
                 return(r);
 199  
         }
 200  
         }
 201  
 
 202  
         /**
 203  
          * <code>Root</code> Update Object.
 204  
          */
 205  
         protected class RootUpdate extends SqlUpdate {
 206  
                 private        DataSource        ds;
 207  
                 
 208  
                 /**
 209  
                  * Create a new instance of RootUpdate.
 210  
                  * @param ds the DataSource to use for the update
 211  
                  */
 212  0
                 protected RootUpdate(DataSource ds) {
 213  0
                         this.ds = ds;
 214  0
                 }
 215  
 
 216  0
                 protected RootUpdate(DataSource ds, Root root) {
 217  0
                         super(ds, "UPDATE roots SET name=?, path=?, filePrefix=?, active=? WHERE rid = ? LIMIT 1");
 218  0
                         declareParameter(new SqlParameter(Types.VARCHAR));
 219  0
                         declareParameter(new SqlParameter(Types.VARCHAR));
 220  0
                         declareParameter(new SqlParameter(Types.VARCHAR));
 221  0
                         declareParameter(new SqlParameter(Types.TINYINT));
 222  0
                         declareParameter(new SqlParameter(Types.INTEGER));
 223  0
                         compile();
 224  0
                 }
 225  
                 /**
 226  
                  * Method to update the <code>Root</code>'s data.
 227  
                  * @param roots The Roots that should be stored
 228  
                  * @return 0
 229  
                  */
 230  
                 protected int update(List<Root> roots) {
 231  
                         // Empty the existing table
 232  0
                         SqlUpdate sf = new SqlUpdate(ds, "TRUNCATE TABLE roots");
 233  0
                         sf.compile();
 234  0
                         int ret = sf.update();
 235  0
                         logger.info("sf.empty() returned" + ret);
 236  
 
 237  
                         // Save each of our attributes
 238  0
                         RootInsert ri = new RootInsert(ds);
 239  0
                         Iterator<Root> it = roots.iterator();
 240  0
                         while (it.hasNext())
 241  0
                                 ri.insert(it.next());
 242  0
                         return(0);
 243  
                 }
 244  
 
 245  
                 protected int update(Root root) {
 246  0
                         return this.update(new Object[] {
 247  0
                                         root.getName(),
 248  0
                                         root.getPath(),
 249  0
                                         root.getFilePrefix(),
 250  0
                                         new Integer(root.isActive() ? 1 : 0),
 251  0
                                         new Integer(root.getRid()),
 252  
                         });
 253  
                 }
 254  
         }
 255  
 
 256  
 
 257  
         /**
 258  
          * <code>Root</code> Insert Object.
 259  
          */
 260  
         protected class RootInsert extends SqlUpdate {
 261  
 
 262  
                 /**
 263  
                  * Create a new instance of SetInsert.
 264  
                  * @param ds the DataSource to use for the insert
 265  
                  */
 266  0
                 protected RootInsert(DataSource ds) {
 267  0
                         super(ds, "INSERT INTO roots VALUES(?,?,?,?,?)");
 268  0
                         declareParameter(new SqlParameter(Types.INTEGER));
 269  0
                         declareParameter(new SqlParameter(Types.TINYINT));
 270  0
                         declareParameter(new SqlParameter(Types.VARCHAR));
 271  0
                         declareParameter(new SqlParameter(Types.VARCHAR));
 272  0
                         declareParameter(new SqlParameter(Types.VARCHAR));
 273  0
                         compile();
 274  0
                 }
 275  
 
 276  
                 protected void insert(Root r) {
 277  0
                         Object[] objs = new Object[] {
 278  0
                                 new Integer(r.getRid()),
 279  0
                                 new Integer(r.isActive() ? 1 : 0),
 280  0
                                 r.getName(), r.getPath(), r.getFilePrefix(),
 281  
                                 };
 282  0
                         super.update(objs);
 283  0
                 }
 284  
         }
 285  
         /**
 286  
          * <code>Root</code> Delete Object.
 287  
          */
 288  
         protected class RootDelete extends SqlUpdate {
 289  
                 
 290  
                 /**
 291  
                  * Create a new instance of SetDelete.
 292  
                  * @param ds the DataSource to use for the delete
 293  
                  */
 294  0
                 protected RootDelete(DataSource ds) {
 295  0
                         super(ds, "DELETE FROM rots WHERE name = (?)");
 296  0
                         declareParameter(new SqlParameter(Types.VARCHAR));
 297  0
                         compile();
 298  0
                 }
 299  
                 
 300  
                 protected void delete(Root root) {
 301  0
                         Object[] objs = new Object[] {
 302  0
                                         root.getName()};
 303  0
                         super.update(objs);
 304  0
                 }
 305  
         }
 306  
 }