View Javadoc
1   /******************************************************************************
2    * MosaicBatchesDaoJdbc.java - Implement the Dao interface for the MosaicBatches
3    * 
4    * PicMan - The BuckoSoft Picture Manager in Java
5    * Copyright(c) 2008 - 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.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  /** Implement the Dao Interface for the {@link com.buckosoft.PicMan.domain.MosaicBatch}es.
29   * @author Dick Balaska
30   * @since 2008/09/19
31   * @see <a href="http://cvs.buckosoft.com/Projects/java/PicMan/PicMan/src/com/buckosoft/PicMan/db/MosaicBatchesDaoJdbc.java">MosaicBatchesDaoJdbc.java</a>
32   */
33  public class MosaicBatchesDaoJdbc implements MosaicBatchesDao {
34  	private static final boolean DEBUG = true;
35  	protected final Log logger = LogFactory.getLog(getClass());
36  
37  	private DataSource		ds;
38  	private	MosaicsDao		mosaicsDao;
39  	
40  	/** Set the reference to the JDBC datasource.
41  	 * @param ds The datasource as configured by Spring.
42  	 * @see <a href="http://cvs.buckosoft.com/Projects/java/PicMan/PicMan/WebContent/WEB-INF/picManDatabase/picManDatabase.xml">picManDatabase.xml</a>
43  	 */ 
44  	public void setDataSource(DataSource ds) {
45  		this.ds = ds;
46  	}
47  
48  	/** AOP setter for the MosaicsDao
49  	 * @param mosaicsDao The MosaicsDao from the Spring Database Setup
50  	 */
51  	public void setMosaicsDao(MosaicsDao mosaicsDao) {
52  		this.mosaicsDao = mosaicsDao;
53  	}
54  	/* (non-Javadoc)
55  	 * @see com.buckosoft.PicMan.db.MosaicBatchesDao#getMosaicBatches()
56  	 */
57  	@SuppressWarnings("unchecked")
58  	public List<MosaicBatch> getMosaicBatches() {
59  		if (DEBUG)
60  			logger.info("GetMosaicBatchesList");
61  		if (sql_getMosaicBatchesListQUERY == null)
62  			sql_getMosaicBatchesListQUERY = new MosaicBatchesQuery(ds);
63  		List<MosaicBatch> list = sql_getMosaicBatchesListQUERY.execute();
64  		Iterator<MosaicBatch> iter = list.iterator();
65  		while (iter.hasNext()) {
66  			MosaicBatch mb = iter.next();
67  			fillMosaic(mb);
68  		}
69  		return(list);
70  	}
71  	private	MosaicBatchesQuery	sql_getMosaicBatchesListQUERY = null;
72  
73  	/* (non-Javadoc)
74  	 * @see com.buckosoft.PicMan.db.MosaicBatchesDao#getMosaicBatch(int)
75  	 */
76  	public MosaicBatch getMosaicBatch(int mbid) {
77  		if (DEBUG)
78  			logger.info("getMosaicBatch: " + mbid);
79  		if (sql_getMosaicBatchQUERY == null)
80  			sql_getMosaicBatchQUERY = new MosaicBatchesQuery(ds, mbid);
81          MosaicBatch mb = (MosaicBatch)sql_getMosaicBatchQUERY.findObject(mbid);
82          if (mb == null)
83          	return(null);
84          return(fillMosaic(mb));
85  	}
86  	private	MosaicBatchesQuery	sql_getMosaicBatchQUERY = null;
87  
88  	private	MosaicBatch fillMosaic(MosaicBatch mb) {
89          Mosaic m = this.mosaicsDao.getMosaic(mb.getMid());
90          mb.setBatch(true);
91          mb.setName(m.getName());
92          mb.setEngine(m.getEngine());
93          mb.setMasterPic(m.getMasterPic());
94          mb.setOutPic(m.getOutPic());
95          mb.setSid(m.getSid());
96          mb.setStartTime(m.getStartTime());
97          mb.setEngineConfig(m.getEngineConfig());
98          return(mb);
99  	}
100 	/* (non-Javadoc)
101 	 * @see com.buckosoft.PicMan.db.MosaicBatchesDao#storeMosaicBatch(com.buckosoft.PicMan.domain.MosaicBatch)
102 	 */
103 	public void storeMosaicBatch(MosaicBatch mbatch) {
104 		if (DEBUG)
105 			logger.info("storeMosaic " + mbatch.getMbid());
106 		this.mosaicsDao.storeMosaic(mbatch);
107 		assert(mbatch.getMid() != 0);
108 		if (mbatch.getMbid() <= 0) {
109 			MosaicBatchesInsert mbi = new MosaicBatchesInsert(ds);
110 			mbi.insert(mbatch);
111 		} else {
112 			MosaicBatchesUpdate mbu = new MosaicBatchesUpdate(ds);
113 			mbu.update(mbatch);
114 		}
115 	}
116 	/* (non-Javadoc)
117 	 * @see com.buckosoft.PicMan.db.MosaicBatchesDao#deleteMosaicBatch(int)
118 	 */
119 	public void deleteMosaicBatch(int mbid) {
120 		// TODO Auto-generated method stub
121 
122 	}
123 
124 	/**
125 	 * Object to Query for MosaicBatches
126 	 */
127 	private class MosaicBatchesQuery extends MappingSqlQuery {
128 		
129 		MosaicBatchesQuery(DataSource ds) {
130 			super(ds, "SELECT * from mosaicBatches");
131 			compile();
132 		}
133 		
134 		MosaicBatchesQuery(DataSource ds, int mbid) {
135 			super(ds, "SELECT * from mosaicBatches where mbid = ?");
136 			declareParameter(new SqlParameter(Types.INTEGER));
137 			compile();
138 		}
139 
140 		protected Object mapRow(ResultSet rs, int rowNum) throws SQLException {
141 			MosaicBatch mb = new MosaicBatch();
142 			mb.setMbid(rs.getInt("mbid"));
143 			mb.setMid(rs.getInt("mid"));
144 			mb.setSizes(rs.getString("sizes"));
145 			return(mb);
146 		}
147 	}
148 	/**
149 	 * <code>Virgin</code> Count Object.
150 	 */
151 	class MosaicBatchesCount extends SqlFunction {
152 		
153 		MosaicBatchesCount(DataSource ds) {
154 			super(ds, "SELECT COUNT(*) from mosaicBatches");
155 			compile();
156 		}		
157 	}
158 
159 	/**
160 	 * <code>Mosaic</code> Insert Object.
161 	 */
162 	private class MosaicBatchesInsert extends SqlUpdate {
163 		
164 		/**
165 		 * Create a new instance of MosaicBatchesInsert.
166 		 * @param ds the DataSource to use for the insert
167 		 */
168 		protected MosaicBatchesInsert(DataSource ds) {
169 			super(ds, "INSERT INTO mosaicBatches VALUES(?,?,?)");
170 			declareParameter(new SqlParameter(Types.INTEGER));
171 			declareParameter(new SqlParameter(Types.INTEGER));
172 			declareParameter(new SqlParameter(Types.VARCHAR));
173 			compile();
174 		}
175 
176 		protected void insert(MosaicBatch mb) {
177 			Object[] objs = new Object[] {
178 				null,
179 				new Integer(mb.getMid()), 
180 				mb.getSizes()
181 			};
182 			super.update(objs);
183 		}
184 	}
185 	///////////////////////////////////////////////////////////////////////////////////////////////
186 	/**
187 	 * <code>Mosaic</code> Update Object.
188 	 */
189 	private class MosaicBatchesUpdate extends SqlUpdate {
190 		//private	DataSource	ds;
191 		
192 		/**
193 		 * Create a new instance of MosaicBatchesUpdate.
194 		 * @param ds the DataSource to use for the update
195 		 */
196 		protected MosaicBatchesUpdate(DataSource ds) {
197 			super(ds, "UPDATE mosaicBatches SET mid=?, sizes=? WHERE mbid = ? LIMIT 1");
198 			declareParameter(new SqlParameter(Types.INTEGER));
199 			declareParameter(new SqlParameter(Types.VARCHAR));
200 			declareParameter(new SqlParameter(Types.INTEGER));
201 			compile();
202 		}
203 		/**
204 		 * Method to update the <code>Set</code>'s data.
205 		 * @param List sets The sets that should be stored
206 		 * @return 0
207 		 */
208 
209 		protected int update(MosaicBatch mb) {
210 			return this.update(new Object[] {
211 					new Integer(mb.getMid()),
212 					mb.getSizes(), 					
213 					new Integer(mb.getMbid())
214 			});
215 		}
216 	}
217 }