| 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.Date; |
| 14 | |
import java.util.Iterator; |
| 15 | |
import java.util.List; |
| 16 | |
|
| 17 | |
import javax.sql.DataSource; |
| 18 | |
|
| 19 | |
import org.apache.commons.logging.Log; |
| 20 | |
import org.apache.commons.logging.LogFactory; |
| 21 | |
import org.springframework.dao.DataAccessException; |
| 22 | |
import org.springframework.jdbc.core.SqlParameter; |
| 23 | |
import org.springframework.jdbc.object.MappingSqlQuery; |
| 24 | |
import org.springframework.jdbc.object.SqlUpdate; |
| 25 | |
|
| 26 | |
import com.buckosoft.PicMan.domain.Set; |
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | 0 | public class SetsDaoJdbc implements SetsDao { |
| 35 | |
|
| 36 | |
|
| 37 | |
private static final boolean DEBUG = false; |
| 38 | 0 | protected final Log logger = LogFactory.getLog(getClass()); |
| 39 | |
|
| 40 | |
private DataSource ds; |
| 41 | |
|
| 42 | 0 | private List<Set> setsCache = null; |
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
public void setDataSource(DataSource ds) { |
| 49 | |
if (DEBUG) |
| 50 | |
logger.info("Set Datasource."); |
| 51 | 0 | this.ds = ds; |
| 52 | 0 | } |
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
public List<Set> getSets() throws DataAccessException { |
| 58 | 0 | if (setsCache != null) |
| 59 | 0 | return(setsCache); |
| 60 | |
if (DEBUG) |
| 61 | |
logger.info("getSets()"); |
| 62 | 0 | SetsQuery sq = new SetsQuery(ds); |
| 63 | |
@SuppressWarnings("unchecked") |
| 64 | 0 | List<Set> l = sq.execute(); |
| 65 | 0 | l.add(0, new Set(Set.FUNC_SID_DATE, Set.FUNC_NAME_DATE)); |
| 66 | 0 | l.add(0, new Set(Set.FUNC_SID_ROOT, Set.FUNC_NAME_ROOT)); |
| 67 | 0 | setSetsCache(l); |
| 68 | 0 | return(l); |
| 69 | |
} |
| 70 | |
|
| 71 | |
private synchronized void setSetsCache(List<Set> list) { |
| 72 | 0 | this.setsCache = list; |
| 73 | 0 | } |
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
public List<Set> getSetsClone() { |
| 79 | 0 | SetsQuery sq = new SetsQuery(ds); |
| 80 | |
@SuppressWarnings("unchecked") |
| 81 | 0 | List<Set> l = sq.execute(); |
| 82 | 0 | l.add(0, new Set(Set.FUNC_SID_DATE, Set.FUNC_NAME_DATE)); |
| 83 | 0 | l.add(0, new Set(Set.FUNC_SID_ROOT, Set.FUNC_NAME_ROOT)); |
| 84 | 0 | return(l); |
| 85 | |
} |
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
public List<Set> getInactiveSets() throws DataAccessException { |
| 91 | 0 | SetsQuery sq = new SetsQuery(ds); |
| 92 | |
@SuppressWarnings("unchecked") |
| 93 | 0 | List<Set> inactiveSets = sq.execute(); |
| 94 | 0 | Iterator<Set> siter = inactiveSets.iterator(); |
| 95 | 0 | while (siter.hasNext()) { |
| 96 | 0 | Set set = siter.next(); |
| 97 | 0 | if (set.isActive()) |
| 98 | 0 | siter.remove(); |
| 99 | 0 | } |
| 100 | 0 | return(inactiveSets); |
| 101 | |
} |
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
public List<Set> getActiveSets() throws DataAccessException { |
| 107 | 0 | SetsQuery sq = new SetsQuery(ds); |
| 108 | |
@SuppressWarnings("unchecked") |
| 109 | 0 | List<Set> activeSets = sq.execute(); |
| 110 | 0 | Iterator<Set> siter = activeSets.iterator(); |
| 111 | 0 | while (siter.hasNext()) { |
| 112 | 0 | Set set = siter.next(); |
| 113 | 0 | if (!set.isActive()) |
| 114 | 0 | siter.remove(); |
| 115 | 0 | } |
| 116 | 0 | return(activeSets); |
| 117 | |
} |
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
public int getSetCount() throws DataAccessException { |
| 123 | 0 | if (setsCache == null) |
| 124 | 0 | getSets(); |
| 125 | 0 | return(setsCache.size()); |
| 126 | |
} |
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
public Set getSet(int sid) throws DataAccessException { |
| 132 | 0 | if (setsCache == null) |
| 133 | 0 | getSets(); |
| 134 | 0 | Iterator<Set> iter = setsCache.iterator(); |
| 135 | 0 | while (iter.hasNext()) { |
| 136 | 0 | Set set = iter.next(); |
| 137 | 0 | if (set.getSid() == sid) |
| 138 | 0 | return(set); |
| 139 | 0 | } |
| 140 | 0 | return(null); |
| 141 | |
} |
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
public Set getSet(String setName) throws DataAccessException { |
| 147 | 0 | if (setsCache == null) |
| 148 | 0 | getSets(); |
| 149 | 0 | Iterator<Set> iter = setsCache.iterator(); |
| 150 | 0 | while (iter.hasNext()) { |
| 151 | 0 | Set set = iter.next(); |
| 152 | 0 | if (set.getName().equals(setName)) |
| 153 | 0 | return(set); |
| 154 | 0 | } |
| 155 | 0 | return(null); |
| 156 | |
} |
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
public void storeSet(Set set) throws DataAccessException { |
| 161 | 0 | if (set.getSid() <= 0) |
| 162 | 0 | return; |
| 163 | 0 | setSetsCache(null); |
| 164 | 0 | SetsUpdate su = new SetsUpdate(ds, set); |
| 165 | 0 | su.update(set); |
| 166 | |
|
| 167 | |
|
| 168 | 0 | } |
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
public void setSets(List<Set> sets) throws DataAccessException { |
| 174 | 0 | setSetsCache(null); |
| 175 | 0 | SetsUpdate su = new SetsUpdate(ds); |
| 176 | 0 | su.update(sets); |
| 177 | 0 | } |
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
public void addSet(Set set) throws DataAccessException { |
| 183 | 0 | set.setSid(getNextAvailableSetNumber()); |
| 184 | 0 | setSetsCache(null); |
| 185 | 0 | SetInsert si = new SetInsert(ds); |
| 186 | 0 | si.insert(set); |
| 187 | 0 | } |
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
|
| 192 | |
public void deleteSet(Set s) throws DataAccessException { |
| 193 | 0 | setSetsCache(null); |
| 194 | 0 | SetDelete sd = new SetDelete(ds); |
| 195 | 0 | sd.delete(s); |
| 196 | 0 | } |
| 197 | |
|
| 198 | |
private int getNextAvailableSetNumber() { |
| 199 | 0 | int high = 1; |
| 200 | 0 | for (Set set : getSets()) { |
| 201 | 0 | if (set.getSid() > high) |
| 202 | 0 | high = set.getSid(); |
| 203 | 0 | } |
| 204 | 0 | return(high+1); |
| 205 | |
} |
| 206 | |
|
| 207 | |
|
| 208 | |
|
| 209 | |
|
| 210 | |
class SetsQuery extends MappingSqlQuery { |
| 211 | |
|
| 212 | 0 | SetsQuery(DataSource ds) { |
| 213 | 0 | super(ds, "SELECT * from sets ORDER BY name"); |
| 214 | 0 | compile(); |
| 215 | 0 | } |
| 216 | |
|
| 217 | |
protected Object mapRow(ResultSet rs, int rowNum) throws SQLException { |
| 218 | 0 | Set s = new Set(); |
| 219 | 0 | s.setSid(rs.getInt("sid")); |
| 220 | 0 | s.setName(rs.getString("name")); |
| 221 | 0 | s.setDescription(rs.getString("description")); |
| 222 | 0 | s.setActive(rs.getBoolean("active")); |
| 223 | 0 | s.setMetaSet(rs.getBoolean("metaSet")); |
| 224 | 0 | s.setMicroSet(rs.getBoolean("microSet")); |
| 225 | 0 | s.setNanoSet(rs.getBoolean("nanoSet")); |
| 226 | |
try { |
| 227 | 0 | s.getEditDate().setTime(rs.getTimestamp("editDate").getTime()); |
| 228 | 0 | } catch (java.sql.SQLException e) { |
| 229 | 0 | s.setEditDate(new Date(0)); |
| 230 | 0 | } |
| 231 | 0 | return(s); |
| 232 | |
} |
| 233 | |
} |
| 234 | |
|
| 235 | |
|
| 236 | |
|
| 237 | |
|
| 238 | |
protected class SetsUpdate extends SqlUpdate { |
| 239 | |
private DataSource ds; |
| 240 | |
|
| 241 | |
|
| 242 | |
|
| 243 | |
|
| 244 | |
|
| 245 | 0 | protected SetsUpdate(DataSource ds) { |
| 246 | 0 | this.ds = ds; |
| 247 | 0 | } |
| 248 | |
|
| 249 | 0 | protected SetsUpdate(DataSource ds, Set set) { |
| 250 | 0 | super(ds, "UPDATE sets SET name=?, description=?, active=?, metaSet=?, microSet=?, nanoSet=?, editDate=? WHERE sid = ? LIMIT 1"); |
| 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.TINYINT)); |
| 255 | 0 | declareParameter(new SqlParameter(Types.TINYINT)); |
| 256 | 0 | declareParameter(new SqlParameter(Types.TINYINT)); |
| 257 | 0 | declareParameter(new SqlParameter(Types.TIMESTAMP)); |
| 258 | 0 | declareParameter(new SqlParameter(Types.INTEGER)); |
| 259 | 0 | compile(); |
| 260 | 0 | } |
| 261 | |
|
| 262 | |
|
| 263 | |
|
| 264 | |
|
| 265 | |
|
| 266 | |
protected int update(List<Set> sets) { |
| 267 | |
|
| 268 | 0 | SqlUpdate sf = new SqlUpdate(ds, "TRUNCATE TABLE sets"); |
| 269 | 0 | sf.compile(); |
| 270 | 0 | int ret = sf.update(); |
| 271 | 0 | logger.info("sf.empty() returned" + ret); |
| 272 | |
|
| 273 | |
|
| 274 | 0 | SetInsert si = new SetInsert(ds); |
| 275 | 0 | Iterator<Set> it = sets.iterator(); |
| 276 | 0 | while (it.hasNext()) |
| 277 | 0 | si.insert(it.next()); |
| 278 | 0 | return(0); |
| 279 | |
} |
| 280 | |
|
| 281 | |
protected int update(Set set) { |
| 282 | 0 | return this.update(new Object[] { |
| 283 | 0 | set.getName(), |
| 284 | 0 | set.getDescription(), |
| 285 | 0 | new Integer(set.isActive() ? 1 : 0), |
| 286 | 0 | new Integer(set.isMetaSet() ? 1 : 0), |
| 287 | 0 | new Integer(set.isMicroSet() ? 1 : 0), |
| 288 | 0 | new Integer(set.isNanoSet() ? 1 : 0), |
| 289 | 0 | set.getEditDate(), |
| 290 | 0 | new Integer(set.getSid()), |
| 291 | |
}); |
| 292 | |
} |
| 293 | |
} |
| 294 | |
|
| 295 | |
|
| 296 | |
|
| 297 | |
|
| 298 | |
|
| 299 | |
protected class SetInsert extends SqlUpdate { |
| 300 | |
|
| 301 | |
|
| 302 | |
|
| 303 | |
|
| 304 | |
|
| 305 | 0 | protected SetInsert(DataSource ds) { |
| 306 | 0 | super(ds, "REPLACE INTO sets VALUES(?,?,?,?,?,?,?,?)"); |
| 307 | 0 | declareParameter(new SqlParameter(Types.INTEGER)); |
| 308 | 0 | declareParameter(new SqlParameter(Types.VARCHAR)); |
| 309 | 0 | declareParameter(new SqlParameter(Types.VARCHAR)); |
| 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 | declareParameter(new SqlParameter(Types.TIMESTAMP)); |
| 315 | 0 | compile(); |
| 316 | 0 | } |
| 317 | |
|
| 318 | |
protected void insert(Set s) { |
| 319 | 0 | Object[] objs = new Object[] { |
| 320 | 0 | new Integer(s.getSid()), |
| 321 | 0 | s.getName(), s.getDescription(), |
| 322 | 0 | new Integer(s.isActive() ? 1 : 0), |
| 323 | 0 | new Integer(s.isMetaSet() ? 1 : 0), |
| 324 | 0 | new Integer(s.isMicroSet() ? 1 : 0), |
| 325 | 0 | new Integer(s.isNanoSet() ? 1 : 0), |
| 326 | 0 | s.getEditDate(), |
| 327 | |
}; |
| 328 | 0 | super.update(objs); |
| 329 | 0 | } |
| 330 | |
} |
| 331 | |
|
| 332 | |
|
| 333 | |
|
| 334 | |
protected class SetDelete extends SqlUpdate { |
| 335 | |
|
| 336 | |
|
| 337 | |
|
| 338 | |
|
| 339 | |
|
| 340 | 0 | protected SetDelete(DataSource ds) { |
| 341 | 0 | super(ds, "DELETE FROM sets WHERE name = (?)"); |
| 342 | 0 | declareParameter(new SqlParameter(Types.VARCHAR)); |
| 343 | 0 | compile(); |
| 344 | 0 | } |
| 345 | |
|
| 346 | |
protected void delete(Set set) { |
| 347 | 0 | Object[] objs = new Object[] { |
| 348 | 0 | set.getName()}; |
| 349 | 0 | super.update(objs); |
| 350 | 0 | } |
| 351 | |
} |
| 352 | |
} |