View Javadoc
1   /******************************************************************************
2    * SpinContactsFormController.java - The Spring controller for the SpinContacts page
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.Enumeration;
11  import java.util.HashMap;
12  import java.util.LinkedList;
13  import java.util.Map;
14  
15  import javax.servlet.http.HttpServletRequest;
16  import javax.servlet.http.HttpServletResponse;
17  
18  import org.apache.commons.logging.Log;
19  import org.apache.commons.logging.LogFactory;
20  import org.springframework.validation.BindException;
21  import org.springframework.web.servlet.ModelAndView;
22  
23  import com.buckosoft.BSAccount.web.BSAccountSimpleFormController;
24  import com.buckosoft.BSAccount.web.BSAccountUserWebSession;
25  import com.buckosoft.PicMan.business.PicManFacade;
26  import com.buckosoft.PicMan.db.DatabaseFacade;
27  import com.buckosoft.PicMan.domain.Chain;
28  import com.buckosoft.PicMan.domain.ContactParams;
29  import com.buckosoft.PicMan.domain.SetSize;
30  
31  public class SpinContactsFormController extends BSAccountSimpleFormController {
32  	
33  	private static final boolean DEBUG = false;
34  	protected final Log logger = LogFactory.getLog(getClass());
35  
36  	private PicManFacade	pmf;
37  	private DatabaseFacade	dbf;
38  	
39  	public void setPicMan(PicManFacade pmf) { this.pmf = pmf; }
40  	public PicManFacade getPicMan() { return(pmf); }
41  
42  	public void setDatabase(DatabaseFacade dbf) { this.dbf = dbf; }
43  	public DatabaseFacade getDatabase() { return(dbf); }
44  
45  	public SpinContactsFormController() {
46  		setSessionForm(true);
47  		setValidateOnBinding(false);
48  		setCommandName("spinContactsForm");
49  		setFormView("SpinContactsForm");
50  	}
51  	
52  	protected void onBindAndValidate(HttpServletRequest request, Object command, BindException errors)
53  	throws Exception {
54  	}
55  	
56  	protected Object formBackingObject(HttpServletRequest request) throws Exception {
57      	BSAccountUserWebSession userWebSession = this.bsAccountMan.getUserWebSession(request);
58      	if (DEBUG)
59      		logger.info("formBackingObject with session " + userWebSession);
60  		SpinContactsForm scf = new SpinContactsForm();
61  
62  		scf.setUserWebSession(userWebSession);
63  		scf.setPicMan(pmf);
64  		scf.setDatabase(dbf);
65  		int		i;
66  		String	v;
67  		Chain	chain	= null;
68  
69  		i = -1;
70  		if ((v = request.getParameter("cid")) != null) {
71  			try {
72  				i = java.lang.Integer.parseInt(v);
73  			} catch ( NumberFormatException e) { }
74  		}
75  		if (i != -1)
76  			chain = dbf.getChain(i);
77  		scf.setChain(chain);
78  		
79  
80  		return(scf);
81  	}
82  
83  	protected Map<String, Object> referenceData(HttpServletRequest request) throws Exception {
84      	BSAccountUserWebSession userWebSession = this.bsAccountMan.getUserWebSession(request);
85  		Map<String, Object> myModel = new HashMap<String, Object>();
86  		myModel.put("userWebSession", userWebSession);
87  
88  		return myModel;
89  	}
90  	
91  	protected ModelAndView onSubmit(
92  			HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)
93  	throws Exception {
94  		if (DEBUG)
95  			logger.info("onSubmit");
96  		boolean success = true;
97  		SpinContactsForm scf = (SpinContactsForm)command;
98  		Chain	chain	= null;
99  		int		i;
100 		String	v;
101 
102 		i = -1;
103 		if ((v = request.getParameter("cid")) != null) {
104 			try {
105 				i = java.lang.Integer.parseInt(v);
106 			} catch ( NumberFormatException e) { }
107 		}
108 		if (i != -1)
109 			chain = dbf.getChain(i);
110 		scf.setChain(chain);
111 		
112 		errors.setNestedPath("SpinContacts");
113 		if (chain == null) {
114 			errors.reject("chain == null");
115 			success = false;
116 		}
117 		//getValidator().validate(account, errors);
118 		errors.setNestedPath("");
119 
120 		if (chain != null) {
121 			LinkedList<ContactParams>	spinList = new LinkedList<ContactParams>();
122 			Enumeration<String> e = request.getParameterNames();
123 			String	a;
124 			// walk the attributes and suck out all the pics
125 			while (e.hasMoreElements()) {
126 				a = e.nextElement();
127 				if (DEBUG)
128 					logger.info("Check isPic '" + a + "'");
129 				if (a.startsWith("x_")) {
130 					String name = a.substring(2);
131 					if (DEBUG)
132 						logger.info("Got setSize '" + name + "'");
133 					addToSpinList(spinList, chain, name);
134 				}
135 			}
136 			pmf.getBatchManager().getContactManager().addSpin(spinList);
137 		}
138 		
139 		if (success && !response.isCommitted())
140 		{
141 			response.sendRedirect("home.do");
142 			return(null);
143 		}
144 		else
145 			return(showForm(request, response, errors));
146 	}
147 
148 	private	void	addToSpinList(LinkedList<ContactParams> spinList, Chain chain, String setSizeName) {
149 		SetSize setSize = new SetSize(setSizeName);
150 		int		count	= chain.getContactCount();
151 		int		i;
152 		for (i=0; i<count; i++) {
153 			ContactParams cp = new ContactParams(setSize);
154 			cp.setCid(chain.getCid());
155 			cp.setRankMinMax(chain.getRatingMin(), chain.getRatingMax());
156 			cp.setContactNumber(i);
157 			spinList.add(cp);
158 		}
159 	}
160 }