1 40 package org.dspace.content; 41 42 import java.io.IOException ; 43 import java.sql.Connection ; 44 import java.sql.PreparedStatement ; 45 import java.sql.ResultSet ; 46 import java.sql.SQLException ; 47 import java.util.HashMap ; 48 import java.util.ArrayList ; 49 import java.util.List ; 50 51 import org.apache.log4j.Logger; 52 import org.dspace.authorize.AuthorizeException; 53 import org.dspace.authorize.AuthorizeManager; 54 import org.dspace.core.Context; 55 import org.dspace.core.LogManager; 56 import org.dspace.storage.rdbms.DatabaseManager; 57 import org.dspace.storage.rdbms.TableRow; 58 import org.dspace.storage.rdbms.TableRowIterator; 59 60 72 public class MetadataSchema 73 { 74 75 private static Logger log = Logger.getLogger(MetadataSchema.class); 76 77 78 public static final int DC_SCHEMA_ID = 1; 79 80 81 public static final String DC_SCHEMA = "dc"; 82 83 84 private TableRow row; 85 86 private int schemaID; 87 private String namespace; 88 private String name; 89 90 private static HashMap id2schema = null; 92 93 private static HashMap name2schema = null; 95 96 97 100 public MetadataSchema() 101 { 102 } 103 104 111 public MetadataSchema(int schemaID, String namespace, String name) 112 { 113 this.schemaID = schemaID; 114 this.namespace = namespace; 115 this.name = name; 116 } 117 118 124 public MetadataSchema(String namespace, String name) 125 { 126 this.namespace = namespace; 127 this.name = name; 128 } 129 130 135 public MetadataSchema(TableRow row) 136 { 137 if (row != null) 138 { 139 this.schemaID = row.getIntColumn("metadata_schema_id"); 140 this.namespace = row.getStringColumn("namespace"); 141 this.name = row.getStringColumn("short_id"); 142 this.row = row; 143 } 144 } 145 146 151 public String getNamespace() 152 { 153 return namespace; 154 } 155 156 161 public void setNamespace(String namespace) 162 { 163 this.namespace = namespace; 164 } 165 166 171 public String getName() 172 { 173 return name; 174 } 175 176 181 public void setName(String name) 182 { 183 this.name = name; 184 } 185 186 191 public int getSchemaID() 192 { 193 return schemaID; 194 } 195 196 205 public void create(Context context) throws SQLException , 206 AuthorizeException, NonUniqueMetadataException 207 { 208 if (!AuthorizeManager.isAdmin(context)) 210 { 211 throw new AuthorizeException( 212 "Only administrators may modify the metadata registry"); 213 } 214 215 if (!uniqueShortName(context, name)) 217 { 218 throw new NonUniqueMetadataException("Please make the name " + name 219 + " unique"); 220 } 221 222 if (!uniqueNamespace(context, namespace)) 224 { 225 throw new NonUniqueMetadataException("Please make the namespace " + namespace 226 + " unique"); 227 } 228 229 230 row = DatabaseManager.create(context, "MetadataSchemaRegistry"); 232 row.setColumn("namespace", namespace); 233 row.setColumn("short_id", name); 234 DatabaseManager.update(context, row); 235 236 decache(); 238 239 this.schemaID = row.getIntColumn("metadata_schema_id"); 241 242 log 243 .info(LogManager.getHeader(context, "create_metadata_schema", 244 "metadata_schema_id=" 245 + row.getIntColumn("metadata_schema_id"))); 246 } 247 248 256 public static MetadataSchema findByNamespace(Context context, 257 String namespace) throws SQLException 258 { 259 TableRowIterator tri = DatabaseManager.queryTable(context,"MetadataSchemaRegistry", 261 "SELECT * FROM MetadataSchemaRegistry WHERE namespace= ? ", 262 namespace); 263 264 TableRow row = null; 265 if (tri.hasNext()) 266 { 267 row = tri.next(); 268 } 269 270 tri.close(); 272 273 if (row == null) 274 { 275 return null; 276 } 277 else 278 { 279 return new MetadataSchema(row); 280 } 281 } 282 283 291 public void update(Context context) throws SQLException , 292 AuthorizeException, NonUniqueMetadataException 293 { 294 if (!AuthorizeManager.isAdmin(context)) 296 { 297 throw new AuthorizeException( 298 "Only administrators may modify the metadata registry"); 299 } 300 301 if (!uniqueShortName(context, name)) 303 { 304 throw new NonUniqueMetadataException("Please make the name " + name 305 + " unique"); 306 } 307 308 if (!uniqueNamespace(context, namespace)) 310 { 311 throw new NonUniqueMetadataException("Please make the namespace " + namespace 312 + " unique"); 313 } 314 315 row.setColumn("namespace", getNamespace()); 316 row.setColumn("short_id", getName()); 317 DatabaseManager.update(context, row); 318 319 decache(); 320 321 log.info(LogManager.getHeader(context, "update_metadata_schema", 322 "metadata_schema_id=" + getSchemaID() + "namespace=" 323 + getNamespace() + "name=" + getName())); 324 } 325 326 333 public void delete(Context context) throws SQLException , AuthorizeException 334 { 335 if (!AuthorizeManager.isAdmin(context)) 337 { 338 throw new AuthorizeException( 339 "Only administrators may modify the metadata registry"); 340 } 341 342 log.info(LogManager.getHeader(context, "delete_metadata_schema", 343 "metadata_schema_id=" + getSchemaID())); 344 345 DatabaseManager.delete(context, row); 346 } 347 348 355 public static MetadataSchema[] findAll(Context context) throws SQLException 356 { 357 List schemas = new ArrayList (); 358 359 TableRowIterator tri = DatabaseManager.queryTable(context, "MetadataSchemaRegistry", 361 "SELECT * FROM MetadataSchemaRegistry ORDER BY metadata_schema_id"); 362 363 while (tri.hasNext()) 365 { 366 schemas.add(new MetadataSchema(tri.next())); 367 } 368 tri.close(); 370 371 MetadataSchema[] typeArray = new MetadataSchema[schemas.size()]; 373 return (MetadataSchema[]) schemas.toArray(typeArray); 374 } 375 376 385 private boolean uniqueNamespace(Context context, String namespace) 386 throws SQLException 387 { 388 Connection con = context.getDBConnection(); 389 TableRow reg = DatabaseManager.row("MetadataSchemaRegistry"); 390 391 String query = "SELECT COUNT(*) FROM " + reg.getTable() + " " + 392 "WHERE metadata_schema_id != ? " + 393 "AND namespace= ? "; 394 PreparedStatement statement = con.prepareStatement(query); 395 statement.setInt(1,schemaID); 396 statement.setString(2,namespace); 397 398 ResultSet rs = statement.executeQuery(); 399 400 int count = 0; 401 if (rs.next()) 402 { 403 count = rs.getInt(1); 404 } 405 406 return (count == 0); 407 } 408 409 417 private boolean uniqueShortName(Context context, String name) 418 throws SQLException 419 { 420 Connection con = context.getDBConnection(); 421 TableRow reg = DatabaseManager.row("MetadataSchemaRegistry"); 422 423 String query = "SELECT COUNT(*) FROM " + reg.getTable() + " " + 424 "WHERE metadata_schema_id != ? " + 425 "AND short_id = ? "; 426 427 PreparedStatement statement = con.prepareStatement(query); 428 statement.setInt(1,schemaID); 429 statement.setString(2,name); 430 431 ResultSet rs = statement.executeQuery(); 432 433 int count = 0; 434 if (rs.next()) 435 { 436 count = rs.getInt(1); 437 } 438 439 return (count == 0); 440 } 441 442 453 public static MetadataSchema find(Context context, int id) 454 throws SQLException 455 { 456 initCache(context); 457 Integer iid = new Integer (id); 458 459 if (!id2schema.containsKey(iid)) 461 return null; 462 463 return (MetadataSchema) id2schema.get(iid); 464 } 465 466 476 public static MetadataSchema find(Context context, String shortName) 477 throws SQLException 478 { 479 if (shortName == null) 481 return null; 482 483 initCache(context); 484 485 if (!name2schema.containsKey(shortName)) 486 return null; 487 488 return (MetadataSchema) name2schema.get(shortName); 489 } 490 491 private static void decache() 493 { 494 id2schema = null; 495 name2schema = null; 496 } 497 498 private static void initCache(Context context) throws SQLException 500 { 501 if (id2schema != null && name2schema != null) 502 return; 503 504 log.info("Loading schema cache for fast finds"); 505 id2schema = new HashMap (); 506 name2schema = new HashMap (); 507 508 TableRowIterator tri = DatabaseManager.queryTable(context,"MetadataSchemaRegistry", 509 "SELECT * from MetadataSchemaRegistry"); 510 while (tri.hasNext()) 511 { 512 TableRow row = tri.next(); 513 514 MetadataSchema s = new MetadataSchema(row); 515 id2schema.put(new Integer (s.schemaID), s); 516 name2schema.put(s.name, s); 517 } 518 tri.close(); 520 } 521 } 522 | Popular Tags |