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