Coverage Report - com.buckosoft.PicMan.web.SetManFormController
 
Classes in this File Line Coverage Branch Coverage Complexity
SetManFormController
0%
0/79
0%
0/44
4.5
 
 1  
 /******************************************************************************
 2  
  * SetManFormController.java - The Spring controller to edit PicMan's sets
 3  
  * 
 4  
  * PicMan - The BuckoSoft Picture Manager in Java
 5  
  * Copyright(c) 2005 - Dick Balaska
 6  
  * 
 7  
  */
 8  
 package com.buckosoft.PicMan.web;
 9  
 
 10  
 import java.util.HashMap;
 11  
 import java.util.Map;
 12  
 
 13  
 import javax.servlet.http.HttpServletRequest;
 14  
 import javax.servlet.http.HttpServletResponse;
 15  
 
 16  
 import org.apache.commons.logging.Log;
 17  
 import org.apache.commons.logging.LogFactory;
 18  
 import org.springframework.validation.BindException;
 19  
 import org.springframework.web.servlet.ModelAndView;
 20  
 
 21  
 import com.buckosoft.BSAccount.web.BSAccountSimpleFormController;
 22  
 import com.buckosoft.BSAccount.web.BSAccountUserWebSession;
 23  
 import com.buckosoft.PicMan.db.DatabaseFacade;
 24  
 import com.buckosoft.PicMan.domain.Set;
 25  
 import com.buckosoft.PicMan.domain.User;
 26  
 
 27  
 /** The Spring controller to edit PicMan's Sets (except MetaSets). <br/>
 28  
  * Either display a list of Sets or the details of a specific set.
 29  
  * See <a href="http://cvs.buckosoft.com/Projects/java/PicMan/PicMan/WebContent/WEB-INF/jsp/SetManForm.jsp">SetManForm.jsp</a>
 30  
  * and its javascript
 31  
  * <a href="http://cvs.buckosoft.com/Projects/java/PicMan/PicMan/WebContent/js/setMan.js">setMan.js</a>
 32  
  * @author Dick Balaska
 33  
  * @since 2005/07/30
 34  
  * @see <a href="http://cvs.buckosoft.com/Projects/java/PicMan/PicMan/src/com/buckosoft/PicMan/web/SetManFormController.java">SetManFormController.java</a>
 35  
  */
 36  
 public class SetManFormController extends BSAccountSimpleFormController {
 37  
         private        final static boolean DEBUG = false;
 38  0
     protected final Log logger = LogFactory.getLog(getClass());
 39  
 
 40  
         private DatabaseFacade dbf;
 41  
         
 42  0
         public void setDatabase(DatabaseFacade dbf) { this.dbf = dbf; }
 43  0
         public DatabaseFacade getDatabase() { return(dbf); }
 44  
 
 45  0
         public SetManFormController() {
 46  0
                 setSessionForm(true);
 47  0
                 setValidateOnBinding(false);
 48  0
                 setCommandName("setManForm");
 49  0
                 setFormView("SetManForm");
 50  0
         }
 51  
 
 52  
         protected Object formBackingObject(HttpServletRequest request) throws Exception {
 53  0
             BSAccountUserWebSession userWebSession = this.bsAccountMan.getUserWebSession(request);
 54  
                 if (DEBUG)
 55  
                         logger.info("formBackingObject SetupSystemForm with session " + userWebSession);
 56  0
                 SetManForm smf = new SetManForm();
 57  
 
 58  0
                 smf.setSets(dbf.getSets());
 59  0
                 smf.setUserWebSession(userWebSession);
 60  
                 String        v;
 61  0
                 if ((v = request.getParameter("s")) != null) {
 62  0
                         Set set = dbf.getSet(Integer.parseInt(v));
 63  0
                         smf.setEditSet(set.clone());
 64  
                         if (DEBUG)
 65  
                                 logger.info("editing set " + v + "(" + set.getName() + ")");
 66  
                         
 67  
                 }
 68  
                 //logger.info("formBacking: roots=" + pmf.getSystem().getRoot(0));
 69  0
                 return(smf);
 70  
         }
 71  
 
 72  
         protected void onBindAndValidate(HttpServletRequest request, Object command, BindException errors)
 73  
                         throws Exception {
 74  0
                 SetManForm smf = (SetManForm) command;
 75  0
                 if (smf.getEditSet() != null) {
 76  0
                         Set set = smf.getEditSet();
 77  0
                         Set set1 = dbf.getSet(set.getSid());
 78  0
                         if (!set.getName().equals(set1.getName()) && dbf.getSet(set.getName()) != null)
 79  0
                                 errors.reject("", "Can't rename set: '" + smf.getNewSet().getName() + "' already exists");
 80  0
                         if (set.getName().length() == 0)
 81  0
                                 errors.reject("", "Can't have an empty set name");
 82  
                 }
 83  0
                 if (smf.getNewSet().getName() != null && smf.getNewSet().getName().length() > 0) {
 84  0
                         Set set = dbf.getSet(smf.getNewSet().getName());
 85  0
                         if (set != null)
 86  0
                                 errors.reject("", "Set '" + smf.getNewSet().getName() + "' already exists");
 87  
                 }
 88  0
         }
 89  
 
 90  
         protected Map<String, Object> referenceData(HttpServletRequest request) throws Exception {
 91  0
             BSAccountUserWebSession userWebSession = this.bsAccountMan.getUserWebSession(request);
 92  0
                 Map<String, Object> myModel = new HashMap<String, Object>();
 93  0
         myModel.put("userWebSession", userWebSession);
 94  
         if (DEBUG)
 95  
                 logger.info("referenceData SetupSystemForm with session " + userWebSession);
 96  
 
 97  0
                 return myModel;
 98  
         }
 99  
 
 100  
         protected ModelAndView onSubmit(
 101  
                         HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)
 102  
                         throws Exception {
 103  
 
 104  0
                 SetManForm smf = (SetManForm) command;
 105  0
             BSAccountUserWebSession userWebSession = this.bsAccountMan.getUserWebSession(request);
 106  0
             User user = (User)userWebSession.getUser();
 107  
 
 108  0
                 boolean success = true;
 109  0
                 logger.info("Set to delete = '" + smf.getSetNameToDelete() + "'");
 110  
                 Set set;
 111  0
                 if (smf.getSetNameToDelete() != null && smf.getSetNameToDelete().length() > 0) {
 112  0
                         set = new Set();
 113  0
                         set.setName(smf.getSetNameToDelete());
 114  0
                         dbf.deleteSet(set);
 115  
                 }
 116  0
                 if (smf.getNewSet().getName() != null && smf.getNewSet().getName().length() > 0) {
 117  
                         if (DEBUG)
 118  
                                 logger.info("Adding new set '" + smf.getNewSet().getName() + "' desc: '" + smf.getNewSet().getDescription() + "'");
 119  0
                         dbf.addSet(smf.getNewSet());
 120  0
                         if (smf.getNewSet().isMetaSet()) {
 121  0
                                 set = dbf.getSet(smf.getNewSet().getName());
 122  0
                                 response.sendRedirect("metaSet.do?s=" + set.getSid());
 123  0
                                 return(null);
 124  
                         }
 125  0
                 } else if (smf.getEditSet() != null) {
 126  0
                         smf.getEditSet().setEditDateNow();
 127  0
                         String n = dbf.getSet(smf.getEditSet().getSid()).getName();
 128  0
                         if (!smf.getEditSet().getName().equals(n)) {
 129  0
                                 dbf.renameSet(n, smf.getEditSet().getName());
 130  
                         }
 131  
                                         
 132  0
                         dbf.storeSet(smf.getEditSet());
 133  0
                 } else {
 134  0
                         checkActiveFlags(request);
 135  
                 }
 136  0
                 boolean showInactiveSets = false;
 137  0
                 if (request.getParameter("showInactiveSets") != null)
 138  0
                         showInactiveSets = true;
 139  0
                 if (user.isSetManShowInactiveSets() != showInactiveSets) {
 140  0
                         user.setSetManShowInactiveSets(showInactiveSets);
 141  0
                         dbf.storeUser(user);
 142  
                 }
 143  
                 //pmf.saveSystem(setupForm.getSystem());
 144  0
                 if (success) {
 145  0
                         response.sendRedirect("home.do");
 146  0
                         return(null);
 147  
                 }
 148  
                 else
 149  0
                         return super.onSubmit(request, response, command, errors);
 150  
         }
 151  
 
 152  
         private        void        checkActiveFlags(HttpServletRequest request) {
 153  0
                 int                count = dbf.getSetCount();
 154  0
                 for (int i=0; i<=count; i++) {
 155  0
                         Set set = dbf.getSet(i);
 156  0
                         if (set == null)
 157  0
                                 continue;
 158  0
                         boolean b = false;
 159  0
                         if (request.getParameter("active_" + i) != null)
 160  0
                                 b = true;
 161  0
                         if (set.isActive() != b) {
 162  0
                                 set.setActive(b);
 163  0
                                 dbf.storeSet(set);
 164  
                         }
 165  
                 }
 166  0
         }
 167  
 }
 168