Coverage Report - com.buckosoft.PicMan.web.SpinContactsFormController
 
Classes in this File Line Coverage Branch Coverage Complexity
SpinContactsFormController
0%
0/68
0%
0/22
2.8
 
 1  
 /******************************************************************************
 2  
  * SpinContactsFormController.java - The Spring controller for the SpinContacts page
 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.Map;
 14  
 
 15  
 import javax.servlet.http.HttpServletRequest;
 16  
 import javax.servlet.http.HttpServletResponse;
 17  
 
 18  
 import org.apache.commons.logging.Log;
 19  
 import org.apache.commons.logging.LogFactory;
 20  
 import org.springframework.validation.BindException;
 21  
 import org.springframework.web.servlet.ModelAndView;
 22  
 
 23  
 import com.buckosoft.BSAccount.web.BSAccountSimpleFormController;
 24  
 import com.buckosoft.BSAccount.web.BSAccountUserWebSession;
 25  
 import com.buckosoft.PicMan.business.PicManFacade;
 26  
 import com.buckosoft.PicMan.db.DatabaseFacade;
 27  
 import com.buckosoft.PicMan.domain.Chain;
 28  
 import com.buckosoft.PicMan.domain.ContactParams;
 29  
 import com.buckosoft.PicMan.domain.SetSize;
 30  
 
 31  
 public class SpinContactsFormController extends BSAccountSimpleFormController {
 32  
         
 33  
         private static final boolean DEBUG = false;
 34  0
         protected final Log logger = LogFactory.getLog(getClass());
 35  
 
 36  
         private PicManFacade        pmf;
 37  
         private DatabaseFacade        dbf;
 38  
         
 39  0
         public void setPicMan(PicManFacade pmf) { this.pmf = pmf; }
 40  0
         public PicManFacade getPicMan() { return(pmf); }
 41  
 
 42  0
         public void setDatabase(DatabaseFacade dbf) { this.dbf = dbf; }
 43  0
         public DatabaseFacade getDatabase() { return(dbf); }
 44  
 
 45  0
         public SpinContactsFormController() {
 46  0
                 setSessionForm(true);
 47  0
                 setValidateOnBinding(false);
 48  0
                 setCommandName("spinContactsForm");
 49  0
                 setFormView("SpinContactsForm");
 50  0
         }
 51  
         
 52  
         protected void onBindAndValidate(HttpServletRequest request, Object command, BindException errors)
 53  
         throws Exception {
 54  0
         }
 55  
         
 56  
         protected Object formBackingObject(HttpServletRequest request) throws Exception {
 57  0
             BSAccountUserWebSession userWebSession = this.bsAccountMan.getUserWebSession(request);
 58  
             if (DEBUG)
 59  
                     logger.info("formBackingObject with session " + userWebSession);
 60  0
                 SpinContactsForm scf = new SpinContactsForm();
 61  
 
 62  0
                 scf.setUserWebSession(userWebSession);
 63  0
                 scf.setPicMan(pmf);
 64  0
                 scf.setDatabase(dbf);
 65  
                 int                i;
 66  
                 String        v;
 67  0
                 Chain        chain        = null;
 68  
 
 69  0
                 i = -1;
 70  0
                 if ((v = request.getParameter("cid")) != null) {
 71  
                         try {
 72  0
                                 i = java.lang.Integer.parseInt(v);
 73  0
                         } catch ( NumberFormatException e) { }
 74  
                 }
 75  0
                 if (i != -1)
 76  0
                         chain = dbf.getChain(i);
 77  0
                 scf.setChain(chain);
 78  
                 
 79  
 
 80  0
                 return(scf);
 81  
         }
 82  
 
 83  
         protected Map<String, Object> referenceData(HttpServletRequest request) throws Exception {
 84  0
             BSAccountUserWebSession userWebSession = this.bsAccountMan.getUserWebSession(request);
 85  0
                 Map<String, Object> myModel = new HashMap<String, Object>();
 86  0
                 myModel.put("userWebSession", userWebSession);
 87  
 
 88  0
                 return myModel;
 89  
         }
 90  
         
 91  
         protected ModelAndView onSubmit(
 92  
                         HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)
 93  
         throws Exception {
 94  
                 if (DEBUG)
 95  
                         logger.info("onSubmit");
 96  0
                 boolean success = true;
 97  0
                 SpinContactsForm scf = (SpinContactsForm)command;
 98  0
                 Chain        chain        = null;
 99  
                 int                i;
 100  
                 String        v;
 101  
 
 102  0
                 i = -1;
 103  0
                 if ((v = request.getParameter("cid")) != null) {
 104  
                         try {
 105  0
                                 i = java.lang.Integer.parseInt(v);
 106  0
                         } catch ( NumberFormatException e) { }
 107  
                 }
 108  0
                 if (i != -1)
 109  0
                         chain = dbf.getChain(i);
 110  0
                 scf.setChain(chain);
 111  
                 
 112  0
                 errors.setNestedPath("SpinContacts");
 113  0
                 if (chain == null) {
 114  0
                         errors.reject("chain == null");
 115  0
                         success = false;
 116  
                 }
 117  
                 //getValidator().validate(account, errors);
 118  0
                 errors.setNestedPath("");
 119  
 
 120  0
                 if (chain != null) {
 121  0
                         LinkedList<ContactParams>        spinList = new LinkedList<ContactParams>();
 122  0
                         Enumeration<String> e = request.getParameterNames();
 123  
                         String        a;
 124  
                         // walk the attributes and suck out all the pics
 125  0
                         while (e.hasMoreElements()) {
 126  0
                                 a = e.nextElement();
 127  
                                 if (DEBUG)
 128  
                                         logger.info("Check isPic '" + a + "'");
 129  0
                                 if (a.startsWith("x_")) {
 130  0
                                         String name = a.substring(2);
 131  
                                         if (DEBUG)
 132  
                                                 logger.info("Got setSize '" + name + "'");
 133  0
                                         addToSpinList(spinList, chain, name);
 134  0
                                 }
 135  
                         }
 136  0
                         pmf.getBatchManager().getContactManager().addSpin(spinList);
 137  
                 }
 138  
                 
 139  0
                 if (success && !response.isCommitted())
 140  
                 {
 141  0
                         response.sendRedirect("home.do");
 142  0
                         return(null);
 143  
                 }
 144  
                 else
 145  0
                         return(showForm(request, response, errors));
 146  
         }
 147  
 
 148  
         private        void        addToSpinList(LinkedList<ContactParams> spinList, Chain chain, String setSizeName) {
 149  0
                 SetSize setSize = new SetSize(setSizeName);
 150  0
                 int                count        = chain.getContactCount();
 151  
                 int                i;
 152  0
                 for (i=0; i<count; i++) {
 153  0
                         ContactParams cp = new ContactParams(setSize);
 154  0
                         cp.setCid(chain.getCid());
 155  0
                         cp.setRankMinMax(chain.getRatingMin(), chain.getRatingMax());
 156  0
                         cp.setContactNumber(i);
 157  0
                         spinList.add(cp);
 158  
                 }
 159  0
         }
 160  
 }