View Javadoc
1   /******************************************************************************
2    * PostersDaoJdbc.java - Implement the Dao interface for the Posters
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.Poster;
26  
27  /** Implement the Dao Interface for the {@link com.buckosoft.PicMan.domain.Poster}s.
28   * @author Dick Balaska
29   * @since 2012/07/22
30   * @see <a href="http://cvs.buckosoft.com/Projects/java/PicMan/PicMan/src/com/buckosoft/PicMan/db/PostersDaoJdbc.java">PostersDaoJdbc.java</a>
31   */
32  public class PostersDaoJdbc implements PostersDao {
33  	private static final boolean DEBUG = true;
34  	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  		this.ds = ds;
44  	}
45  
46  	/* (non-Javadoc)
47  	 * @see com.buckosoft.PicMan.db.PostersDao#getPoster(int)
48  	 */
49  	public Poster getPoster(int pid) {
50  		if (DEBUG)
51  			logger.info("getPoster: " + pid);
52  		if (sql_getPosterQUERY == null)
53  			sql_getPosterQUERY = new PostersQuery(ds, pid);
54          Poster m = (Poster)sql_getPosterQUERY.findObject(pid);
55         	getPosterConfig(m);
56          return(m);
57  	}
58  	private	PostersQuery	sql_getPosterQUERY = null;
59  
60  	/* (non-Javadoc)
61  	 * @see com.buckosoft.PicMan.db.PostersDao#getPosters()
62  	 */
63  	@SuppressWarnings("unchecked")
64  	public List<Poster> getPosters() {
65  		if (DEBUG)
66  			logger.info("GetPostersList");
67  		if (sql_getPostersListQUERY == null)
68  			sql_getPostersListQUERY = new PostersQuery(ds);
69  		List<Poster> PosterList = (List<Poster>)sql_getPostersListQUERY.execute();
70  		for (Poster m : PosterList)
71  			this.getPosterConfig(m);
72  		return(PosterList);
73  	}
74  	private	PostersQuery	sql_getPostersListQUERY = null;
75  
76  	/* (non-Javadoc)
77  	 * @see com.buckosoft.PicMan.db.PostersDao#deletePoster(int)
78  	 */
79  	public void deletePoster(int mid) {
80  		if (DEBUG)
81  			logger.info("deletePoster");
82  		// TODO deletePoster not implemented
83  		throw new RuntimeException("Not implemented");
84  	}
85  
86  	/* (non-Javadoc)
87  	 * @see com.buckosoft.PicMan.db.PostersDao#storePoster(com.buckosoft.PicMan.domain.Poster)
88  	 */
89  	public void storePoster(Poster poster) {
90  		if (DEBUG)
91  			logger.info("storePoster " + poster.getPid());
92  		if (poster.getPid() <= 0) {
93  			PostersInsert mi = new PostersInsert(ds);
94  			mi.insert(poster);
95  			int c = getPosterCount();
96  			poster.setPid(c);
97  		} else {
98  			PostersUpdate mu = new PostersUpdate(ds);
99  			mu.update(poster);
100 		}
101 		deletePosterConfigs(poster.getPid());
102 		if (sql_storePosterConfigsINSERT == null)
103 			sql_storePosterConfigsINSERT = new PosterConfigsInsert(ds);
104 		for (String key : poster.getConfig().keySet()) {
105 			sql_storePosterConfigsINSERT.insert(poster.getPid(), key, poster.getConfig().get(key));
106 		}
107 	}
108 	private	PosterConfigsInsert	sql_storePosterConfigsINSERT = null;
109 
110 	/** Get the number of Virgins in the database
111 	 * @return the count
112 	 */
113 	public int	getPosterCount() {
114 		if (DEBUG)
115 			logger.info("getPosterCount:");
116 		if (sql_getPosterCountQUERYr == null)
117 			sql_getPosterCountQUERYr = new PostersCount(ds);
118 		return(sql_getPosterCountQUERYr.run());
119 	}
120 	private PostersCount	sql_getPosterCountQUERYr;
121 	
122 	@SuppressWarnings("unchecked")
123 	private	void getPosterConfig(Poster poster) {
124 		if (poster == null)
125 			return;
126 		if (sql_getPosterConfigQUERY == null)
127 			sql_getPosterConfigQUERY = new PosterConfigsQuery(ds, 0);
128 		List<KeyValue>kvlist = (List<KeyValue>)sql_getPosterConfigQUERY.execute(poster.getPid());
129 		for (KeyValue kv : kvlist) {
130 			poster.getConfig().put(kv.key, kv.value);
131 		}
132 	}
133 
134 	private	PosterConfigsQuery	sql_getPosterConfigQUERY = null;
135 
136 	private void deletePosterConfigs(int sid) throws DataAccessException {
137 		PosterConfigsDelete msd = new PosterConfigsDelete(ds);
138 		msd.delete(sid);
139 	}
140 	
141 
142 	/**
143 	 * Object to Query for Posters
144 	 */
145 	private class PostersQuery extends MappingSqlQuery {
146 		
147 		PostersQuery(DataSource ds) {
148 			super(ds, "SELECT * from posters");
149 			compile();
150 		}
151 		
152 		PostersQuery(DataSource ds, int pid) {
153 			super(ds, "SELECT * from posters where pid = ?");
154 			declareParameter(new SqlParameter(Types.INTEGER));
155 			compile();
156 		}
157 
158 
159 		protected Object mapRow(ResultSet rs, int rowNum) throws SQLException {
160 			Poster p = new Poster();
161 			p.setName(rs.getString("name"));
162 			p.setOutputPath(rs.getString("outputPath"));
163 			p.setPid(rs.getInt("pid"));
164 			p.setMasterMid(rs.getInt("mosaicId"));
165 //			try {
166 //				m.setStartTime(rs.getDate("startTime"));
167 //			} catch (Exception e) {logger.info(e);}
168 			return(p);
169 		}
170 	}
171 	/**
172 	 * <code>Virgin</code> Count Object.
173 	 */
174 	class PostersCount extends SqlFunction {
175 		
176 		PostersCount(DataSource ds) {
177 			super(ds, "SELECT COUNT(*) from posters");
178 			compile();
179 		}		
180 	}
181 
182 	/**
183 	 * <code>Poster</code> Insert Object.
184 	 */
185 	private class PostersInsert extends SqlUpdate {
186 		
187 		/**
188 		 * Create a new instance of PostersInsert.
189 		 * @param ds the DataSource to use for the insert
190 		 */
191 		protected PostersInsert(DataSource ds) {
192 			super(ds, "INSERT INTO posters VALUES(?,?,?,?)");
193 			declareParameter(new SqlParameter(Types.INTEGER));
194 			declareParameter(new SqlParameter(Types.VARCHAR));
195 			declareParameter(new SqlParameter(Types.INTEGER));
196 			declareParameter(new SqlParameter(Types.VARCHAR));
197 			compile();
198 		}
199 
200 		protected void insert(Poster p) {
201 			Object[] objs = new Object[] {
202 				null,
203 				p.getName(),
204 				new Integer(p.getMasterMid()),
205 				p.getOutputPath(),
206 			};
207 			super.update(objs);
208 		}
209 	}
210 	///////////////////////////////////////////////////////////////////////////////////////////////
211 	/**
212 	 * <code>Poster</code> Update Object.
213 	 */
214 	private class PostersUpdate extends SqlUpdate {
215 		//private	DataSource	ds;
216 		
217 		/**
218 		 * Create a new instance of PostersUpdate.
219 		 * @param ds the DataSource to use for the update
220 		 */
221 		protected PostersUpdate(DataSource ds) {
222 			super(ds, "UPDATE posters SET name=?, mosaicId=?, outputPath=? WHERE pid = ? LIMIT 1");
223 			declareParameter(new SqlParameter(Types.VARCHAR));
224 			declareParameter(new SqlParameter(Types.INTEGER));
225 			declareParameter(new SqlParameter(Types.VARCHAR));
226 			declareParameter(new SqlParameter(Types.INTEGER));
227 			compile();
228 		}
229 		/**
230 		 * Method to update the <code>Set</code>'s data.
231 		 * @param List sets The sets that should be stored
232 		 * @return 0
233 		 */
234 
235 		protected int update(Poster p) {
236 			return this.update(new Object[] {
237 					p.getName(),
238 					new Integer(p.getMasterMid()),
239 					p.getOutputPath(),
240 					new Integer(p.getPid())
241 			});
242 		}
243 	}
244 	private class KeyValue {
245 		public String	key;
246 		public String	value;
247 	}
248 	
249 	private class PosterConfigsQuery extends MappingSqlQuery {
250 
251 		PosterConfigsQuery(DataSource ds, int pid) {
252             super(ds, "SELECT * from posterConfigs where pid=?");
253 			declareParameter(new SqlParameter(Types.INTEGER));
254             compile();
255         }
256  
257         protected Object mapRow(ResultSet rs, int rowNum) throws SQLException {
258         	KeyValue k = new KeyValue();
259         	k.key = rs.getString("key");
260         	k.value = rs.getString("value");
261         	return(k);
262         }
263 	}
264 
265 	/**
266 	 * <code>PosterConfig</code> Delete Object.
267 	 */
268 	protected class PosterConfigsDelete extends SqlUpdate {
269 		
270 		/**
271 		 * Create a new instance of MetaSetRemove.
272 		 * @param ds the DataSource to use for the delete
273 		 */
274 		protected PosterConfigsDelete(DataSource ds) {
275 			super(ds, "DELETE FROM posterConfigs WHERE pid = (?)");
276 			declareParameter(new SqlParameter(Types.INTEGER));
277 			compile();
278 		}
279 		
280 		protected void delete(int sid) {
281 			super.update(sid);
282 		}
283 	}
284 	/**
285 	 * <code>PosterConfigs</code> Insert Object.
286 	 */
287 	protected class PosterConfigsInsert extends SqlUpdate {
288 		
289 		/**
290 		 * Create a new instance of MetaSetInsert.
291 		 * @param ds the DataSource to use for the insert
292 		 */
293 		protected PosterConfigsInsert(DataSource ds) {
294 			super(ds, "INSERT INTO posterConfigs VALUES(?,?,?)");
295 			declareParameter(new SqlParameter(Types.INTEGER));
296 			declareParameter(new SqlParameter(Types.VARCHAR));
297 			declareParameter(new SqlParameter(Types.VARCHAR));
298 			compile();
299 		}
300 
301 		protected void insert(int pid, String key, String value) {
302 			Object[] objs = new Object[] {
303 					new Integer(pid),
304 					key,
305 					value,
306 			};
307 			super.update(objs);
308 		}
309 	}
310 }