Coverage Report - com.buckosoft.PicMan.db.ContactsDaoJdbc
 
Classes in this File Line Coverage Branch Coverage Complexity
ContactsDaoJdbc
0%
0/65
0%
0/24
2.368
ContactsDaoJdbc$ContactInsert
0%
0/12
N/A
2.368
ContactsDaoJdbc$ContactQuery
0%
0/13
N/A
2.368
ContactsDaoJdbc$ContactSummaryInsert
0%
0/13
N/A
2.368
ContactsDaoJdbc$ContactsSummaryQuery
0%
0/37
N/A
2.368
ContactsDaoJdbc$Frag
0%
0/1
N/A
2.368
 
 1  
 /******************************************************************************
 2  
  * ContactsDaoJdbc.java - Implement the JDBC Dao interface for the Contacts
 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.Date;
 11  
 import java.util.HashMap;
 12  
 import java.util.Iterator;
 13  
 import java.util.List;
 14  
 
 15  
 import java.sql.ResultSet;
 16  
 import java.sql.SQLException;
 17  
 import java.sql.Types;
 18  
 
 19  
 import javax.sql.DataSource;
 20  
 
 21  
 import org.apache.commons.logging.Log;
 22  
 import org.apache.commons.logging.LogFactory;
 23  
 
 24  
 import org.springframework.jdbc.core.SqlParameter;
 25  
 import org.springframework.jdbc.object.MappingSqlQuery;
 26  
 import org.springframework.jdbc.object.SqlUpdate;
 27  
 
 28  
 import com.buckosoft.PicMan.domain.Chain;
 29  
 import com.buckosoft.PicMan.domain.Contact;
 30  
 import com.buckosoft.PicMan.domain.ContactParams;
 31  
 import com.buckosoft.PicMan.domain.SetSize;
 32  
 
 33  
 /** Implement the Dao interface for the {@link com.buckosoft.PicMan.domain.Contact}s.
 34  
  * @author Dick Balaska
 35  
  * @since 2005/08/07
 36  
  */
 37  0
 public class ContactsDaoJdbc implements ContactsDao {
 38  
 
 39  
         private final static boolean DEBUG = false;
 40  0
         private final Log logger = LogFactory.getLog(getClass());
 41  
         
 42  
         private DataSource ds;
 43  
         
 44  0
         private        Contact lastContactAdded = null;
 45  
 
 46  
         /** Set the JDBC datasource reference
 47  
      * @param ds The DataSource as generated by the Spring config/setup.
 48  
      */
 49  
         public void setDataSource(DataSource ds) {
 50  0
                 this.ds = ds;
 51  0
                 lastContactAdded = getNewestContact();
 52  0
         }
 53  
         
 54  
         /* (non-Javadoc)
 55  
          * @see com.buckosoft.PicMan.db.ContactsDao#addContact(com.buckosoft.PicMan.domain.Contact)
 56  
          */
 57  
         public void        addContact(Contact c) {
 58  0
                 String s = "DELETE FROM contacts WHERE name = \"" + c.getName() + "\" AND cid =\"" + c.getCid() + "\"";
 59  
                 if (DEBUG)
 60  
                         logger.info(s);
 61  0
                 SqlUpdate sf = new SqlUpdate(ds, s);
 62  0
                 sf.update();
 63  0
                 s = "DELETE FROM contactSummary WHERE name = \"" + c.getName() + "\" AND cid =\"" + c.getCid() + "\"";
 64  
                 if (DEBUG)
 65  
                         logger.info(s);
 66  0
                 SqlUpdate sf1 = new SqlUpdate(ds, s);
 67  0
                 sf1.update();
 68  0
                 ContactInsert ci = new ContactInsert(ds);
 69  0
                 Frag        f = new Frag();
 70  0
                 f.name = c.getName();
 71  0
                 f.cid = c.getCid();
 72  
                 String        pic;
 73  0
                 for (int row = 0; row<c.getRowCount(); row++) {
 74  0
                         for (int col = 0; ; col++) {
 75  0
                                 pic = c.getPic(row, col);
 76  0
                                 if (pic == null)
 77  0
                                         break;
 78  0
                                 f.row = row;
 79  0
                                 f.col = col;
 80  0
                                 f.pic = pic;
 81  0
                                 ci.insert(f);
 82  
                         }
 83  
                 }
 84  0
                 ContactSummaryInsert csi = new ContactSummaryInsert(ds);
 85  0
                 csi.insert(c);
 86  0
                 lastContactAdded = c;
 87  0
         }
 88  
 
 89  
         /* (non-Javadoc)
 90  
          * @see com.buckosoft.PicMan.db.ContactsDao#getNewestContact()
 91  
          */
 92  
         public Contact getNewestContact() {
 93  0
                 if (lastContactAdded != null)
 94  0
                         return(lastContactAdded);
 95  
 
 96  0
                 Date        longago = new Date(0);
 97  0
                 if (sql_getNewestContactDateQUERYr == null)
 98  0
                         sql_getNewestContactDateQUERYr = new ContactsSummaryQuery(ds, longago);
 99  
                 
 100  0
                 Contact c = (Contact)sql_getNewestContactDateQUERYr.findObject(new Object[0]);
 101  
                 if (DEBUG)
 102  
                         logger.info("newest contact date: " + ((c != null) ? c.getStartTime() : longago));
 103  0
                 return(c);                
 104  
         }
 105  
         private ContactsSummaryQuery        sql_getNewestContactDateQUERYr;
 106  
 
 107  
 /*        public HashMap                getContactOldestMap(List sets, List sizes) {
 108  
                 if (DEBUG)
 109  
                         logger.info("getContactOldestMap");
 110  
                 HashMap hm = new HashMap();
 111  
                 Iterator it = sets.iterator();
 112  
                 while (it.hasNext()) {
 113  
                         Set set = (Set)it.next();
 114  
                         Iterator is = sizes.iterator();
 115  
                         while (is.hasNext()) {
 116  
                                 int size = ((Integer)is.next()).intValue();
 117  
                                 String uuid = System.getUuid(set.getName(), size);
 118  
 //                                if (sql_getContactOldestMapQUERY == null)
 119  
 //                                        sql_getContactOldestMapQUERY = new ContactsSummaryQuery(ds, 0, 0);
 120  
                                 //X:
 121  
                                 Contact c = null;
 122  
                                 ContactsSummaryQuery csq = new ContactsSummaryQuery(ds, 0, 0, "SELECT * from contactSummary WHERE name LIKE \"" + uuid + "%\" ORDER BY startTime ASC LIMIT 1");
 123  
                                 List l = csq.execute();
 124  
 //                                sql_getContactOldestMapQUERY.setSql("SELECT * from contactSummary WHERE name LIKE \"" + uuid + "%\" ORDER BY startTime ASC LIMIT 1");
 125  
 //                                sql_getContactOldestMapQUERY.compile();
 126  
 //                                Contact c = (Contact)sql_getContactOldestMapQUERY.findObject(uuid + "%");
 127  
 //                                Contact c = (Contact)sql_getContactOldestMapQUERY.execute();
 128  
 //                                List l = sql_getContactOldestMapQUERY.execute();
 129  
                                 if (!l.isEmpty())
 130  
                                         c = (Contact)l.get(0);
 131  
                                 if (c != null) {
 132  
                                         if (DEBUG)
 133  
                                                 logger.info("map: " + uuid + " : " + c.getStartTime());
 134  
                                         hm.put(uuid, c.getStartTime());
 135  
                                 }
 136  
                         }
 137  
                 }
 138  
                 return(hm);
 139  
         }
 140  
 */
 141  
         
 142  
         /* (non-Javadoc)
 143  
          * @see com.buckosoft.PicMan.db.ContactsDao#getContactOldestMap(com.buckosoft.PicMan.domain.Chain)
 144  
          */
 145  
         public HashMap<String, Date>                getContactOldestMap(Chain chain) {
 146  
                 if (DEBUG)
 147  
                         logger.info("getContactOldestMap: " + chain.getName());
 148  0
                 HashMap<String, Date> hm = new HashMap<String, Date>();
 149  0
                 Iterator<SetSize> iter = chain.getSetSizes().iterator();
 150  0
                 while (iter.hasNext()) {
 151  0
                         SetSize ss = (SetSize)iter.next();
 152  0
                         String        uuid = ss.getSetSizeDash() + "%";
 153  0
                         if (sql_getContactOldestMapQUERY == null)
 154  0
                                 sql_getContactOldestMapQUERY = new ContactsSummaryQuery(ds, 0, 0);
 155  
                         if (DEBUG)
 156  
                                 logger.info("getContactOldestMap: " + chain.getCid() + " '" + uuid + "'");
 157  0
                         Contact c = null;
 158  0
                         c = (Contact)sql_getContactOldestMapQUERY.findObject(new Object[] {new Integer(chain.getCid()), uuid});
 159  0
                         if (c != null) {
 160  
                                 if (DEBUG)
 161  
                                         logger.info("map: " + uuid + " : " + c.getStartTime());
 162  0
                                 hm.put(ss.getGuiSetSize(), c.getStartTime());
 163  
                         }
 164  0
                 }
 165  0
                 return(hm);
 166  
         }
 167  
         private        ContactsSummaryQuery sql_getContactOldestMapQUERY;
 168  
         
 169  
         /* (non-Javadoc)
 170  
          * @see com.buckosoft.PicMan.db.ContactsDao#getContact(int, java.lang.String)
 171  
          */
 172  
         public Contact getContact(int cid, String uuid) {
 173  
                 if (DEBUG)
 174  
                         logger.info("getContact: '" + uuid + "'");
 175  
                 Contact c;
 176  0
                 if (sql_getContactSUMMARYQUERY == null)
 177  0
                         sql_getContactSUMMARYQUERY = new ContactsSummaryQuery(ds, cid, uuid);
 178  
 //                ContactsSummaryQuery csq = new ContactsSummaryQuery(ds, uuid);
 179  0
                 c = (Contact)sql_getContactSUMMARYQUERY.findObject(new Object[]{new Integer(cid), uuid});
 180  0
                 if (c == null)
 181  0
                         return(null);
 182  
                 
 183  0
                 if (sql_getContactQUERY == null)
 184  0
                         sql_getContactQUERY = new ContactQuery(ds, cid, uuid);
 185  
 //                ContactQuery cq = new ContactQuery(ds, uuid);
 186  
                 @SuppressWarnings("unchecked")
 187  0
                 List<Frag> l = sql_getContactQUERY.execute(new Object[] {new Integer(cid), uuid});
 188  0
                 if (l.isEmpty())
 189  0
                         return(null);
 190  0
                 c.setName(uuid);
 191  0
                 for (Frag f : l) {
 192  0
                         c.setPic(f.pic, f.row, f.col);
 193  0
                 }
 194  0
                 return(c);
 195  
         }
 196  
         private        ContactsSummaryQuery        sql_getContactSUMMARYQUERY;
 197  
         private        ContactQuery                        sql_getContactQUERY;
 198  
 
 199  
         
 200  
         /* (non-Javadoc)
 201  
          * @see com.buckosoft.PicMan.db.ContactsDao#getContact(int, java.lang.String, int, int)
 202  
          */
 203  
         public Contact        getContact(int cid, String setName, int size, int index) {
 204  0
                 return(getContact(cid, (new ContactParams(cid, setName, size, index)).getUuid()));
 205  
         }
 206  
 
 207  
         /** 
 208  
          * A single Contact is made up of many (short) rows, the db calls these <code>Frag</code>s. 
 209  
          */
 210  0
         protected class Frag {
 211  
                 int                cid;
 212  
                 String        name;
 213  
                 int                row;
 214  
                 int                col;
 215  
                 String        pic;
 216  
         }
 217  
         
 218  
         class ContactQuery extends MappingSqlQuery {
 219  
                 
 220  0
                 ContactQuery(DataSource ds, int cid, String name) {
 221  0
                         super(ds, "SELECT * FROM contacts WHERE cid = ? and name = ? ORDER BY row,col");
 222  0
             declareParameter(new SqlParameter(Types.INTEGER));
 223  0
             declareParameter(new SqlParameter(Types.VARCHAR));
 224  0
                         compile();
 225  0
                 }
 226  
                 
 227  
                 protected Object mapRow(ResultSet rs, int rowNum) throws SQLException {
 228  0
                         Frag f = new Frag();
 229  0
                         f.cid        = rs.getInt("cid");
 230  0
                         f.name        = rs.getString("name");
 231  0
                         f.row        = rs.getInt("row");
 232  0
                         f.col        = rs.getInt("col");
 233  0
                         f.pic        = rs.getString("pic");
 234  0
                         return(f);
 235  
                 }
 236  
                 
 237  
         }
 238  
 
 239  
         /**
 240  
          * <code>Contact</code> Insert Object.
 241  
          */
 242  
         protected class ContactInsert extends SqlUpdate {
 243  
                 
 244  
                 /**
 245  
                  * Create a new instance of SystemInsert.
 246  
                  * @param ds the DataSource to use for the insert
 247  
                  */
 248  0
                 protected ContactInsert(DataSource ds) {
 249  0
                         super(ds, "INSERT INTO contacts VALUES(?,?,?,?,?)");
 250  0
                         declareParameter(new SqlParameter(Types.INTEGER));
 251  0
                         declareParameter(new SqlParameter(Types.VARCHAR));
 252  0
                         declareParameter(new SqlParameter(Types.INTEGER));
 253  0
                         declareParameter(new SqlParameter(Types.INTEGER));
 254  0
                         declareParameter(new SqlParameter(Types.VARCHAR));
 255  0
                         compile();
 256  0
                 }
 257  
                 
 258  
                 protected void insert(Frag f) {
 259  0
                         Object[] objs = new Object[] {
 260  
                                         new Integer(f.cid), f.name, 
 261  
                                         new Integer(f.row), new Integer(f.col), f.pic};
 262  0
                         super.update(objs);
 263  0
                 }
 264  
         }
 265  
         class ContactsSummaryQuery extends MappingSqlQuery {
 266  
                 
 267  0
                 ContactsSummaryQuery(DataSource ds) {
 268  0
                         super(ds, "SELECT * from contactSummary");
 269  0
                         compile();
 270  0
                 }
 271  
                 
 272  0
                 ContactsSummaryQuery(DataSource ds, String name) {
 273  0
                         super(ds, "SELECT * from contactSummary where name = ?");
 274  0
             declareParameter(new SqlParameter(Types.VARCHAR));
 275  0
                         compile();
 276  0
                 }
 277  
 
 278  0
                 ContactsSummaryQuery(DataSource ds, int cid, String name) {
 279  0
                         super(ds, "SELECT * from contactSummary where cid = ? and name = ?");
 280  0
             declareParameter(new SqlParameter(Types.INTEGER));
 281  0
             declareParameter(new SqlParameter(Types.VARCHAR));
 282  0
                         compile();
 283  0
                 }
 284  
 
 285  0
                 ContactsSummaryQuery(DataSource ds, Date date) {
 286  0
                         super(ds, "SELECT * from contactSummary ORDER BY startTime DESC LIMIT 1");
 287  0
                         compile();
 288  0
                 }
 289  
 
 290  
                 /** Get the single oldest contact in a named ChainSet
 291  
                  * @param ds The DataSource
 292  
                  * @param unused Select the correct constructor
 293  
                  * @param unused1 Select the correct constructor
 294  
                  */
 295  0
                 ContactsSummaryQuery(DataSource ds, int unused, int unused1) {
 296  0
                         super(ds, "SELECT * from contactSummary WHERE cid = ? AND name LIKE ? ORDER BY `startTime` ASC LIMIT 1");
 297  0
             declareParameter(new SqlParameter(Types.INTEGER));
 298  0
             declareParameter(new SqlParameter(Types.VARCHAR));
 299  0
                         compile();
 300  0
                 }
 301  
 
 302  0
                 ContactsSummaryQuery(DataSource ds, int unused0, int unused1, String s) {
 303  0
                         super(ds, s);
 304  0
                         compile();
 305  0
                 }
 306  
 
 307  
                 protected Object mapRow(ResultSet rs, int rowNum) throws SQLException {
 308  0
                         Contact c = new Contact();
 309  0
                         c.setCid(rs.getInt("cid"));
 310  0
                         c.setName(rs.getString("name"));
 311  
                         try {
 312  0
                                 c.setStartTime(rs.getTimestamp("startTime"));
 313  0
                         } catch (Exception e) {}
 314  
                         try {
 315  0
                                 c.setEndTime(rs.getTimestamp("endTime"));
 316  0
                         } catch (Exception e) {}
 317  0
                         return(c);
 318  
                 }
 319  
         }
 320  
         /**
 321  
          * <code>ContactSummary</code> Insert Object.
 322  
          */
 323  
         protected class ContactSummaryInsert extends SqlUpdate {
 324  
                 
 325  
                 /**
 326  
                  * Create a new instance of SystemInsert.
 327  
                  * @param ds the DataSource to use for the insert
 328  
                  */
 329  0
                 protected ContactSummaryInsert(DataSource ds) {
 330  0
                         super(ds, "INSERT INTO contactSummary VALUES(?,?,?,?)");
 331  0
                         declareParameter(new SqlParameter(Types.INTEGER));
 332  0
                         declareParameter(new SqlParameter(Types.VARCHAR));
 333  0
                         declareParameter(new SqlParameter(Types.TIMESTAMP));
 334  0
                         declareParameter(new SqlParameter(Types.TIMESTAMP));
 335  0
                         compile();
 336  0
                 }
 337  
                 
 338  
                 protected void insert(Contact c) {
 339  0
                         Object[] objs = new Object[] {
 340  0
                                         new Integer(c.getCid()), 
 341  0
                                         c.getName(), c.getStartTime(), c.getEndTime() };
 342  0
                         super.update(objs);
 343  0
                 }
 344  
         }
 345  
 }