1
2
3
4
5
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
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
32
33
34
35
36 public class RootsDaoJdbc implements RootsDao {
37
38
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
47
48
49
50 public void setDataSource(DataSource ds) {
51 if (DEBUG)
52 logger.info("Set Datasource.");
53 this.ds = ds;
54 }
55
56
57
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
72
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
88
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
104
105
106 public int getRootCount() throws DataAccessException {
107 if (rootsCache == null)
108 getRoots();
109 return(rootsCache.size());
110 }
111
112
113
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
128
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
143
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
155
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
164
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
173
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
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
204
205 protected class RootUpdate extends SqlUpdate {
206 private DataSource ds;
207
208
209
210
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
227
228
229
230 protected int update(List<Root> roots) {
231
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
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
259
260 protected class RootInsert extends SqlUpdate {
261
262
263
264
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
287
288 protected class RootDelete extends SqlUpdate {
289
290
291
292
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 }