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