| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
package com.buckosoft.PicMan.db; |
| 9 | |
|
| 10 | |
import java.util.List; |
| 11 | |
import java.util.Iterator; |
| 12 | |
|
| 13 | |
import javax.sql.DataSource; |
| 14 | |
|
| 15 | |
import java.sql.ResultSet; |
| 16 | |
import java.sql.SQLException; |
| 17 | |
import java.sql.Types; |
| 18 | |
|
| 19 | |
import org.apache.commons.logging.Log; |
| 20 | |
import org.apache.commons.logging.LogFactory; |
| 21 | |
|
| 22 | |
import org.springframework.dao.DataAccessException; |
| 23 | |
import org.springframework.jdbc.core.SqlParameter; |
| 24 | |
|
| 25 | |
import org.springframework.jdbc.object.SqlUpdate; |
| 26 | |
import org.springframework.jdbc.object.MappingSqlQuery; |
| 27 | |
|
| 28 | |
import com.buckosoft.PicMan.domain.Root; |
| 29 | |
import com.buckosoft.PicMan.db.RootsDao; |
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | 0 | public class RootsDaoJdbc implements RootsDao { |
| 37 | |
|
| 38 | |
|
| 39 | |
private static final boolean DEBUG = false; |
| 40 | 0 | protected final Log logger = LogFactory.getLog(getClass()); |
| 41 | |
|
| 42 | |
private DataSource ds; |
| 43 | |
|
| 44 | 0 | private List<Root> rootsCache = null; |
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
public void setDataSource(DataSource ds) { |
| 51 | |
if (DEBUG) |
| 52 | |
logger.info("Set Datasource."); |
| 53 | 0 | this.ds = ds; |
| 54 | 0 | } |
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
@SuppressWarnings("unchecked") |
| 60 | |
public List<Root> getRoots() throws DataAccessException { |
| 61 | 0 | if (rootsCache != null) |
| 62 | 0 | return(rootsCache); |
| 63 | |
if (DEBUG) |
| 64 | |
logger.info("getSets()"); |
| 65 | 0 | RootsQuery rq = new RootsQuery(ds); |
| 66 | 0 | List<Root> l = rq.execute(); |
| 67 | 0 | rootsCache = l; |
| 68 | 0 | return(l); |
| 69 | |
} |
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
public List<Root> getInactiveRoots() throws DataAccessException { |
| 75 | 0 | RootsQuery rq = new RootsQuery(ds); |
| 76 | |
@SuppressWarnings("unchecked") |
| 77 | 0 | List<Root> inactiveRoots = rq.execute(); |
| 78 | 0 | Iterator<Root> siter = inactiveRoots.iterator(); |
| 79 | 0 | while (siter.hasNext()) { |
| 80 | 0 | Root root = (Root)siter.next(); |
| 81 | 0 | if (root.isActive()) |
| 82 | 0 | siter.remove(); |
| 83 | 0 | } |
| 84 | 0 | return(inactiveRoots); |
| 85 | |
} |
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
@SuppressWarnings("unchecked") |
| 91 | |
public List<Root> getActiveRoots() throws DataAccessException { |
| 92 | 0 | RootsQuery rq = new RootsQuery(ds); |
| 93 | 0 | List<Root> activeRoots = rq.execute(); |
| 94 | 0 | Iterator<Root> siter = activeRoots.iterator(); |
| 95 | 0 | while (siter.hasNext()) { |
| 96 | 0 | Root root = siter.next(); |
| 97 | 0 | if (!root.isActive()) |
| 98 | 0 | siter.remove(); |
| 99 | 0 | } |
| 100 | 0 | return(activeRoots); |
| 101 | |
} |
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
public int getRootCount() throws DataAccessException { |
| 107 | 0 | if (rootsCache == null) |
| 108 | 0 | getRoots(); |
| 109 | 0 | return(rootsCache.size()); |
| 110 | |
} |
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
public Root getRoot(int rid) throws DataAccessException { |
| 116 | 0 | if (rootsCache == null) |
| 117 | 0 | getRoots(); |
| 118 | 0 | Iterator<Root> iter = rootsCache.iterator(); |
| 119 | 0 | while (iter.hasNext()) { |
| 120 | 0 | Root root = (Root)iter.next(); |
| 121 | 0 | if (root.getRid() == rid) |
| 122 | 0 | return(root); |
| 123 | 0 | } |
| 124 | 0 | return(null); |
| 125 | |
} |
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
public Root getRoot(String name) throws DataAccessException { |
| 131 | 0 | if (rootsCache == null) |
| 132 | 0 | getRoots(); |
| 133 | 0 | Iterator<Root> iter = rootsCache.iterator(); |
| 134 | 0 | while (iter.hasNext()) { |
| 135 | 0 | Root root = (Root)iter.next(); |
| 136 | 0 | if (root.getName().equals(name)) |
| 137 | 0 | return(root); |
| 138 | 0 | } |
| 139 | 0 | return(null); |
| 140 | |
} |
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
public void storeRoot(Root root) throws DataAccessException { |
| 146 | 0 | if (root.getRid() <= 0) |
| 147 | 0 | throw(new RuntimeException("Can't store Root with rid=" + root.getRid())); |
| 148 | 0 | rootsCache = null; |
| 149 | 0 | RootUpdate ru = new RootUpdate(ds, root); |
| 150 | 0 | ru.update(root); |
| 151 | 0 | } |
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
public void setRoots(List<Root> roots) throws DataAccessException { |
| 158 | 0 | rootsCache = null; |
| 159 | 0 | RootUpdate ru = new RootUpdate(ds); |
| 160 | 0 | ru.update(roots); |
| 161 | 0 | } |
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
public void addRoot(Root r) throws DataAccessException { |
| 167 | 0 | rootsCache = null; |
| 168 | 0 | RootInsert ri = new RootInsert(ds); |
| 169 | 0 | ri.insert(r); |
| 170 | 0 | } |
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
public void deleteRoot(Root r) throws DataAccessException { |
| 176 | 0 | rootsCache = null; |
| 177 | 0 | RootDelete rd = new RootDelete(ds); |
| 178 | 0 | rd.delete(r); |
| 179 | 0 | } |
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
class RootsQuery extends MappingSqlQuery { |
| 185 | |
|
| 186 | 0 | RootsQuery(DataSource ds) { |
| 187 | 0 | super(ds, "SELECT * from roots"); |
| 188 | 0 | compile(); |
| 189 | 0 | } |
| 190 | |
|
| 191 | |
protected Object mapRow(ResultSet rs, int rowNum) throws SQLException { |
| 192 | 0 | Root r = new Root(); |
| 193 | 0 | r.setRid(rs.getInt("rid")); |
| 194 | 0 | r.setName(rs.getString("name")); |
| 195 | 0 | r.setPath(rs.getString("path")); |
| 196 | 0 | r.setFilePrefix(rs.getString("filePrefix")); |
| 197 | 0 | r.setActive(rs.getBoolean("active")); |
| 198 | 0 | return(r); |
| 199 | |
} |
| 200 | |
} |
| 201 | |
|
| 202 | |
|
| 203 | |
|
| 204 | |
|
| 205 | |
protected class RootUpdate extends SqlUpdate { |
| 206 | |
private DataSource ds; |
| 207 | |
|
| 208 | |
|
| 209 | |
|
| 210 | |
|
| 211 | |
|
| 212 | 0 | protected RootUpdate(DataSource ds) { |
| 213 | 0 | this.ds = ds; |
| 214 | 0 | } |
| 215 | |
|
| 216 | 0 | protected RootUpdate(DataSource ds, Root root) { |
| 217 | 0 | super(ds, "UPDATE roots SET name=?, path=?, filePrefix=?, active=? WHERE rid = ? LIMIT 1"); |
| 218 | 0 | declareParameter(new SqlParameter(Types.VARCHAR)); |
| 219 | 0 | declareParameter(new SqlParameter(Types.VARCHAR)); |
| 220 | 0 | declareParameter(new SqlParameter(Types.VARCHAR)); |
| 221 | 0 | declareParameter(new SqlParameter(Types.TINYINT)); |
| 222 | 0 | declareParameter(new SqlParameter(Types.INTEGER)); |
| 223 | 0 | compile(); |
| 224 | 0 | } |
| 225 | |
|
| 226 | |
|
| 227 | |
|
| 228 | |
|
| 229 | |
|
| 230 | |
protected int update(List<Root> roots) { |
| 231 | |
|
| 232 | 0 | SqlUpdate sf = new SqlUpdate(ds, "TRUNCATE TABLE roots"); |
| 233 | 0 | sf.compile(); |
| 234 | 0 | int ret = sf.update(); |
| 235 | 0 | logger.info("sf.empty() returned" + ret); |
| 236 | |
|
| 237 | |
|
| 238 | 0 | RootInsert ri = new RootInsert(ds); |
| 239 | 0 | Iterator<Root> it = roots.iterator(); |
| 240 | 0 | while (it.hasNext()) |
| 241 | 0 | ri.insert(it.next()); |
| 242 | 0 | return(0); |
| 243 | |
} |
| 244 | |
|
| 245 | |
protected int update(Root root) { |
| 246 | 0 | return this.update(new Object[] { |
| 247 | 0 | root.getName(), |
| 248 | 0 | root.getPath(), |
| 249 | 0 | root.getFilePrefix(), |
| 250 | 0 | new Integer(root.isActive() ? 1 : 0), |
| 251 | 0 | new Integer(root.getRid()), |
| 252 | |
}); |
| 253 | |
} |
| 254 | |
} |
| 255 | |
|
| 256 | |
|
| 257 | |
|
| 258 | |
|
| 259 | |
|
| 260 | |
protected class RootInsert extends SqlUpdate { |
| 261 | |
|
| 262 | |
|
| 263 | |
|
| 264 | |
|
| 265 | |
|
| 266 | 0 | protected RootInsert(DataSource ds) { |
| 267 | 0 | super(ds, "INSERT INTO roots VALUES(?,?,?,?,?)"); |
| 268 | 0 | declareParameter(new SqlParameter(Types.INTEGER)); |
| 269 | 0 | declareParameter(new SqlParameter(Types.TINYINT)); |
| 270 | 0 | declareParameter(new SqlParameter(Types.VARCHAR)); |
| 271 | 0 | declareParameter(new SqlParameter(Types.VARCHAR)); |
| 272 | 0 | declareParameter(new SqlParameter(Types.VARCHAR)); |
| 273 | 0 | compile(); |
| 274 | 0 | } |
| 275 | |
|
| 276 | |
protected void insert(Root r) { |
| 277 | 0 | Object[] objs = new Object[] { |
| 278 | 0 | new Integer(r.getRid()), |
| 279 | 0 | new Integer(r.isActive() ? 1 : 0), |
| 280 | 0 | r.getName(), r.getPath(), r.getFilePrefix(), |
| 281 | |
}; |
| 282 | 0 | super.update(objs); |
| 283 | 0 | } |
| 284 | |
} |
| 285 | |
|
| 286 | |
|
| 287 | |
|
| 288 | |
protected class RootDelete extends SqlUpdate { |
| 289 | |
|
| 290 | |
|
| 291 | |
|
| 292 | |
|
| 293 | |
|
| 294 | 0 | protected RootDelete(DataSource ds) { |
| 295 | 0 | super(ds, "DELETE FROM rots WHERE name = (?)"); |
| 296 | 0 | declareParameter(new SqlParameter(Types.VARCHAR)); |
| 297 | 0 | compile(); |
| 298 | 0 | } |
| 299 | |
|
| 300 | |
protected void delete(Root root) { |
| 301 | 0 | Object[] objs = new Object[] { |
| 302 | 0 | root.getName()}; |
| 303 | 0 | super.update(objs); |
| 304 | 0 | } |
| 305 | |
} |
| 306 | |
} |