KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dspace > app > webui > servlet > admin > MetadataSchemaRegistryServlet


1 /*
2  * MetadataSchemaRegistryServlet.java
3  *
4  * Version: $Revision: 1.1 $
5  *
6  * Date: $Date: 2005/11/16 21:40:50 $
7  *
8  * Copyright (c) 2002-2005, Hewlett-Packard Company and Massachusetts
9  * Institute of Technology. All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions are
13  * met:
14  *
15  * - Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  *
18  * - Redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in the
20  * documentation and/or other materials provided with the distribution.
21  *
22  * - Neither the name of the Hewlett-Packard Company nor the name of the
23  * Massachusetts Institute of Technology nor the names of their
24  * contributors may be used to endorse or promote products derived from
25  * this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
38  * DAMAGE.
39  */

40 package org.dspace.app.webui.servlet.admin;
41
42 import java.io.IOException JavaDoc;
43 import java.sql.SQLException JavaDoc;
44 import java.util.Locale JavaDoc;
45 import java.util.ResourceBundle JavaDoc;
46
47 import javax.servlet.ServletException JavaDoc;
48 import javax.servlet.http.HttpServletRequest JavaDoc;
49 import javax.servlet.http.HttpServletResponse JavaDoc;
50 import javax.servlet.jsp.jstl.fmt.LocaleSupport;
51
52 import org.apache.log4j.Logger;
53 import org.dspace.app.webui.servlet.DSpaceServlet;
54 import org.dspace.app.webui.util.JSPManager;
55 import org.dspace.app.webui.util.UIUtil;
56 import org.dspace.authorize.AuthorizeException;
57 import org.dspace.content.MetadataSchema;
58 import org.dspace.content.NonUniqueMetadataException;
59 import org.dspace.core.Context;
60
61 /**
62  * Servlet for editing the Dublin Core schema registry.
63  *
64  * @author Martin Hald
65  * @version $Revision: 1.1 $
66  */

67 public class MetadataSchemaRegistryServlet extends DSpaceServlet
68 {
69     /** Logger */
70     private static Logger log = Logger.getLogger(MetadataSchemaRegistryServlet.class);
71     private String JavaDoc clazz = "org.dspace.app.webui.servlet.admin.MetadataSchemaRegistryServlet";
72
73     protected void doDSGet(Context context, HttpServletRequest JavaDoc request,
74             HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc,
75             SQLException JavaDoc, AuthorizeException
76     {
77         // GET just displays the list of type
78
showSchemas(context, request, response);
79     }
80
81     protected void doDSPost(Context context, HttpServletRequest JavaDoc request,
82             HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc,
83             SQLException JavaDoc, AuthorizeException
84     {
85         String JavaDoc button = UIUtil.getSubmitButton(request, "submit");
86
87         if (button.equals("submit_add"))
88         {
89             // We are either going to create a new dc schema or update and
90
// existing one depending on if a schema_id was passed in
91
String JavaDoc id = request.getParameter("dc_schema_id");
92
93             // The sanity check will update the request error string if needed
94
if (!sanityCheck(request))
95             {
96                 showSchemas(context, request, response);
97                 context.abort();
98                 return;
99             }
100
101             try
102             {
103                 if (id.equals(""))
104                 {
105                     // Create a new metadata schema
106
MetadataSchema schema = new MetadataSchema();
107                     schema.setNamespace(request.getParameter("namespace"));
108                     schema.setName(request.getParameter("short_name"));
109                     schema.create(context);
110                     showSchemas(context, request, response);
111                     context.complete();
112                 }
113                 else
114                 {
115                     // Update an existing schema
116
MetadataSchema schema = MetadataSchema.find(context,
117                             UIUtil.getIntParameter(request, "dc_schema_id"));
118                     schema.setNamespace(request.getParameter("namespace"));
119                     schema.setName(request.getParameter("short_name"));
120                     schema.update(context);
121                     showSchemas(context, request, response);
122                     context.complete();
123                 }
124             }
125             catch (NonUniqueMetadataException e)
126             {
127                 request.setAttribute("error",
128                         "Please make the namespace and short name unique.");
129                 showSchemas(context, request, response);
130                 context.abort();
131                 return;
132             }
133         }
134         else if (button.equals("submit_delete"))
135         {
136             // Start delete process - go through verification step
137
MetadataSchema schema = MetadataSchema.find(context, UIUtil
138                     .getIntParameter(request, "dc_schema_id"));
139             request.setAttribute("schema", schema);
140             JSPManager.showJSP(request, response,
141                     "/dspace-admin/confirm-delete-mdschema.jsp");
142         }
143         else if (button.equals("submit_confirm_delete"))
144         {
145             // User confirms deletion of type
146
MetadataSchema dc = MetadataSchema.find(context, UIUtil
147                     .getIntParameter(request, "dc_schema_id"));
148             dc.delete(context);
149             showSchemas(context, request, response);
150             context.complete();
151         }
152         else
153         {
154             // Cancel etc. pressed - show list again
155
showSchemas(context, request, response);
156         }
157     }
158
159     /**
160      * Return false if the schema arguments fail to pass the constraints. If
161      * there is an error the request error String will be updated with an error
162      * description.
163      *
164      * @param request
165      * @return true of false
166      */

167     private boolean sanityCheck(HttpServletRequest JavaDoc request)
168     {
169         Locale JavaDoc locale = request.getLocale();
170         ResourceBundle JavaDoc labels =
171             ResourceBundle.getBundle("Messages", locale);
172         
173         // TODO: add more namespace checks
174
String JavaDoc namespace = request.getParameter("namespace");
175         if (namespace.length() == 0)
176         {
177             return error(request, labels.getString(clazz + ".emptynamespace"));
178         }
179
180         String JavaDoc name = request.getParameter("short_name");
181         if (name.length() == 0)
182         {
183             return error(request, labels.getString(clazz + ".emptyname"));
184         }
185         if (name.length() > 32)
186         {
187             return error(request, labels.getString(clazz + ".nametolong"));
188         }
189         for (int ii = 0; ii < name.length(); ii++)
190         {
191             if (name.charAt(ii) == ' ' || name.charAt(ii) == '_'
192                     || name.charAt(ii) == '.')
193             {
194                 return error(request,
195                         labels.getString(clazz + ".illegalchar"));
196             }
197         }
198
199         return true;
200     }
201
202     /**
203      * Bind the error text to the request object.
204      *
205      * @param request
206      * @param text
207      * @return false
208      */

209     private boolean error(HttpServletRequest JavaDoc request, String JavaDoc text)
210     {
211         request.setAttribute("error", text);
212         return false;
213     }
214
215     /**
216      * Show list of DC type
217      *
218      * @param context
219      * Current DSpace context
220      * @param request
221      * Current HTTP request
222      * @param response
223      * Current HTTP response
224      * @throws ServletException
225      * @throws IOException
226      * @throws SQLException
227      * @throws IOException
228      */

229     private void showSchemas(Context context, HttpServletRequest JavaDoc request,
230             HttpServletResponse JavaDoc response) throws ServletException JavaDoc,
231             SQLException JavaDoc, IOException JavaDoc
232     {
233         MetadataSchema[] schemas = MetadataSchema.findAll(context);
234         request.setAttribute("schemas", schemas);
235         log.info("Showing Schemas");
236         JSPManager.showJSP(request, response,
237                 "/dspace-admin/list-metadata-schemas.jsp");
238     }
239 }
240
Popular Tags