View Javadoc
1   /******************************************************************************
2    * MosaicTileDaoJdbc.java - Implement the Dao interface for the MosaicTiles
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.List;
14  
15  import javax.sql.DataSource;
16  
17  import org.apache.commons.logging.Log;
18  import org.apache.commons.logging.LogFactory;
19  import org.springframework.jdbc.core.SqlParameter;
20  import org.springframework.jdbc.object.MappingSqlQuery;
21  import org.springframework.jdbc.object.SqlFunction;
22  import org.springframework.jdbc.object.SqlUpdate;
23  
24  import com.buckosoft.PicMan.domain.MosaicTile;
25  
26  /** Implement the Dao Interface for the {@link com.buckosoft.PicMan.domain.MosaicTile}s.
27   * @author Dick Balaska
28   * @since 2008/01/21
29   * @see <a href="http://cvs.buckosoft.com/Projects/java/PicMan/PicMan/src/com/buckosoft/PicMan/db/MosaicTilesDaoJdbc.java">MosaicTilesDaoJdbc.java</a>
30   */
31  public class MosaicTilesDaoJdbc implements MosaicTilesDao {
32  	private static final boolean DEBUG = false;
33  	protected final Log logger = LogFactory.getLog(getClass());
34  
35  	private DataSource ds;
36  	
37  	/** Set the reference to the JDBC datasource.
38  	 * @param ds The datasource as configured by Spring.
39  	 * @see <a href="http://cvs.buckosoft.com/Projects/java/PicMan/PicMan/WebContent/WEB-INF/picManDatabase/picManDatabase.xml">picManDatabase.xml</a>
40  	 */ 
41  	public void setDataSource(DataSource ds) {
42  		this.ds = ds;
43  	}
44  
45  	/* (non-Javadoc)
46  	 * @see com.buckosoft.PicMan.db.MosaicTilesDao#deleteMosaicTiles(int)
47  	 */
48  	public void deleteMosaicTiles(int mid) {
49  		if (DEBUG)
50  			logger.info("deleteMosaicTile: " + mid);
51  		MosaicTileDelete mtd = new MosaicTileDelete(ds);
52  		mtd.delete(mid);
53  
54  	}
55  
56  	/* (non-Javadoc)
57  	 * @see com.buckosoft.PicMan.db.MosaicTilesDao#getMosaicTiles(int)
58  	 */
59  	@SuppressWarnings("unchecked")
60  	public List<MosaicTile> getMosaicTiles(int mid) {
61  		if (sql_getMosaicTilesQUERY == null)
62  			sql_getMosaicTilesQUERY = new MosaicTilesQuery(ds, mid);
63  		if (DEBUG)
64  			logger.info("getMosaicTiles: " + mid);
65          return(sql_getMosaicTilesQUERY.execute(mid));
66   	}
67  	private	MosaicTilesQuery	sql_getMosaicTilesQUERY = null;
68  
69  	/* (non-Javadoc)
70  	 * @see com.buckosoft.PicMan.db.MosaicTilesDao#getTileCount(int)
71  	 */
72  	public int getTileCount(int mid) {
73  		if (DEBUG)
74  			logger.info("getTileCount:");
75  		if (sql_getTileCountQUERYr == null)
76  			sql_getTileCountQUERYr = new MosaicTilesCount(ds);
77  		return(sql_getTileCountQUERYr.run(mid));
78  	}
79  		private MosaicTilesCount	sql_getTileCountQUERYr;
80  		
81  	/* (non-Javadoc)
82  	 * @see com.buckosoft.PicMan.db.MosaicTilesDao#storeMosaicTile(com.buckosoft.PicMan.domain.MosaicTile)
83  	 */
84  	public void storeMosaicTile(MosaicTile tile) {
85  		MosaicTileInsert mti = new MosaicTileInsert(ds);
86  		mti.insert(tile);
87  	}
88  
89  	/////////////////////////////////////////////////////////////////////////////////////
90  	/**
91  	 * <code>MosaicTiles</code> Count Object.
92  	 */
93  	class MosaicTilesCount extends SqlFunction {
94  		
95  		MosaicTilesCount(DataSource ds) {
96  			super(ds, "SELECT COUNT(*) from mosaicTiles where mid=?");
97  			declareParameter(new SqlParameter(Types.INTEGER));
98  			compile();
99  		}		
100 	}
101 
102 
103 	/////////////////////////////////////////////////////////////////////////////////////
104 	/**
105 	 * <code>MosaicTiles</code> Query object.
106 	 */
107 	class MosaicTilesQuery extends MappingSqlQuery {
108 
109 /*		MosaicTilesQuery(DataSource ds) {
110             super(ds, "SELECT * from MosaicTiles");
111             compile();
112         }
113 */
114 		MosaicTilesQuery(DataSource ds, int mid) {
115             super(ds, "SELECT * from mosaicTiles where mid = ?");
116 			declareParameter(new SqlParameter(Types.INTEGER));
117             compile();
118         }
119  
120         protected Object mapRow(ResultSet rs, int rowNum) throws SQLException {
121         	MosaicTile mt = new MosaicTile();
122         	mt.setMid(rs.getInt("mid"));
123         	mt.setPid(rs.getInt("pid"));
124         	mt.setX(rs.getInt("x"));
125         	mt.setY(rs.getInt("y"));
126         	mt.setWidth(rs.getInt("width"));
127         	mt.setHeight(rs.getInt("height"));
128         	return(mt);
129         }
130 	}
131 
132 	/////////////////////////////////////////////////////////////////////////////////////
133 	/**
134 	 * <code>MosaicTile</code> Delete Object.
135 	 */
136 	protected class MosaicTileDelete extends SqlUpdate {
137 		
138 		/**
139 		 * Create a new instance of MosaicTileRemove.
140 		 * @param ds the DataSource to use for the delete
141 		 */
142 		protected MosaicTileDelete(DataSource ds) {
143 			super(ds, "DELETE FROM mosaicTiles WHERE mid = (?)");
144 			declareParameter(new SqlParameter(Types.INTEGER));
145 			compile();
146 		}
147 		
148 		protected void delete(int mid) {
149 			super.update(mid);
150 		}
151 	}
152 
153 	/////////////////////////////////////////////////////////////////////////////////////
154 	/**
155 	 * <code>MosaicTile</code> Insert Object.
156 	 */
157 	protected class MosaicTileInsert extends SqlUpdate {
158 		
159 		/**
160 		 * Create a new instance of MosaicTileInsert.
161 		 * @param ds the DataSource to use for the insert
162 		 */
163 		protected MosaicTileInsert(DataSource ds) {
164 			super(ds, "REPLACE INTO mosaicTiles VALUES(?,?,?,?,?,?)");
165 			declareParameter(new SqlParameter(Types.INTEGER));
166 			declareParameter(new SqlParameter(Types.INTEGER));
167 			declareParameter(new SqlParameter(Types.INTEGER));
168 			declareParameter(new SqlParameter(Types.INTEGER));
169 			declareParameter(new SqlParameter(Types.INTEGER));
170 			declareParameter(new SqlParameter(Types.INTEGER));
171 			compile();
172 		}
173 
174 		protected void insert(MosaicTile mt) {
175 			Object[] objs = new Object[] {
176 					new Integer(mt.getMid()),
177 					new Integer(mt.getPid()),
178 					new Integer(mt.getX()),
179 					new Integer(mt.getY()),
180 					new Integer(mt.getWidth()),
181 					new Integer(mt.getHeight()),
182 			};
183 			super.update(objs);
184 			// retrieveIdentity(owner);
185 		}
186 	}
187 	
188 
189 }