View Javadoc
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  public class RootsDaoJdbc implements RootsDao {
37  
38  	/** Logger for this class and subclasses */
39  	private static final boolean DEBUG = false;
40  	protected final Log logger = LogFactory.getLog(getClass());
41  
42  	private DataSource ds;
43  
44  	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          this.ds = ds;
54      }
55  
56  	/* (non-Javadoc)
57  	 * @see com.buckosoft.PicMan.db.RootsDao#getRoots()
58  	 */
59  	@SuppressWarnings("unchecked")
60  	public List<Root> getRoots() throws DataAccessException {
61  		if (rootsCache != null)
62  			return(rootsCache);
63  		if (DEBUG)
64  			logger.info("getSets()");
65          RootsQuery rq = new RootsQuery(ds);
66          List<Root> l = rq.execute();
67          rootsCache = l;
68          return(l);
69  	}
70  
71  	/* (non-Javadoc)
72  	 * @see com.buckosoft.PicMan.db.RootsDao#getInactiveRoots()
73  	 */
74  	public List<Root> getInactiveRoots() throws DataAccessException {
75  		RootsQuery rq = new RootsQuery(ds);
76  		@SuppressWarnings("unchecked")
77          List<Root> inactiveRoots = rq.execute();
78  		Iterator<Root> siter = inactiveRoots.iterator();
79  		while (siter.hasNext()) {
80  			Root root = (Root)siter.next();
81  			if (root.isActive())
82  				siter.remove();
83  		}
84  		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  		RootsQuery rq = new RootsQuery(ds);
93          List<Root> activeRoots = rq.execute();
94  		Iterator<Root> siter = activeRoots.iterator();
95  		while (siter.hasNext()) {
96  			Root root = siter.next();
97  			if (!root.isActive())
98  				siter.remove();
99  		}
100 		return(activeRoots);
101 	}
102 
103 		/* (non-Javadoc)
104 	 * @see com.buckosoft.PicMan.db.RootsDao#getRootCount()
105 	 */
106 	public int	getRootCount() throws DataAccessException {
107 		if (rootsCache == null)
108 			getRoots();
109 		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 		if (rootsCache == null)
117 			getRoots();
118 		Iterator<Root> iter = rootsCache.iterator();
119 		while (iter.hasNext()) {
120 			Root root = (Root)iter.next();
121 			if (root.getRid() == rid)
122 				return(root);
123 		}
124 		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 		if (rootsCache == null)
132 			getRoots();
133 		Iterator<Root> iter = rootsCache.iterator();
134 		while (iter.hasNext()) {
135 			Root root = (Root)iter.next();
136 			if (root.getName().equals(name))
137 				return(root);
138 		}
139 		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 		if (root.getRid() <= 0)
147 			throw(new RuntimeException("Can't store Root with rid=" + root.getRid()));
148 		rootsCache = null;
149 		RootUpdate ru = new RootUpdate(ds, root);
150 		ru.update(root);			
151 	}
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 		rootsCache = null;
159 		RootUpdate ru = new RootUpdate(ds);
160 		ru.update(roots);
161 	}
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 		rootsCache = null;
168 		RootInsert ri = new RootInsert(ds);
169 		ri.insert(r);
170 	}
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 		rootsCache = null;
177 		RootDelete rd = new RootDelete(ds);
178 		rd.delete(r);
179 	}
180 
181 	/**
182 	 * <code>Root</code> Query object.
183 	 */
184 	class RootsQuery extends MappingSqlQuery {
185 
186 		RootsQuery(DataSource ds) {
187             super(ds, "SELECT * from roots");
188             compile();
189         }
190  
191         protected Object mapRow(ResultSet rs, int rowNum) throws SQLException {
192         	Root r = new Root();
193         	r.setRid(rs.getInt("rid"));
194         	r.setName(rs.getString("name"));
195         	r.setPath(rs.getString("path"));
196         	r.setFilePrefix(rs.getString("filePrefix"));
197         	r.setActive(rs.getBoolean("active"));
198         	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 		protected RootUpdate(DataSource ds) {
213 			this.ds = ds;
214 		}
215 
216 		protected RootUpdate(DataSource ds, Root root) {
217 			super(ds, "UPDATE roots SET name=?, path=?, filePrefix=?, active=? WHERE rid = ? LIMIT 1");
218 			declareParameter(new SqlParameter(Types.VARCHAR));
219 			declareParameter(new SqlParameter(Types.VARCHAR));
220 			declareParameter(new SqlParameter(Types.VARCHAR));
221 			declareParameter(new SqlParameter(Types.TINYINT));
222 			declareParameter(new SqlParameter(Types.INTEGER));
223 			compile();
224 		}
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 			SqlUpdate sf = new SqlUpdate(ds, "TRUNCATE TABLE roots");
233 			sf.compile();
234 			int ret = sf.update();
235 			logger.info("sf.empty() returned" + ret);
236 
237 			// Save each of our attributes
238 			RootInsert ri = new RootInsert(ds);
239 			Iterator<Root> it = roots.iterator();
240 			while (it.hasNext())
241 				ri.insert(it.next());
242 			return(0);
243 		}
244 
245 		protected int update(Root root) {
246 			return this.update(new Object[] {
247 					root.getName(),
248 					root.getPath(),
249 					root.getFilePrefix(),
250 					new Integer(root.isActive() ? 1 : 0),
251 					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 		protected RootInsert(DataSource ds) {
267 			super(ds, "INSERT INTO roots VALUES(?,?,?,?,?)");
268 			declareParameter(new SqlParameter(Types.INTEGER));
269 			declareParameter(new SqlParameter(Types.TINYINT));
270 			declareParameter(new SqlParameter(Types.VARCHAR));
271 			declareParameter(new SqlParameter(Types.VARCHAR));
272 			declareParameter(new SqlParameter(Types.VARCHAR));
273 			compile();
274 		}
275 
276 		protected void insert(Root r) {
277 			Object[] objs = new Object[] {
278 				new Integer(r.getRid()),
279 				new Integer(r.isActive() ? 1 : 0),
280 				r.getName(), r.getPath(), r.getFilePrefix(),
281 				};
282 			super.update(objs);
283 		}
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 		protected RootDelete(DataSource ds) {
295 			super(ds, "DELETE FROM rots WHERE name = (?)");
296 			declareParameter(new SqlParameter(Types.VARCHAR));
297 			compile();
298 		}
299 		
300 		protected void delete(Root root) {
301 			Object[] objs = new Object[] {
302 					root.getName()};
303 			super.update(objs);
304 		}
305 	}
306 }