Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A Class that implements organization 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-24 13:52:51 -0700 (Thu, 24 Jul 2008) $'
10
 * '$Revision: 4159 $'
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.Set;
31
import java.util.SortedMap;
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.util.OrganizationUtil;
42
import edu.ucsb.nceas.metacat.util.RequestUtil;
43
import edu.ucsb.nceas.metacat.util.UtilException;
44
import edu.ucsb.nceas.utilities.FileUtil;
45
import edu.ucsb.nceas.utilities.GeneralPropertyException;
46
import edu.ucsb.nceas.utilities.MetaDataProperty;
47
import edu.ucsb.nceas.utilities.PropertiesMetaData;
48
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
49
import edu.ucsb.nceas.utilities.SortedProperties;
50

    
51
/**
52
 * Control the display of the organization configuration page and the processing
53
 * of the configuration values.
54
 */
55
public class OrganizationAdmin extends MetaCatAdmin {
56

    
57
	private static OrganizationAdmin organizationAdmin = null;
58
	private static Logger logMetacat = Logger.getLogger(OrganizationAdmin.class);
59

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

    
65
	/**
66
	 * Get the single instance of the MetaCatConfig.
67
	 * 
68
	 * @return the single instance of MetaCatConfig
69
	 */
70
	public static OrganizationAdmin getInstance() {
71
		if (organizationAdmin == null) {
72
			organizationAdmin = new OrganizationAdmin();
73
		}
74
		return organizationAdmin;
75
	}
76
	
77
	/**
78
	 * Handle configuration of the Organization properties
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 configureOrganization(HttpServletRequest request,
86
			HttpServletResponse response) 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
			
96
			try {
97
				// Load the properties metadata file so that the JSP page can
98
				// use
99
				// the metadata to construct the editing form
100
				PropertiesMetaData metadata = PropertyService.getOrgMetaData();
101
				request.setAttribute("metadata", metadata);
102
				request.setAttribute("groupMap", metadata.getGroups());
103
				
104
				request.setAttribute("orgList", OrganizationUtil.getOrganizations());
105

    
106
				// add the list of organization options and their values to the request
107
				Vector<String> propertyNames = 
108
					PropertyService.getPropertyNamesByGroup("organization");
109
				for (String name : propertyNames) {
110
					request.setAttribute(name, PropertyService.getProperty(name));
111
				} 
112

    
113
				// Check for any backup properties and apply them to the request.
114
				// These are properties from previous configurations. They keep
115
				// the user from having to re-enter all values when upgrading.
116
				// If this is a first time install, getBackupProperties will return
117
				// null.
118
				SortedProperties backupProperties = PropertyService.getOrgBackupProperties();
119
				if (backupProperties != null) {
120
					Vector<String> backupKeys = backupProperties.getPropertyNames();
121
					for (String key : backupKeys) {
122
						String value = backupProperties.getProperty(key);
123
						if (value != null) {
124
							request.setAttribute(key, value);
125
						}
126
					}
127
				}
128
				// Forward the request to the JSP page
129
				RequestUtil.forwardRequest(request, response,
130
						"/admin/organization-configuration.jsp");
131
			} catch (PropertyNotFoundException pnfe) {
132
				throw new AdminException("Problem getting property while initializing "
133
						+ "organization properties page: " + pnfe.getMessage());
134
			} catch (UtilException ue) {
135
				throw new AdminException("Utility problem while " 
136
						+ "initializing organization properties page: " + ue.getMessage());
137
			} catch (IOException ioe) {
138
				throw new AdminException("IO problem while initializing "
139
						+ "organization properties page:" + ioe.getMessage());
140
			} catch (ServletException se) {
141
				throw new AdminException("problem forwarding request while " 
142
						+ "initializing organization properties page: " + se.getMessage());
143
			}
144
		} else {
145
			// The configuration form is being submitted and needs to be
146
			// processed.
147
			Vector<String> processingSuccess = new Vector<String>();
148
			Vector<String> processingErrors = new Vector<String>();
149
			Vector<String> validationErrors = new Vector<String>();
150

    
151
			try {
152
				// For each property, check if it is changed and save it
153
				PropertiesMetaData orgMetaData = 
154
					PropertyService.getOrgMetaData();
155

    
156
				Set<String> orgKeys = orgMetaData.getKeys();
157
				for (String orgName : OrganizationUtil.getOrganizations()) {
158
					for (String key : orgKeys) {
159
						PropertyService.checkAndSetProperty(request, key + "." + orgName);
160
					}
161
				}
162

    
163
				// we need to write the options from memory to the properties
164
				// file
165
				PropertyService.persistProperties();
166

    
167
				// Validate that the options provided are legitimate. Note that
168
				// we've allowed them to persist their entries. As of this point
169
				// there is no other easy way to go back to the configure form
170
				// and preserve their entries.
171
				validationErrors.addAll(validateOptions(request));
172

    
173
				// Try to create data file and backup directories if
174
				// necessary.
175
				String backupDir = PropertyService.getBackupDir();
176
				if (!FileUtil.createDirectory(backupDir)) {
177
					String errorString = "Could not create directory: " + backupDir;
178
					logMetacat.error(errorString);
179
					validationErrors.add(errorString);
180
				}
181

    
182
				// Write out the configurable properties to a backup file
183
				// outside the install directory.  Note that we allow them to
184
				// do this even if they have validation errors.  They will
185
				// need to go back and fix the errors before they can run metacat.
186
				PropertyService.persistOrgBackupProperties(request.getSession()
187
						.getServletContext());
188
			
189
			} catch (GeneralPropertyException gpe) {
190
				String errorMessage = "Problem getting or setting property while "
191
					+ "processing organization properties page: " + gpe.getMessage();
192
				logMetacat.error(errorMessage);
193
				processingErrors.add(errorMessage);
194
			} catch (IOException ioe) {
195
				String errorMessage = "IO problem while processing organization "
196
						+ "properties page: " + ioe.getMessage();
197
				logMetacat.error(errorMessage);
198
				processingErrors.add(errorMessage);
199
			} catch (UtilException ue) {
200
				String errorMessage = "Error in utilities while: " 
201
						+ "processing organization properties page: " + ue.getMessage();
202
				logMetacat.error(errorMessage);
203
				processingErrors.add(errorMessage);
204
			}
205
			
206
			try {
207
				if (validationErrors.size() > 0 || processingErrors.size() > 0) {
208
					RequestUtil.clearRequestMessages(request);
209
					RequestUtil.setRequestFormErrors(request, validationErrors);
210
					RequestUtil.setRequestErrors(request, processingErrors);
211
					RequestUtil.forwardRequest(request, response, "/admin");
212
				} else {
213
					// Now that the options have been set, change the
214
					// 'organizationsConfigured' option to 'true'
215
					PropertyService.setProperty("configutil.organizationsConfigured",
216
							PropertyService.CONFIGURED);
217
					
218
					// Reload the main metacat configuration page
219
					processingSuccess.add("Organizations successfully configured");
220
					RequestUtil.clearRequestMessages(request);
221
					RequestUtil.setRequestSuccess(request, processingSuccess);
222
					RequestUtil.forwardRequest(request, response,
223
							"/admin?configureType=configure&processForm=false");
224
				}
225
			} catch (ServletException se) {
226
				throw new AdminException("problem forwarding request while "
227
						+ "processing organization properties page: " + se.getMessage());
228
			} catch (IOException ioe) {
229
				throw new AdminException("IO problem while processing organization "
230
						+ "properties page: " + ioe.getMessage());
231
			} catch (GeneralPropertyException gpe) {
232
				String errorMessage = "Problem getting or setting property while "
233
					+ "processing organization properties page: " + gpe.getMessage();
234
				logMetacat.error(errorMessage);
235
				processingErrors.add(errorMessage);
236
			}
237
		}
238
	}
239
	
240
	/**
241
	 * Validate the most important configuration options submitted by the user.
242
	 * 
243
	 * @return a vector holding error message for any fields that fail
244
	 *         validation.
245
	 */
246
	protected Vector<String> validateOptions(HttpServletRequest request) {
247
		Vector<String> errorVector = new Vector<String>();
248

    
249
		//TODO MCD validate options.
250

    
251
		return errorVector;
252
	}
253
}
(7-7/9)