View Javadoc
1   /******************************************************************************
2    * UsersDaoJdbc - This object implements the User table interface
3    * 
4    * PicMan - The BuckoSoft Picture Manager in Java
5    * Copyright(c) 2007 - Dick Balaska
6    * 
7    */
8   
9   package com.buckosoft.PicMan.db;
10  
11  import java.sql.ResultSet;
12  import java.sql.SQLException;
13  import java.sql.Types;
14  import java.util.Iterator;
15  import java.util.List;
16  
17  import javax.sql.DataSource;
18  
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  import org.springframework.dao.DataAccessException;
22  import org.springframework.jdbc.core.SqlParameter;
23  import org.springframework.jdbc.object.MappingSqlQuery;
24  import org.springframework.jdbc.object.SqlUpdate;
25  
26  import com.buckosoft.PicMan.domain.User;
27  
28  
29  /** Implement the Dao Interface for the {@link com.buckosoft.PicMan.domain.User}s. <br>
30   * This table is key / value pairs so that {@link com.buckosoft.PicMan.domain.User} can be extended without
31   * altering the table. 
32   * @author Dick Balaska
33   * @since 2007/12/01
34   * @see <a href="http://cvs.buckosoft.com/Projects/java/PicMan/PicMan/src/com/buckosoft/PicMan/db/UsersDaoJdbc.java">UsersDaoJdbc.java</a>
35   */
36  public class UsersDaoJdbc implements UsersDao {
37  	private static final boolean DEBUG = false;
38  	protected final Log logger = LogFactory.getLog(getClass());
39  
40  	private DataSource ds;
41  
42  	/** Set the reference to the JDBC datasource.
43  	 * @param ds The datasource as configured by Spring.
44  	 * @see <a href="http://cvs.buckosoft.com/Projects/java/PicMan/PicMan/WebContent/WEB-INF/picManDatabase/picManDatabase.xml">picManDatabase.xml</a>
45  	 */ 
46  	public void setDataSource(DataSource ds) {
47  		if (DEBUG)
48  			logger.info("Set Datasource.");
49  		this.ds = ds;
50  	}
51  
52  	/* (non-Javadoc)
53  	 * @see com.buckosoft.PicMan.db.UsersDao#getUser(int)
54  	 */
55  	@SuppressWarnings("unchecked")
56  	public User getUser(int userid) throws DataAccessException {
57  		if (sql_getUserQUERYint == null)
58  			sql_getUserQUERYint = new UserQuery(ds, userid);
59  		if (DEBUG)
60  			logger.info("Getting account by id '" + userid + "'");
61  		List<KeyValue> list = sql_getUserQUERYint.execute(userid);
62  		User user = new User(null);
63  		Iterator<KeyValue>	iter = list.iterator();
64  		while (iter.hasNext()) {
65  			KeyValue k = iter.next();
66  			if (k.key.equals("authLevel"))
67  				user.setAuthLevel(Integer.parseInt(k.value));
68  			else if (k.key.equals("editorShowFileDates"))
69  				user.setEditorShowFileDates(k.value.equals("1") ? true : false);
70  			else if (k.key.equals("editorShowChangedFilters"))
71  				user.setEditorShowChangedFilters(k.value.equals("1") ? true : false);
72  			else if (k.key.equals("engineReportColumns"))
73  				user.setEngineReportColumns(Integer.parseInt(k.value));
74  			else if (k.key.equals("engineReportRows"))
75  				user.setEngineReportRows(Integer.parseInt(k.value));
76  			else if (k.key.equals("filterPagePicCount"))
77  				user.setFilterPagePicCount(Integer.parseInt(k.value));
78          	else if (k.key.equals("homePageThumbWidth"))
79          		user.setHomePageThumbWidth(new Integer(k.value).intValue());
80  			else if (k.key.equals("homeThumbDelay"))
81  				user.setHomeThumbDelay(Integer.parseInt(k.value));
82  			else if (k.key.equals("homeThumbSetName"))
83  				user.setHomeThumbSetName(k.value);
84  			else if (k.key.equals("homeDebug"))
85  				user.setHomeDebug(k.value.equals("1") ? true : false);
86  			else if (k.key.equals("mosaicOutputFile"))
87  				user.setMosaicOutputFile(k.value);
88  			else if (k.key.equals("mosaicPreviewHeight"))
89  				user.setMosaicPreviewHeight(Integer.parseInt(k.value));
90  			else if (k.key.equals("mosaicSetName"))
91  				user.setMosaicSetName(k.value);
92  			else if (k.key.equals("mosaicTileHeight"))
93  				user.setMosaicTileHeight(Integer.parseInt(k.value));
94  			else if (k.key.equals("mosaicTestPic"))
95  				user.setMosaicTestPic(k.value);
96  			else if (k.key.equals("picBrowserPicsPerPage"))
97  				user.setPicBrowserPicsPerPage(Integer.parseInt(k.value));
98  			else if (k.key.equals("picBrowserShowRatings"))
99  				user.setPicBrowserShowRatings(k.value.equals("1") ? true : false);
100 			else if (k.key.equals("showInactiveSets"))
101 				user.setSetManShowInactiveSets(k.value.equals("1") ? true : false);
102 			else if (k.key.equals("showRatings"))
103 				user.setShowRatings(k.value.equals("1") ? true : false);
104 			else if (k.key.equals("slideShowDelay"))
105 				user.setSlideShowDelay(Integer.parseInt(k.value));
106 			else if (k.key.equals("slideShowRandom"))
107 				user.setSlideShowRandom(k.value.equals("1") ? true : false);
108 			else if (k.key.equals("slideShowSid"))
109 				user.setSlideShowSid(Integer.parseInt(k.value));
110 			else if (k.key.equals("showInactiveChains"))
111 				user.setShowInactiveChains(k.value.equals("1") ? true : false);
112 			else if (k.key.equals("showInactiveRoots"))
113 				user.setShowInactiveRoots(k.value.equals("1") ? true : false);
114 
115 		}
116 		return(user);
117 	}
118 	private	UserQuery	sql_getUserQUERYint;
119 
120 	/* (non-Javadoc)
121 	 * @see com.buckosoft.PicMan.db.UsersDao#storeUser(com.buckosoft.PicMan.domain.User)
122 	 */
123 	public	void	storeUser(User user) {
124 		UserUpdate uu = new UserUpdate(ds);
125 		uu.update(user);
126 	}
127 
128 	class KeyValue {
129 		public int		userid;
130 		public String	key;
131 		public String	value;
132 	}
133 	
134 	class UserQuery extends MappingSqlQuery {
135 
136 		UserQuery(DataSource ds, int userid) {
137 			super(ds,  "SELECT * from users where userid = ?");
138 			declareParameter(new SqlParameter(Types.INTEGER));
139 			compile();
140 		}
141 
142 		protected Object mapRow(ResultSet rs, int rowNum) throws SQLException {
143 			KeyValue k = new KeyValue();
144 			k.key = rs.getString("key");
145 			k.value = rs.getString("value");
146 			return(k);
147 		}
148 	}
149 	/**
150 	 * <code>User</code> Update Object.
151 	 */
152 	private class UserUpdate extends SqlUpdate {
153 		private	DataSource	ds;
154 		
155 		/**
156 		 * Create a new instance of UserUpdate.
157 		 * @param ds the DataSource to use for the update
158 		 */
159 		protected UserUpdate(DataSource ds) {
160 			this.ds = ds;
161 		}
162 
163 		/**
164 		 * Method to update the <code>Users</code>'s data.
165 		 * @param User
166 		 * @return 0
167 		 */
168 		protected int update(User user) {
169 			// Delete the user from the table
170 			SqlUpdate sf = new SqlUpdate(ds, "DELETE FROM users WHERE userid = " + user.getUserId());
171 			sf.compile();
172 			int ret = sf.update();
173 			if (DEBUG)
174 				logger.info("user.delete() returned" + ret);
175 			
176 			// Save each of our attributes
177 			UserInsert si = new UserInsert(ds);
178 			KeyValue kv = new KeyValue();
179 			kv.userid = user.getUserId();
180 
181 			kv.key = "authLevel";
182 			kv.value = String.valueOf(user.getAuthLevel());
183 			si.insert(kv);
184 
185 			kv.key = "editorShowChangedFilters";
186 			kv.value = user.isEditorShowChangedFilters() ? "1" : "0";
187 			si.insert(kv);
188 
189 			kv.key = "editorShowFileDates";
190 			kv.value = user.isEditorShowFileDates() ? "1" : "0";
191 			si.insert(kv);
192 
193 			kv.key = "engineReportColumns";
194 			kv.value = String.valueOf(user.getEngineReportColumns());
195 			si.insert(kv);
196 
197 			kv.key = "engineReportRows";
198 			kv.value = String.valueOf(user.getEngineReportRows());
199 			si.insert(kv);
200 
201 			kv.key = "filterPagePicCount";
202 			kv.value = String.valueOf(user.getFilterPagePicCount());
203 			si.insert(kv);
204 
205 			kv.key = "homePageThumbWidth";
206 			kv.value = String.valueOf(user.getHomePageThumbWidth());
207 			si.insert(kv);
208 
209 			kv.key = "homeThumbDelay";
210 			kv.value = String.valueOf(user.getHomeThumbDelay());
211 			si.insert(kv);
212 
213 			kv.key = "homeThumbSetName";
214 			kv.value = user.getHomeThumbSetName();
215 			si.insert(kv);
216 
217 			kv.key = "homeDebug";
218 			kv.value = user.isHomeDebug() ? "1" : "0";
219 			si.insert(kv);
220 
221 			kv.key = "mosaicTestPic";
222 			kv.value = user.getMosaicTestPic();
223 			si.insert(kv);
224 
225 			kv.key = "mosaicSetName";
226 			kv.value = user.getMosaicSetName();
227 			si.insert(kv);
228 
229 			kv.key = "mosaicTileHeight";
230 			kv.value = String.valueOf(user.getMosaicTileHeight());
231 			si.insert(kv);
232 
233 			kv.key = "mosaicPreviewHeight";
234 			kv.value = String.valueOf(user.getMosaicPreviewHeight());
235 			si.insert(kv);
236 
237 			kv.key = "mosaicOutputFile";
238 			kv.value = user.getMosaicOutputFile();
239 			si.insert(kv);
240 
241 			kv.key = "picBrowserPicsPerPage";
242 			kv.value = String.valueOf(user.getPicBrowserPicsPerPage());
243 			si.insert(kv);
244 
245 			kv.key = "picBrowserShowRatings";
246 			kv.value = user.isPicBrowserShowRatings() ? "1" : "0";
247 			si.insert(kv);
248 
249 			kv.key = "showInactiveSets";
250 			kv.value = user.isSetManShowInactiveSets() ? "1" : "0";
251 			si.insert(kv);
252 
253 			kv.key = "showRatings";
254 			kv.value = user.isShowRatings() ? "1" : "0";
255 			si.insert(kv);
256 
257 			kv.key = "slideShowDelay";
258 			kv.value = String.valueOf(user.getSlideShowDelay());
259 			si.insert(kv);
260 
261 			kv.key = "slideShowRandom";
262 			kv.value = user.isSlideShowRandom() ? "1" : "0";
263 			si.insert(kv);
264 
265 			kv.key = "slideShowSid";
266 			kv.value = String.valueOf(user.getSlideShowSid());
267 			si.insert(kv);
268 
269 			kv.key = "showInactiveChains";
270 			kv.value = user.isShowInactiveChains() ? "1" : "0";
271 			si.insert(kv);
272 
273 			kv.key = "showInactiveRoots";
274 			kv.value = user.isShowInactiveRoots() ? "1" : "0";
275 			si.insert(kv);
276 
277 			
278 			return(0);
279 		}
280 	}
281 
282 
283 	/**
284 	 * <code>User</code> Insert Object.
285 	 */
286 	private class UserInsert extends SqlUpdate {
287 
288 		/**
289 		 * Create a new instance of SystemInsert.
290 		 * @param ds the DataSource to use for the insert
291 		 */
292 		protected UserInsert(DataSource ds) {
293 			super(ds, "INSERT INTO users VALUES(?,?,?)");
294 			declareParameter(new SqlParameter(Types.INTEGER));
295 			declareParameter(new SqlParameter(Types.VARCHAR));
296 			declareParameter(new SqlParameter(Types.VARCHAR));
297 			compile();
298 		}
299 
300 		protected void insert(KeyValue kv) {
301 			Object[] objs = new Object[] {
302 				new Integer(kv.userid), kv.key, kv.value 
303 				};
304 			
305 			super.update(objs);
306 		}
307 	}
308 }