View Javadoc
1   /******************************************************************************
2    * MetaSetsDaoJdbc.java - Implement the Dao interface for the MetaSets
3    * 
4    * PicMan - The BuckoSoft Picture Manager in Java
5    * Copyright(c) 2006 - Dick Balaska
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  /** Implement the Dao Interface for the {@link com.buckosoft.PicMan.domain.MetaSet}s.
30   * @author Dick Balaska
31   * @since 2006/07/04
32   * @see <a href="http://cvs.buckosoft.com/Projects/java/PicMan/PicMan/src/com/buckosoft/PicMan/db/MetaSetsDaoJdbc.java">MetaSetsDaoJdbc.java</a>
33   */
34  public class MetaSetsDaoJdbc implements MetaSetsDao, DataStrings {
35  	private static final boolean DEBUG = false;
36  	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  	/** Set the reference to the JDBC datasource.
45  	 * @param ds The datasource as configured by Spring.
46  	 * @see <a href="http://cvs.buckosoft.com/Projects/java/PicMan/PicMan/WebContent/WEB-INF/picManDatabase/picManDatabase.xml">picManDatabase.xml</a>
47  	 */ 
48  	public void setDataSource(DataSource ds) {
49  		this.ds = ds;
50  	}
51  
52  	/* (non-Javadoc)
53  	 * @see com.buckosoft.PicMan.db.MetaSetsDao#getMetaSet(int)
54  	 */
55  	public MetaSet getMetaSet(int sid) {
56  		if (DEBUG)
57  			logger.info("getMetaSet: " + sid);
58  		if (sql_getMetaSetSidQUERY == null)
59  			sql_getMetaSetSidQUERY = new MetaSetsQuery(ds, sid);
60          @SuppressWarnings("unchecked")
61  		List<MetaSetRule> list = sql_getMetaSetSidQUERY.execute(sid);
62          MetaSet ms = new MetaSet();
63          ms.setSid(sid);
64          int	master = 0;
65          while (master < list.size()) {
66          	Iterator<MetaSetRule> iter = list.iterator();
67          	boolean found = false;
68          	while (iter.hasNext()) {
69          		MetaSetRule msr = iter.next();
70          		if (msr.getIndex() == master) {
71          			ms.addRule(msr);
72          			master++;
73          			found = true;
74          			break;
75          		}
76          	}
77          	if (!found) {
78          		throw new RuntimeException("Failed to parse up MetaSet: " + sid + " master=" + master);
79          	}
80          }
81  		return(ms);
82  	}
83  	private	MetaSetsQuery	sql_getMetaSetSidQUERY = null;
84  
85  	/* (non-Javadoc)
86  	 * @see com.buckosoft.PicMan.db.MetaSetsDao#storeMetaSet(com.buckosoft.PicMan.domain.MetaSet)
87  	 */
88  	public void storeMetaSet(MetaSet mset) {
89  		if (mset.getSid() <= 0) {
90  			throw new RuntimeException("Bad sid");
91  		}
92  		if (sql_storeMetaSetINSERT == null)
93  			sql_storeMetaSetINSERT = new MetaSetInsert(ds);
94  		deleteMetaSet(mset);
95  		Iterator<MetaSetRule> iter = mset.getRules().iterator();
96  		while (iter.hasNext()) {
97  			MetaSetRule msr = iter.next();
98  			sql_storeMetaSetINSERT.insert(msr);
99  			
100 		}
101 
102 	}
103 	private	MetaSetInsert	sql_storeMetaSetINSERT = null;
104 	
105 	/* (non-Javadoc)
106 	 * @see com.buckosoft.PicMan.db.MetaSetsDao#deleteMetaSet(com.buckosoft.PicMan.domain.MetaSet)
107 	 */
108 	public void	deleteMetaSet(MetaSet mset) throws DataAccessException {
109 		if (DEBUG)
110 			logger.info("deleteMetaSet: " + mset.getSid());
111 		MetaSetDelete msd = new MetaSetDelete(ds);
112 		msd.delete(mset);
113 	}
114 
115 	/////////////////////////////////////////////////////////////////////////////////////
116 	/**
117 	 * <code>MetaSets</code> Query object.
118 	 */
119 	class MetaSetsQuery extends MappingSqlQuery {
120 
121 /*		MetaSetsQuery(DataSource ds) {
122             super(ds, "SELECT * from metaSets");
123             compile();
124         }
125 */
126 		MetaSetsQuery(DataSource ds, int sid) {
127             super(ds, "SELECT * from metaSets where sid = ? ORDER BY `index`");
128 			declareParameter(new SqlParameter(Types.INTEGER));
129             compile();
130         }
131  
132         protected Object mapRow(ResultSet rs, int rowNum) throws SQLException {
133         	MetaSetRule msr = new MetaSetRule();
134         	msr.setSid(rs.getInt("sid"));
135         	msr.setIndex(rs.getInt("index"));
136         	String s = rs.getString("type");
137         	if (s.equals(s_NAME)) {
138         		msr.setType(MetaSet.NAME);
139         		msr.setRateOp(rs.getInt("rateOp"));
140         		msr.setRateVal(rs.getInt("rateVal"));
141         	} else if (s.equals(s_FUNC)) {
142         		msr.setType(MetaSet.FUNCTION);
143         		msr.setRateOp(rs.getInt("rateOp"));
144         		msr.setRateVal(rs.getInt("rateVal"));
145         	} else if (s.equals(s_OPERATOR)) {
146         		msr.setType(MetaSet.OPERATOR);
147         	}
148         	s = rs.getString("value");
149        		msr.setValue(s);
150         	return(msr);
151         }
152 	}
153 	/**
154 	 * <code>MetaSet</code> Delete Object.
155 	 */
156 	protected class MetaSetDelete extends SqlUpdate {
157 		
158 		/**
159 		 * Create a new instance of MetaSetRemove.
160 		 * @param ds the DataSource to use for the delete
161 		 */
162 		protected MetaSetDelete(DataSource ds) {
163 			super(ds, "DELETE FROM metaSets WHERE sid = (?)");
164 			declareParameter(new SqlParameter(Types.INTEGER));
165 			compile();
166 		}
167 		
168 		protected void delete(MetaSet ms) {
169 			super.update(ms.getSid());
170 		}
171 	}
172 	/**
173 	 * <code>MetaSet</code> Insert Object.
174 	 */
175 	protected class MetaSetInsert extends SqlUpdate {
176 		
177 		/**
178 		 * Create a new instance of MetaSetInsert.
179 		 * @param ds the DataSource to use for the insert
180 		 */
181 		protected MetaSetInsert(DataSource ds) {
182 			super(ds, "INSERT INTO metaSets VALUES(?,?,?,?,?,?)");
183 			declareParameter(new SqlParameter(Types.INTEGER));
184 			declareParameter(new SqlParameter(Types.INTEGER));
185 			declareParameter(new SqlParameter(Types.VARCHAR));
186 			declareParameter(new SqlParameter(Types.VARCHAR));
187 			declareParameter(new SqlParameter(Types.INTEGER));
188 			declareParameter(new SqlParameter(Types.INTEGER));
189 			compile();
190 		}
191 
192 		protected void insert(MetaSetRule msr) {
193 			Object[] objs = new Object[] {
194 					new Integer(msr.getSid()),
195 					new Integer(msr.getIndex()),
196 					(msr.getType() == 0) ? s_NAME : ((msr.getType() == MetaSet.FUNCTION) ? s_FUNC : s_OPERATOR),
197 					msr.getValue(),
198 //					new Integer((msr.getType() == 0) ? msr.getRateOp() : 0),
199 //					new Integer((msr.getType() == 0) ? msr.getRateVal() : 0),
200 					new Integer(msr.getRateOp()),
201 					new Integer(msr.getRateVal()),
202 			};
203 			super.update(objs);
204 			// retrieveIdentity(owner);
205 		}
206 	}
207 	
208 
209 }