Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose:  A Class that implements skins configuration methods
4
 *  Copyright: 2008 Regents of the University of California and the
5
 *             National Center for Ecological Analysis and Synthesis
6
 *    Authors: Michael Daigle
7
 *
8
 *   '$Author: daigle $'
9
 *     '$Date: 2008-07-11 10:06:20 -0700 (Fri, 11 Jul 2008) $'
10
 * '$Revision: 4106 $'
11
 *
12
 * This program is free software; you can redistribute it and/or modify
13
 * it under the terms of the GNU General Public License as published by
14
 * the Free Software Foundation; either version 2 of the License, or
15
 * (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU General Public License
23
 * along with this program; if not, write to the Free Software
24
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
 */
26

    
27
package edu.ucsb.nceas.metacat.admin;
28

    
29
import java.io.IOException;
30
import java.util.HashMap;
31
import java.util.Vector;
32

    
33
import javax.servlet.ServletException;
34
import javax.servlet.http.HttpServletRequest;
35
import javax.servlet.http.HttpServletResponse;
36

    
37
import org.apache.log4j.Logger;
38

    
39
import edu.ucsb.nceas.metacat.service.PropertyService;
40
import edu.ucsb.nceas.metacat.service.SkinPropertyService;
41
import edu.ucsb.nceas.metacat.util.RequestUtil;
42
import edu.ucsb.nceas.metacat.util.SkinUtil;
43
import edu.ucsb.nceas.utilities.FileUtil;
44
import edu.ucsb.nceas.utilities.PropertiesMetaData;
45
import edu.ucsb.nceas.utilities.GeneralPropertyException;
46
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
47
import edu.ucsb.nceas.utilities.SortedProperties;
48

    
49
/**
50
 * Control the display of the skins configuration page and the processing
51
 * of the configuration values.
52
 */
53
public class SkinsAdmin extends MetaCatAdmin {
54

    
55
	private static SkinsAdmin skinsAdmin = null;
56
	private static Logger logMetacat = Logger.getLogger(SkinsAdmin.class);
57

    
58
	/**
59
	 * private constructor since this is a singleton
60
	 */
61
	private SkinsAdmin() {}
62

    
63
	/**
64
	 * Get the single instance of the MetaCatConfig.
65
	 * 
66
	 * @return the single instance of MetaCatConfig
67
	 */
68
	public static SkinsAdmin getInstance() {
69
		if (skinsAdmin == null) {
70
			skinsAdmin = new SkinsAdmin();
71
		}
72
		return skinsAdmin;
73
	}
74
	
75
	/**
76
	 * Handle configuration of the application the first time that Metacat
77
	 * starts or when it is explicitly called in the url. Collect necessary
78
	 * configuration information from the administrator.
79
	 * 
80
	 * @param request
81
	 *            the http request information
82
	 * @param response
83
	 *            the http response to be sent back to the client
84
	 */
85
	public void configureSkins(HttpServletRequest request, HttpServletResponse response)
86
			throws AdminException {
87

    
88
		String processForm = request.getParameter("processForm");
89
		String formErrors = (String) request.getAttribute("formErrors");
90

    
91
		if (processForm == null || !processForm.equals("true") || formErrors != null) {
92
			// The servlet configuration parameters have not been set, or there
93
			// were form errors on the last attempt to configure, so redirect to
94
			// the web form for configuring metacat
95
			try {
96
				Vector<String> skinNames = SkinUtil.getSkinNames();
97
				String defaultStyle = 
98
					PropertyService.getProperty("application.default-style");
99

    
100
				request.setAttribute("defaultStyle", defaultStyle);
101
				request.setAttribute("skinNameList", skinNames);
102
				
103
				// add skin metadata to the request.  The configuration form
104
				// will use this data to determine what the configuration 
105
				// fields should look like.
106
				HashMap<String, PropertiesMetaData> propertyMetaData = 
107
					SkinPropertyService.getMetaData();
108
				request.setAttribute("metadataMap", propertyMetaData);
109
				
110
				// get a deep copy of properties so we can override with 
111
				// backup values without changing originals
112
				HashMap<String, SortedProperties> originalPropertyMap =
113
					SkinPropertyService.getProperties();
114
				HashMap<String, HashMap<String, String>> localPropertyMap =
115
					new HashMap<String, HashMap<String, String>>();
116
				HashMap<String, SortedProperties> backupPropertiesMap = 
117
					SkinPropertyService.getBackupProperties();
118
					
119
				for (String skinName : skinNames) {
120
					// first, get a deep copy of this skin's properties
121

    
122
					SortedProperties skinProperties = originalPropertyMap.get(skinName);
123
					if (skinProperties == null) {
124
						throw new GeneralPropertyException("Could not find properties " 
125
								+ "for skin: " + skinName);
126
					}
127

    
128
					HashMap<String, String> originalSkinProperties = 
129
						skinProperties.getProperties();
130
					HashMap<String, String> localSkinProperties = 
131
						new HashMap<String, String>(originalSkinProperties);
132

    
133
					// now get the backup properties for this skin.  Overwrite the 
134
					// properties with backup properties.  This allows previously 
135
					// set properties to be preserved in an application upgrade.
136
					SortedProperties backupProperties = backupPropertiesMap.get(skinName);
137
					for (String propertyName : backupProperties.getPropertyNames())
138
					{
139
						localSkinProperties.put(propertyName, 
140
							backupProperties.getProperty(propertyName));
141
					}
142
								
143
					localPropertyMap.put(skinName, localSkinProperties);					
144
				}				
145
				request.setAttribute("skinProperties", localPropertyMap);
146
				
147
				// Forward the request to the JSP page
148
				RequestUtil.forwardRequest(request, response,
149
						"/admin/skins-configuration.jsp");
150

    
151
			} catch (GeneralPropertyException pnfe) {
152
				throw new AdminException("Problem getting property while initializing "
153
						+ "skins properties page: " + pnfe.getMessage());
154
			} catch (IOException ioe) {
155
				throw new AdminException("IO problem while initializing "
156
						+ "skins properties page:" + ioe.getMessage());
157
			} catch (ServletException se) {
158
				throw new AdminException("problem forwarding request while "
159
						+ "initializing skins properties page: " + se.getMessage());
160
			}
161

    
162
		} else {
163
			// The configuration form is being submitted and needs to be
164
			// processed.
165
			Vector<String> processingSuccess = new Vector<String>();
166
			Vector<String> processingErrors = new Vector<String>();
167
			Vector<String> validationErrors = new Vector<String>();
168

    
169
			try {
170
				// For each skin property, check if it is changed and save it
171
				Vector<String> skinNames = SkinUtil.getSkinNames();
172
				for (String skinName : skinNames) {
173
					Vector<String> propertyNames = SkinPropertyService
174
							.getPropertyNames(skinName);
175
					for (String propertyName : propertyNames) {
176
						SkinPropertyService.checkAndSetProperty(request, skinName,
177
								propertyName);
178
					}
179

    
180
					// we need to write the options from memory to the
181
					// properties file
182
					SkinPropertyService.persistProperties(skinName);
183

    
184
					// Validate that the options provided are legitimate. Note
185
					// that we've allowed them to persist their entries. As of 
186
					// this point there is no other easy way to go back to the 
187
					// configure form and preserve their entries.
188
					validationErrors.addAll(validateOptions(request, skinName));
189

    
190
					// Try to create backup directories if necessary.
191
					String backupDir = PropertyService.getBackupDir();
192
					if (!FileUtil.createDirectory(backupDir)) {
193
						String errorString = "Could not create directory: " + backupDir;
194
						logMetacat.error(errorString);
195
						validationErrors.add(errorString);
196
					}
197

    
198
					// write the backup properties to a location outside the
199
					// application directories so they will be available after
200
					// the next upgrade
201
					SkinPropertyService.persistBackupProperties(skinName, 
202
							request.getSession().getServletContext());
203
				}
204
				
205
				// Now that the options have been set, change the
206
				// 'skinsConfigured'
207
				// option to 'true' so that normal metacat requests will go
208
				// through
209
				PropertyService.setProperty("configutil.skinsConfigured",
210
						PropertyService.CONFIGURED);
211
			} catch (GeneralPropertyException gpe) {
212
				String errorMessage = "problem setting property while processing skins "
213
						+ "properties page: " + gpe.getMessage();
214
				logMetacat.error(errorMessage);
215
				processingErrors.add(errorMessage);
216
			} catch (IOException ioe) {
217
				String errorMessage = "IO problem while processing skins "
218
						+ "properties page: " + ioe.getMessage();
219
				logMetacat.error(errorMessage);
220
				processingErrors.add(errorMessage);
221
			}
222
			try {
223
				if (validationErrors.size() > 0 || processingErrors.size() > 0) {
224
					RequestUtil.clearRequestMessages(request);
225
					RequestUtil.setRequestFormErrors(request, validationErrors);
226
					RequestUtil.setRequestErrors(request, processingErrors);
227
					RequestUtil.forwardRequest(request, response, "/admin");
228
				} else {
229
					// Now that the options have been set, change the
230
					// 'skinsConfigured' option to 'true'
231
					PropertyService.setProperty("configutil.skinsConfigured",
232
							PropertyService.CONFIGURED);
233

    
234
					// Reload the main metacat configuration page
235
					processingSuccess.add("Skins successfully configured");
236
					RequestUtil.clearRequestMessages(request);
237
					RequestUtil.setRequestSuccess(request, processingSuccess);
238
					RequestUtil.forwardRequest(request, response,
239
							"/admin?configureType=configure&processForm=false");
240
				}
241
			} catch (ServletException se) {
242
				throw new AdminException("problem forwarding request while "
243
						+ "processing skins properties page: " + se.getMessage());
244
			} catch (IOException ioe) {
245
				throw new AdminException("IO problem while processing skins "
246
						+ "properties page: " + ioe.getMessage());
247
			} catch (GeneralPropertyException gpe) {
248
				String errorMessage = "problem setting property while processing skins "
249
						+ "properties page: " + gpe.getMessage();
250
				logMetacat.error(errorMessage);
251
				processingErrors.add(errorMessage);
252
			}
253
		}
254
	}
255
	
256
	protected Vector<String> validateOptions(HttpServletRequest request) {
257
		Vector<String> errorVector = new Vector<String>();
258
		Vector<String> skinNames = null;
259
		try {
260
			skinNames = SkinUtil.getSkinNames();
261
		} catch (PropertyNotFoundException pnfe) {
262
			errorVector.add("Could not find skin names: " + pnfe.getMessage());
263
		}
264
		
265
		for (String skinName : skinNames) {
266
			errorVector.addAll(validateOptions(request, skinName));
267
		}
268
		
269
		return errorVector;
270
	}
271
	
272
	/**
273
	 * Validate the most important configuration options submitted by the user.
274
	 * 
275
	 * @return a vector holding error message for any fields that fail
276
	 *         validation.
277
	 */
278
	protected Vector<String> validateOptions(HttpServletRequest request, String skinName) {
279
		Vector<String> errorVector = new Vector<String>();
280

    
281
		//TODO MCD validate options.
282

    
283
		return errorVector;
284
	}
285
}
(9-9/9)