Revision 4702
Added by daigle over 15 years ago
src/edu/ucsb/nceas/metacat/admin/OrganizationAdmin.java | ||
---|---|---|
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$' |
|
9 |
* '$Date$' |
|
10 |
* '$Revision$' |
|
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.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.util.OrganizationUtil; |
|
41 |
import edu.ucsb.nceas.metacat.util.RequestUtil; |
|
42 |
import edu.ucsb.nceas.metacat.util.UtilException; |
|
43 |
import edu.ucsb.nceas.utilities.FileUtil; |
|
44 |
import edu.ucsb.nceas.utilities.GeneralPropertyException; |
|
45 |
import edu.ucsb.nceas.utilities.PropertiesMetaData; |
|
46 |
import edu.ucsb.nceas.utilities.PropertyNotFoundException; |
|
47 |
import edu.ucsb.nceas.utilities.SortedProperties; |
|
48 |
|
|
49 |
/** |
|
50 |
* Control the display of the organization configuration page and the processing |
|
51 |
* of the configuration values. |
|
52 |
*/ |
|
53 |
public class OrganizationAdmin extends MetaCatAdmin { |
|
54 |
|
|
55 |
private static OrganizationAdmin organizationAdmin = null; |
|
56 |
private static Logger logMetacat = Logger.getLogger(OrganizationAdmin.class); |
|
57 |
|
|
58 |
/** |
|
59 |
* private constructor since this is a singleton |
|
60 |
*/ |
|
61 |
private OrganizationAdmin() {} |
|
62 |
|
|
63 |
/** |
|
64 |
* Get the single instance of the MetaCatConfig. |
|
65 |
* |
|
66 |
* @return the single instance of MetaCatConfig |
|
67 |
*/ |
|
68 |
public static OrganizationAdmin getInstance() { |
|
69 |
if (organizationAdmin == null) { |
|
70 |
organizationAdmin = new OrganizationAdmin(); |
|
71 |
} |
|
72 |
return organizationAdmin; |
|
73 |
} |
|
74 |
|
|
75 |
/** |
|
76 |
* Handle configuration of the Organization properties |
|
77 |
* |
|
78 |
* @param request |
|
79 |
* the http request information |
|
80 |
* @param response |
|
81 |
* the http response to be sent back to the client |
|
82 |
*/ |
|
83 |
public void configureOrganization(HttpServletRequest request, |
|
84 |
HttpServletResponse response) throws AdminException { |
|
85 |
|
|
86 |
String processForm = request.getParameter("processForm"); |
|
87 |
String formErrors = (String) request.getAttribute("formErrors"); |
|
88 |
|
|
89 |
if (processForm == null || !processForm.equals("true") || formErrors != null) { |
|
90 |
// The servlet configuration parameters have not been set, or there |
|
91 |
// were form errors on the last attempt to configure, so redirect to |
|
92 |
// the web form for configuring metacat |
|
93 |
|
|
94 |
try { |
|
95 |
// Load the properties metadata file so that the JSP page can |
|
96 |
// use |
|
97 |
// the metadata to construct the editing form |
|
98 |
PropertiesMetaData metadata = PropertyService.getOrgMetaData(); |
|
99 |
request.setAttribute("metadata", metadata); |
|
100 |
request.setAttribute("groupMap", metadata.getGroups()); |
|
101 |
|
|
102 |
request.setAttribute("orgList", OrganizationUtil.getOrganizations()); |
|
103 |
|
|
104 |
// add the list of organization options and their values to the request |
|
105 |
Vector<String> propertyNames = |
|
106 |
PropertyService.getPropertyNamesByGroup("organization"); |
|
107 |
for (String name : propertyNames) { |
|
108 |
request.setAttribute(name, PropertyService.getProperty(name)); |
|
109 |
} |
|
110 |
|
|
111 |
// Check for any backup properties and apply them to the request. |
|
112 |
// These are properties from previous configurations. They keep |
|
113 |
// the user from having to re-enter all values when upgrading. |
|
114 |
// If this is a first time install, getBackupProperties will return |
|
115 |
// null. |
|
116 |
SortedProperties backupProperties = PropertyService.getOrgBackupProperties(); |
|
117 |
if (backupProperties != null) { |
|
118 |
Vector<String> backupKeys = backupProperties.getPropertyNames(); |
|
119 |
for (String key : backupKeys) { |
|
120 |
String value = backupProperties.getProperty(key); |
|
121 |
if (value != null) { |
|
122 |
request.setAttribute(key, value); |
|
123 |
} |
|
124 |
} |
|
125 |
} |
|
126 |
// Forward the request to the JSP page |
|
127 |
RequestUtil.forwardRequest(request, response, |
|
128 |
"/admin/organization-configuration.jsp"); |
|
129 |
} catch (PropertyNotFoundException pnfe) { |
|
130 |
throw new AdminException("Problem getting property while initializing " |
|
131 |
+ "organization properties page: " + pnfe.getMessage()); |
|
132 |
} catch (UtilException ue) { |
|
133 |
throw new AdminException("Utility problem while " |
|
134 |
+ "initializing organization properties page: " + ue.getMessage()); |
|
135 |
} catch (IOException ioe) { |
|
136 |
throw new AdminException("IO problem while initializing " |
|
137 |
+ "organization properties page:" + ioe.getMessage()); |
|
138 |
} catch (ServletException se) { |
|
139 |
throw new AdminException("problem forwarding request while " |
|
140 |
+ "initializing organization properties page: " + se.getMessage()); |
|
141 |
} |
|
142 |
} else { |
|
143 |
// The configuration form is being submitted and needs to be |
|
144 |
// processed. |
|
145 |
Vector<String> processingSuccess = new Vector<String>(); |
|
146 |
Vector<String> processingErrors = new Vector<String>(); |
|
147 |
Vector<String> validationErrors = new Vector<String>(); |
|
148 |
|
|
149 |
try { |
|
150 |
// For each property, check if it is changed and save it |
|
151 |
PropertiesMetaData orgMetaData = |
|
152 |
PropertyService.getOrgMetaData(); |
|
153 |
|
|
154 |
Set<String> orgKeys = orgMetaData.getKeys(); |
|
155 |
for (String orgName : OrganizationUtil.getOrganizations()) { |
|
156 |
for (String key : orgKeys) { |
|
157 |
PropertyService.checkAndSetProperty(request, key + "." + orgName); |
|
158 |
} |
|
159 |
} |
|
160 |
|
|
161 |
// we need to write the options from memory to the properties |
|
162 |
// file |
|
163 |
PropertyService.persistProperties(); |
|
164 |
|
|
165 |
// Validate that the options provided are legitimate. Note that |
|
166 |
// we've allowed them to persist their entries. As of this point |
|
167 |
// there is no other easy way to go back to the configure form |
|
168 |
// and preserve their entries. |
|
169 |
validationErrors.addAll(validateOptions(request)); |
|
170 |
|
|
171 |
// Try to create data file and backup directories if |
|
172 |
// necessary. |
|
173 |
String backupDir = PropertyService.getBackupDir(); |
|
174 |
try { |
|
175 |
FileUtil.createDirectory(backupDir); |
|
176 |
} catch (IOException ioe) { |
|
177 |
String errorString = "Could not create directory: " + backupDir |
|
178 |
+ " : " + ioe.getMessage(); |
|
179 |
logMetacat.error(errorString); |
|
180 |
validationErrors.add(errorString); |
|
181 |
} |
|
182 |
|
|
183 |
// Write out the configurable properties to a backup file |
|
184 |
// outside the install directory. Note that we allow them to |
|
185 |
// do this even if they have validation errors. They will |
|
186 |
// need to go back and fix the errors before they can run metacat. |
|
187 |
PropertyService.persistOrgBackupProperties(request.getSession() |
|
188 |
.getServletContext()); |
|
189 |
|
|
190 |
} catch (GeneralPropertyException gpe) { |
|
191 |
String errorMessage = "Problem getting or setting property while " |
|
192 |
+ "processing organization properties page: " + gpe.getMessage(); |
|
193 |
logMetacat.error(errorMessage); |
|
194 |
processingErrors.add(errorMessage); |
|
195 |
} catch (UtilException ue) { |
|
196 |
String errorMessage = "Error in utilities while: " |
|
197 |
+ "processing organization properties page: " + ue.getMessage(); |
|
198 |
logMetacat.error(errorMessage); |
|
199 |
processingErrors.add(errorMessage); |
|
200 |
} |
|
201 |
|
|
202 |
try { |
|
203 |
if (validationErrors.size() > 0 || processingErrors.size() > 0) { |
|
204 |
RequestUtil.clearRequestMessages(request); |
|
205 |
RequestUtil.setRequestFormErrors(request, validationErrors); |
|
206 |
RequestUtil.setRequestErrors(request, processingErrors); |
|
207 |
RequestUtil.forwardRequest(request, response, "/admin"); |
|
208 |
} else { |
|
209 |
// Now that the options have been set, change the |
|
210 |
// 'organizationsConfigured' option to 'true' |
|
211 |
PropertyService.setProperty("configutil.organizationsConfigured", |
|
212 |
PropertyService.CONFIGURED); |
|
213 |
|
|
214 |
// Reload the main metacat configuration page |
|
215 |
processingSuccess.add("Organizations successfully configured"); |
|
216 |
RequestUtil.clearRequestMessages(request); |
|
217 |
RequestUtil.setRequestSuccess(request, processingSuccess); |
|
218 |
RequestUtil.forwardRequest(request, response, |
|
219 |
"/admin?configureType=configure&processForm=false"); |
|
220 |
} |
|
221 |
} catch (ServletException se) { |
|
222 |
throw new AdminException("problem forwarding request while " |
|
223 |
+ "processing organization properties page: " + se.getMessage()); |
|
224 |
} catch (IOException ioe) { |
|
225 |
throw new AdminException("IO problem while processing organization " |
|
226 |
+ "properties page: " + ioe.getMessage()); |
|
227 |
} catch (GeneralPropertyException gpe) { |
|
228 |
String errorMessage = "Problem getting or setting property while " |
|
229 |
+ "processing organization properties page: " + gpe.getMessage(); |
|
230 |
logMetacat.error(errorMessage); |
|
231 |
processingErrors.add(errorMessage); |
|
232 |
} |
|
233 |
} |
|
234 |
} |
|
235 |
|
|
236 |
/** |
|
237 |
* Validate the most important configuration options submitted by the user. |
|
238 |
* |
|
239 |
* @return a vector holding error message for any fields that fail |
|
240 |
* validation. |
|
241 |
*/ |
|
242 |
protected Vector<String> validateOptions(HttpServletRequest request) { |
|
243 |
Vector<String> errorVector = new Vector<String>(); |
|
244 |
|
|
245 |
//TODO MCD validate options. |
|
246 |
|
|
247 |
return errorVector; |
|
248 |
} |
|
249 |
} |
|
250 | 0 |
Also available in: Unified diff
Deleted this file. Organization level configuration is not used.