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: 2009-08-14 17:38:05 -0700 (Fri, 14 Aug 2009) $'
10
 * '$Revision: 5028 $'
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.Set;
32
import java.util.Vector;
33

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

    
38
import org.apache.log4j.Logger;
39

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

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

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

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

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

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

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

    
101
				request.setAttribute("defaultStyle", defaultStyle);
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
				// Just in case a skin is not configured correctly, we will want to remove 
120
				// it from the skins list
121
				Vector<String> removeSkins = new Vector<String>();
122
					
123
				for (String skinName : skinNames) {
124
					// first, get a deep copy of this skin's properties
125

    
126
					SortedProperties skinProperties = originalPropertyMap.get(skinName);
127
					if (skinProperties == null) {
128
						logMetacat.error("Could not find properties for skin: " + skinName);
129
						removeSkins.add(skinName);
130
						continue;
131
					}
132

    
133
					HashMap<String, String> originalSkinProperties = 
134
						skinProperties.getProperties();
135
					HashMap<String, String> localSkinProperties = 
136
						new HashMap<String, String>(originalSkinProperties);
137

    
138
					// now get the backup properties for this skin.  Overwrite the 
139
					// properties with backup properties.  This allows previously 
140
					// set properties to be preserved in an application upgrade.
141
					SortedProperties backupProperties = backupPropertiesMap.get(skinName);
142
					if (backupProperties == null) {
143
						logMetacat.warn("Could not find backup properties for skin: "
144
								+ skinName);
145
					} else {
146
						for (String propertyName : backupProperties.getPropertyNames()) {
147
							localSkinProperties.put(propertyName, 
148
									backupProperties.getProperty(propertyName));
149
						}
150
					}
151
								
152
					localPropertyMap.put(skinName, localSkinProperties);					
153
				}	
154
				
155
				// If there are any skins for which we could not get properties, remove them from 
156
				// the skins list we will send to the configuration page.
157
				for (String skinName : removeSkins) {
158
					skinNames.remove(skinName);
159
				}
160
				
161
				request.setAttribute("skinNameList", skinNames);
162
				request.setAttribute("skinProperties", localPropertyMap);
163
				
164
				// Forward the request to the JSP page
165
				RequestUtil.forwardRequest(request, response,
166
						"/admin/skins-configuration.jsp", null);
167

    
168
			} catch (GeneralPropertyException pnfe) {
169
				throw new AdminException("Problem getting property while initializing "
170
						+ "skins properties page: " + pnfe.getMessage());
171
			} catch (IOException ioe) {
172
				throw new AdminException("IO problem while initializing "
173
						+ "skins properties page:" + ioe.getMessage());
174
			} catch (ServletException se) {
175
				throw new AdminException("problem forwarding request while "
176
						+ "initializing skins properties page: " + se.getMessage());
177
			}
