View Javadoc
1   /******************************************************************************
2    * RootManFormController.java - The Spring controller to edit PicMan's Roots
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.db.DatabaseFacade;
24  import com.buckosoft.PicMan.domain.Root;
25  
26  /**
27   * The Spring controller to edit the Roots.
28   * @author Dick Balaska
29   * @since 2006/10/02
30   */
31  public class RootManFormController extends BSAccountSimpleFormController {
32  	private	final static boolean DEBUG = false;
33      protected final Log logger = LogFactory.getLog(getClass());
34  
35  	private DatabaseFacade dbf;
36  	
37  	/** Set the reference to the PicMan Database
38  	 * @param dbf The DatabaseFacade
39  	 */
40  	public void setDatabase(DatabaseFacade dbf) { this.dbf = dbf; }
41  //	public DatabaseFacade getDatabase() { return(dbf); }
42  
43  	public RootManFormController() {
44  		setSessionForm(true);
45  		setValidateOnBinding(false);
46  		setCommandName("rootManForm");
47  		setFormView("RootManForm");
48  	}
49  
50  	protected Object formBackingObject(HttpServletRequest request) throws Exception {
51      	BSAccountUserWebSession userWebSession = this.bsAccountMan.getUserWebSession(request);
52  		if (DEBUG)
53  			logger.info("formBackingObject RootupSystemForm with session " + userWebSession);
54  		RootManForm rmf = new RootManForm();
55  
56  		rmf.setRoots(dbf.getRoots());
57  		rmf.setUserWebSession(userWebSession);
58  		rmf.setEditRoot(null);
59  		String v = request.getParameter("edit");
60  		if (v != null) {
61  			int rid = Integer.parseInt(v);
62  			rmf.setEditRoot(dbf.getRoot(rid));
63  		}
64  		//logger.info("formBacking: roots=" + pmf.getSystem().getRoot(0));
65  		return(rmf);
66  	}
67  
68  	protected void onBindAndValidate(HttpServletRequest request, Object command, BindException errors)
69  			throws Exception {
70  	}
71  
72  	protected Map<String, Object> referenceData(HttpServletRequest request) throws Exception {
73      	BSAccountUserWebSession userWebSession = this.bsAccountMan.getUserWebSession(request);
74  		Map<String, Object> myModel = new HashMap<String, Object>();
75          myModel.put("userWebSession", userWebSession);
76          if (DEBUG)
77          	logger.info("referenceData RootupSystemForm with session " + userWebSession);
78  
79  		return myModel;
80  	}
81  
82  	protected ModelAndView onSubmit(
83  			HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)
84  			throws Exception {
85  
86  		RootManForm rmf = (RootManForm) command;
87  		
88  		checkActiveFlags(request);
89  
90  		boolean success = true;
91  		logger.info("Root to delete = '" + rmf.getRootToDelete() + "'");
92  		if (rmf.getRootToDelete() != null && rmf.getRootToDelete().length() > 0) {
93  			Root root = new Root();
94  			root.setName(rmf.getRootToDelete());
95  			dbf.deleteRoot(root);
96  		}
97  		if (rmf.getNewRoot() != null 
98  				&& rmf.getNewRoot().getName() != null
99  				&& rmf.getNewRoot().getName().length() > 0) {
100 			if (DEBUG)
101 				logger.info("Adding new root '" + rmf.getNewRoot().getName() + "' desc: '" + rmf.getNewRoot().getPath() + "'");
102 			dbf.addRoot(rmf.getNewRoot());
103 		}
104 		if (rmf.getEditRoot() != null) {
105 			if (DEBUG)
106 				logger.info("Updating root " + rmf.getEditRoot().getRid());
107 			dbf.storeRoot(rmf.getEditRoot());
108 		}
109 		if (success) {
110 			response.sendRedirect("home.do");
111 			return(null);
112 		}
113 		else
114 			return super.onSubmit(request, response, command, errors);
115 	}
116 
117 	private	void	checkActiveFlags(HttpServletRequest request) {
118 		int		count = dbf.getRootCount();
119 		for (int i=1; i<=count; i++) {
120 			Root root = dbf.getRoot(i);
121 			boolean b = false;
122 			if (request.getParameter("active_" + i) != null)
123 				b = true;
124 			if (root.isActive() != b) {
125 				root.setActive(b);
126 				dbf.storeRoot(root);
127 			}
128 		}
129 	}
130 
131 }
132