View Javadoc
1   /******************************************************************************
2    * SetTimestampsDaoJdbc.java - Implement the Dao Interface for the SetTimestamp table
3    *
4    * PicMan - The BuckoSoft Picture Manager in Java
5    * Copyright(c) 2011 - 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.HashMap;
15  import java.util.Iterator;
16  import java.util.LinkedHashMap;
17  import java.util.List;
18  
19  import javax.sql.DataSource;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  import org.springframework.jdbc.core.SqlParameter;
24  import org.springframework.jdbc.object.MappingSqlQuery;
25  import org.springframework.jdbc.object.SqlUpdate;
26  
27  /** Implement the Dao Interface for the SetTimestamp table
28   * @author Dick Balaska
29   * @since 2011/12/30
30   * @see <a href="http://cvs.buckosoft.com/Projects/java/PicMan/PicMan/src/com/buckosoft/PicMan/db/SetTimestampsDaoJdbc.java">SetTimestampsDaoJdbc.java</a>
31   */
32  public class SetTimestampsDaoJdbc implements SetTimestampsDao {
33  
34  	private final static boolean DEBUG = false;
35  	protected final Log logger = LogFactory.getLog(getClass());
36  
37  	private DataSource ds;
38  	
39  	/** Set the JDBC datasource reference
40       * @param ds The DataSource as generated by the Spring config/setup.
41       */
42  	public void setDataSource(DataSource ds) {
43  		this.ds = ds;
44  	}
45  
46  	/* (non-Javadoc)
47  	 * @see com.buckosoft.PicMan.db.FiltersDao#getFilterDates()
48  	 */
49  	public HashMap<String, Date>	getSetTimestamps() {
50  		HashMap<String, Date> hm = new HashMap<String, Date>();
51  		if (sql_getFilterDatesQUERY == null)
52  			sql_getFilterDatesQUERY = new FilterSetsTimestampQuery(ds);
53  		@SuppressWarnings("unchecked")
54  		List<SetsTimestamp> l = sql_getFilterDatesQUERY.execute();
55  		Iterator<SetsTimestamp> it = l.iterator();
56  		while (it.hasNext()) {
57  			SetsTimestamp st = it.next();
58  			hm.put(st.uid, st.timestamp);
59  		}
60  		return(hm);
61  	}
62  	private FilterSetsTimestampQuery sql_getFilterDatesQUERY = null;
63  
64  	/* (non-Javadoc)
65  	 * @see com.buckosoft.PicMan.db.FiltersDao#updateFilterDates(java.util.LinkedHashMap)
66  	 */
67  	public	void	updateFilterDates(LinkedHashMap<String, String> changed) {
68  		for (String s : changed.keySet())
69  			updateSetTimestamp(s);
70  	}
71  
72  	@Override
73  	public void updateSetTimestamp(String setName) {
74  		String s = "DELETE FROM setsTimestamp WHERE uid = \"" + setName + "\"";
75  		if (DEBUG)
76  			logger.info(s);
77  		SqlUpdate sf1 = new SqlUpdate(ds, s);
78  		sf1.update();
79  
80  		if (sql_updateFilterDatesINSERT == null)
81  			sql_updateFilterDatesINSERT = new FilterSetsTimestampInsert(ds);
82  		sql_updateFilterDatesINSERT.insert(setName);
83  
84  	}
85  	FilterSetsTimestampInsert sql_updateFilterDatesINSERT;
86  
87  	
88  	/** Mini-domain object representing the setsTimestamp table */
89  	class SetsTimestamp {
90  		String	uid;
91  		Date	timestamp;
92  	}
93  	
94  	class FilterSetsTimestampQuery extends MappingSqlQuery {
95  		FilterSetsTimestampQuery(DataSource ds) {
96  			super(ds, "SELECT * from setsTimestamp");
97  			compile();
98  		}
99  		
100 		protected Object mapRow(ResultSet rs, int rowNum) throws SQLException {
101 			SetsTimestamp st = new SetsTimestamp();
102 			st.uid = rs.getString("uid");
103 			try {
104 				st.timestamp = rs.getTimestamp("timestamp");
105 			} catch (Exception e) {}
106 			
107 			return(st);
108 		}
109 		
110 	}
111 	
112 	/**
113 	 * <code>FilterSetsTimestamp</code> Insert Object.
114 	 */
115 	protected class FilterSetsTimestampInsert extends SqlUpdate {
116 		/**
117 		 * Create a new instance of FilterSetsTimestampInsert.
118 		 * @param ds the DataSource to use for the insert
119 		 */
120 		protected FilterSetsTimestampInsert(DataSource ds) {
121 			super(ds, "INSERT INTO setsTimestamp VALUES(?,?)");
122 			declareParameter(new SqlParameter(Types.VARCHAR));
123 			declareParameter(new SqlParameter(Types.TIMESTAMP));
124 			compile();
125 		}
126 		
127 		protected void insert(SetsTimestamp st) {
128 			Object[] objs = new Object[] {
129 					st.uid, st.timestamp };
130 			super.update(objs);
131 		}
132 		protected void insert(String key) {
133 			Object[] objs = new Object[] {
134 					key, new Date() };
135 			super.update(objs);
136 		}
137 	}	
138 	
139 }