1
2
3
4
5
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
28
29
30
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
39
40
41
42 public void setDataSource(DataSource ds) {
43 this.ds = ds;
44 }
45
46
47
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
61
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
77
78
79 public void deletePoster(int mid) {
80 if (DEBUG)
81 logger.info("deletePoster");
82
83 throw new RuntimeException("Not implemented");
84 }
85
86
87
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
111
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
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
166
167
168 return(p);
169 }
170 }
171
172
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
184
185 private class PostersInsert extends SqlUpdate {
186
187
188
189
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
213
214 private class PostersUpdate extends SqlUpdate {
215
216
217
218
219
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
231
232
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
267
268 protected class PosterConfigsDelete extends SqlUpdate {
269
270
271
272
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
286
287 protected class PosterConfigsInsert extends SqlUpdate {
288
289
290
291
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 }