View Javadoc
1   /******************************************************************************
2    * VirginsDaoJdbc.java - Implement the Dao interface for the Virgins
3    * 
4    * PicMan - The BuckoSoft Picture Manager in Java
5    * Copyright(c) 2005 - Dick Balaska
6    * 
7    */
8   package com.buckosoft.PicMan.db;
9   
10  import java.util.List;
11  
12  import javax.sql.DataSource;
13  
14  import java.sql.ResultSet;
15  import java.sql.SQLException;
16  import java.sql.Types;
17  
18  import org.apache.commons.logging.Log;
19  import org.apache.commons.logging.LogFactory;
20  
21  import org.springframework.dao.DataAccessException;
22  import org.springframework.jdbc.core.SqlParameter;
23  import org.springframework.jdbc.object.SqlFunction;
24  import org.springframework.jdbc.object.SqlUpdate;
25  import org.springframework.jdbc.object.MappingSqlQuery;
26  
27  import com.buckosoft.PicMan.domain.Virgin;
28  
29  /** Implement the Dao Interface for the {@link com.buckosoft.PicMan.domain.Virgin}s.
30   * @author Dick Balaska
31   * @since 2005/07/31
32   * @see <a href="http://cvs.buckosoft.com/Projects/java/PicMan/PicMan/src/com/buckosoft/PicMan/db/VirginsDaoJdbc.java">VirginsDaoJdbc.java</a>
33   */
34  public class VirginsDaoJdbc implements VirginsDao {
35  
36  	private final static boolean DEBUG = false;
37  	private final static boolean DEBUGCONNECTIONLEAK = false;
38  	
39  	protected final Log logger = LogFactory.getLog(getClass());
40  	
41  //	private	PicManFacade	pmf;
42  	private DataSource ds;
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  	/** Get the list of Virgins, the Pics that haven't been filtered yet.
53  	 * @return The List
54  	 */
55  	@SuppressWarnings("unchecked")
56  	public List<Virgin> getVirgins() throws DataAccessException {
57  		if (DEBUG)
58  			logger.info("Getting Virgins");
59  		VirginsQuery vq = new VirginsQuery(ds);
60  		return(vq.execute());
61  	}
62  
63  	
64  	/* (non-Javadoc)
65  	 * @see com.buckosoft.PicMan.db.VirginsDao#getVirgins(int)
66  	 */
67  	@SuppressWarnings("unchecked")
68  	public List<Virgin> getVirgins(int max) throws DataAccessException {
69  		if (DEBUG)
70  			logger.info("Getting Virgins(" + max + ")");
71  		VirginsQuery vq = new VirginsQuery(ds, max);
72  		return(vq.execute(max));
73  	}
74  
75  	/* (non-Javadoc)
76  	 * @see com.buckosoft.PicMan.db.VirginsDao#isVirgin(java.lang.String)
77  	 */
78  	public boolean isVirgin(String picName) {
79  		VirginsQuery vq = new VirginsQuery(ds, picName);
80  		Virgin v = (Virgin)vq.findObject(picName);
81  		if (DEBUG)
82  			logger.info("IsVirgin: " + picName + ": " + v != null ? "true" : "false");
83  		return(v != null ? true : false);
84  	}
85  	
86  	/** Add a pic to the Virgin database
87  	 * @param picName The name of the pic to add
88  	 */
89  	public void	addVirgin(String picName) throws DataAccessException {
90  		if (DEBUG)
91  			logger.info("addVirgin: " + picName);
92  		VirginInsert vi = new VirginInsert(ds);
93  		Virgin v = new Virgin();
94  		v.setName(picName);
95  		vi.insert(v);
96  	}
97  	
98  	/** Delete a pic from the Virgin database
99  	 * @param picName The name of the pic to remove
100 	 */
101 	public void	deleteVirgin(String picName) throws DataAccessException {
102 		if (DEBUG)
103 			logger.info("deleteVirgin: " + picName);
104 		VirginDelete vd = new VirginDelete(ds);
105 		Virgin v = new Virgin();
106 		v.setName(picName);
107 		vd.delete(v);
108 	}
109 
110 	/** Get the number of Virgins in the database
111 	 * @return the count
112 	 */
113 	public int	getVirginCount() {
114 		if (DEBUG)
115 			logger.info("getVirginCount:");
116 		if (DEBUGCONNECTIONLEAK)
117 			return(69);
118 		if (sql_getVirginCountQUERYr == null)
119 			sql_getVirginCountQUERYr = new VirginsCount(ds);
120 		return(sql_getVirginCountQUERYr.run());
121 	}
122 	private VirginsCount	sql_getVirginCountQUERYr;
123 	
124 	///////////////////////////////////////////////////////////////////////////
125 	class VirginsQuery extends MappingSqlQuery {
126 		
127 		VirginsQuery(DataSource ds) {
128 			super(ds, "SELECT * from virgins");
129 			compile();
130 		}
131 		
132 		VirginsQuery(DataSource ds, int max) {
133 			super(ds, "SELECT * from virgins LIMIT ?");
134 			declareParameter(new SqlParameter(Types.INTEGER));
135 			compile();
136 		}
137 		
138 		VirginsQuery(DataSource ds, String picName) {
139 			super(ds, "SELECT * from virgins WHERE pic = ?");
140 			declareParameter(new SqlParameter(Types.VARCHAR));
141 			compile();
142 		}
143 		
144 		
145 		protected Object mapRow(ResultSet rs, int rowNum) throws SQLException {
146 			Virgin v = new Virgin();
147 			v.setName(rs.getString("pic"));
148 			return(v);
149 		}
150 	}
151 	
152 	/**
153 	 * <code>Virgin</code> Count Object.
154 	 */
155 	class VirginsCount extends SqlFunction {
156 		
157 		VirginsCount(DataSource ds) {
158 			super(ds, "SELECT COUNT(*) from virgins");
159 			compile();
160 		}		
161 	}
162 
163 	/**
164 	 * <code>Virgin</code> Insert Object.
165 	 */
166 	protected class VirginInsert extends SqlUpdate {
167 		
168 		/**
169 		 * Create a new instance of VirginInsert.
170 		 * @param ds the DataSource to use for the insert
171 		 */
172 		protected VirginInsert(DataSource ds) {
173 			super(ds, "INSERT INTO virgins VALUES(?)");
174 			declareParameter(new SqlParameter(Types.VARCHAR));
175 			compile();
176 		}
177 		
178 		protected void insert(Virgin v) {
179 			Object[] objs = new Object[] {
180 					v.getName()};
181 			super.update(objs);
182 			// retrieveIdentity(owner);
183 		}
184 	}
185 	
186 	/**
187 	 * <code>Virgin</code> Delete Object.
188 	 */
189 	protected class VirginDelete extends SqlUpdate {
190 		
191 		/**
192 		 * Create a new instance of VirginRemove.
193 		 * @param ds the DataSource to use for the delete
194 		 */
195 		protected VirginDelete(DataSource ds) {
196 			super(ds, "DELETE FROM virgins WHERE pic = (?)");
197 			declareParameter(new SqlParameter(Types.VARCHAR));
198 			compile();
199 		}
200 		
201 		protected void delete(Virgin v) {
202 			Object[] objs = new Object[] {
203 					v.getName()};
204 			super.update(objs);
205 			// retrieveIdentity(owner);
206 		}
207 	}
208 }