001: /*
002: * MetadataSchemaRegistryServlet.java
003: *
004: * Version: $Revision: 1373 $
005: *
006: * Date: $Date: 2005-11-16 15:40:53 -0600 (Wed, 16 Nov 2005) $
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: import javax.servlet.jsp.jstl.fmt.LocaleSupport;
051:
052: import org.apache.log4j.Logger;
053: import org.dspace.app.webui.servlet.DSpaceServlet;
054: import org.dspace.app.webui.util.JSPManager;
055: import org.dspace.app.webui.util.UIUtil;
056: import org.dspace.authorize.AuthorizeException;
057: import org.dspace.content.MetadataSchema;
058: import org.dspace.content.NonUniqueMetadataException;
059: import org.dspace.core.Context;
060:
061: /**
062: * Servlet for editing the Dublin Core schema registry.
063: *
064: * @author Martin Hald
065: * @version $Revision: 1373 $
066: */
067: public class MetadataSchemaRegistryServlet extends DSpaceServlet {
068: /** Logger */
069: private static Logger log = Logger
070: .getLogger(MetadataSchemaRegistryServlet.class);
071: private String clazz = "org.dspace.app.webui.servlet.admin.MetadataSchemaRegistryServlet";
072:
073: protected void doDSGet(Context context, HttpServletRequest request,
074: HttpServletResponse response) throws ServletException,
075: IOException, SQLException, AuthorizeException {
076: // GET just displays the list of type
077: showSchemas(context, request, response);
078: }
079:
080: protected void doDSPost(Context context,
081: HttpServletRequest request, HttpServletResponse response)
082: throws ServletException, IOException, SQLException,
083: AuthorizeException {
084: String button = UIUtil.getSubmitButton(request, "submit");
085:
086: if (button.equals("submit_add")) {
087: // We are either going to create a new dc schema or update and
088: // existing one depending on if a schema_id was passed in
089: String id = request.getParameter("dc_schema_id");
090:
091: // The sanity check will update the request error string if needed
092: if (!sanityCheck(request)) {
093: showSchemas(context, request, response);
094: context.abort();
095: return;
096: }
097:
098: try {
099: if (id.equals("")) {
100: // Create a new metadata schema
101: MetadataSchema schema = new MetadataSchema();
102: schema.setNamespace(request
103: .getParameter("namespace"));
104: schema.setName(request.getParameter("short_name"));
105: schema.create(context);
106: showSchemas(context, request, response);
107: context.complete();
108: } else {
109: // Update an existing schema
110: MetadataSchema schema = MetadataSchema.find(
111: context, UIUtil.getIntParameter(request,
112: "dc_schema_id"));
113: schema.setNamespace(request
114: .getParameter("namespace"));
115: schema.setName(request.getParameter("short_name"));
116: schema.update(context);
117: showSchemas(context, request, response);
118: context.complete();
119: }
120: } catch (NonUniqueMetadataException e) {
121: request
122: .setAttribute("error",
123: "Please make the namespace and short name unique.");
124: showSchemas(context, request, response);
125: context.abort();
126: return;
127: }
128: } else if (button.equals("submit_delete")) {
129: // Start delete process - go through verification step
130: MetadataSchema schema = MetadataSchema.find(context, UIUtil
131: .getIntParameter(request, "dc_schema_id"));
132: request.setAttribute("schema", schema);
133: JSPManager.showJSP(request, response,
134: "/dspace-admin/confirm-delete-mdschema.jsp");
135: } else if (button.equals("submit_confirm_delete")) {
136: // User confirms deletion of type
137: MetadataSchema dc = MetadataSchema.find(context, UIUtil
138: .getIntParameter(request, "dc_schema_id"));
139: dc.delete(context);
140: showSchemas(context, request, response);
141: context.complete();
142: } else {
143: // Cancel etc. pressed - show list again
144: showSchemas(context, request, response);
145: }
146: }
147:
148: /**
149: * Return false if the schema arguments fail to pass the constraints. If
150: * there is an error the request error String will be updated with an error
151: * description.
152: *
153: * @param request
154: * @return true of false
155: */
156: private boolean sanityCheck(HttpServletRequest request) {
157: Locale locale = request.getLocale();
158: ResourceBundle labels = ResourceBundle.getBundle("Messages",
159: locale);
160:
161: // TODO: add more namespace checks
162: String namespace = request.getParameter("namespace");
163: if (namespace.length() == 0) {
164: return error(request, labels.getString(clazz
165: + ".emptynamespace"));
166: }
167:
168: String name = request.getParameter("short_name");
169: if (name.length() == 0) {
170: return error(request, labels
171: .getString(clazz + ".emptyname"));
172: }
173: if (name.length() > 32) {
174: return error(request, labels.getString(clazz
175: + ".nametolong"));
176: }
177: for (int ii = 0; ii < name.length(); ii++) {
178: if (name.charAt(ii) == ' ' || name.charAt(ii) == '_'
179: || name.charAt(ii) == '.') {
180: return error(request, labels.getString(clazz
181: + ".illegalchar"));
182: }
183: }
184:
185: return true;
186: }
187:
188: /**
189: * Bind the error text to the request object.
190: *
191: * @param request
192: * @param text
193: * @return false
194: */
195: private boolean error(HttpServletRequest request, String text) {
196: request.setAttribute("error", text);
197: return false;
198: }
199:
200: /**
201: * Show list of DC type
202: *
203: * @param context
204: * Current DSpace context
205: * @param request
206: * Current HTTP request
207: * @param response
208: * Current HTTP response
209: * @throws ServletException
210: * @throws IOException
211: * @throws SQLException
212: * @throws IOException
213: */
214: private void showSchemas(Context context,
215: HttpServletRequest request, HttpServletResponse response)
216: throws ServletException, SQLException, IOException {
217: MetadataSchema[] schemas = MetadataSchema.findAll(context);
218: request.setAttribute("schemas", schemas);
219: log.info("Showing Schemas");
220: JSPManager.showJSP(request, response,
221: "/dspace-admin/list-metadata-schemas.jsp");
222: }
223: }
|