View Javadoc
1   /******************************************************************************
2    * MetaSetFormController.java - The Spring controller to edit PicMan's Meta sets
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.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.business.PicManFacade;
24  import com.buckosoft.PicMan.db.DatabaseFacade;
25  import com.buckosoft.PicMan.domain.MetaSet;
26  import com.buckosoft.PicMan.domain.MetaSetRule;
27  import com.buckosoft.PicMan.domain.Set;
28  import com.buckosoft.PicMan.domain.SetSize;
29  
30  /** The Spring controller to edit PicMan's MetaSets. <br/>
31   * See <a href="http://cvs.buckosoft.com/Projects/java/PicMan/PicMan/WebContent/WEB-INF/jsp/MetaSetForm.jsp">MetaSetForm.jsp</a>
32   * which is pretty boring.  All the good stuff happens in the javascript
33   * <a href="http://cvs.buckosoft.com/Projects/java/PicMan/PicMan/WebContent/js/metaSet.js">metaSet.js</a>
34   * @author Dick Balaska
35   * @since 2006/07/17
36   * @see <a href="http://cvs.buckosoft.com/Projects/java/PicMan/PicMan/src/com/buckosoft/PicMan/web/MetaSetFormController.java">MetaSetFormController.java</a>
37   */
38  public class MetaSetFormController extends BSAccountSimpleFormController {
39  	private	static boolean DEBUG = false;
40      protected final Log logger = LogFactory.getLog(getClass());
41  
42  	private DatabaseFacade dbf;
43  	private PicManFacade	pmf;
44  	
45  	public void setDatabase(DatabaseFacade dbf) { this.dbf = dbf; }
46  	public DatabaseFacade getDatabase() { return(dbf); }
47  	
48  	public void setPicMan(PicManFacade pmf) { this.pmf = pmf; }
49  	public PicManFacade getPicMan() { return(pmf); }
50  
51  	/** Enable debug logger output on this module
52  	 * @param debugFlag true == turn on debugging.
53  	 */
54  	public void setDEBUG(boolean debugFlag) {
55  		DEBUG = debugFlag;
56  	}
57  
58  	public MetaSetFormController() {
59  		setSessionForm(true);
60  		setValidateOnBinding(false);
61  		setCommandName("metaSetForm");
62  		setFormView("MetaSetForm");
63  	}
64  
65  	protected Object formBackingObject(HttpServletRequest request) throws Exception {
66      	BSAccountUserWebSession userWebSession = this.bsAccountMan.getUserWebSession(request);
67  		if (DEBUG)
68  			logger.info("formBackingObject SetupSystemForm with session " + userWebSession);
69  		MetaSetForm msf = new MetaSetForm();
70  
71  		msf.setUserWebSession(userWebSession);
72  		msf.setSets(dbf.getSets());
73  
74  		msf.setMaxRules(MetaSet.MAX_RULES);
75  		String	v;
76  		if ((v = request.getParameter("s")) != null) {
77  			Set set = dbf.getSet(Integer.parseInt(v)).clone();
78  			msf.setSet(set);
79  			if (set != null) {
80  				MetaSet mset = dbf.getMetaSet(set.getSid());
81  				for (int i=mset.getRuleCount(); i<MetaSet.MAX_RULES; i+=2) {
82  					if (i == 0) {
83  						mset.addRule(new MetaSetRule(set.getSid(), i, 0, "", 1));
84  						i++;
85  					}
86  					mset.addRule(new MetaSetRule(set.getSid(), i, 1, "", 0));
87  					mset.addRule(new MetaSetRule(set.getSid(), i+1, 0, "", 1));
88  				}
89  				msf.setMetaSet(mset);
90  			}
91  		}
92  
93  		return(msf);
94  	}
95  
96  	protected void onBindAndValidate(HttpServletRequest request, Object command, BindException errors)
97  			throws Exception {
98  	}
99  
100 	protected Map<String, Object> referenceData(HttpServletRequest request) throws Exception {
101     	BSAccountUserWebSession userWebSession = this.bsAccountMan.getUserWebSession(request);
102 		Map<String, Object> myModel = new HashMap<String, Object>();
103         myModel.put("userWebSession", userWebSession);
104         if (DEBUG)
105         	logger.info("referenceData SetupSystemForm with session " + userWebSession);
106 
107 		return myModel;
108 	}
109 
110 	protected ModelAndView onSubmit(
111 			HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)
112 			throws Exception {
113 		response.addHeader("Cache-Control", "max-age=0");
114 		response.addHeader("Cache-Control", "no-cache");
115 		response.addHeader("expires", "0");
116 		response.addHeader("Expires", "Tue, 01 Jan 1980 1:00:00 GMT");
117 		response.addHeader("Pragma", "no-cache");
118 
119 		MetaSetForm msf = (MetaSetForm)command;
120 		
121 
122 		boolean success = true;
123 		MetaSet	ms = new MetaSet();
124 		Set set = msf.getSet();
125 		ms.setSid(set.getSid());
126 		String v;
127 		for (int i=0; i<MetaSet.MAX_RULES; i++) {
128 			if ((i & 1) == 0) {
129 				v = request.getParameter("r_" + i);
130 				if (v == null) {
131 					errors.reject("X", "Failed to parse name " + i);
132 					return(showForm(request, response, errors));
133 				}
134 				if (v.charAt(0) == '[') {
135 					int funcSid = 0;
136 					if (v.equals(Set.FUNC_NAME_DATE))
137 						funcSid = Set.FUNC_SID_DATE;
138 					else if (v.equals(Set.FUNC_NAME_ROOT))
139 						funcSid = Set.FUNC_SID_ROOT;
140 					if (funcSid == 0) {
141 						errors.reject("X", "Unknown func '" + v + "'");
142 						return(showForm(request, response, errors));
143 					}
144 					MetaSetRule msr = new MetaSetRule(set.getSid(), i, MetaSet.FUNCTION, v, funcSid);
145 					msr.setRateVal(funcSid);
146 					v = request.getParameter("e_" + i); 
147 					if (v == null) {
148 						errors.reject("X", "Failed to parse func rate operator " + i);
149 						return(showForm(request, response, errors));
150 					}
151 					int rateOp = Integer.parseInt(v);
152 					msr.setRateOp(rateOp);
153 					v = request.getParameter("text_" + i); 
154 					if (v == null) {
155 						errors.reject("X", "Failed to parse rate operator " + i);
156 						return(showForm(request, response, errors));
157 					}
158 					msr.setValue(v);
159 					ms.addRule(msr);
160 				} else {
161 					MetaSetRule msr = new MetaSetRule(set.getSid(), i, MetaSet.NAME, v, -1);
162 					v = request.getParameter("e_" + i); 
163 					if (v == null) {
164 						errors.reject("X", "Failed to parse rate operator " + i);
165 						return(showForm(request, response, errors));
166 					}
167 					int rateOp = Integer.parseInt(v);
168 					msr.setRateOp(rateOp);
169 					v = request.getParameter("rate_" + i);
170 					int rateVal = Integer.parseInt(v);
171 					msr.setRateVal(rateVal);
172 					ms.addRule(msr);
173 				}
174 			} else {
175 				v = request.getParameter("o_" + i);
176 				if (v == null) {
177 					errors.reject("X", "Failed to parse operator " + i);
178 					return(showForm(request, response, errors));
179 				}
180 				if (v.equals("0"))
181 					break;
182 				MetaSetRule msr = new MetaSetRule(set.getSid(), i, 1, null, Integer.parseInt(v));
183 				ms.addRule(msr);
184 			}
185 		}
186 		if (DEBUG) {
187 			for (int j=0; j<ms.getRuleCount(); j++) {
188 				MetaSetRule msr = ms.getRule(j);
189 				logger.info("Rule:" + j + " t:" + msr.getType() + " v:" + msr.getValue());
190 			}
191 		}
192 		dbf.storeMetaSet(ms);
193 		set.setEditDateNow();
194 		String n = dbf.getSet(set.getSid()).getName();
195 		if (!set.getName().equals(n)) {
196 			dbf.renameSet(n, set.getName());
197 		}
198 
199 		dbf.storeSet(set);
200 		pmf.fireSetSizeChanged(new SetSize(set.getName()));
201 
202 		if (success) {
203 			response.sendRedirect("metaSet.do?s=" + set.getSid());
204 			return(null);
205 		}
206 		else
207 			return(showForm(request, response, errors));
208 	}
209 
210 }