1
2
3
4
5
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.List;
14 import java.util.Map;
15
16 import javax.servlet.http.HttpServletRequest;
17 import javax.servlet.http.HttpServletResponse;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.springframework.validation.BindException;
22 import org.springframework.web.servlet.ModelAndView;
23
24 import com.buckosoft.BSAccount.web.BSAccountSimpleFormController;
25 import com.buckosoft.BSAccount.web.BSAccountUserWebSession;
26 import com.buckosoft.PicMan.business.PicManFacade;
27 import com.buckosoft.PicMan.business.util.Classes;
28 import com.buckosoft.PicMan.db.DatabaseFacade;
29 import com.buckosoft.PicMan.domain.Chain;
30 import com.buckosoft.PicMan.domain.SetSize;
31
32
33
34
35
36
37
38 public class ChainManFormController extends BSAccountSimpleFormController {
39
40 private static final boolean DEBUG = false;
41 protected final Log logger = LogFactory.getLog(getClass());
42
43 private DatabaseFacade dbf;
44 private PicManFacade pmf;
45
46 public ChainManFormController() {
47 setSessionForm(true);
48 setValidateOnBinding(false);
49 setCommandName("chainManForm");
50 setFormView("ChainManForm");
51 }
52
53 public void setPicMan(PicManFacade pmf) {
54 this.pmf = pmf;
55 }
56
57 public void setDatabase(DatabaseFacade dbf) {
58 this.dbf = dbf;
59 }
60
61 protected Object formBackingObject(HttpServletRequest request) throws Exception {
62 BSAccountUserWebSession userWebSession = this.bsAccountMan.getUserWebSession(request);
63
64 ChainManForm cmf = new ChainManForm();
65
66 cmf.setChains(dbf.getChains());
67 if (userWebSession != null)
68 cmf.setUserWebSession(userWebSession);
69 String v;
70 if ((v = request.getParameter("c")) != null) {
71 Chain c = dbf.getChain(v);
72 cmf.setActiveChain(c);
73 cmf.setSetSizeWebTable(pmf.getSetSizeWebTable(c.getSetSizes(), c.isShowUnusedSets()));
74 }
75
76 int i;
77 String[] cl = Classes.getClasses("com.buckosoft.PicMan.business.contact.engine");
78 List<String> classes = new LinkedList<String>();
79 for (i=0; i<cl.length; i++) {
80 if (cl[i].indexOf('$') != -1)
81 continue;
82 classes.add(cl[i]);
83 }
84 cmf.setEngines(classes);
85 if (DEBUG) {
86 for (i=0; i<cl.length; i++) {
87 logger.info("class: " + cl[i]);
88 }
89 }
90 return(cmf);
91 }
92
93 protected void onBindAndValidate(HttpServletRequest request, Object command, BindException errors)
94 throws Exception {
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111 }
112
113 protected Map<String, Object> referenceData(HttpServletRequest request) throws Exception {
114
115 Map<String, Object> myModel = new HashMap<String, Object>();
116
117
118
119
120
121
122 return myModel;
123 }
124
125 protected ModelAndView onSubmit(
126 HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)
127 throws Exception {
128
129 ChainManForm cmf = (ChainManForm) command;
130
131 boolean success = true;
132 boolean action = false;
133 String newChainName = "";
134
135 checkActiveFlags(request);
136 logger.info("Set to delete = '" + cmf.getChainToDelete() + "'");
137 if (cmf.getChainToDelete().length() > 0) {
138 Chain chain = new Chain();
139 chain.setName(cmf.getChainToDelete());
140 dbf.deleteChain(chain);
141 action = true;
142 }
143 if (!action && cmf.getNewChainName().length() > 0) {
144 if (DEBUG)
145 logger.info("Adding new Chain '" + cmf.getNewChainName());
146 Chain c = new Chain();
147 c.setName(cmf.getNewChainName());
148 dbf.storeChain(c);
149 newChainName = c.getName();
150 action = true;
151
152 }
153 if (!action && cmf.getActiveChain() != null) {
154 Chain c = cmf.getActiveChain();
155 LinkedList<SetSize> newSetsSizes = parseSetsSizes(request);
156 c.setSetSizes(newSetsSizes);
157 c.setShowUnusedSets(getShowUnusedSets(request));
158 dbf.storeChain(c);
159 action = true;
160 }
161 if (success)
162 {
163 if (newChainName.length() > 0)
164 response.sendRedirect("chainMan.do?c=" + newChainName);
165 else
166 response.sendRedirect("chainMan.do");
167 return(null);
168 }
169 return super.onSubmit(request, response, command, errors);
170 }
171
172 private LinkedList<SetSize> parseSetsSizes(HttpServletRequest request) {
173 LinkedList<SetSize> ssList = new LinkedList<SetSize>();
174 Enumeration<String> e = request.getParameterNames();
175 String a;
176
177 while (e.hasMoreElements()) {
178 a = (String)e.nextElement();
179 if (DEBUG)
180 logger.info("Check isPic '" + a + "'");
181 if (a.startsWith("x_")) {
182 String name = a.substring(2);
183 if (DEBUG)
184 logger.info("Got setSize '" + name + "'");
185 ssList.add(new SetSize(name));
186 }
187 }
188 return(ssList);
189 }
190
191 private void checkActiveFlags(HttpServletRequest request) {
192 int count = dbf.getChainCount();
193 for (int i=0; i<count; i++) {
194 Chain c = dbf.getChain(i);
195 boolean b = false;
196 if (request.getParameter("active_" + i) != null)
197 b = true;
198 if (c.isActive() != b) {
199 c.setActive(b);
200 dbf.storeChain(c);
201 }
202 }
203 }
204 private boolean getShowUnusedSets(HttpServletRequest request) {
205 if (request.getParameter("showUnused") != null)
206 return(true);
207 return(false);
208 }
209
210 }