View Javadoc
1   /******************************************************************************
2    * SystemDaoJdbc.java - Implement the Dao interface for the System bean
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  import java.util.Iterator;
12  
13  import javax.sql.DataSource;
14  
15  import java.sql.ResultSet;
16  import java.sql.SQLException;
17  import java.sql.Types;
18  
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  
22  import org.springframework.dao.DataAccessException;
23  import org.springframework.jdbc.core.SqlParameter;
24  import org.springframework.jdbc.object.SqlUpdate;
25  import org.springframework.jdbc.object.MappingSqlQuery;
26  
27  import com.buckosoft.PicMan.domain.System;
28  import com.buckosoft.PicMan.db.SystemDao;
29  
30  /** Implement the Dao Interface for the {@link com.buckosoft.PicMan.domain.System}.
31   * This table is key / value pairs so that {@link com.buckosoft.PicMan.domain.System} can be extended without
32   * altering the table. 
33   * @author Dick Balaska
34   * @since 2005/07/25
35   * @see <a href="http://cvs.buckosoft.com/Projects/java/PicMan/PicMan/src/com/buckosoft/PicMan/db/SystemDaoJdbc.java">SystemDaoJdbc.java</a>
36   */
37  public class SystemDaoJdbc implements SystemDao {
38  
39  	private final static boolean DEBUG = false;
40      protected final Log logger = LogFactory.getLog(getClass());
41  
42      private DataSource ds;
43      private	System	systemCache;
44      
45      /** Set the database reference
46       * @param ds The DataSource as generated by the Spring config/setup.
47       */
48      public void setDataSource(DataSource ds) {
49      	if (DEBUG)
50      		logger.info("Set Datasource.");
51          this.ds = ds;
52      }
53  
54      /** Return a copy of the PicMan System attributes.
55       * @return The System
56       */
57  	public System getSystem() throws DataAccessException {
58  		if (DEBUG)
59  			logger.info("getSystem()");
60  		if (systemCache != null)
61  			return(systemCache);
62  		SystemQuery sq = new SystemQuery(ds);
63  		@SuppressWarnings("unchecked")
64  		List<KeyValue> l = sq.execute();
65  		systemCache = new System();
66  		Iterator<KeyValue> it = l.iterator();
67  		while (it.hasNext()) {
68          	KeyValue k = (KeyValue)it.next();
69      		//logger.info("getSystem() k=" + k.key + " v=" + k.value );
70  //        	if (k.key.equals("root"))
71   //       		systemCache.addRoot(k.value);
72          	if (k.key.equals("picExtension"))
73          		systemCache.addPicExtension(k.value);
74          	else if (k.key.equals("thumbHeight"))
75          		systemCache.setThumbHeight(new Integer(k.value).intValue());
76          	else if (k.key.equals("mosaicDepth"))
77          		systemCache.setMosaicDepth(new Integer(k.value).intValue());
78          	else if (k.key.equals("mosaicThumbHeight"))
79          		systemCache.setMosaicThumbHeight(new Integer(k.value).intValue());
80          	else if (k.key.equals("mosaicPicsPerPage"))
81          		systemCache.setMosaicPicsPerPage(new Integer(k.value).intValue());
82          	else if (k.key.equals("useThumbCache"))
83          		systemCache.setUseThumbCache(new Boolean(k.value).booleanValue());
84          	else if (k.key.equals("thumbCacheDirectory"))
85          		systemCache.setThumbCacheDirectory(k.value);
86          	else if (k.key.equals("thumbCacheSize"))
87          		systemCache.setThumbCacheSize(new Integer(k.value).intValue());
88          	else if (k.key.equals("importDirectory"))
89          		systemCache.setImportDirectory(k.value);
90          	else if (k.key.equals("importsToDo"))
91          		systemCache.setImportsToDo(new Integer(k.value).intValue());
92          	else if (k.key.equals("virginsToDo"))
93          		systemCache.setVirginsToDo(new Integer(k.value).intValue());
94          	else if (k.key.equals("engineOn"))
95          		systemCache.setEngineOn(new Boolean(k.value).booleanValue());
96          	else if (k.key.equals("engineRunOnce"))
97          		systemCache.setEngineRunOnce(new Boolean(k.value).booleanValue());
98          	else if (k.key.equals("skipXThumbs"))
99          		systemCache.setSkipXThumbs(new Boolean(k.value).booleanValue());
100         	else if (k.key.equals("showRateOnPics"))
101         		systemCache.setShowRateOnPics(new Boolean(k.value).booleanValue());
102         	else if (k.key.equals("jobLogSummaryOnly"))
103         		systemCache.setJobLogSummaryOnly(new Boolean(k.value).booleanValue());
104         	else if (k.key.equals("engineCronExpression"))
105         		systemCache.setEngineCronExpression(k.value);
106         	else if (k.key.equals("mosaicThumbCacheDir"))
107         		systemCache.setMosaicThumbCacheDir(k.value);
108         	else if (k.key.equals("mosThumbCacheHeight"))
109         		systemCache.setMosaicThumbCacheHeight(new Integer(k.value).intValue());
110         	else if (k.key.equals("syncEnable"))
111         		systemCache.setSyncEnable(new Boolean(k.value).booleanValue());
112         	else if (k.key.equals("syncUrl"))
113         		systemCache.setSyncUrl(k.value);
114         }
115         return (systemCache);
116 	}
117 
118 	/** Save the System to the database
119 	 * @param system The System to write
120 	 * @throws DataAccessException whatever.
121 	 */
122 	public void setSystem(System system) throws DataAccessException {
123 		SystemUpdate au = new SystemUpdate(ds);
124 		au.update(system);
125 		systemCache = null;
126 	}
127 
128 	private class KeyValue {
129 		public String	key;
130 		public String	value;
131 	}
132 	
133 	private class SystemQuery extends MappingSqlQuery {
134 
135 		SystemQuery(DataSource ds) {
136             super(ds, "SELECT * from system");
137             compile();
138         }
139  
140         protected Object mapRow(ResultSet rs, int rowNum) throws SQLException {
141         	KeyValue k = new KeyValue();
142         	k.key = rs.getString("key");
143         	k.value = rs.getString("value");
144         	return(k);
145         }
146 	}
147 
148 	/**
149 	 * <code>System</code> Update Object.
150 	 */
151 	private class SystemUpdate extends SqlUpdate {
152 		private	DataSource	ds;
153 		
154 		/**
155 		 * Create a new instance of OwnerUpdate.
156 		 * @param ds the DataSource to use for the update
157 		 */
158 		protected SystemUpdate(DataSource ds) {
159 			this.ds = ds;
160 		}
161 
162 		/**
163 		 * Method to update the <code>System</code>'s data.
164 		 * @param System
165 		 * @return 0
166 		 */
167 		protected int update(System sys) {
168 			// Empty the existing table
169 			SqlUpdate sf = new SqlUpdate(ds, "TRUNCATE TABLE system");
170 			sf.compile();
171 			int ret = sf.update();
172 			if (DEBUG)
173 				logger.info("sf.empty() returned" + ret);
174 			
175 			// Save each of our attributes
176 			SystemInsert si = new SystemInsert(ds);
177 			Iterator<String> it;
178 			KeyValue kv = new KeyValue();
179 			it = sys.getPicExtensions().iterator();
180 			kv.key = "picExtension";
181 			while (it.hasNext())
182 			{
183 				kv.value = (String)it.next();
184 				si.insert(kv);
185 			}
186 			
187 			kv.key = "thumbHeight";
188 			kv.value = String.valueOf(sys.getThumbHeight());
189 			si.insert(kv);
190 
191 			kv.key = "mosaicDepth";
192 			kv.value = String.valueOf(sys.getMosaicDepth());
193 			si.insert(kv);
194 
195 			kv.key = "mosaicThumbHeight";
196 			kv.value = String.valueOf(sys.getMosaicThumbHeight());
197 			si.insert(kv);
198 
199 			kv.key = "mosaicPicsPerPage";
200 			kv.value = String.valueOf(sys.getMosaicPicsPerPage());
201 			si.insert(kv);
202 
203 			kv.key = "mosThumbCacheHeight";
204 			kv.value = String.valueOf(sys.getMosaicThumbCacheHeight());
205 			si.insert(kv);
206 
207 			kv.key = "useThumbCache";
208 			kv.value = String.valueOf(sys.isUseThumbCache());
209 			si.insert(kv);
210 
211 			kv.key = "thumbCacheDirectory";
212 			kv.value = sys.getThumbCacheDirectory();
213 			si.insert(kv);
214 			
215 			kv.key = "mosaicThumbCacheDir";
216 			kv.value = sys.getMosaicThumbCacheDir();
217 			si.insert(kv);
218 			
219 			kv.key = "thumbCacheSize";
220 			kv.value = String.valueOf(sys.getThumbCacheSize());
221 			si.insert(kv);
222 			
223 			kv.key = "importDirectory";
224 			kv.value = sys.getImportDirectory();
225 			si.insert(kv);
226 			
227 			kv.key = "importsToDo";
228 			kv.value = String.valueOf(sys.getImportsToDo());
229 			si.insert(kv);
230 
231 			kv.key = "virginsToDo";
232 			kv.value = String.valueOf(sys.getVirginsToDo());
233 			si.insert(kv);
234 
235 			kv.key = "engineOn";
236 			kv.value = String.valueOf(sys.isEngineOn());
237 			si.insert(kv);
238 
239 			kv.key = "engineRunOnce";
240 			kv.value = String.valueOf(sys.isEngineRunOnce());
241 			si.insert(kv);
242 
243 			kv.key = "skipXThumbs";
244 			kv.value = String.valueOf(sys.isSkipXThumbs());
245 			si.insert(kv);
246 
247 			kv.key = "showRateOnPics";
248 			kv.value = String.valueOf(sys.isShowRateOnPics());
249 			si.insert(kv);
250 						
251 			kv.key = "jobLogSummaryOnly";
252 			kv.value = String.valueOf(sys.isJobLogSummaryOnly());
253 			si.insert(kv);
254 
255 			kv.key = "engineCronExpression";
256 			kv.value = sys.getEngineCronExpression();
257 			si.insert(kv);
258 			
259 			kv.key = "syncEnable";
260 			kv.value = String.valueOf(sys.isSyncEnable());
261 			si.insert(kv);
262 
263 			kv.key = "syncUrl";
264 			kv.value = sys.getSyncUrl();
265 			si.insert(kv);
266 			
267 			return(0);
268 		}
269 	}
270 
271 
272 	/**
273 	 * <code>System</code> Insert Object.
274 	 */
275 	private class SystemInsert extends SqlUpdate {
276 
277 		/**
278 		 * Create a new instance of SystemInsert.
279 		 * @param ds the DataSource to use for the insert
280 		 */
281 		protected SystemInsert(DataSource ds) {
282 			super(ds, "INSERT INTO system VALUES(?,?)");
283 			declareParameter(new SqlParameter(Types.VARCHAR));
284 			declareParameter(new SqlParameter(Types.VARCHAR));
285 			compile();
286 		}
287 
288 		protected void insert(KeyValue kv) {
289 			Object[] objs = new Object[] {
290 				kv.key, kv.value};
291 			super.update(objs);
292 		}
293 	}
294 }