1 40 package org.dspace.app.webui.servlet.admin; 41 42 import java.io.IOException ; 43 import java.sql.SQLException ; 44 import java.util.Locale ; 45 import java.util.ResourceBundle ; 46 47 import javax.servlet.ServletException ; 48 import javax.servlet.http.HttpServletRequest ; 49 import javax.servlet.http.HttpServletResponse ; 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 68 public class MetadataFieldRegistryServlet extends DSpaceServlet 69 { 70 71 private static Logger log = Logger.getLogger(MetadataFieldRegistryServlet.class); 72 private String clazz = "org.dspace.app.webui.servlet.admin.MetadataFieldRegistryServlet"; 73 74 79 protected void doDSGet(Context context, HttpServletRequest request, 80 HttpServletResponse response) throws ServletException , IOException , 81 SQLException , AuthorizeException 82 { 83 int schemaID = getSchemaID(request); 85 showTypes(context, request, response, schemaID); 86 } 87 88 93 protected void doDSPost(Context context, HttpServletRequest request, 94 HttpServletResponse response) throws ServletException , IOException , 95 SQLException , AuthorizeException 96 { 97 String button = UIUtil.getSubmitButton(request, "submit"); 98 int schemaID = getSchemaID(request); 99 100 Locale locale = request.getLocale(); 102 ResourceBundle labels = ResourceBundle.getBundle("Messages", locale); 103 104 if (button.equals("submit_update")) 105 { 106 if (!sanityCheck(request, labels)) 108 { 109 showTypes(context, request, response, schemaID); 110 context.abort(); 111 return; 112 } 113 114 try 115 { 116 MetadataField dc = MetadataField.find(context, UIUtil 118 .getIntParameter(request, "dc_type_id")); 119 dc.setElement(request.getParameter("element")); 120 121 String 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 if (!sanityCheck(request, labels)) 144 { 145 showTypes(context, request, response, schemaID); 146 context.abort(); 147 return; 148 } 149 150 try 153 { 154 MetadataField dc = new MetadataField(); 155 dc.setSchemaID(schemaID); 156 dc.setElement(request.getParameter("element")); 157 158 String 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 log.warn(e); 174 175 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 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 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 try 207 { 208 schemaID = Integer.parseInt(request 209 .getParameter("dc_dest_schema_id")); 210 String [] 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 response.sendRedirect(request.getContextPath() 234 + "/dspace-admin/metadata-schema-registry?dc_schema_id=" + schemaID); 235 } 236 } 237 catch (NonUniqueMetadataException e) 238 { 239 log.warn(e); 241 242 request.setAttribute("error", labels.getString(clazz 245 + ".movefailed")); 246 showTypes(context, request, response, schemaID); 247 context.abort(); 248 } 249 } 250 else 251 { 252 showTypes(context, request, response, schemaID); 254 } 255 } 256 257 264 private int getSchemaID(HttpServletRequest 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 289 private void showTypes(Context context, HttpServletRequest request, 290 HttpServletResponse response, int schemaID) 291 throws ServletException , IOException , SQLException , 292 AuthorizeException 293 { 294 MetadataField[] types = MetadataField 296 .findAllInSchema(context, schemaID); 297 request.setAttribute("types", types); 298 299 MetadataSchema schema = MetadataSchema.find(context, schemaID); 301 request.setAttribute("schema", schema); 302 303 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 320 private boolean sanityCheck(HttpServletRequest request, 321 ResourceBundle labels) 322 { 323 String 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 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 373 private boolean error(HttpServletRequest request, String text) 374 { 375 request.setAttribute("error", text); 376 return false; 377 } 378 } 379 | Popular Tags |