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