178

    
179
		} else {
180
			// The configuration form is being submitted and needs to be
181
			// processed.
182
			Vector<String> processingSuccess = new Vector<String>();
183
			Vector<String> processingErrors = new Vector<String>();
184
			Vector<String> validationErrors = new Vector<String>();
185

    
186
			try {
187
				//Default style is global.
188
				String defaultStyle = request.getParameter("application.default-style");
189
				PropertyService.setProperty("application.default-style", defaultStyle);
190
				
191
				// For each skin property, check if it is changed and save it
192
				Vector<String> skinNames = SkinUtil.getSkinNames();
193
				for (String skinName : skinNames) {
194
					PropertiesMetaData skinMetaData = 
195
						SkinPropertyService.getMetaData(skinName);
196
					
197
					if (skinMetaData == null) {
198
						logMetacat.error("Could not find metadata for skin: " + skinName);
199
						continue;
200
					}
201
					
202
					Set<String> metaDataKeySet = skinMetaData.getKeys();
203
					for (String metaDataKey : metaDataKeySet) {
204
						MetaDataProperty metaDataProperty = 
205
							skinMetaData.getProperties().get(metaDataKey);
206
						String fieldType = metaDataProperty.getFieldType();
207
						String newValue = request.getParameter(skinName + "." + metaDataKey);
208
						if (fieldType != null && fieldType.equals(MetaDataProperty.CHECKBOX_TYPE)) {
209
							if (newValue != null && newValue.equals("on")) {
210
								SkinPropertyService.checkAndSetProperty("true", skinName, metaDataKey);
211
							} else {
212
								SkinPropertyService.checkAndSetProperty("false", skinName, metaDataKey);
213
							}								
214
						} else {		
215
							SkinPropertyService.checkAndSetProperty(request, skinName, metaDataKey);
216
						}
217
					}
218

    
219
					// we need to write the options from memory to the
220
					// properties file
221
					SkinPropertyService.persistProperties(skinName);
222

    
223
					// Validate that the options provided are legitimate. Note
224
					// that we've allowed them to persist their entries. As of 
225
					// this point there is no other easy way to go back to the 
226
					// configure form and preserve their entries.
227
					validationErrors.addAll(validateOptions(request, skinName));
228

    
229
					// write the backup properties to a location outside the
230
					// application directories so they will be available after
231
					// the next upgrade
232
					SkinPropertyService.persistBackupProperties(skinName);
233
					PropertyService.persistMainBackupProperties();
234
				}
235
				
236
				// Now that the options have been set, change the 'skinsConfigured'
237
				// option to 'true' so that normal metacat requests will go through
238
				PropertyService.setProperty("configutil.skinsConfigured",
239
						PropertyService.CONFIGURED);
240
			} catch (GeneralPropertyException gpe) {
241
				String errorMessage = "problem setting property while processing skins "
242
						+ "properties page: " + gpe.getMessage();
243
				logMetacat.error(errorMessage);
244
				processingErrors.add(errorMessage);
245
			} catch (IOException ioe) {
246
				String errorMessage = "IO problem while processing skins "
247
						+ "properties page: " + ioe.getMessage();
248
				logMetacat.error(errorMessage);
249
				processingErrors.add(errorMessage);
250
			}
251
			try {
252
				if (validationErrors.size() > 0 || processingErrors.size() > 0) {
253
					RequestUtil.clearRequestMessages(request);
254
					RequestUtil.setRequestFormErrors(request, validationErrors);
255
					RequestUtil.setRequestErrors(request, processingErrors);
256
					RequestUtil.forwardRequest(request, response, "/admin", null);
257
				} else {
258
					// Now that the options have been set, change the
259
					// 'skinsConfigured' option to 'true'
260
					PropertyService.setProperty("configutil.skinsConfigured",
261
							PropertyService.CONFIGURED);
262

    
263
					// Reload the main metacat configuration page
264
					processingSuccess.add("Skins successfully configured");
265
					RequestUtil.clearRequestMessages(request);
266
					RequestUtil.setRequestSuccess(request, processingSuccess);
267
					RequestUtil.forwardRequest(request, response,
268
							"/admin?configureType=configure&processForm=false", null);
269
				}
270
			} catch (ServletException se) {
271
				throw new AdminException("problem forwarding request while "
272
						+ "processing skins properties page: " + se.getMessage());
273
			} catch (IOException ioe) {
274
				throw new AdminException("IO problem while processing skins "
275
						+ "properties page: " + ioe.getMessage());
276
			} catch (GeneralPropertyException gpe) {
277
				String errorMessage = "problem setting property while processing skins "
278
						+ "properties page: " + gpe.getMessage();
279
				logMetacat.error(errorMessage);
280
				processingErrors.add(errorMessage);
281
			}
282
		}
283
	}
284
	
285
	protected Vector<String> validateOptions(HttpServletRequest request) {
286
		Vector<String> errorVector = new Vector<String>();
287
		Vector<String> skinNames = null;
288
		try {
289
			skinNames = SkinUtil.getSkinNames();
290
		} catch (PropertyNotFoundException pnfe) {
291
			errorVector.add("Could not find skin names: " + pnfe.getMessage());
292
		}
293
		
294
		for (String skinName : skinNames) {
295
			errorVector.addAll(validateOptions(request, skinName));
296
		}
297
		
298
		return errorVector;
299
	}
300
	
301
	/**
302
	 * Validate the most important configuration options submitted by the user.
303
	 * 
304
	 * @return a vector holding error message for any fields that fail
305
	 *         validation.
306
	 */
307
	protected Vector<String> validateOptions(HttpServletRequest request, String skinName) {
308
		Vector<String> errorVector = new Vector<String>();
309

    
310
		//TODO MCD validate options.
311

    
312
		return errorVector;
313
	}
314
}
(10-10/10)