1
2
3
4
5
6
7
8 package com.buckosoft.PicMan.web;
9
10 import java.text.DecimalFormat;
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.HashMap;
14 import java.util.LinkedList;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.StringTokenizer;
18
19 import javax.servlet.http.HttpServletRequest;
20 import javax.servlet.http.HttpServletResponse;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.springframework.validation.BindException;
25 import org.springframework.web.servlet.ModelAndView;
26
27 import com.buckosoft.BSAccount.web.BSAccountSimpleFormController;
28 import com.buckosoft.BSAccount.web.BSAccountUserWebSession;
29 import com.buckosoft.PicMan.business.PicManFacade;
30 import com.buckosoft.PicMan.business.mosaic.MosaicEngine;
31 import com.buckosoft.PicMan.business.mosaic.MosaicEngine.ConfigItem;
32 import com.buckosoft.PicMan.db.DatabaseFacade;
33 import com.buckosoft.PicMan.domain.Mosaic;
34 import com.buckosoft.PicMan.domain.MosaicBatch;
35 import com.buckosoft.PicMan.domain.User;
36 import com.buckosoft.PicMan.web.MosaicSetupForm.MosaicConfig;
37
38
39
40
41
42
43 public class MosaicBatchSetupFormController extends BSAccountSimpleFormController {
44 private final static boolean DEBUG = true;
45 protected final Log logger = LogFactory.getLog(getClass());
46
47 private DatabaseFacade dbf;
48 private PicManFacade pmf;
49
50
51
52
53 public void setDatabase(DatabaseFacade dbf) { this.dbf = dbf; }
54
55
56
57
58
59 public void setPicMan(PicManFacade pmf) { this.pmf = pmf; }
60
61
62 public MosaicBatchSetupFormController() {
63 setSessionForm(true);
64 setValidateOnBinding(false);
65 setCommandName("mosaicSetupForm");
66 setFormView("MosaicSetupForm");
67 }
68
69 protected Object formBackingObject(HttpServletRequest request) throws Exception {
70 BSAccountUserWebSession userWebSession = this.bsAccountMan.getUserWebSession(request);
71 User user = (User)userWebSession.getUser();
72 if (DEBUG)
73 logger.info("formBackingObject mosaicForm with session " + userWebSession);
74
75 MosaicSetupForm mf = new MosaicSetupForm();
76 mf.setUserWebSession(userWebSession);
77 mf.setBatchMode(true);
78 mf.setMosaic(new Mosaic());
79 mf.setMasterPic(user.getMosaicTestPic());
80 mf.setMosaicHeight(user.getMosaicTileHeight());
81 mf.setMosaicOutputFile(user.getMosaicOutputFile());
82 mf.setSets(dbf.getSets());
83
84 List<MosaicBatch> list = dbf.getMosaicBatches();
85
86
87
88
89
90
91 Collections.sort(list);
92 mf.setMosaicBatches(list);
93 List<Mosaic> list1 = new LinkedList<Mosaic>();
94 for (MosaicBatch mb : list)
95 list1.add(mb);
96 MosaicConfig mc = MosaicSetupForm.getConfigs(list1);
97 mf.setMosaicConfigs(mc.mosaicConfig);
98 mf.setMosaicEngineConfig(mc.engineConfig);
99 mf.setEngines(mc.engines);
100 return(mf);
101 }
102
103 protected void onBindAndValidate(HttpServletRequest request, Object command, BindException errors)
104 throws Exception {
105 MosaicSetupForm msf = (MosaicSetupForm)command;
106 if (DEBUG)
107 logger.info("onBindAndValidate");
108 if (request.getParameter("startBuilding") == null) {
109 msf.setStartBuilding(false);
110 } else
111 msf.setStartBuilding(true);
112
113 }
114
115 protected Map<String, Object> referenceData(HttpServletRequest request) throws Exception {
116 BSAccountUserWebSession userWebSession = this.bsAccountMan.getUserWebSession(request);
117 Map<String, Object> myModel = new HashMap<String, Object>();
118 myModel.put("userWebSession", userWebSession);
119 if (DEBUG)
120 logger.info("referenceData mosaicForm with session " + userWebSession);
121
122 return myModel;
123 }
124
125 protected ModelAndView onSubmit(
126 HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)
127 throws Exception {
128 String v;
129 BSAccountUserWebSession userWebSession = this.bsAccountMan.getUserWebSession(request);
130 User user = (User)userWebSession.getUser();
131 MosaicSetupForm msf = (MosaicSetupForm)command;
132 boolean userSettingsModified = false;
133 boolean success = true;
134
135 MosaicBatch mbatch = pmf.getDB().getMosaicBatch(msf.getMosaic().getMid());
136 if (mbatch == null)
137 mbatch = new MosaicBatch();
138
139 mbatch.setName(msf.getMosaic().getName());
140 mbatch.setMasterPic(msf.getMasterPic());
141 mbatch.setSid(msf.getMosaicSid());
142 mbatch.setEngine(msf.getMosaicEngineName());
143 mbatch.setSizes(msf.getMosaicSizes());
144
145 mbatch.setOutPic(msf.getMosaicOutputFile());
146 mbatch.setBatch(true);
147
148 HashMap<String, String> engineConfig = mbatch.getEngineConfig();
149 engineConfig.clear();
150 String n = msf.getMosaicEngineName();
151 if (n != null && n.length() > 0) {
152 MosaicEngine mosaicEngine = null;
153 try {
154 mosaicEngine = (MosaicEngine)Class.forName("com.buckosoft.PicMan.business.mosaic.engine."
155 + n).newInstance();
156 } catch (Exception e) {
157 Exception ex = new Exception("Can't instantiate Mosaic Engine: " + n, e);
158 pmf.addError(ex);
159 logger.error(ex);
160 }
161 if (mosaicEngine != null) {
162 HashMap<String, ConfigItem> configMap = mosaicEngine.getConfigMap();
163 if (!configMap.isEmpty()) {
164 for (String key : configMap.keySet()) {
165
166 if ((v = request.getParameter(key)) != null) {
167 engineConfig.put(key, v);
168 logger.info("put k=" + key + " v=" + v);
169 }
170 }
171 }
172 }
173 }
174 pmf.getDB().storeMosaicBatch(mbatch);
175 if (user.getMosaicTestPic() != msf.getMasterPic()) {
176 user.setMosaicTestPic(msf.getMasterPic());
177 userSettingsModified = true;
178 }
179 if (userSettingsModified)
180 this.dbf.storeUser(user);
181
182 if (msf.isStartBuilding()) {
183
184 success = startBuilding(mbatch, errors);
185 }
186 if (success && !response.isCommitted()) {
187 response.sendRedirect("mosaicBatchSetup.do");
188 return(null);
189 }
190 else
191 return(showForm(request, response, errors));
192 }
193
194 DecimalFormat df3 = new DecimalFormat("000");
195
196 private boolean startBuilding(MosaicBatch mbatch, BindException errors) {
197 StringTokenizer st = new StringTokenizer(mbatch.getSizes(), ",\n\r");
198 if (DEBUG)
199 logger.info("startBuilding: sizes='" + mbatch.getSizes() + "'");
200 String range;
201 String start;
202 String end;
203 String increment;
204 ArrayList<Integer> list = new ArrayList<Integer>();
205 while (st.hasMoreTokens()) {
206 range = st.nextToken().trim();
207 increment = "1";
208 if (DEBUG)
209 logger.info("parsing token '" + range + "'");
210 if (range.indexOf('/') != -1) {
211 StringTokenizer sst = new StringTokenizer(range,"/");
212 String s = sst.nextToken().trim();
213 increment = sst.nextToken().trim();
214 range = s.trim();
215 }
216 if (range.indexOf('-') != -1) {
217 StringTokenizer sst = new StringTokenizer(range,"-");
218 start = sst.nextToken().trim();
219 end = sst.nextToken().trim();
220 } else {
221 start = range;
222 end = range;
223 }
224 int istart = -1;
225 int iend = -1;
226 int inc = -1;
227 try {
228 istart = Integer.parseInt(start);
229 iend = Integer.parseInt(end);
230 inc = Integer.parseInt(increment);
231 } catch (NumberFormatException e) {
232 e.printStackTrace();
233 pmf.addError(e);
234 errors.reject("XXX", e.getLocalizedMessage());
235 return(false);
236 }
237 if (istart == -1 || iend == -1 || inc == -1) {
238 errors.reject("XXX", "Nothing parsed?");
239 return(false);
240 }
241 if (DEBUG) {
242 logger.info("istart= " + istart);
243 logger.info("iend = " + iend);
244 logger.info("inc = " + inc);
245 }
246 if (istart <= iend) {
247 for (int i=istart; i<=iend; i += inc)
248 list.add(new Integer(i));
249 } else {
250 for (int i=istart; i>=iend; i -= inc)
251 list.add(new Integer(i));
252 }
253 }
254 Integer[] a = new Integer[list.size()];
255 list.toArray(a);
256
257 for (int i=0; i<a.length; i++) {
258 if (DEBUG)
259 logger.info("Process size: " + a[i]);
260
261
262 Mosaic mosaic = null;
263 if (mosaic == null) {
264 mosaic = new Mosaic();
265 mosaic.setMasterPic(mbatch.getMasterPic());
266 mosaic.setEngine(mbatch.getEngine());
267 mosaic.setSid(mbatch.getSid());
268 mosaic.setTileHeight(a[i]);
269 mosaic.setOutPic(mbatch.getOutPic() + df3.format(a[i]) + ".jpg");
270 mosaic.setName(mbatch.getName() + df3.format(a[i]));
271 mosaic.setEngineConfig(mbatch.getEngineConfig());
272 mosaic.setBatch(true);
273 }
274 if (mosaic.getMid() != 0)
275 pmf.getDB().deleteMosaicTiles(mosaic.getMid());
276 pmf.getMosaicMan().queueForBuilding(mosaic);
277 }
278 return(true);
279 }
280 }