View Javadoc
1   /******************************************************************************
2    * SystemSetupFormController.java - The Spring controller to edit PicMan's sytem config
3    * 
4    * PicMan - The BuckoSoft Picture Manager in Java
5    * Copyright(c) 2005 - 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.db.DatabaseFacade;
25  import com.buckosoft.PicMan.domain.System;
26  
27  /** The Spring controller to edit PicMan's System setup
28   * @author Dick Balaska
29   * @since 2005/07/27
30   * @see <a href="http://cvs.buckosoft.com/Projects/java/PicMan/PicMan/src/com/buckosoft/PicMan/web/SystemSetupFormController.java">SystemSetupFormController.java</a>
31   */
32  public class SystemSetupFormController extends BSAccountSimpleFormController {
33  
34  	private static final boolean DEBUG = false;
35      protected final Log logger = LogFactory.getLog(getClass());
36  
37  	private PicManFacade	pmf;
38  	private DatabaseFacade	dbf;
39  	
40  	/** Set the reference to the PicMan API.
41  	 * @param pmf The PicManFacade
42  	 */
43  	public void setPicMan(PicManFacade pmf) { this.pmf = pmf; }
44  //	public PicManFacade getPicMan() { return(pmf); }
45  
46  	public void setDatabase(DatabaseFacade dbf) { this.dbf = dbf; }
47  	public DatabaseFacade getDatabase() { return(dbf); }
48  
49  	public SystemSetupFormController() {
50  		setSessionForm(true);
51  		setValidateOnBinding(false);
52  		setCommandName("systemSetupForm");
53  		setFormView("SystemSetupForm");
54  	}
55  
56  	protected Object formBackingObject(HttpServletRequest request) throws Exception {
57  		BSAccountUserWebSession userWebSession = this.bsAccountMan.getUserWebSession(request);
58  		if (DEBUG)
59  			logger.info("formBackingObject session " + userWebSession);
60  		SystemSetupForm ssf = new SystemSetupForm();
61  
62  		ssf.setSystem(dbf.getSystem());
63  		ssf.setDbUrl(dbf.getDbUrl());
64  		ssf.setDbUser(dbf.getDbUser());
65  		ssf.setUserWebSession(userWebSession);
66  		//logger.info("formBacking: roots=" + pmf.getSystem().getRoot(0));
67  		String v;
68  		v = request.getParameter("deleteErrors");
69  		if (v != null) {
70  			pmf.emptyErrorLog();
71  			ssf.setMessage("Emptied Error Log");
72  		}
73  		v = request.getParameter("buildThumbCache");
74  		if (v != null) {
75  			ssf.setMessage("Queued build thumb Cache");
76  			pmf.getThumbCache().setBuildThumbCache(true);
77  		}
78  		v = request.getParameter("deleteThumbCache");
79  		if (v != null) {
80  			ssf.setClickedDeleteCache(true);
81  		}
82  		return(ssf);
83  	}
84  
85  	protected void onBindAndValidate(HttpServletRequest request, Object command, BindException errors)
86  			throws Exception {
87  
88  		SystemSetupForm ssf = (SystemSetupForm) command;
89  		if (DEBUG)
90  			logger.info("onBindAndValidate");
91  		if (request.getParameter("deleteCacheConfirmed") != null)
92  			return;
93  
94  		if (request.getParameter("system.engineOn") == null) {
95  			ssf.getSystem().setEngineOn(false);
96  		}
97  		if (request.getParameter("system.engineRunOnce") == null) {
98  			ssf.getSystem().setEngineRunOnce(false);
99  		}
100 		if (request.getParameter("system.syncEnable") == null) {
101 			ssf.getSystem().setSyncEnable(false);
102 		}
103 		if (request.getParameter("system.useThumbCache") == null) {
104 			ssf.getSystem().setUseThumbCache(false);
105 		}
106 		if (request.getParameter("system.skipXThumbs") == null) {
107 			ssf.getSystem().setSkipXThumbs(false);
108 		}
109 		if (request.getParameter("system.showRateOnPics") == null) {
110 			ssf.getSystem().setShowRateOnPics(false);
111 		}		
112 		if (request.getParameter("system.jobLogSummaryOnly") == null) {
113 			ssf.getSystem().setJobLogSummaryOnly(false);
114 		}
115 		if (ssf.getSystem().isUseThumbCache()) {
116 			if (ssf.getSystem().getThumbCacheDirectory() == null
117 			 || ssf.getSystem().getThumbCacheDirectory().length() == 0) {
118 				errors.reject("X", "Can't use Thumb Cache with no directory");
119 			}
120 			if (ssf.getSystem().getThumbCacheSize() == 0) {
121 				errors.reject("X", "Thumbs subdir size must be greater than 0");
122 			}
123 		}	 
124 	}
125 
126 	protected Map<String, Object> referenceData(HttpServletRequest request) throws Exception {
127     	BSAccountUserWebSession userWebSession = this.bsAccountMan.getUserWebSession(request);
128 		Map<String, Object> myModel = new HashMap<String, Object>();
129         myModel.put("userWebSession", userWebSession);
130         if (DEBUG)
131         	logger.info("referenceData session " + userWebSession);
132 
133 		return myModel;
134 	}
135 
136 	protected ModelAndView onSubmit(
137 			HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)
138 			throws Exception {
139 
140 		SystemSetupForm ssf = (SystemSetupForm) command;
141 
142 		boolean success = true;
143 		if (ssf.isClickedDeleteCache())
144 			success = false;
145 		if (ssf.isDeleteCacheConfirmed()) {
146 			pmf.getThumbCache().deleteCache();
147 			success = true;
148 		} else {
149 			if (DEBUG)
150 				logger.info("engineOn=" + String.valueOf(ssf.getSystem().isEngineOn()));
151 //			System oldSystem = dbf.getSystem();
152 			System newSystem = ssf.getSystem();
153 			
154 			dbf.saveSystem(newSystem);
155 //			pmf.setEngineCronExpression(pmf.getSystem().getEngineCronExpression());
156 		}
157 		pmf.setupThumbCache();
158 		if (success) {
159 			response.sendRedirect("home.do");
160 			return(null);
161 		}
162 		else
163 			return super.onSubmit(request, response, command, errors);
164 	}
165 }