001: /*
002: * MetadataFieldRegistryServlet.java
003: *
004: * Version: $Revision: 1947 $
005: *
006: * Date: $Date: 2007-05-18 08:50:29 -0500 (Fri, 18 May 2007) $
007: *
008: * Copyright (c) 2002-2005, Hewlett-Packard Company and Massachusetts
009: * Institute of Technology. All rights reserved.
010: *
011: * Redistribution and use in source and binary forms, with or without
012: * modification, are permitted provided that the following conditions are
013: * met:
014: *
015: * - Redistributions of source code must retain the above copyright
016: * notice, this list of conditions and the following disclaimer.
017: *
018: * - Redistributions in binary form must reproduce the above copyright
019: * notice, this list of conditions and the following disclaimer in the
020: * documentation and/or other materials provided with the distribution.
021: *
022: * - Neither the name of the Hewlett-Packard Company nor the name of the
023: * Massachusetts Institute of Technology nor the names of their
024: * contributors may be used to endorse or promote products derived from
025: * this software without specific prior written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
030: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
032: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
033: * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
034: * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
035: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
036: * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
037: * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
038: * DAMAGE.
039: */
040: package org.dspace.app.webui.servlet.admin;
041:
042: import java.io.IOException;
043: import java.sql.SQLException;
044: import java.util.Locale;
045: import java.util.ResourceBundle;
046:
047: import javax.servlet.ServletException;
048: import javax.servlet.http.HttpServletRequest;
049: import javax.servlet.http.HttpServletResponse;
050:
051: import org.apache.log4j.Logger;
052: import org.dspace.app.webui.servlet.DSpaceServlet;
053: import org.dspace.app.webui.util.JSPManager;
054: import org.dspace.app.webui.util.UIUtil;
055: import org.dspace.authorize.AuthorizeException;
056: import org.dspace.content.MetadataField;
057: import org.dspace.content.MetadataSchema;
058: import org.dspace.content.NonUniqueMetadataException;
059: import org.dspace.core.Context;
060: import org.dspace.core.I18nUtil;
061:
062: /**
063: * Servlet for editing the Dublin Core registry
064: *
065: * @author Robert Tansley
066: * @author Martin Hald
067: * @version $Revision: 1947 $
068: */
069: public class MetadataFieldRegistryServlet extends DSpaceServlet {
070: /** Logger */
071: private static Logger log = Logger
072: .getLogger(MetadataFieldRegistryServlet.class);
073: private String clazz = "org.dspace.app.webui.servlet.admin.MetadataFieldRegistryServlet";
074:
075: /**
076: * @see org.dspace.app.webui.servlet.DSpaceServlet#doDSGet(org.dspace.core.Context,
077: * javax.servlet.http.HttpServletRequest,
078: * javax.servlet.http.HttpServletResponse)
079: */
080: protected void doDSGet(Context context, HttpServletRequest request,
081: HttpServletResponse response) throws ServletException,
082: IOException, SQLException, AuthorizeException {
083: // GET just displays the list of type
084: int schemaID = getSchemaID(request);
085: showTypes(context, request, response, schemaID);
086: }
087:
088: /**
089: * @see org.dspace.app.webui.servlet.DSpaceServlet#doDSPost(org.dspace.core.Context,
090: * javax.servlet.http.HttpServletRequest,
091: * javax.servlet.http.HttpServletResponse)
092: */
093: protected void doDSPost(Context context,
094: HttpServletRequest request, HttpServletResponse response)
095: throws ServletException, IOException, SQLException,
096: AuthorizeException {
097: String button = UIUtil.getSubmitButton(request, "submit");
098: int schemaID = getSchemaID(request);
099:
100: // Get access to the localized resource bundle
101: Locale locale = context.getCurrentLocale();
102: ResourceBundle labels = ResourceBundle.getBundle("Messages",
103: locale);
104:
105: if (button.equals("submit_update")) {
106: // The sanity check will update the request error string if needed
107: if (!sanityCheck(request, labels)) {
108: showTypes(context, request, response, schemaID);
109: context.abort();
110: return;
111: }
112:
113: try {
114: // Update the metadata for a DC type
115: MetadataField dc = MetadataField.find(context, UIUtil
116: .getIntParameter(request, "dc_type_id"));
117: dc.setElement(request.getParameter("element"));
118:
119: String qual = request.getParameter("qualifier");
120: if (qual.equals("")) {
121: qual = null;
122: }
123:
124: dc.setQualifier(qual);
125: dc.setScopeNote(request.getParameter("scope_note"));
126: dc.update(context);
127: showTypes(context, request, response, schemaID);
128: context.complete();
129: } catch (NonUniqueMetadataException e) {
130: context.abort();
131: log.error(e);
132: }
133: } else if (button.equals("submit_add")) {
134:
135: // The sanity check will update the request error string if needed
136: if (!sanityCheck(request, labels)) {
137: showTypes(context, request, response, schemaID);
138: context.abort();
139: return;
140: }
141:
142: // Add a new DC type - simply add to the list, and let the user
143: // edit with the main form
144: try {
145: MetadataField dc = new MetadataField();
146: dc.setSchemaID(schemaID);
147: dc.setElement(request.getParameter("element"));
148:
149: String qual = request.getParameter("qualifier");
150: if (qual.equals("")) {
151: qual = null;
152: }
153:
154: dc.setQualifier(qual);
155: dc.setScopeNote(request.getParameter("scope_note"));
156: dc.create(context);
157: showTypes(context, request, response, schemaID);
158: context.complete();
159: } catch (NonUniqueMetadataException e) {
160: // Record the exception as a warning
161: log.warn(e);
162:
163: // Show the page again but with an error message to inform the
164: // user that the metadata field was not created and why
165: request.setAttribute("error", labels.getString(clazz
166: + ".createfailed"));
167: showTypes(context, request, response, schemaID);
168: context.abort();
169: }
170: } else if (button.equals("submit_delete")) {
171: // Start delete process - go through verification step
172: MetadataField dc = MetadataField.find(context, UIUtil
173: .getIntParameter(request, "dc_type_id"));
174: request.setAttribute("type", dc);
175: JSPManager.showJSP(request, response,
176: "/dspace-admin/confirm-delete-mdfield.jsp");
177: } else if (button.equals("submit_confirm_delete")) {
178: // User confirms deletion of type
179: MetadataField dc = MetadataField.find(context, UIUtil
180: .getIntParameter(request, "dc_type_id"));
181: dc.delete(context);
182: showTypes(context, request, response, schemaID);
183: context.complete();
184: } else if (button.equals("submit_move")) {
185: // User requests that one or more metadata elements be moved to a
186: // new metadata schema. Note that we change the default schema ID to
187: // be the destination schema.
188: try {
189: schemaID = Integer.parseInt(request
190: .getParameter("dc_dest_schema_id"));
191: String[] param = request
192: .getParameterValues("dc_field_id");
193: if (schemaID == 0 || param == null) {
194: request.setAttribute("error", labels
195: .getString(clazz + ".movearguments"));
196: showTypes(context, request, response, schemaID);
197: context.abort();
198: } else {
199: for (int ii = 0; ii < param.length; ii++) {
200: int fieldID = Integer.parseInt(param[ii]);
201: MetadataField field = MetadataField.find(
202: context, fieldID);
203: field.setSchemaID(schemaID);
204: field.update(context);
205:
206: }
207: context.complete();
208:
209: // Send the user to the metadata schema in which they just moved
210: // the metadata fields
211: response
212: .sendRedirect(request.getContextPath()
213: + "/dspace-admin/metadata-schema-registry?dc_schema_id="
214: + schemaID);
215: }
216: } catch (NonUniqueMetadataException e) {
217: // Record the exception as a warning
218: log.warn(e);
219:
220: // Show the page again but with an error message to inform the
221: // user that the metadata field could not be moved
222: request.setAttribute("error", labels.getString(clazz
223: + ".movefailed"));
224: showTypes(context, request, response, schemaID);
225: context.abort();
226: }
227: } else {
228: // Cancel etc. pressed - show list again
229: showTypes(context, request, response, schemaID);
230: }
231: }
232:
233: /**
234: * Get the schema that we are currently working in from the HTTP request. If
235: * not present then default to the DSpace Dublin Core schema (schemaID 1).
236: *
237: * @param request
238: * @return the current schema ID
239: */
240: private int getSchemaID(HttpServletRequest request) {
241: int schemaID = MetadataSchema.DC_SCHEMA_ID;
242: if (request.getParameter("dc_schema_id") != null) {
243: schemaID = Integer.parseInt(request
244: .getParameter("dc_schema_id"));
245: }
246: return schemaID;
247: }
248:
249: /**
250: * Show list of DC type
251: *
252: * @param context
253: * Current DSpace context
254: * @param request
255: * Current HTTP request
256: * @param response
257: * Current HTTP response
258: * @param schemaID
259: * @throws ServletException
260: * @throws IOException
261: * @throws SQLException
262: * @throws AuthorizeException
263: */
264: private void showTypes(Context context, HttpServletRequest request,
265: HttpServletResponse response, int schemaID)
266: throws ServletException, IOException, SQLException,
267: AuthorizeException {
268: // Find matching metadata fields
269: MetadataField[] types = MetadataField.findAllInSchema(context,
270: schemaID);
271: request.setAttribute("types", types);
272:
273: // Pull the metadata schema object as well
274: MetadataSchema schema = MetadataSchema.find(context, schemaID);
275: request.setAttribute("schema", schema);
276:
277: // Pull all metadata schemas for the pulldown
278: MetadataSchema[] schemas = MetadataSchema.findAll(context);
279: request.setAttribute("schemas", schemas);
280:
281: JSPManager.showJSP(request, response,
282: "/dspace-admin/list-metadata-fields.jsp");
283: }
284:
285: /**
286: * Return false if the metadata field fail to pass the constraint tests. If
287: * there is an error the request error String will be updated with an error
288: * description.
289: *
290: * @param request
291: * @param labels
292: * @return true of false
293: */
294: private boolean sanityCheck(HttpServletRequest request,
295: ResourceBundle labels) {
296: String element = request.getParameter("element");
297: if (element.length() == 0) {
298: return error(request, labels
299: .getString(clazz + ".elemempty"));
300: }
301: for (int ii = 0; ii < element.length(); ii++) {
302: if (element.charAt(ii) == '.' || element.charAt(ii) == '_'
303: || element.charAt(ii) == ' ') {
304: return error(request, labels.getString(clazz
305: + ".badelemchar"));
306: }
307: }
308: if (element.length() > 64) {
309: return error(request, labels.getString(clazz
310: + ".elemtoolong"));
311: }
312:
313: String qualifier = request.getParameter("qualifier");
314: if (qualifier == "") {
315: qualifier = null;
316: }
317: if (qualifier != null) {
318: if (qualifier.length() > 64) {
319: return error(request, labels.getString(clazz
320: + ".qualtoolong"));
321: }
322: for (int ii = 0; ii < qualifier.length(); ii++) {
323: if (qualifier.charAt(ii) == '.'
324: || qualifier.charAt(ii) == '_'
325: || qualifier.charAt(ii) == ' ') {
326: return error(request, labels.getString(clazz
327: + ".badqualchar"));
328: }
329: }
330: }
331:
332: return true;
333: }
334:
335: /**
336: * Bind the error text to the request object.
337: *
338: * @param request
339: * @param text
340: * @return false
341: */
342: private boolean error(HttpServletRequest request, String text) {
343: request.setAttribute("error", text);
344: return false;
345: }
346: }
|