Coverage Report - com.buckosoft.PicMan.web.ChainManFormController
 
Classes in this File Line Coverage Branch Coverage Complexity
ChainManFormController
0%
0/83
0%
0/34
3.4
 
 1  
 /******************************************************************************
 2  
  * ChainManFormController.java - The Spring controller to edit PicMan's chains
 3  
  * 
 4  
  * PicMan - The BuckoSoft Picture Manager in Java
 5  
  * Copyright(c) 2006 - Dick Balaska
 6  
  * 
 7  
  */
 8  
 package com.buckosoft.PicMan.web;
 9  
 
 10  
 import java.util.Enumeration;
 11  
 import java.util.HashMap;
 12  
 import java.util.LinkedList;
 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.util.Classes;
 28  
 import com.buckosoft.PicMan.db.DatabaseFacade;
 29  
 import com.buckosoft.PicMan.domain.Chain;
 30  
 import com.buckosoft.PicMan.domain.SetSize;
 31  
 
 32  
 /** The Spring controller to edit PicMan's Chains
 33  
  * @author Dick Balaska
 34  
  * @since 2006/01/24
 35  
  * @version $Revision: 1.2 $ <br> $Date: 2014/06/21 04:37:09 $
 36  
  * @see <a href="http://cvs.buckosoft.com/Projects/PicMan/PicMan/src/main/java/com/buckosoft/PicMan/web/ChainManFormController.java">ChainManFormController.java</a>
 37  
  */
 38  
 public class ChainManFormController extends BSAccountSimpleFormController {
 39  
 
 40  
         private static final boolean DEBUG = false;
 41  0
     protected final Log logger = LogFactory.getLog(getClass());
 42  
 
 43  
     private DatabaseFacade        dbf;
 44  
     private PicManFacade        pmf;
 45  
 
 46  0
         public ChainManFormController() {
 47  0
                 setSessionForm(true);
 48  0
                 setValidateOnBinding(false);
 49  0
                 setCommandName("chainManForm");
 50  0
                 setFormView("ChainManForm");
 51  0
         }
 52  
 
 53  
         public void setPicMan(PicManFacade pmf) {
 54  0
                 this.pmf = pmf;
 55  0
         }
 56  
 
 57  
         public void setDatabase(DatabaseFacade dbf) {
 58  0
                 this.dbf = dbf;
 59  0
         }
 60  
 
 61  
         protected Object formBackingObject(HttpServletRequest request) throws Exception {
 62  0
             BSAccountUserWebSession userWebSession = this.bsAccountMan.getUserWebSession(request);
 63  
                 // logger.info("formBackingObject SetupSystemForm with session " + userSession);
 64  0
                 ChainManForm cmf = new ChainManForm();
 65  
 
 66  0
                 cmf.setChains(dbf.getChains());
 67  0
                 if (userWebSession != null)
 68  0
                         cmf.setUserWebSession(userWebSession);
 69  
                 String        v;
 70  0
                 if ((v = request.getParameter("c")) != null) {
 71  0
                         Chain c = dbf.getChain(v);
 72  0
                         cmf.setActiveChain(c);
 73  0
                         cmf.setSetSizeWebTable(pmf.getSetSizeWebTable(c.getSetSizes(), c.isShowUnusedSets()));
 74  
                 }
 75  
 
 76  
                 int i;
 77  0
                 String[] cl = Classes.getClasses("com.buckosoft.PicMan.business.contact.engine");
 78  0
                 List<String> classes = new LinkedList<String>();
 79  0
                 for (i=0; i<cl.length; i++) {
 80  0
                         if (cl[i].indexOf('$') != -1)
 81  0
                                 continue;
 82  0
                         classes.add(cl[i]);
 83  
                 }
 84  0
                 cmf.setEngines(classes);
 85  
                 if (DEBUG) {
 86  
                         for (i=0; i<cl.length; i++) {
 87  
                                 logger.info("class: " + cl[i]);
 88  
                         }
 89  
                 }
 90  0
                 return(cmf);
 91  
         }
 92  
 
 93  
         protected void onBindAndValidate(HttpServletRequest request, Object command, BindException errors)
 94  
                         throws Exception {
 95  
 
 96  
 /*
 97  
                 AccountForm accountForm = (AccountForm) command;
 98  
                 Account account = accountForm.getAccount();
 99  
 
 100  
                 if (request.getParameter("account.listOption") == null) {
 101  
                         account.setListOption(false);
 102  
                 }
 103  
                 if (request.getParameter("account.bannerOption") == null) {
 104  
                         account.setBannerOption(false);
 105  
                 }
 106  
 
 107  
                 errors.setNestedPath("account");
 108  
                 getValidator().validate(account, errors);
 109  
                 errors.setNestedPath("");
 110  
 */
 111  0
         }
 112  
 
 113  
         protected Map<String, Object> referenceData(HttpServletRequest request) throws Exception {
 114  
 //            BSAccountUserWebSession userWebSession = this.bsAccountMan.getUserWebSession(request);
 115  0
                 Map<String, Object> myModel = new HashMap<String, Object>();
 116  
 //        myModel.put("userSession", userSession);
 117  
 //        if (userSession != null)
 118  
 //                myModel.put("account", userSession.getAccount());
 119  
 //        if (DEBUG)
 120  
 //                logger.info("referenceData ChainManForm with session " + userSession);
 121  
 
 122  0
                 return myModel;
 123  
         }
 124  
 
 125  
         protected ModelAndView onSubmit(
 126  
                         HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)
 127  
                         throws Exception {
 128  
 
 129  0
                 ChainManForm cmf = (ChainManForm) command;
 130  
 
 131  0
                 boolean success = true;
 132  0
                 boolean action = false;
 133  0
                 String        newChainName = "";
 134  
 
 135  0
                 checkActiveFlags(request);
 136  0
                  logger.info("Set to delete = '" + cmf.getChainToDelete() + "'");
 137  0
                 if (cmf.getChainToDelete().length() > 0) {
 138  0
                         Chain chain = new Chain();
 139  0
                         chain.setName(cmf.getChainToDelete());
 140  0
                         dbf.deleteChain(chain);
 141  0
                         action = true;
 142  
                 }
 143  0
                 if (!action && cmf.getNewChainName().length() > 0) {
 144  
                         if (DEBUG)
 145  
                                 logger.info("Adding new Chain '" + cmf.getNewChainName());
 146  0
                         Chain c = new Chain();
 147  0
                         c.setName(cmf.getNewChainName());
 148  0
                         dbf.storeChain(c);
 149  0
                         newChainName = c.getName();
 150  0
                         action = true;
 151  
                         
 152  
                 }
 153  0
                 if (!action && cmf.getActiveChain() != null) {
 154  0
                         Chain c = cmf.getActiveChain();
 155  0
                         LinkedList<SetSize> newSetsSizes = parseSetsSizes(request);
 156  0
                         c.setSetSizes(newSetsSizes);
 157  0
                         c.setShowUnusedSets(getShowUnusedSets(request));
 158  0
                         dbf.storeChain(c);
 159  0
                         action = true;
 160  
                 }
 161  0
                 if (success)
 162  
                 {
 163  0
                         if (newChainName.length() > 0)
 164  0
                                 response.sendRedirect("chainMan.do?c=" + newChainName);
 165  
                         else
 166  0
                                 response.sendRedirect("chainMan.do");
 167  0
                         return(null);
 168  
                 }
 169  0
                 return super.onSubmit(request, response, command, errors);
 170  
         }
 171  
 
 172  
         private        LinkedList<SetSize>        parseSetsSizes(HttpServletRequest request) {
 173  0
                 LinkedList<SetSize>        ssList = new LinkedList<SetSize>();
 174  0
                 Enumeration<String> e = request.getParameterNames();
 175  
                 String        a;
 176  
                 // walk the attributes and suck out all the pics
 177  0
                 while (e.hasMoreElements()) {
 178  0
                         a = (String)e.nextElement();
 179  
                         if (DEBUG)
 180  
                                 logger.info("Check isPic '" + a + "'");
 181  0
                         if (a.startsWith("x_")) {
 182  0
                                 String name = a.substring(2);
 183  
                                 if (DEBUG)
 184  
                                         logger.info("Got setSize '" + name + "'");
 185  0
                                 ssList.add(new SetSize(name));
 186  0
                         }
 187  
                 }
 188  0
                 return(ssList);
 189  
         }
 190  
 
 191  
         private        void        checkActiveFlags(HttpServletRequest request) {
 192  0
                 int                count = dbf.getChainCount();
 193  0
                 for (int i=0; i<count; i++) {
 194  0
                         Chain c = dbf.getChain(i);
 195  0
                         boolean b = false;
 196  0
                         if (request.getParameter("active_" + i) != null)
 197  0
                                 b = true;
 198  0
                         if (c.isActive() != b) {
 199  0
                                 c.setActive(b);
 200  0
                                 dbf.storeChain(c);
 201  
                         }
 202  
                 }
 203  0
         }
 204  
         private        boolean        getShowUnusedSets(HttpServletRequest request) {
 205  0
                 if (request.getParameter("showUnused") != null)
 206  0
                         return(true);
 207  0
                 return(false);
 208  
         }
 209  
         
 210  
 }