| 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.dao.DataAccessException; |
| 21 | |
import org.springframework.jdbc.core.SqlParameter; |
| 22 | |
import org.springframework.jdbc.object.MappingSqlQuery; |
| 23 | |
import org.springframework.jdbc.object.SqlFunction; |
| 24 | |
import org.springframework.jdbc.object.SqlUpdate; |
| 25 | |
|
| 26 | |
import com.buckosoft.PicMan.domain.Chain; |
| 27 | |
import com.buckosoft.PicMan.domain.SetSize; |
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | 0 | public class ChainsDaoJdbc implements ChainsDao { |
| 36 | |
|
| 37 | |
private static final boolean DEBUG = false; |
| 38 | 0 | protected final Log logger = LogFactory.getLog(getClass()); |
| 39 | |
|
| 40 | |
private DataSource ds; |
| 41 | |
|
| 42 | |
private List<Chain> chainsCache; |
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
public void setDataSource(DataSource ds) { |
| 48 | |
if (DEBUG) |
| 49 | |
logger.info("Set Datasource."); |
| 50 | 0 | this.ds = ds; |
| 51 | 0 | } |
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
public List<Chain> getChains() throws DataAccessException { |
| 57 | 0 | if (chainsCache != null) |
| 58 | 0 | return(chainsCache); |
| 59 | |
if (DEBUG) |
| 60 | |
logger.info("getChains()"); |
| 61 | 0 | ChainsQuery sq = new ChainsQuery(ds); |
| 62 | |
@SuppressWarnings("unchecked") |
| 63 | 0 | List<Chain> l = sq.execute(); |
| 64 | 0 | chainsCache = l; |
| 65 | 0 | return(l); |
| 66 | |
} |
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
public Chain getChain(String name) throws DataAccessException { |
| 72 | |
if (DEBUG) |
| 73 | |
logger.info("getChain: " + name); |
| 74 | 0 | if (sql_getChainQUERY == null) |
| 75 | 0 | sql_getChainQUERY = new ChainsQuery(ds, name); |
| 76 | 0 | return (Chain)sql_getChainQUERY.findObject(name); |
| 77 | |
|
| 78 | |
} |
| 79 | 0 | private ChainsQuery sql_getChainQUERY = null; |
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
public Chain getChain(int cid) throws DataAccessException { |
| 85 | |
if (DEBUG) |
| 86 | |
logger.info("getChain: " + cid); |
| 87 | 0 | if (chainsCache != null) { |
| 88 | 0 | for (Chain c : chainsCache) { |
| 89 | 0 | if (c.getCid() == cid) |
| 90 | 0 | return(c); |
| 91 | 0 | } |
| 92 | |
} |
| 93 | 0 | if (sql_getChainCidQUERY == null) |
| 94 | 0 | sql_getChainCidQUERY = new ChainsQuery(ds, cid); |
| 95 | 0 | return (Chain)sql_getChainCidQUERY.findObject(cid); |
| 96 | |
|
| 97 | |
} |
| 98 | 0 | private ChainsQuery sql_getChainCidQUERY = null; |
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
private void addChain(Chain c) throws DataAccessException { |
| 104 | 0 | c.setCid(getChainCount()); |
| 105 | 0 | if (sql_addChainINSERT == null) |
| 106 | 0 | sql_addChainINSERT = new ChainInsert(ds); |
| 107 | 0 | chainsCache = null; |
| 108 | 0 | sql_addChainINSERT.insert(c); |
| 109 | 0 | storeChainSetSizes(c); |
| 110 | 0 | } |
| 111 | 0 | private ChainInsert sql_addChainINSERT = null; |
| 112 | |
|
| 113 | |
private void updateChain(Chain c) throws DataAccessException { |
| 114 | 0 | ChainUpdate cu = new ChainUpdate(ds); |
| 115 | 0 | chainsCache = null; |
| 116 | 0 | cu.update(c); |
| 117 | 0 | storeChainSetSizes(c); |
| 118 | |
|
| 119 | 0 | } |
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
public void storeChain(Chain chain) throws DataAccessException { |
| 125 | 0 | if (chain.getCid() == -1) |
| 126 | 0 | addChain(chain); |
| 127 | |
else |
| 128 | 0 | updateChain(chain); |
| 129 | 0 | } |
| 130 | |
|
| 131 | |
private void storeChainSetSizes(Chain c) throws DataAccessException { |
| 132 | 0 | ChainSetSizesDelete cssd = new ChainSetSizesDelete(ds); |
| 133 | 0 | cssd.delete(c); |
| 134 | 0 | Iterator<SetSize> iter = c.getSetSizes().iterator(); |
| 135 | 0 | while (iter.hasNext()) { |
| 136 | 0 | SetSize ss = (SetSize)iter.next(); |
| 137 | 0 | if (sql_storeChainSetSizesINSERT == null) |
| 138 | 0 | sql_storeChainSetSizesINSERT = new ChainSetSizesInsert(ds); |
| 139 | 0 | sql_storeChainSetSizesINSERT.insert(c.getCid(), ss.getGuiSetSize()); |
| 140 | 0 | } |
| 141 | 0 | } |
| 142 | 0 | private ChainSetSizesInsert sql_storeChainSetSizesINSERT = null; |
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
public void deleteChain(Chain chain) throws DataAccessException { |
| 148 | 0 | ChainDelete sd = new ChainDelete(ds); |
| 149 | 0 | sd.delete(chain); |
| 150 | 0 | ChainSetSizesDelete cssd = new ChainSetSizesDelete(ds); |
| 151 | 0 | cssd.delete(chain); |
| 152 | 0 | chainsCache = null; |
| 153 | 0 | } |
| 154 | |
|
| 155 | |
private void getChainSetSizes(Chain c) throws DataAccessException { |
| 156 | 0 | if (sql_getChainSetSizesQUERY == null) |
| 157 | 0 | sql_getChainSetSizesQUERY = new ChainSetSizesQuery(ds); |
| 158 | 0 | List<?> l = sql_getChainSetSizesQUERY.execute(c.getCid()); |
| 159 | 0 | if (l.isEmpty()) |
| 160 | 0 | return; |
| 161 | 0 | Iterator<?> iter = l.iterator(); |
| 162 | 0 | while (iter.hasNext()) { |
| 163 | 0 | SetSize ss = (SetSize)iter.next(); |
| 164 | 0 | c.addSetSize(ss); |
| 165 | 0 | } |
| 166 | 0 | } |
| 167 | |
private ChainSetSizesQuery sql_getChainSetSizesQUERY; |
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
public int getChainCount() { |
| 173 | 0 | if (sql_getChainCountCOUNTr == null) |
| 174 | 0 | sql_getChainCountCOUNTr = new ChainCount(ds); |
| 175 | |
if (DEBUG) |
| 176 | |
logger.info("getChainCount:"); |
| 177 | 0 | return(sql_getChainCountCOUNTr.run()); |
| 178 | |
} |
| 179 | |
private ChainCount sql_getChainCountCOUNTr; |
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
|
| 185 | |
class ChainsQuery extends MappingSqlQuery { |
| 186 | |
|
| 187 | 0 | ChainsQuery(DataSource ds) { |
| 188 | 0 | super(ds, "SELECT * from chains"); |
| 189 | 0 | compile(); |
| 190 | 0 | } |
| 191 | 0 | ChainsQuery(DataSource ds, String name) { |
| 192 | 0 | super(ds, "SELECT * from chains where name = ?"); |
| 193 | 0 | declareParameter(new SqlParameter(Types.VARCHAR)); |
| 194 | 0 | compile(); |
| 195 | 0 | } |
| 196 | |
|
| 197 | 0 | ChainsQuery(DataSource ds, int cid) { |
| 198 | 0 | super(ds, "SELECT * from chains where cid = ?"); |
| 199 | 0 | declareParameter(new SqlParameter(Types.INTEGER)); |
| 200 | 0 | compile(); |
| 201 | 0 | } |
| 202 | |
|
| 203 | |
protected Object mapRow(ResultSet rs, int rowNum) throws SQLException { |
| 204 | 0 | Chain c = new Chain(); |
| 205 | 0 | c.setCid(rs.getInt("cid")); |
| 206 | 0 | c.setName(rs.getString("name")); |
| 207 | 0 | c.setShortName(rs.getString("shortName")); |
| 208 | 0 | c.setDescription(rs.getString("description")); |
| 209 | 0 | c.setActive(rs.getBoolean("active")); |
| 210 | 0 | c.setEngine(rs.getString("engine")); |
| 211 | 0 | c.setContactPrefix(rs.getString("contactPrefix")); |
| 212 | 0 | c.setContactDirectory(rs.getString("contactDirectory")); |
| 213 | 0 | c.setContactCount(rs.getInt("contactCount")); |
| 214 | 0 | c.setContactWidth(rs.getInt("contactWidth")); |
| 215 | 0 | c.setContactHeight(rs.getInt("contactHeight")); |
| 216 | 0 | c.setPicSelector(rs.getInt("picSelector")); |
| 217 | 0 | c.setRatingMin(rs.getInt("ratingMin")); |
| 218 | 0 | c.setRatingMax(rs.getInt("ratingMax")); |
| 219 | 0 | c.setShowUnusedSets(rs.getBoolean("showUnused")); |
| 220 | |
|
| 221 | 0 | getChainSetSizes(c); |
| 222 | 0 | return(c); |
| 223 | |
} |
| 224 | |
} |
| 225 | |
|
| 226 | |
|
| 227 | |
|
| 228 | |
class ChainCount extends SqlFunction { |
| 229 | |
|
| 230 | 0 | ChainCount(DataSource ds) { |
| 231 | 0 | super(ds, "SELECT COUNT(*) from chains"); |
| 232 | 0 | compile(); |
| 233 | 0 | } |
| 234 | |
} |
| 235 | |
|
| 236 | |
|
| 237 | |
|
| 238 | |
|
| 239 | |
|
| 240 | |
protected class ChainUpdate extends SqlUpdate { |
| 241 | |
|
| 242 | |
|
| 243 | |
|
| 244 | |
|
| 245 | |
|
| 246 | 0 | protected ChainUpdate(DataSource ds) { |
| 247 | 0 | super(ds, "UPDATE chains SET name=?, shortName=?, description=?, active=?, engine=?, " |
| 248 | |
+ "contactPrefix=?, contactDirectory=?, contactCount=?, contactWidth=?, contactHeight=?, " |
| 249 | |
+ "picSelector=?, ratingMin=?, ratingMax=?, showUnused=? WHERE cid = ? LIMIT 1"); |
| 250 | 0 | declareParameter(new SqlParameter(Types.VARCHAR)); |
| 251 | 0 | declareParameter(new SqlParameter(Types.VARCHAR)); |
| 252 | 0 | declareParameter(new SqlParameter(Types.VARCHAR)); |
| 253 | 0 | declareParameter(new SqlParameter(Types.TINYINT)); |
| 254 | 0 | declareParameter(new SqlParameter(Types.VARCHAR)); |
| 255 | 0 | declareParameter(new SqlParameter(Types.VARCHAR)); |
| 256 | 0 | declareParameter(new SqlParameter(Types.VARCHAR)); |
| 257 | 0 | declareParameter(new SqlParameter(Types.INTEGER)); |
| 258 | 0 | declareParameter(new SqlParameter(Types.INTEGER)); |
| 259 | 0 | declareParameter(new SqlParameter(Types.INTEGER)); |
| 260 | 0 | declareParameter(new SqlParameter(Types.TINYINT)); |
| 261 | 0 | declareParameter(new SqlParameter(Types.TINYINT)); |
| 262 | 0 | declareParameter(new SqlParameter(Types.TINYINT)); |
| 263 | 0 | declareParameter(new SqlParameter(Types.TINYINT)); |
| 264 | 0 | declareParameter(new SqlParameter(Types.INTEGER)); |
| 265 | 0 | compile(); |
| 266 | 0 | } |
| 267 | |
|
| 268 | |
|
| 269 | |
|
| 270 | |
|
| 271 | |
|
| 272 | |
|
| 273 | |
protected int update(Chain c) { |
| 274 | 0 | return this.update(new Object[] { |
| 275 | 0 | c.getName(), c.getShortName(), |
| 276 | 0 | c.getDescription(), new Integer(c.isActive() ? 1 : 0), |
| 277 | 0 | c.getEngine(), c.getContactPrefix(), |
| 278 | 0 | c.getContactDirectory(), new Integer(c.getContactCount()), |
| 279 | 0 | new Integer(c.getContactWidth()), new Integer(c.getContactHeight()), |
| 280 | 0 | new Integer(c.getPicSelector()), new Integer(c.getRatingMin()), new Integer(c.getRatingMax()), |
| 281 | 0 | new Integer(c.isShowUnusedSets() ? 1 : 0), |
| 282 | 0 | new Integer(c.getCid()), |
| 283 | |
|
| 284 | |
}); |
| 285 | |
} |
| 286 | |
} |
| 287 | |
|
| 288 | |
|
| 289 | |
|
| 290 | |
|
| 291 | |
protected class ChainInsert extends SqlUpdate { |
| 292 | |
|
| 293 | |
|
| 294 | |
|
| 295 | |
|
| 296 | |
|
| 297 | 0 | protected ChainInsert(DataSource ds) { |
| 298 | 0 | super(ds, "INSERT INTO chains VALUES(?,?,?, ?,?,?, ?,?,?, ?,?,?, ?,?,?)"); |
| 299 | 0 | declareParameter(new SqlParameter(Types.INTEGER)); |
| 300 | 0 | declareParameter(new SqlParameter(Types.VARCHAR)); |
| 301 | 0 | declareParameter(new SqlParameter(Types.VARCHAR)); |
| 302 | 0 | declareParameter(new SqlParameter(Types.VARCHAR)); |
| 303 | 0 | declareParameter(new SqlParameter(Types.TINYINT)); |
| 304 | 0 | declareParameter(new SqlParameter(Types.VARCHAR)); |
| 305 | 0 | declareParameter(new SqlParameter(Types.VARCHAR)); |
| 306 | 0 | declareParameter(new SqlParameter(Types.VARCHAR)); |
| 307 | 0 | declareParameter(new SqlParameter(Types.INTEGER)); |
| 308 | 0 | declareParameter(new SqlParameter(Types.INTEGER)); |
| 309 | 0 | declareParameter(new SqlParameter(Types.INTEGER)); |
| 310 | 0 | declareParameter(new SqlParameter(Types.TINYINT)); |
| 311 | 0 | declareParameter(new SqlParameter(Types.TINYINT)); |
| 312 | 0 | declareParameter(new SqlParameter(Types.TINYINT)); |
| 313 | 0 | declareParameter(new SqlParameter(Types.TINYINT)); |
| 314 | 0 | compile(); |
| 315 | 0 | } |
| 316 | |
|
| 317 | |
protected void insert(Chain c) { |
| 318 | 0 | Object[] objs = new Object[] { |
| 319 | 0 | new Integer(c.getCid()), |
| 320 | 0 | c.getName(), c.getShortName(), |
| 321 | 0 | c.getDescription(), new Integer(c.isActive() ? 1 : 0), |
| 322 | 0 | c.getEngine(), c.getContactPrefix(), |
| 323 | 0 | c.getContactDirectory(), new Integer(c.getContactCount()), |
| 324 | 0 | new Integer(c.getContactWidth()), new Integer(c.getContactHeight()), |
| 325 | 0 | new Integer(c.getPicSelector()), new Integer(c.getRatingMin()), new Integer(c.getRatingMax()), |
| 326 | 0 | new Integer(c.isShowUnusedSets() ? 1 : 0), |
| 327 | |
}; |
| 328 | 0 | super.update(objs); |
| 329 | 0 | } |
| 330 | |
} |
| 331 | |
|
| 332 | |
|
| 333 | |
|
| 334 | |
|
| 335 | |
protected class ChainDelete extends SqlUpdate { |
| 336 | |
|
| 337 | |
|
| 338 | |
|
| 339 | |
|
| 340 | |
|
| 341 | 0 | protected ChainDelete(DataSource ds) { |
| 342 | 0 | super(ds, "DELETE FROM chains WHERE cid = (?) LIMIT 1"); |
| 343 | 0 | declareParameter(new SqlParameter(Types.INTEGER)); |
| 344 | 0 | compile(); |
| 345 | 0 | } |
| 346 | |
|
| 347 | |
protected void delete(Chain c) { |
| 348 | 0 | Object[] objs = new Object[] { |
| 349 | 0 | new Integer(c.getCid())}; |
| 350 | 0 | super.update(objs); |
| 351 | 0 | } |
| 352 | |
} |
| 353 | |
|
| 354 | |
|
| 355 | |
|
| 356 | |
class ChainSetSizesQuery extends MappingSqlQuery { |
| 357 | |
|
| 358 | 0 | ChainSetSizesQuery(DataSource ds) { |
| 359 | 0 | super(ds, "SELECT * FROM chainsSetSizes WHERE cid = ?"); |
| 360 | 0 | declareParameter(new SqlParameter(Types.INTEGER)); |
| 361 | 0 | compile(); |
| 362 | 0 | } |
| 363 | |
|
| 364 | |
protected Object mapRow(ResultSet rs, int rowNum) throws SQLException { |
| 365 | 0 | SetSize ss = new SetSize(); |
| 366 | 0 | ss.setSetSize(rs.getString("setsSize")); |
| 367 | 0 | return(ss); |
| 368 | |
} |
| 369 | |
} |
| 370 | |
|
| 371 | |
|
| 372 | |
|
| 373 | |
|
| 374 | |
protected class ChainSetSizesInsert extends SqlUpdate { |
| 375 | |
|
| 376 | |
|
| 377 | |
|
| 378 | |
|
| 379 | |
|
| 380 | 0 | protected ChainSetSizesInsert(DataSource ds) { |
| 381 | 0 | super(ds, "INSERT INTO chainsSetSizes VALUES(?,?)"); |
| 382 | 0 | declareParameter(new SqlParameter(Types.INTEGER)); |
| 383 | 0 | declareParameter(new SqlParameter(Types.VARCHAR)); |
| 384 | 0 | compile(); |
| 385 | 0 | } |
| 386 | |
|
| 387 | |
protected void insert(int cid, String setSizeName) { |
| 388 | 0 | Object[] objs = new Object[] { |
| 389 | |
new Integer(cid), setSizeName, |
| 390 | |
}; |
| 391 | 0 | super.update(objs); |
| 392 | 0 | } |
| 393 | |
} |
| 394 | |
|
| 395 | |
|
| 396 | |
|
| 397 | |
|
| 398 | |
protected class ChainSetSizesDelete extends SqlUpdate { |
| 399 | |
|
| 400 | |
|
| 401 | |
|
| 402 | |
|
| 403 | |
|
| 404 | 0 | protected ChainSetSizesDelete(DataSource ds) { |
| 405 | 0 | super(ds, "DELETE FROM chainsSetSizes WHERE cid = (?)"); |
| 406 | 0 | declareParameter(new SqlParameter(Types.INTEGER)); |
| 407 | 0 | compile(); |
| 408 | 0 | } |
| 409 | |
|
| 410 | |
protected void delete(Chain c) { |
| 411 | 0 | Object[] objs = new Object[] { |
| 412 | 0 | new Integer(c.getCid())}; |
| 413 | 0 | super.update(objs); |
| 414 | 0 | } |
| 415 | |
} |
| 416 | |
|
| 417 | |
} |