View Javadoc
1   /******************************************************************************
2    * MosaicForm.java - The backing form to edit PicMan's Mosaic setup
3    * 
4    * PicMan - The BuckoSoft Picture Manager in Java
5    * Copyright(c) 2007 - Dick Balaska
6    * 
7    */
8   package com.buckosoft.PicMan.web;
9   
10  import java.util.HashMap;
11  import java.util.LinkedList;
12  import java.util.List;
13  
14  import org.apache.commons.logging.Log;
15  import org.apache.commons.logging.LogFactory;
16  
17  import com.buckosoft.BSAccount.web.BSAccountUserWebSession;
18  import com.buckosoft.PicMan.business.PicManFacade;
19  import com.buckosoft.PicMan.business.mosaic.MosaicEngine;
20  import com.buckosoft.PicMan.business.mosaic.MosaicEngine.ConfigItem;
21  import com.buckosoft.PicMan.business.util.Classes;
22  import com.buckosoft.PicMan.domain.Mosaic;
23  import com.buckosoft.PicMan.domain.MosaicBatch;
24  import com.buckosoft.PicMan.domain.Set;
25  
26  /** The backing form to edit PicMan's Mosaic setup
27   * @author dick
28   *
29   */
30  public class MosaicSetupForm {
31  	private	final static boolean DEBUG = true;
32      protected final Log logger = LogFactory.getLog(getClass());
33  
34      private	BSAccountUserWebSession	userWebSession;
35  	private	Mosaic			mosaic;
36  	private	String			masterPic;
37  	private	int				mosaicHeight;
38  	private	String			mosaicOutputFile;
39  
40  //	private	String			mosaicSetName;
41  	private	int				mosaicSid;
42  	private	List<Set>		sets;
43  	private	boolean			startBuilding;
44  	private	boolean			makeBackups = true;
45  	private	String			mosaicName = "New Mosaic";
46  	private	List<Mosaic>	mosaics;
47  	private	List<MosaicBatch>	mosaicBatches;
48  
49  	private	String			mosaicEngineName;
50  	private	List<String>	engines;
51  	private	String			mosaicSelectName = "";
52  	private	boolean			batchMode = false;
53  	private	String			mosaicSizes;
54  
55  	private	String			mosaicEngineConfig;
56  	private	String			mosaicConfigs;
57  	
58  	@SuppressWarnings("unused")
59  	private PicManFacade pmf;
60  
61  	/** Set the reference to the PicMan API.
62  	 * @param pmf The PicManFacade
63  	 */
64  	public void setPicMan(PicManFacade pmf) { this.pmf = pmf; }
65  	
66  	public	BSAccountUserWebSession	getUserWebSession() { return(userWebSession); }
67  	public	void		setUserWebSession(BSAccountUserWebSession us) { userWebSession = us; }
68  
69  	
70  	/**
71  	 * @return the masterPic
72  	 */
73  	public String getMasterPic() {
74  		return masterPic;
75  	}
76  	/**
77  	 * @param masterPic the masterPic to set
78  	 */
79  	public void setMasterPic(String masterPic) {
80  		this.masterPic = masterPic;
81  	}
82  	/**
83  	 * @return the mosaic
84  	 */
85  	public Mosaic getMosaic() {
86  		return mosaic;
87  	}
88  	/**
89  	 * @param mosaic the mosaic to set
90  	 */
91  	public void setMosaic(Mosaic mosaic) {
92  		this.mosaic = mosaic;
93  	}
94  	
95  	/**
96  	 * @return the mosaicSelectName
97  	 */
98  	public String getMosaicSelectName() {
99  		return mosaicSelectName;
100 	}
101 	/**
102 	 * @param mosaicSelectName the mosaicSelectName to set
103 	 */
104 	public void setMosaicSelectName(String mosaicSelectName) {
105 		this.mosaicSelectName = mosaicSelectName;
106 	}
107 	/**
108 	 * @return the mosaicName
109 	 */
110 	public String getMosaicName() {
111 		return mosaicName;
112 	}
113 	/**
114 	 * @param mosaicName the mosaicName to set
115 	 */
116 	public void setMosaicName(String mosaicName) {
117 		this.mosaicName = mosaicName;
118 	}
119 	
120 	/**
121 	 * @return the mosaics
122 	 */
123 	public List<Mosaic> getMosaics() {
124 		return mosaics;
125 	}
126 	/**
127 	 * @param mosaics the mosaics to set
128 	 */
129 	public void setMosaics(List<Mosaic> mosaics) {
130 		this.mosaics = mosaics;
131 	}
132 	
133 	/**
134 	 * @param mbatches the mbatches to set
135 	 */
136 	public void setMosaicBatches(List<MosaicBatch> mbatches) {
137 		this.mosaicBatches = mbatches;	
138 	}	
139 	/**
140 	 * @return the mosaicBatches
141 	 */
142 	public List<MosaicBatch> getMosaicBatches() {
143 		return mosaicBatches;
144 	}
145 	/**
146 	 * @return the mosaicEngineName
147 	 */
148 	public String getMosaicEngineName() {
149 		return mosaicEngineName;
150 	}
151 	/**
152 	 * @param mosaicEngineName the mosaicEngineName to set
153 	 */
154 	public void setMosaicEngineName(String mosaicEngineName) {
155 		this.mosaicEngineName = mosaicEngineName;
156 	}
157 	/**
158 	 * @return the engines
159 	 */
160 	public List<String> getEngines() {
161 		return engines;
162 	}
163 	/**
164 	 * @param engines the engines to set
165 	 */
166 	public void setEngines(List<String> engines) {
167 		this.engines = engines;
168 	}
169 	/**
170 	 * @return the mosaicHeight
171 	 */
172 	public int getMosaicHeight() {
173 		return mosaicHeight;
174 	}
175 	/**
176 	 * @param mosaicHeight the mosaicHeight to set
177 	 */
178 	public void setMosaicHeight(int mosaicHeight) {
179 		this.mosaicHeight = mosaicHeight;
180 	}
181 
182 	/** Where to write the mosaic output
183 	 * @return the mosaicOutputFile
184 	 */
185 	public String getMosaicOutputFile() {
186 		return mosaicOutputFile;
187 	}
188 	/**
189 	 * @param mosaicOutputFile the mosaicOutputFile to set
190 	 */
191 	public void setMosaicOutputFile(String mosaicOutputFile) {
192 		this.mosaicOutputFile = mosaicOutputFile;
193 	}
194 	/**
195 	 * @return the startBuilding
196 	 */
197 	public boolean isStartBuilding() {
198 		return startBuilding;
199 	}
200 	/**
201 	 * @param startBuilding the startBuilding to set
202 	 */
203 	public void setStartBuilding(boolean startBuilding) {
204 		this.startBuilding = startBuilding;
205 	}
206 
207 	/** Should we save the mosaic output jpeg before overwriting it with a new file?
208 	 * @return the makeBackups
209 	 */
210 	public boolean isMakeBackups() {
211 		return makeBackups;
212 	}
213 
214 	/** Should we save the mosaic output jpeg before overwriting it with a new file?
215 	 * @param makeBackups the makeBackups to set
216 	 */
217 	public void setMakeBackups(boolean makeBackups) {
218 		this.makeBackups = makeBackups;
219 	}
220 	/**
221 	 * @return the mosaicSid
222 	 */
223 	public int getMosaicSid() {
224 		return mosaicSid;
225 	}
226 	/**
227 	 * @param mosaicSid the mosaicSid to set
228 	 */
229 	public void setMosaicSid(int mosaicSid) {
230 		this.mosaicSid = mosaicSid;
231 	}
232 	/**
233 	 * @return the sets
234 	 */
235 	public List<Set> getSets() {
236 		return sets;
237 	}
238 	/**
239 	 * @param sets the sets to set
240 	 */
241 	public void setSets(List<Set> sets) {
242 		this.sets = sets;
243 	}
244 	/**
245 	 * @return the batchMode
246 	 */
247 	public boolean isBatchMode() {
248 		return batchMode;
249 	}
250 	/**
251 	 * @param batchMode the batchMode to set
252 	 */
253 	public void setBatchMode(boolean batchMode) {
254 		this.batchMode = batchMode;
255 	}
256 	/** If this is a Batch Mosaic, then this is the sizes to make.
257 	 * @return the mosaicSizes
258 	 */
259 	public String getMosaicSizes() {
260 		return mosaicSizes;
261 	}
262 	/**
263 	 * @param mosaicSizes the mosaicSizes to set
264 	 */
265 	public void setMosaicSizes(String mosaicSizes) {
266 		this.mosaicSizes = mosaicSizes;
267 	}
268 
269 	/**
270 	 * @return the mosaicEngineConfig
271 	 */
272 	public String getMosaicEngineConfig() {
273 		return mosaicEngineConfig;
274 	}
275 
276 	/**
277 	 * @param mosaicEngineConfig the mosaicEngineConfig to set
278 	 */
279 	public void setMosaicEngineConfig(String mosaicEngineConfig) {
280 		this.mosaicEngineConfig = mosaicEngineConfig;
281 	}
282 
283 	/**
284 	 * @return the mosaicConfigs
285 	 */
286 	public String getMosaicConfigs() {
287 		return mosaicConfigs;
288 	}
289 
290 	/**
291 	 * @param mosaicConfigs the mosaicConfigs to set
292 	 */
293 	public void setMosaicConfigs(String mosaicConfigs) {
294 		this.mosaicConfigs = mosaicConfigs;
295 	}
296 
297 	
298 /*	public String getEngineConfig() {
299 		if (mosaicEngineConfig == null && mosaicEngineName != null && mosaicEngineName.length() > 1) 
300 			mosaicEngineConfig = pmf.getMosaicMan().getMosaicConfigParams(mosaicEngineName);
301 		return(mosaicEngineConfig);
302 	}
303 */
304 
305 	public static class MosaicConfig {
306 		public String engineConfig;
307 		public String mosaicConfig;
308 		public List<String>	engines;
309 		
310 	}
311 	
312 	public static MosaicConfig getConfigs(List<Mosaic> list) {
313 		MosaicConfig mosaicConfig = new MosaicConfig();
314 		mosaicConfig.engines = new LinkedList<String>();
315 		StringBuffer sb = new StringBuffer();
316 		sb.append("var mosaicConfigs = new Array();\n");
317 		for (Mosaic mb : list) {
318 			if (!mb.getEngineConfig().isEmpty()) {
319 				sb.append("mosaicConfigs[" + mb.getMid() + "] = [\n");
320 				for (String key : mb.getEngineConfig().keySet()) {
321 					sb.append("new KeyValue(\"" + key + "\",\"" + mb.getEngineConfig().get(key) + "\"),\n");
322 				}
323 				sb.append("];\n");
324 			}
325 		}
326 		mosaicConfig.mosaicConfig = sb.toString();
327 /*
328 		Enumeration<URL> eu	= ClassLoader.getSystemResources("com/buckosoft/PicMan/business/mosaic/engine/*");
329 		if (DEBUG) {
330 			//logger.info("eu check");
331 			while (eu.hasMoreElements()) {
332 				URL u = eu.nextElement();
333 				//logger.info("u=" + u.toString());
334 			}
335 		}
336 */
337 		//Package p = this.getClass().getClassLoader().getPackage("com/buckosoft/PicMan/business/mosaic/engine");
338 		int i;
339 		sb.setLength(0);
340 		String[] cl = Classes.getClasses("com.buckosoft.PicMan.business.mosaic.engine");
341 //		List<String> classes = new LinkedList<String>();
342 		for (i=0; i<cl.length; i++) {
343 			if (cl[i].indexOf('$') != -1)
344 				continue;
345 			mosaicConfig.engines.add(cl[i]);
346 			addEngineConfig(sb, cl[i]);
347 		}
348 		if (DEBUG) {
349 			for (i=0; i<cl.length; i++) {
350 				//logger.info("class: " + cl[i]);
351 			}
352 		}
353 		mosaicConfig.engineConfig = sb.toString();
354 		
355 		return(mosaicConfig);
356 	}
357 
358 	private	static void addEngineConfig(StringBuffer sb, String className) {
359 		MosaicEngine	mosaicEngine;
360 	
361 		try {
362 			mosaicEngine = (MosaicEngine)Class.forName("com.buckosoft.PicMan.business.mosaic.engine."
363 							+ className).newInstance();
364 		} catch (Exception e) {
365 			Exception ex = new Exception("Can't instantiate Mosaic Engine: " + className, e);
366 			LogFactory.getLog(MosaicSetupForm.class).warn("addEngineConfig", ex);
367 			//pmf.addError(ex);
368 			sb.append("engineConfigArray[\"" + className + "\"]=\"<tr><td colspan=\"2\">addEngineConfig Can't instantiate '" + className + "'</td></tr>\";\n");
369 			return;
370 		}
371 		HashMap<String, ConfigItem> configMap = mosaicEngine.getConfigMap();
372 		if (configMap.isEmpty())
373 			return;
374 		sb.append("engineConfigArray[\"" + className + "\"]=\'");
375 		int flipFlop = 0;
376 		for (String key : configMap.keySet()) {
377 			ConfigItem item = configMap.get(key);
378 			sb.append("<tr class=\"back" + flipFlop + "\"><td>");
379 			sb.append(item.getDescription());
380 			sb.append("</td><td><input type=\"text\" name=\"");
381 			sb.append(key);
382 			sb.append("\" id=\"inp_");
383 			sb.append(key);
384 			sb.append("\" value=\"");
385 			if (item.getType() == ConfigItem.TYPE_INT)
386 				sb.append("" + item.getValueInt());
387 			sb.append("\"/>");
388 			flipFlop++;
389 			if (flipFlop >= 2)
390 				flipFlop = 0;
391 		}
392 		sb.append("';\n");
393 	}
394 	
395 
396 }