View Javadoc
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      protected final Log logger = LogFactory.getLog(getClass());
39  
40  	private DatabaseFacade dbf;
41  	
42  	public void setDatabase(DatabaseFacade dbf) { this.dbf = dbf; }
43  	public DatabaseFacade getDatabase() { return(dbf); }
44  
45  	public SetManFormController() {
46  		setSessionForm(true);
47  		setValidateOnBinding(false);
48  		setCommandName("setManForm");
49  		setFormView("SetManForm");
50  	}
51  
52  	protected Object formBackingObject(HttpServletRequest request) throws Exception {
53      	BSAccountUserWebSession userWebSession = this.bsAccountMan.getUserWebSession(request);
54  		if (DEBUG)
55  			logger.info("formBackingObject SetupSystemForm with session " + userWebSession);
56  		SetManForm smf = new SetManForm();
57  
58  		smf.setSets(dbf.getSets());
59  		smf.setUserWebSession(userWebSession);
60  		String	v;
61  		if ((v = request.getParameter("s")) != null) {
62  			Set set = dbf.getSet(Integer.parseInt(v));
63  			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  		return(smf);
70  	}
71  
72  	protected void onBindAndValidate(HttpServletRequest request, Object command, BindException errors)
73  			throws Exception {
74  		SetManForm smf = (SetManForm) command;
75  		if (smf.getEditSet() != null) {
76  			Set set = smf.getEditSet();
77  			Set set1 = dbf.getSet(set.getSid());
78  			if (!set.getName().equals(set1.getName()) && dbf.getSet(set.getName()) != null)
79  				errors.reject("", "Can't rename set: '" + smf.getNewSet().getName() + "' already exists");
80  			if (set.getName().length() == 0)
81  				errors.reject("", "Can't have an empty set name");
82  		}
83  		if (smf.getNewSet().getName() != null && smf.getNewSet().getName().length() > 0) {
84  			Set set = dbf.getSet(smf.getNewSet().getName());
85  			if (set != null)
86  				errors.reject("", "Set '" + smf.getNewSet().getName() + "' already exists");
87  		}
88  	}
89  
90  	protected Map<String, Object> referenceData(HttpServletRequest request) throws Exception {
91      	BSAccountUserWebSession userWebSession = this.bsAccountMan.getUserWebSession(request);
92  		Map<String, Object> myModel = new HashMap<String, Object>();
93          myModel.put("userWebSession", userWebSession);
94          if (DEBUG)
95          	logger.info("referenceData SetupSystemForm with session " + userWebSession);
96  
97  		return myModel;
98  	}
99  
100 	protected ModelAndView onSubmit(
101 			HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)
102 			throws Exception {
103 
104 		SetManForm smf = (SetManForm) command;
105     	BSAccountUserWebSession userWebSession = this.bsAccountMan.getUserWebSession(request);
106     	User user = (User)userWebSession.getUser();
107 
108 		boolean success = true;
109 		logger.info("Set to delete = '" + smf.getSetNameToDelete() + "'");
110 		Set set;
111 		if (smf.getSetNameToDelete() != null && smf.getSetNameToDelete().length() > 0) {
112 			set = new Set();
113 			set.setName(smf.getSetNameToDelete());
114 			dbf.deleteSet(set);
115 		}
116 		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 			dbf.addSet(smf.getNewSet());
120 			if (smf.getNewSet().isMetaSet()) {
121 				set = dbf.getSet(smf.getNewSet().getName());
122 				response.sendRedirect("metaSet.do?s=" + set.getSid());
123 				return(null);
124 			}
125 		} else if (smf.getEditSet() != null) {
126 			smf.getEditSet().setEditDateNow();
127 			String n = dbf.getSet(smf.getEditSet().getSid()).getName();
128 			if (!smf.getEditSet().getName().equals(n)) {
129 				dbf.renameSet(n, smf.getEditSet().getName());
130 			}
131 					
132 			dbf.storeSet(smf.getEditSet());
133 		} else {
134 			checkActiveFlags(request);
135 		}
136 		boolean showInactiveSets = false;
137 		if (request.getParameter("showInactiveSets") != null)
138 			showInactiveSets = true;
139 		if (user.isSetManShowInactiveSets() != showInactiveSets) {
140 			user.setSetManShowInactiveSets(showInactiveSets);
141 			dbf.storeUser(user);
142 		}
143 		//pmf.saveSystem(setupForm.getSystem());
144 		if (success) {
145 			response.sendRedirect("home.do");
146 			return(null);
147 		}
148 		else
149 			return super.onSubmit(request, response, command, errors);
150 	}
151 
152 	private	void	checkActiveFlags(HttpServletRequest request) {
153 		int		count = dbf.getSetCount();
154 		for (int i=0; i<=count; i++) {
155 			Set set = dbf.getSet(i);
156 			if (set == null)
157 				continue;
158 			boolean b = false;
159 			if (request.getParameter("active_" + i) != null)
160 				b = true;
161 			if (set.isActive() != b) {
162 				set.setActive(b);
163 				dbf.storeSet(set);
164 			}
165 		}
166 	}
167 }
168