| 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.SqlUpdate; |
| 24 | |
|
| 25 | |
import com.buckosoft.PicMan.domain.DataStrings; |
| 26 | |
import com.buckosoft.PicMan.domain.MetaSet; |
| 27 | |
import com.buckosoft.PicMan.domain.MetaSetRule; |
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | 0 | public class MetaSetsDaoJdbc implements MetaSetsDao, DataStrings { |
| 35 | |
private static final boolean DEBUG = false; |
| 36 | 0 | protected final Log logger = LogFactory.getLog(getClass()); |
| 37 | |
|
| 38 | |
private DataSource ds; |
| 39 | |
|
| 40 | |
private final static String s_NAME = "name"; |
| 41 | |
private final static String s_OPERATOR = "operator"; |
| 42 | |
private final static String s_FUNC = "func"; |
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
public void setDataSource(DataSource ds) { |
| 49 | 0 | this.ds = ds; |
| 50 | 0 | } |
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
public MetaSet getMetaSet(int sid) { |
| 56 | |
if (DEBUG) |
| 57 | |
logger.info("getMetaSet: " + sid); |
| 58 | 0 | if (sql_getMetaSetSidQUERY == null) |
| 59 | 0 | sql_getMetaSetSidQUERY = new MetaSetsQuery(ds, sid); |
| 60 | |
@SuppressWarnings("unchecked") |
| 61 | 0 | List<MetaSetRule> list = sql_getMetaSetSidQUERY.execute(sid); |
| 62 | 0 | MetaSet ms = new MetaSet(); |
| 63 | 0 | ms.setSid(sid); |
| 64 | 0 | int master = 0; |
| 65 | 0 | while (master < list.size()) { |
| 66 | 0 | Iterator<MetaSetRule> iter = list.iterator(); |
| 67 | 0 | boolean found = false; |
| 68 | 0 | while (iter.hasNext()) { |
| 69 | 0 | MetaSetRule msr = iter.next(); |
| 70 | 0 | if (msr.getIndex() == master) { |
| 71 | 0 | ms.addRule(msr); |
| 72 | 0 | master++; |
| 73 | 0 | found = true; |
| 74 | 0 | break; |
| 75 | |
} |
| 76 | 0 | } |
| 77 | 0 | if (!found) { |
| 78 | 0 | throw new RuntimeException("Failed to parse up MetaSet: " + sid + " master=" + master); |
| 79 | |
} |
| 80 | 0 | } |
| 81 | 0 | return(ms); |
| 82 | |
} |
| 83 | 0 | private MetaSetsQuery sql_getMetaSetSidQUERY = null; |
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
public void storeMetaSet(MetaSet mset) { |
| 89 | 0 | if (mset.getSid() <= 0) { |
| 90 | 0 | throw new RuntimeException("Bad sid"); |
| 91 | |
} |
| 92 | 0 | if (sql_storeMetaSetINSERT == null) |
| 93 | 0 | sql_storeMetaSetINSERT = new MetaSetInsert(ds); |
| 94 | 0 | deleteMetaSet(mset); |
| 95 | 0 | Iterator<MetaSetRule> iter = mset.getRules().iterator(); |
| 96 | 0 | while (iter.hasNext()) { |
| 97 | 0 | MetaSetRule msr = iter.next(); |
| 98 | 0 | sql_storeMetaSetINSERT.insert(msr); |
| 99 | |
|
| 100 | 0 | } |
| 101 | |
|
| 102 | 0 | } |
| 103 | 0 | private MetaSetInsert sql_storeMetaSetINSERT = null; |
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
public void deleteMetaSet(MetaSet mset) throws DataAccessException { |
| 109 | |
if (DEBUG) |
| 110 | |
logger.info("deleteMetaSet: " + mset.getSid()); |
| 111 | 0 | MetaSetDelete msd = new MetaSetDelete(ds); |
| 112 | 0 | msd.delete(mset); |
| 113 | 0 | } |
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
class MetaSetsQuery extends MappingSqlQuery { |
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | 0 | MetaSetsQuery(DataSource ds, int sid) { |
| 127 | 0 | super(ds, "SELECT * from metaSets where sid = ? ORDER BY `index`"); |
| 128 | 0 | declareParameter(new SqlParameter(Types.INTEGER)); |
| 129 | 0 | compile(); |
| 130 | 0 | } |
| 131 | |
|
| 132 | |
protected Object mapRow(ResultSet rs, int rowNum) throws SQLException { |
| 133 | 0 | MetaSetRule msr = new MetaSetRule(); |
| 134 | 0 | msr.setSid(rs.getInt("sid")); |
| 135 | 0 | msr.setIndex(rs.getInt("index")); |
| 136 | 0 | String s = rs.getString("type"); |
| 137 | 0 | if (s.equals(s_NAME)) { |
| 138 | 0 | msr.setType(MetaSet.NAME); |
| 139 | 0 | msr.setRateOp(rs.getInt("rateOp")); |
| 140 | 0 | msr.setRateVal(rs.getInt("rateVal")); |
| 141 | 0 | } else if (s.equals(s_FUNC)) { |
| 142 | 0 | msr.setType(MetaSet.FUNCTION); |
| 143 | 0 | msr.setRateOp(rs.getInt("rateOp")); |
| 144 | 0 | msr.setRateVal(rs.getInt("rateVal")); |
| 145 | 0 | } else if (s.equals(s_OPERATOR)) { |
| 146 | 0 | msr.setType(MetaSet.OPERATOR); |
| 147 | |
} |
| 148 | 0 | s = rs.getString("value"); |
| 149 | 0 | msr.setValue(s); |
| 150 | 0 | return(msr); |
| 151 | |
} |
| 152 | |
} |
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
protected class MetaSetDelete extends SqlUpdate { |
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | 0 | protected MetaSetDelete(DataSource ds) { |
| 163 | 0 | super(ds, "DELETE FROM metaSets WHERE sid = (?)"); |
| 164 | 0 | declareParameter(new SqlParameter(Types.INTEGER)); |
| 165 | 0 | compile(); |
| 166 | 0 | } |
| 167 | |
|
| 168 | |
protected void delete(MetaSet ms) { |
| 169 | 0 | super.update(ms.getSid()); |
| 170 | 0 | } |
| 171 | |
} |
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
protected class MetaSetInsert extends SqlUpdate { |
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | 0 | protected MetaSetInsert(DataSource ds) { |
| 182 | 0 | super(ds, "INSERT INTO metaSets VALUES(?,?,?,?,?,?)"); |
| 183 | 0 | declareParameter(new SqlParameter(Types.INTEGER)); |
| 184 | 0 | declareParameter(new SqlParameter(Types.INTEGER)); |
| 185 | 0 | declareParameter(new SqlParameter(Types.VARCHAR)); |
| 186 | 0 | declareParameter(new SqlParameter(Types.VARCHAR)); |
| 187 | 0 | declareParameter(new SqlParameter(Types.INTEGER)); |
| 188 | 0 | declareParameter(new SqlParameter(Types.INTEGER)); |
| 189 | 0 | compile(); |
| 190 | 0 | } |
| 191 | |
|
| 192 | |
protected void insert(MetaSetRule msr) { |
| 193 | 0 | Object[] objs = new Object[] { |
| 194 | 0 | new Integer(msr.getSid()), |
| 195 | 0 | new Integer(msr.getIndex()), |
| 196 | 0 | (msr.getType() == 0) ? s_NAME : ((msr.getType() == MetaSet.FUNCTION) ? s_FUNC : s_OPERATOR), |
| 197 | 0 | msr.getValue(), |
| 198 | |
|
| 199 | |
|
| 200 | 0 | new Integer(msr.getRateOp()), |
| 201 | 0 | new Integer(msr.getRateVal()), |
| 202 | |
}; |
| 203 | 0 | super.update(objs); |
| 204 | |
|
| 205 | 0 | } |
| 206 | |
} |
| 207 | |
|
| 208 | |
|
| 209 | |
} |