KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * MetadataFieldRegistryServlet.java
3  *
4  * Version: $Revision: 1.3 $
5  *
6  * Date: $Date: 2006/11/06 04:59:55 $
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
51 import org.apache.log4j.Logger;
52 import org.dspace.app.webui.servlet.DSpaceServlet;
53 import org.dspace.app.webui.util.JSPManager;
54 import org.dspace.app.webui.util.UIUtil;
55 import org.dspace.authorize.AuthorizeException;
56 import org.dspace.content.MetadataField;
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 registry
63  *
64  * @author Robert Tansley
65  * @author Martin Hald
66  * @version $Revision: 1.3 $
67  */

68 public class MetadataFieldRegistryServlet extends DSpaceServlet
69 {
70     /** Logger */
71     private static Logger log = Logger.getLogger(MetadataFieldRegistryServlet.class);
72     private String JavaDoc clazz = "org.dspace.app.webui.servlet.admin.MetadataFieldRegistryServlet";
73
74     /**
75      * @see org.dspace.app.webui.servlet.DSpaceServlet#doDSGet(org.dspace.core.Context,
76      * javax.servlet.http.HttpServletRequest,
77      * javax.servlet.http.HttpServletResponse)
78      */

79     protected void doDSGet(Context context, HttpServletRequest JavaDoc request,
80             HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc,
81             SQLException JavaDoc, AuthorizeException
82     {
83         // GET just displays the list of type
84
int schemaID = getSchemaID(request);
85         showTypes(context, request, response, schemaID);
86     }
87
88     /**
89      * @see org.dspace.app.webui.servlet.DSpaceServlet#doDSPost(org.dspace.core.Context,
90      * javax.servlet.http.HttpServletRequest,
91      * javax.servlet.http.HttpServletResponse)
92      */

93     protected void doDSPost(Context context, HttpServletRequest JavaDoc request,
94             HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc,
95             SQLException JavaDoc, AuthorizeException
96     {
97         String JavaDoc button = UIUtil.getSubmitButton(request, "submit");
98         int schemaID = getSchemaID(request);
99
100         // Get access to the localized resource bundle
101
Locale JavaDoc locale = request.getLocale();
102         ResourceBundle JavaDoc labels = ResourceBundle.getBundle("Messages", locale);
103
104         if (button.equals("submit_update"))
105         {
106             // The sanity check will update the request error string if needed
107
if (!sanityCheck(request, labels))
108             {
109                 showTypes(context, request, response, schemaID);
110                 context.abort();
111                 return;
112             }
113
114             try
115             {
116                 // Update the metadata for a DC type
117
MetadataField dc = MetadataField.find(context, UIUtil
118                         .getIntParameter(request, "dc_type_id"));
119             dc.setElement(request.getParameter("element"));
120
121             String JavaDoc qual = request.getParameter("qualifier");
122             if (qual.equals(""))
123             {
124                 qual = null;
125             }
126
127             dc.setQualifier(qual);
128             dc.setScopeNote(request.getParameter("scope_note"));
129                 dc.update(context);
130                 showTypes(context, request, response, schemaID);
131             context.complete();
132         }
133             catch (NonUniqueMetadataException e)
134             {
135                 context.abort();
136                 log.error(e);
137             }
138         }
139         else if (button.equals("submit_add"))
140         {
141
142             // The sanity check will update the request error string if needed
143
if (!sanityCheck(request, labels))
144             {
145                 showTypes(context, request, response, schemaID);
146                 context.abort();
147                 return;
148             }
149
150             // Add a new DC type - simply add to the list, and let the user
151
// edit with the main form
152
try
153             {
154                 MetadataField dc = new MetadataField();
155                 dc.setSchemaID(schemaID);
156                 dc.setElement(request.getParameter("element"));
157
158                 String JavaDoc qual = request.getParameter("qualifier");
159                 if (qual.equals(""))
160                 {
161                     qual = null;
162                 }
163
164                 dc.setQualifier(qual);
165                 dc.setScopeNote(request.getParameter("scope_note"));
166                 dc.create(context);
167                 showTypes(context, request, response, schemaID);
168             context.complete();
169         }
170             catch (NonUniqueMetadataException e)
171             {
172                 // Record the exception as a warning
173
log.warn(e);
174
175                 // Show the page again but with an error message to inform the
176
// user that the metadata field was not created and why
177
request.setAttribute("error", labels.getString(clazz
178                         + ".createfailed"));
179                 showTypes(context, request, response, schemaID);
180                 context.abort();
181             }
182         }
183         else if (button.equals("submit_delete"))
184         {
185             // Start delete process - go through verification step
186
MetadataField dc = MetadataField.find(context, UIUtil
187                     .getIntParameter(request, "dc_type_id"));
188             request.setAttribute("type", dc);
189             JSPManager.showJSP(request, response,
190                     "/dspace-admin/confirm-delete-mdfield.jsp");
191         }
192         else if (button.equals("submit_confirm_delete"))
193         {
194             // User confirms deletion of type
195
MetadataField dc = MetadataField.find(context, UIUtil
196                     .getIntParameter(request, "dc_type_id"));
197             dc.delete(context);
198             showTypes(context, request, response, schemaID);
199             context.complete();
200         }
201         else if (button.equals("submit_move"))
202         {
203             // User requests that one or more metadata elements be moved to a
204
// new metadata schema. Note that we change the default schema ID to
205
// be the destination schema.
206
try
207             {
208                 schemaID = Integer.parseInt(request
209                         .getParameter("dc_dest_schema_id"));
210                 String JavaDoc[] param = request.getParameterValues("dc_field_id");
211                 if (schemaID == 0 || param == null)
212                 {
213                     request.setAttribute("error", labels.getString(clazz
214                             + ".movearguments"));
215                     showTypes(context, request, response, schemaID);
216                     context.abort();
217                 }
218                 else
219                 {
220                     for (int ii = 0; ii < param.length; ii++)
221                     {
222                         int fieldID = Integer.parseInt(param[ii]);
223                         MetadataField field = MetadataField.find(context,
224                                 fieldID);
225                         field.setSchemaID(schemaID);
226                         field.update(context);
227
228                     }
229             context.complete();
230                      
231                     // Send the user to the metadata schema in which they just moved
232
// the metadata fields
233
response.sendRedirect(request.getContextPath()
234                             + "/dspace-admin/metadata-schema-registry?dc_schema_id=" + schemaID);
235                 }
236             }
237             catch (NonUniqueMetadataException e)
238             {
239                 // Record the exception as a warning
240
log.warn(e);
241
242                 // Show the page again but with an error message to inform the
243
// user that the metadata field could not be moved
244
request.setAttribute("error", labels.getString(clazz
245                         + ".movefailed"));
246                 showTypes(context, request, response, schemaID);
247                 context.abort();
248             }
249         }
250         else
251         {
252             // Cancel etc. pressed - show list again
253
showTypes(context, request, response, schemaID);
254         }
255     }
256
257     /**
258      * Get the schema that we are currently working in from the HTTP request. If
259      * not present then default to the DSpace Dublin Core schema (schemaID 1).
260      *
261      * @param request
262      * @return the current schema ID
263      */

264     private int getSchemaID(HttpServletRequest JavaDoc request)
265     {
266         int schemaID = MetadataSchema.DC_SCHEMA_ID;
267         if (request.getParameter("dc_schema_id") != null)
268         {
269             schemaID = Integer.parseInt(request.getParameter("dc_schema_id"));
270         }
271         return schemaID;
272     }
273
274     /**
275      * Show list of DC type
276      *
277      * @param context
278      * Current DSpace context
279      * @param request
280      * Current HTTP request
281      * @param response
282      * Current HTTP response
283      * @param schemaID
284      * @throws ServletException
285      * @throws IOException
286      * @throws SQLException
287      * @throws AuthorizeException
288      */

289     private void showTypes(Context context, HttpServletRequest JavaDoc request,
290             HttpServletResponse JavaDoc response, int schemaID)
291             throws ServletException JavaDoc, IOException JavaDoc, SQLException JavaDoc,
292             AuthorizeException
293     {
294         // Find matching metadata fields
295
MetadataField[] types = MetadataField
296                 .findAllInSchema(context, schemaID);
297         request.setAttribute("types", types);
298
299         // Pull the metadata schema object as well
300
MetadataSchema schema = MetadataSchema.find(context, schemaID);
301         request.setAttribute("schema", schema);
302
303         // Pull all metadata schemas for the pulldown
304
MetadataSchema[] schemas = MetadataSchema.findAll(context);
305         request.setAttribute("schemas", schemas);
306
307         JSPManager
308                 .showJSP(request, response, "/dspace-admin/list-metadata-fields.jsp");
309     }
310
311     /**
312      * Return false if the metadata field fail to pass the constraint tests. If
313      * there is an error the request error String will be updated with an error
314      * description.
315      *
316      * @param request
317      * @param labels
318      * @return true of false
319      */

320     private boolean sanityCheck(HttpServletRequest JavaDoc request,
321             ResourceBundle JavaDoc labels)
322     {
323         String JavaDoc element = request.getParameter("element");
324         if (element.length() == 0)
325         {
326             return error(request, labels.getString(clazz + ".elemempty"));
327         }
328         for (int ii = 0; ii < element.length(); ii++)
329         {
330             if (element.charAt(ii) == '.' || element.charAt(ii) == '_'
331                     || element.charAt(ii) == ' ')
332             {
333                 return error(request, labels.getString(clazz + ".badelemchar"));
334             }
335         }
336         if (element.length() > 64)
337         {
338             return error(request, labels.getString(clazz + ".elemtoolong"));
339         }
340
341         String JavaDoc qualifier = request.getParameter("qualifier");
342         if (qualifier == "")
343         {
344             qualifier = null;
345         }
346         if (qualifier != null)
347         {
348             if (qualifier.length() > 64)
349             {
350                 return error(request, labels.getString(clazz + ".qualtoolong"));
351             }
352             for (int ii = 0; ii < qualifier.length(); ii++)
353             {
354                 if (qualifier.charAt(ii) == '.' || qualifier.charAt(ii) == '_'
355                         || qualifier.charAt(ii) == ' ')
356                 {
357                     return error(request, labels.getString(clazz
358                             + ".badqualchar"));
359                 }
360             }
361         }
362
363         return true;
364     }
365
366     /**
367      * Bind the error text to the request object.
368      *
369      * @param request
370      * @param text
371      * @return false
372      */

373     private boolean error(HttpServletRequest JavaDoc request, String JavaDoc text)
374     {
375         request.setAttribute("error", text);
376         return false;
377     }
378 }
379
Popular Tags