View Javadoc
1   /******************************************************************************
2    * MosaicSetupFormController.java - The Spring controller 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.Collections;
11  import java.util.HashMap;
12  import java.util.Iterator;
13  import java.util.List;
14  import java.util.Map;
15  
16  import javax.servlet.http.HttpServletRequest;
17  import javax.servlet.http.HttpServletResponse;
18  
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  import org.springframework.validation.BindException;
22  import org.springframework.web.servlet.ModelAndView;
23  
24  import com.buckosoft.BSAccount.web.BSAccountSimpleFormController;
25  import com.buckosoft.BSAccount.web.BSAccountUserWebSession;
26  import com.buckosoft.PicMan.business.PicManFacade;
27  import com.buckosoft.PicMan.business.mosaic.MosaicEngine;
28  import com.buckosoft.PicMan.business.mosaic.MosaicEngine.ConfigItem;
29  import com.buckosoft.PicMan.db.DatabaseFacade;
30  import com.buckosoft.PicMan.domain.Mosaic;
31  import com.buckosoft.PicMan.domain.User;
32  import com.buckosoft.PicMan.web.MosaicSetupForm.MosaicConfig;
33  
34  /** The Spring controller to edit PicMan's Mosaic setup
35   * @author Dick Balaska
36   * @since 2007/12/17
37   * @see <a href="http://cvs.buckosoft.com/Projects/java/PicMan/PicMan/src/com/buckosoft/PicMan/web/MosaicSetupFormController.java">MosaicSetupFormController.java</a>
38   */
39  public class MosaicSetupFormController extends BSAccountSimpleFormController {
40  	private	final static boolean DEBUG = true;
41      protected final Log logger = LogFactory.getLog(getClass());
42  
43  	private DatabaseFacade dbf;
44  	private PicManFacade pmf;
45  
46  	/** Set the reference to the PicMan Database
47  	 * @param dbf The DatabaseFacade
48  	 */
49  	public void setDatabase(DatabaseFacade dbf) { this.dbf = dbf; }
50  	//public DatabaseFacade getDatabase() { return(dbf); }
51  
52  	/** Set the reference to the PicMan API.
53  	 * @param pmf The PicManFacade
54  	 */
55  	public void setPicMan(PicManFacade pmf) { this.pmf = pmf; }
56  	//public PicManFacade getPicMan() { return(pmf); }
57  
58  	public MosaicSetupFormController() {
59  		setSessionForm(true);
60  		setValidateOnBinding(false);
61  		setCommandName("mosaicSetupForm");
62  		setFormView("MosaicSetupForm");
63  	}
64  
65  	protected Object formBackingObject(HttpServletRequest request) throws Exception {
66      	BSAccountUserWebSession userWebSession = this.bsAccountMan.getUserWebSession(request);
67      	User user = (User)userWebSession.getUser();
68  		if (DEBUG)
69  			logger.info("formBackingObject mosaicForm with session " + userWebSession);
70  
71  		MosaicSetupForm mf = new MosaicSetupForm();
72  		mf.setUserWebSession(userWebSession);
73  		mf.setPicMan(pmf);
74  		mf.setMosaic(new Mosaic());
75  		mf.setMasterPic(user.getMosaicTestPic());
76  		mf.setMosaicHeight(user.getMosaicTileHeight());
77  		mf.setMosaicOutputFile(user.getMosaicOutputFile());
78  		mf.setSets(dbf.getSets());
79  //		mf.setMosaicSetName(user.getMosaicSetName());
80  		List<Mosaic> list = dbf.getMosaics();
81  		Iterator<Mosaic> iter = list.iterator();
82  		while (iter.hasNext()) {
83  			Mosaic m = iter.next();
84  			if (m.isBatch())
85  				iter.remove();
86  		}
87  		Collections.sort(list);
88  		mf.setMosaics(list);
89  		
90  		MosaicConfig mc = MosaicSetupForm.getConfigs(list);
91  		mf.setMosaicConfigs(mc.mosaicConfig);
92  		mf.setMosaicEngineConfig(mc.engineConfig);
93  		mf.setEngines(mc.engines);
94  		return(mf);
95  	}
96  
97  	protected void onBindAndValidate(HttpServletRequest request, Object command, BindException errors)
98  			throws Exception {
99  		MosaicSetupForm msf = (MosaicSetupForm)command;
100 		if (request.getParameter("startBuilding") == null) {
101 			msf.setStartBuilding(false);
102 		} else
103 			msf.setStartBuilding(true);
104 
105 	}
106 
107 	protected Map<String, Object> referenceData(HttpServletRequest request) throws Exception {
108     	BSAccountUserWebSession userWebSession = this.bsAccountMan.getUserWebSession(request);
109 		Map<String, Object> myModel = new HashMap<String, Object>();
110         myModel.put("userWebSession", userWebSession);
111         if (DEBUG)
112         	logger.info("referenceData mosaicForm with session " + userWebSession);
113 
114 		return myModel;
115 	}
116 
117 	protected ModelAndView onSubmit(
118 			HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)
119 			throws Exception {
120 
121     	BSAccountUserWebSession userWebSession = this.bsAccountMan.getUserWebSession(request);
122     	User user = (User)userWebSession.getUser();
123 		MosaicSetupForm msf = (MosaicSetupForm)command;
124 		boolean userSettingsModified = false;
125 		boolean success = true;
126 
127 		Mosaic mosaic = pmf.getDB().getMosaic(msf.getMosaic().getMid());
128 		if (mosaic == null)
129 			mosaic = new Mosaic();
130 	
131 		mosaic.setName(msf.getMosaic().getName());
132 		mosaic.setMasterPic(msf.getMasterPic());
133 		mosaic.setSid(msf.getMosaicSid());
134 		mosaic.setEngine(msf.getMosaicEngineName());
135 		mosaic.setTileHeight(msf.getMosaicHeight());
136 		mosaic.setOutPic(msf.getMosaicOutputFile());
137 		
138 		String v;
139 		HashMap<String, String> engineConfig = mosaic.getEngineConfig();
140 		engineConfig.clear();
141 		String n = msf.getMosaicEngineName();
142 		if (n != null && n.length() > 0) {
143 			MosaicEngine mosaicEngine = null;
144 			try {
145 				mosaicEngine = (MosaicEngine)Class.forName("com.buckosoft.PicMan.business.mosaic.engine."
146 								+ n).newInstance();
147 			} catch (Exception e) {
148 				Exception ex = new Exception("Can't instantiate Mosaic Engine: " + n, e);
149 				pmf.addError(ex);
150 				logger.error(ex);
151 			}
152 			if (mosaicEngine != null) {
153 				HashMap<String, ConfigItem> configMap = mosaicEngine.getConfigMap();
154 				if (!configMap.isEmpty()) {
155 					for (String key : configMap.keySet()) {
156 						//ConfigItem item = configMap.get(key);
157 						if ((v = request.getParameter(key)) != null) {
158 							engineConfig.put(key, v);
159 							logger.info("put k=" + key + " v=" + v);
160 						}
161 					}
162 				}
163 			}
164 		}
165 
166 		pmf.getDB().storeMosaic(mosaic);
167 		if (user.getMosaicTileHeight() != msf.getMosaicHeight()) {
168 			user.setMosaicTileHeight(msf.getMosaicHeight());
169 			userSettingsModified = true;
170 		}
171 		if (user.getMosaicTestPic() != msf.getMasterPic()) {
172 			user.setMosaicTestPic(msf.getMasterPic());
173 			userSettingsModified = true;
174 		}
175 //		if (!user.getMosaicSetName().equals(msf.getMosaicSetName())) {
176 //			user.setMosaicSetName(msf.getMosaicSetName());
177 //			userSettingsModified = true;
178 //		}
179 		if (!user.getMosaicOutputFile().equals(msf.getMosaicOutputFile())) {
180 			user.setMosaicOutputFile(msf.getMosaicOutputFile());
181 			userSettingsModified = true;
182 		}
183 		if (userSettingsModified)
184 			this.dbf.storeUser(user);
185 
186 		if (msf.isStartBuilding()) {
187 			pmf.getMosaicMan().queueForBuilding(mosaic);
188 		}
189 		if (success && !response.isCommitted()) {
190 			response.sendRedirect("mosaicSetup.do");
191 			return(null);
192 		}
193 		else
194 			return(showForm(request, response, errors));
195 	}
196 
197 }