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 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 67 public class MetadataSchemaRegistryServlet extends DSpaceServlet 68 { 69 70 private static Logger log = Logger.getLogger(MetadataSchemaRegistryServlet.class); 71 private String clazz = "org.dspace.app.webui.servlet.admin.MetadataSchemaRegistryServlet"; 72 73 protected void doDSGet(Context context, HttpServletRequest request, 74 HttpServletResponse response) throws ServletException , IOException , 75 SQLException , AuthorizeException 76 { 77 showSchemas(context, request, response); 79 } 80 81 protected void doDSPost(Context context, HttpServletRequest request, 82 HttpServletResponse response) throws ServletException , IOException , 83 SQLException , AuthorizeException 84 { 85 String button = UIUtil.getSubmitButton(request, "submit"); 86 87 if (button.equals("submit_add")) 88 { 89 String id = request.getParameter("dc_schema_id"); 92 93 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 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 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 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 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 showSchemas(context, request, response); 156 } 157 } 158 159 167 private boolean sanityCheck(HttpServletRequest request) 168 { 169 Locale locale = request.getLocale(); 170 ResourceBundle labels = 171 ResourceBundle.getBundle("Messages", locale); 172 173 String namespace = request.getParameter("namespace"); 175 if (namespace.length() == 0) 176 { 177 return error(request, labels.getString(clazz + ".emptynamespace")); 178 } 179 180 String 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 209 private boolean error(HttpServletRequest request, String text) 210 { 211 request.setAttribute("error", text); 212 return false; 213 } 214 215 229 private void showSchemas(Context context, HttpServletRequest request, 230 HttpServletResponse response) throws ServletException , 231 SQLException , IOException 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 |