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.Iterator;
14 import java.util.List;
15
16 import javax.sql.DataSource;
17
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
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.Mosaic;
26 import com.buckosoft.PicMan.domain.MosaicBatch;
27
28
29
30
31
32
33 public class MosaicBatchesDaoJdbc implements MosaicBatchesDao {
34 private static final boolean DEBUG = true;
35 protected final Log logger = LogFactory.getLog(getClass());
36
37 private DataSource ds;
38 private MosaicsDao mosaicsDao;
39
40
41
42
43
44 public void setDataSource(DataSource ds) {
45 this.ds = ds;
46 }
47
48
49
50
51 public void setMosaicsDao(MosaicsDao mosaicsDao) {
52 this.mosaicsDao = mosaicsDao;
53 }
54
55
56
57 @SuppressWarnings("unchecked")
58 public List<MosaicBatch> getMosaicBatches() {
59 if (DEBUG)
60 logger.info("GetMosaicBatchesList");
61 if (sql_getMosaicBatchesListQUERY == null)
62 sql_getMosaicBatchesListQUERY = new MosaicBatchesQuery(ds);
63 List<MosaicBatch> list = sql_getMosaicBatchesListQUERY.execute();
64 Iterator<MosaicBatch> iter = list.iterator();
65 while (iter.hasNext()) {
66 MosaicBatch mb = iter.next();
67 fillMosaic(mb);
68 }
69 return(list);
70 }
71 private MosaicBatchesQuery sql_getMosaicBatchesListQUERY = null;
72
73
74
75
76 public MosaicBatch getMosaicBatch(int mbid) {
77 if (DEBUG)
78 logger.info("getMosaicBatch: " + mbid);
79 if (sql_getMosaicBatchQUERY == null)
80 sql_getMosaicBatchQUERY = new MosaicBatchesQuery(ds, mbid);
81 MosaicBatch mb = (MosaicBatch)sql_getMosaicBatchQUERY.findObject(mbid);
82 if (mb == null)
83 return(null);
84 return(fillMosaic(mb));
85 }
86 private MosaicBatchesQuery sql_getMosaicBatchQUERY = null;
87
88 private MosaicBatch fillMosaic(MosaicBatch mb) {
89 Mosaic m = this.mosaicsDao.getMosaic(mb.getMid());
90 mb.setBatch(true);
91 mb.setName(m.getName());
92 mb.setEngine(m.getEngine());
93 mb.setMasterPic(m.getMasterPic());
94 mb.setOutPic(m.getOutPic());
95 mb.setSid(m.getSid());
96 mb.setStartTime(m.getStartTime());
97 mb.setEngineConfig(m.getEngineConfig());
98 return(mb);
99 }
100
101
102
103 public void storeMosaicBatch(MosaicBatch mbatch) {
104 if (DEBUG)
105 logger.info("storeMosaic " + mbatch.getMbid());
106 this.mosaicsDao.storeMosaic(mbatch);
107 assert(mbatch.getMid() != 0);
108 if (mbatch.getMbid() <= 0) {
109 MosaicBatchesInsert mbi = new MosaicBatchesInsert(ds);
110 mbi.insert(mbatch);
111 } else {
112 MosaicBatchesUpdate mbu = new MosaicBatchesUpdate(ds);
113 mbu.update(mbatch);
114 }
115 }
116
117
118
119 public void deleteMosaicBatch(int mbid) {
120
121
122 }
123
124
125
126
127 private class MosaicBatchesQuery extends MappingSqlQuery {
128
129 MosaicBatchesQuery(DataSource ds) {
130 super(ds, "SELECT * from mosaicBatches");
131 compile();
132 }
133
134 MosaicBatchesQuery(DataSource ds, int mbid) {
135 super(ds, "SELECT * from mosaicBatches where mbid = ?");
136 declareParameter(new SqlParameter(Types.INTEGER));
137 compile();
138 }
139
140 protected Object mapRow(ResultSet rs, int rowNum) throws SQLException {
141 MosaicBatch mb = new MosaicBatch();
142 mb.setMbid(rs.getInt("mbid"));
143 mb.setMid(rs.getInt("mid"));
144 mb.setSizes(rs.getString("sizes"));
145 return(mb);
146 }
147 }
148
149
150
151 class MosaicBatchesCount extends SqlFunction {
152
153 MosaicBatchesCount(DataSource ds) {
154 super(ds, "SELECT COUNT(*) from mosaicBatches");
155 compile();
156 }
157 }
158
159
160
161
162 private class MosaicBatchesInsert extends SqlUpdate {
163
164
165
166
167
168 protected MosaicBatchesInsert(DataSource ds) {
169 super(ds, "INSERT INTO mosaicBatches VALUES(?,?,?)");
170 declareParameter(new SqlParameter(Types.INTEGER));
171 declareParameter(new SqlParameter(Types.INTEGER));
172 declareParameter(new SqlParameter(Types.VARCHAR));
173 compile();
174 }
175
176 protected void insert(MosaicBatch mb) {
177 Object[] objs = new Object[] {
178 null,
179 new Integer(mb.getMid()),
180 mb.getSizes()
181 };
182 super.update(objs);
183 }
184 }
185
186
187
188
189 private class MosaicBatchesUpdate extends SqlUpdate {
190
191
192
193
194
195
196 protected MosaicBatchesUpdate(DataSource ds) {
197 super(ds, "UPDATE mosaicBatches SET mid=?, sizes=? WHERE mbid = ? LIMIT 1");
198 declareParameter(new SqlParameter(Types.INTEGER));
199 declareParameter(new SqlParameter(Types.VARCHAR));
200 declareParameter(new SqlParameter(Types.INTEGER));
201 compile();
202 }
203
204
205
206
207
208
209 protected int update(MosaicBatch mb) {
210 return this.update(new Object[] {
211 new Integer(mb.getMid()),
212 mb.getSizes(),
213 new Integer(mb.getMbid())
214 });
215 }
216 }
217 }