Coverage Report - com.buckosoft.PicMan.web.MosaicSetupFormController
 
Classes in this File Line Coverage Branch Coverage Complexity
MosaicSetupFormController
0%
0/96
0%
0/34
4
 
 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  0
     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  0
         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  0
         public void setPicMan(PicManFacade pmf) { this.pmf = pmf; }
 56  
         //public PicManFacade getPicMan() { return(pmf); }
 57  
 
 58  0
         public MosaicSetupFormController() {
 59  0
                 setSessionForm(true);
 60  0
                 setValidateOnBinding(false);
 61  0
                 setCommandName("mosaicSetupForm");
 62  0
                 setFormView("MosaicSetupForm");
 63  0
         }
 64  
 
 65  
         protected Object formBackingObject(HttpServletRequest request) throws Exception {
 66  0
             BSAccountUserWebSession userWebSession = this.bsAccountMan.getUserWebSession(request);
 67  0
             User user = (User)userWebSession.getUser();
 68  
                 if (DEBUG)
 69  0
                         logger.info("formBackingObject mosaicForm with session " + userWebSession);
 70  
 
 71  0
                 MosaicSetupForm mf = new MosaicSetupForm();
 72  0
                 mf.setUserWebSession(userWebSession);
 73  0
                 mf.setPicMan(pmf);
 74  0
                 mf.setMosaic(new Mosaic());
 75  0
                 mf.setMasterPic(user.getMosaicTestPic());
 76  0
                 mf.setMosaicHeight(user.getMosaicTileHeight());
 77  0
                 mf.setMosaicOutputFile(user.getMosaicOutputFile());
 78  0
                 mf.setSets(dbf.getSets());
 79  
 //                mf.setMosaicSetName(user.getMosaicSetName());
 80  0
                 List<Mosaic> list = dbf.getMosaics();
 81  0
                 Iterator<Mosaic> iter = list.iterator();
 82  0
                 while (iter.hasNext()) {
 83  0
                         Mosaic m = iter.next();
 84  0
                         if (m.isBatch())
 85  0
                                 iter.remove();
 86  0
                 }
 87  0
                 Collections.sort(list);
 88  0
                 mf.setMosaics(list);
 89  
                 
 90  0
                 MosaicConfig mc = MosaicSetupForm.getConfigs(list);
 91  0
                 mf.setMosaicConfigs(mc.mosaicConfig);
 92  0
                 mf.setMosaicEngineConfig(mc.engineConfig);
 93  0
                 mf.setEngines(mc.engines);
 94  0
                 return(mf);
 95  
         }
 96  
 
 97  
         protected void onBindAndValidate(HttpServletRequest request, Object command, BindException errors)
 98  
                         throws Exception {
 99  0
                 MosaicSetupForm msf = (MosaicSetupForm)command;
 100  0
                 if (request.getParameter("startBuilding") == null) {
 101  0
                         msf.setStartBuilding(false);
 102  
                 } else
 103  0
                         msf.setStartBuilding(true);
 104  
 
 105  0
         }
 106  
 
 107  
         protected Map<String, Object> referenceData(HttpServletRequest request) throws Exception {
 108  0
             BSAccountUserWebSession userWebSession = this.bsAccountMan.getUserWebSession(request);
 109  0
                 Map<String, Object> myModel = new HashMap<String, Object>();
 110  0
         myModel.put("userWebSession", userWebSession);
 111  
         if (DEBUG)
 112  0
                 logger.info("referenceData mosaicForm with session " + userWebSession);
 113  
 
 114  0
                 return myModel;
 115  
         }
 116  
 
 117  
         protected ModelAndView onSubmit(
 118  
                         HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)
 119  
                         throws Exception {
 120  
 
 121  0
             BSAccountUserWebSession userWebSession = this.bsAccountMan.getUserWebSession(request);
 122  0
             User user = (User)userWebSession.getUser();
 123  0
                 MosaicSetupForm msf = (MosaicSetupForm)command;
 124  0
                 boolean userSettingsModified = false;
 125  0
                 boolean success = true;
 126  
 
 127  0
                 Mosaic mosaic = pmf.getDB().getMosaic(msf.getMosaic().getMid());
 128  0
                 if (mosaic == null)
 129  0
                         mosaic = new Mosaic();
 130  
         
 131  0
                 mosaic.setName(msf.getMosaic().getName());
 132  0
                 mosaic.setMasterPic(msf.getMasterPic());
 133  0
                 mosaic.setSid(msf.getMosaicSid());
 134  0
                 mosaic.setEngine(msf.getMosaicEngineName());
 135  0
                 mosaic.setTileHeight(msf.getMosaicHeight());
 136  0
                 mosaic.setOutPic(msf.getMosaicOutputFile());
 137  
                 
 138  
                 String v;
 139  0
                 HashMap<String, String> engineConfig = mosaic.getEngineConfig();
 140  0
                 engineConfig.clear();
 141  0
                 String n = msf.getMosaicEngineName();
 142  0
                 if (n != null && n.length() > 0) {
 143  0
                         MosaicEngine mosaicEngine = null;
 144  
                         try {
 145  0
                                 mosaicEngine = (MosaicEngine)Class.forName("com.buckosoft.PicMan.business.mosaic.engine."
 146  0
                                                                 + n).newInstance();
 147  0
                         } catch (Exception e) {
 148  0
                                 Exception ex = new Exception("Can't instantiate Mosaic Engine: " + n, e);
 149  0
                                 pmf.addError(ex);
 150  0
                                 logger.error(ex);
 151  0
                         }
 152  0
                         if (mosaicEngine != null) {
 153  0
                                 HashMap<String, ConfigItem> configMap = mosaicEngine.getConfigMap();
 154  0
                                 if (!configMap.isEmpty()) {
 155  0
                                         for (String key : configMap.keySet()) {
 156  
                                                 //ConfigItem item = configMap.get(key);
 157  0
                                                 if ((v = request.getParameter(key)) != null) {
 158  0
                                                         engineConfig.put(key, v);
 159  0
                                                         logger.info("put k=" + key + " v=" + v);
 160  
                                                 }
 161  0
                                         }
 162  
                                 }
 163  
                         }
 164  
                 }
 165  
 
 166  0
                 pmf.getDB().storeMosaic(mosaic);
 167  0
                 if (user.getMosaicTileHeight() != msf.getMosaicHeight()) {
 168  0
                         user.setMosaicTileHeight(msf.getMosaicHeight());
 169  0
                         userSettingsModified = true;
 170  
                 }
 171  0
                 if (user.getMosaicTestPic() != msf.getMasterPic()) {
 172  0
                         user.setMosaicTestPic(msf.getMasterPic());
 173  0
                         userSettingsModified = true;
 174  
                 }
 175  
 //                if (!user.getMosaicSetName().equals(msf.getMosaicSetName())) {
 176  
 //                        user.setMosaicSetName(msf.getMosaicSetName());
 177  
 //                        userSettingsModified = true;
 178  
 //                }
 179  0
                 if (!user.getMosaicOutputFile().equals(msf.getMosaicOutputFile())) {
 180  0
                         user.setMosaicOutputFile(msf.getMosaicOutputFile());
 181  0
                         userSettingsModified = true;
 182  
                 }
 183  0
                 if (userSettingsModified)
 184  0
                         this.dbf.storeUser(user);
 185  
 
 186  0
                 if (msf.isStartBuilding()) {
 187  0
                         pmf.getMosaicMan().queueForBuilding(mosaic);
 188  
                 }
 189  0
                 if (success && !response.isCommitted()) {
 190  0
                         response.sendRedirect("mosaicSetup.do");
 191  0
                         return(null);
 192  
                 }
 193  
                 else
 194  0
                         return(showForm(request, response, errors));
 195  
         }
 196  
 
 197  
 }