View Javadoc
1   /******************************************************************************
2    * MosaicDisplayConfigFormController.java - The Spring controller to edit PicMan's Mosaic Display config
3    * 
4    * PicMan - The BuckoSoft Picture Manager in Java
5    * Copyright(c) 2009 - 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.domain.User;
25  
26  /** The Spring controller to edit the User's preferences for Mosaic Display
27   * @author Dick Balaska
28   * @since 2009/07/27
29   * @see <a href="http://cvs.buckosoft.com/Projects/java/PicMan/PicMan/src/com/buckosoft/PicMan/web/MosaicDisplayConfigFormController.java">MosaicDisplayConfigFormController.java</a>
30   */
31  public class MosaicDisplayConfigFormController extends BSAccountSimpleFormController {
32  
33  	private static final boolean DEBUG = false;
34      protected final Log logger = LogFactory.getLog(getClass());
35  
36  	private PicManFacade	pmf;
37  	
38  	/** Set the reference to the PicMan API.
39  	 * @param pmf The PicManFacade
40  	 */
41  	public void setPicMan(PicManFacade pmf) { this.pmf = pmf; }
42  
43  	public MosaicDisplayConfigFormController() {
44  		setSessionForm(true);
45  		setValidateOnBinding(false);
46  		setCommandName("mosaicDisplayConfigForm");
47  		setFormView("MosaicDisplayConfigForm");
48  	}
49  
50  	protected Object formBackingObject(HttpServletRequest request) throws Exception {
51  		BSAccountUserWebSession userWebSession = this.bsAccountMan.getUserWebSession(request);
52  		User user = null;
53  		if (userWebSession != null)
54  			user = (User)userWebSession.getUser();
55  		if (DEBUG)
56  			logger.info("formBackingObject session " + userWebSession);
57  		MosaicDisplayConfigForm mdcf = new MosaicDisplayConfigForm();
58  
59  		mdcf.setUserWebSession(userWebSession);
60  		mdcf.setUser(user);
61  
62  		String v;
63  		v = request.getParameter("removeMosaics");
64  		if (v != null) {
65  			int i = pmf.getMosaicMan().emptyMosaicQueue();
66  			mdcf.setMessage("Removed " + i + " queued Mosaics");
67  		}
68  		return(mdcf);
69  	}
70  
71  	protected void onBindAndValidate(HttpServletRequest request, Object command, BindException errors)
72  			throws Exception {
73  
74  		if (DEBUG)
75  			logger.info("onBindAndValidate");
76  	}
77  
78  	protected Map<String, Object> referenceData(HttpServletRequest request) throws Exception {
79      	BSAccountUserWebSession userWebSession = this.bsAccountMan.getUserWebSession(request);
80  		Map<String, Object> myModel = new HashMap<String, Object>();
81          myModel.put("userWebSession", userWebSession);
82          if (DEBUG)
83          	logger.info("referenceData session " + userWebSession);
84  
85  		return myModel;
86  	}
87  
88  	protected ModelAndView onSubmit(
89  			HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)
90  			throws Exception {
91  		boolean success = true;
92  //		BSAccountUserWebSession userWebSession = this.bsAccountMan.getUserWebSession(request);
93  //		User user = null;
94  //		if (userWebSession != null)
95  //			user = (User)userWebSession.getUser();
96  
97  		MosaicDisplayConfigForm mdcf = (MosaicDisplayConfigForm) command;
98  
99  			
100 		pmf.getDB().storeUser(mdcf.getUser());
101 		
102 		if (success) {
103 			try {
104 				if (!response.isCommitted())
105 					response.sendRedirect("mosaic.do");
106 			} catch (IllegalStateException e) {
107 				return super.onSubmit(request, response, command, errors);
108 			}
109 			return(null);
110 		}
111 		else
112 			return super.onSubmit(request, response, command, errors);
113 	}
114 }