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