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.ArrayList ; 48 import java.util.List ; 49 import java.util.HashMap ; 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 69 public class MetadataField 70 { 71 private int fieldID = 0; 72 private int schemaID = 0; 73 private String element; 74 private String qualifier; 75 private String scopeNote; 76 77 78 private static Logger log = Logger.getLogger(MetadataField.class); 79 80 81 private TableRow row; 82 83 private static HashMap id2field = null; 85 86 87 90 public MetadataField() 91 { 92 } 93 94 99 public MetadataField(MetadataSchema schema) 100 { 101 this.schemaID = schema.getSchemaID(); 102 } 103 104 112 public MetadataField(MetadataSchema schema, String element, 113 String qualifier, String scopeNote) 114 { 115 this.schemaID = schema.getSchemaID(); 116 this.element = element; 117 this.qualifier = qualifier; 118 this.scopeNote = scopeNote; 119 } 120 121 130 public MetadataField(int schemaID, int fieldID, String element, 131 String qualifier, String scopeNote) 132 { 133 this.schemaID = schemaID; 134 this.fieldID = fieldID; 135 this.element = element; 136 this.qualifier = qualifier; 137 this.scopeNote = scopeNote; 138 } 139 140 145 public MetadataField(TableRow row) 146 { 147 if (row != null) 148 { 149 this.fieldID = row.getIntColumn("metadata_field_id"); 150 this.schemaID = row.getIntColumn("metadata_schema_id"); 151 this.element = row.getStringColumn("element"); 152 this.qualifier = row.getStringColumn("qualifier"); 153 this.scopeNote = row.getStringColumn("scope_note"); 154 this.row = row; 155 } 156 } 157 158 163 public String getElement() 164 { 165 return element; 166 } 167 168 173 public void setElement(String element) 174 { 175 this.element = element; 176 } 177 178 183 public int getFieldID() 184 { 185 return fieldID; 186 } 187 188 193 public String getQualifier() 194 { 195 return qualifier; 196 } 197 198 203 public void setQualifier(String qualifier) 204 { 205 this.qualifier = qualifier; 206 } 207 208 213 public int getSchemaID() 214 { 215 return schemaID; 216 } 217 218 223 public void setSchemaID(int schemaID) 224 { 225 this.schemaID = schemaID; 226 } 227 228 233 public String getScopeNote() 234 { 235 return scopeNote; 236 } 237 238 243 public void setScopeNote(String scopeNote) 244 { 245 this.scopeNote = scopeNote; 246 } 247 248 258 public void create(Context context) throws IOException , AuthorizeException, 259 SQLException , NonUniqueMetadataException 260 { 261 if (!AuthorizeManager.isAdmin(context)) 263 { 264 throw new AuthorizeException( 265 "Only administrators may modify the metadata registry"); 266 } 267 268 if (!unique(context, schemaID, element, qualifier)) 270 { 271 throw new NonUniqueMetadataException("Please make " + element + "." 272 + qualifier + " unique within schema #" + schemaID); 273 } 274 275 row = DatabaseManager.row("MetadataFieldRegistry"); 277 row.setColumn("metadata_schema_id", schemaID); 278 row.setColumn("element", element); 279 row.setColumn("qualifier", qualifier); 280 row.setColumn("scope_note", scopeNote); 281 DatabaseManager.insert(context, row); 282 decache(); 283 284 this.fieldID = row.getIntColumn("metadata_field_id"); 286 287 log.info(LogManager.getHeader(context, "create_metadata_field", 288 "metadata_field_id=" + row.getIntColumn("metadata_field_id"))); 289 } 290 291 302 public static MetadataField findByElement(Context context, int schemaID, 303 String element, String qualifier) throws SQLException , 304 AuthorizeException 305 { 306 TableRowIterator tri; 308 if (qualifier == null) 309 { 310 tri = DatabaseManager.queryTable(context,"MetadataFieldRegistry", 311 "SELECT * FROM MetadataFieldRegistry WHERE metadata_schema_id= ? " + 312 "AND element= ? AND qualifier is NULL ", 313 schemaID, element); 314 } 315 else 316 { 317 tri = DatabaseManager.queryTable(context,"MetadataFieldRegistry", 318 "SELECT * FROM MetadataFieldRegistry WHERE metadata_schema_id= ? " + 319 "AND element= ? AND qualifier= ? ", 320 schemaID, element, qualifier); 321 } 322 323 324 TableRow row = null; 325 if (tri.hasNext()) 326 { 327 row = tri.next(); 328 } 329 330 tri.close(); 332 333 if (row == null) 334 { 335 return null; 336 } 337 else 338 { 339 return new MetadataField(row); 340 } 341 } 342 343 350 public static MetadataField[] findAll(Context context) throws SQLException 351 { 352 List fields = new ArrayList (); 353 354 TableRowIterator tri = DatabaseManager.queryTable(context, "MetadataFieldRegistry", 356 "SELECT mfr.* FROM MetadataFieldRegistry mfr, MetadataSchemaRegistry msr where mfr.metadata_schema_id= msr.metadata_schema_id ORDER BY msr.short_id, mfr.element, mfr.qualifier"); 357 358 while (tri.hasNext()) 360 { 361 fields.add(new MetadataField(tri.next())); 362 } 363 tri.close(); 365 366 MetadataField[] typeArray = new MetadataField[fields.size()]; 368 return (MetadataField[]) fields.toArray(typeArray); 369 } 370 371 379 public static MetadataField[] findAllInSchema(Context context, int schemaID) 380 throws SQLException 381 { 382 List fields = new ArrayList (); 383 384 TableRowIterator tri = DatabaseManager.queryTable(context,"MetadataFieldRegistry", 386 "SELECT * FROM MetadataFieldRegistry WHERE metadata_schema_id= ? " + 387 " ORDER BY element, qualifier", schemaID); 388 389 while (tri.hasNext()) 391 { 392 fields.add(new MetadataField(tri.next())); 393 } 394 tri.close(); 396 397 MetadataField[] typeArray = new MetadataField[fields.size()]; 399 return (MetadataField[]) fields.toArray(typeArray); 400 } 401 402 411 public void update(Context context) throws SQLException , 412 AuthorizeException, NonUniqueMetadataException, IOException 413 { 414 if (!AuthorizeManager.isAdmin(context)) 416 { 417 throw new AuthorizeException( 418 "Only administrators may modiffy the Dublin Core registry"); 419 } 420 421 if (row.getIntColumn("metadata_schema_id") != schemaID) 424 { 425 if (MetadataField.hasElement(context, schemaID, element, qualifier)) 426 { 427 throw new NonUniqueMetadataException( 428 "Duplcate field name found in target schema"); 429 } 430 } 431 432 if (!unique(context, schemaID, element, qualifier)) 434 { 435 throw new NonUniqueMetadataException("Please make " + element + "." 436 + qualifier); 437 } 438 439 row.setColumn("metadata_schema_id", schemaID); 440 row.setColumn("element", element); 441 row.setColumn("qualifier", qualifier); 442 row.setColumn("scope_note", scopeNote); 443 DatabaseManager.update(context, row); 444 decache(); 445 446 log.info(LogManager.getHeader(context, "update_metadatafieldregistry", 447 "metadata_field_id=" + getFieldID() + "element=" + getElement() 448 + "qualifier=" + getQualifier())); 449 } 450 451 463 private static boolean hasElement(Context context, int schemaID, 464 String element, String qualifier) throws SQLException , 465 AuthorizeException 466 { 467 return MetadataField.findByElement(context, schemaID, element, 468 qualifier) != null; 469 } 470 471 478 public void delete(Context context) throws SQLException , AuthorizeException 479 { 480 if (!AuthorizeManager.isAdmin(context)) 482 { 483 throw new AuthorizeException( 484 "Only administrators may modify the metadata registry"); 485 } 486 487 log.info(LogManager.getHeader(context, "delete_metadata_field", 488 "metadata_field_id=" + getFieldID())); 489 490 DatabaseManager.delete(context, row); 491 decache(); 492 } 493 494 508 private boolean unique(Context context, int schemaID, String element, 509 String qualifier) throws IOException , SQLException , 510 AuthorizeException 511 { 512 Connection con = context.getDBConnection(); 513 TableRow reg = DatabaseManager.row("MetadataFieldRegistry"); 514 515 String query = "SELECT COUNT(*) FROM " + reg.getTable() 516 + " WHERE metadata_schema_id= ? " 517 + " and metadata_field_id != ? " 518 + " and element= ? and qualifier= ? "; 519 520 PreparedStatement statement = con.prepareStatement(query); 521 statement.setInt(1,schemaID); 522 statement.setInt(2,fieldID); 523 statement.setString(3,element); 524 statement.setString(4,qualifier); 525 526 ResultSet rs = statement.executeQuery(); 527 528 int count = 0; 529 if (rs.next()) 530 { 531 count = rs.getInt(1); 532 } 533 534 return (count == 0); 535 } 536 537 545 public static String formKey(String schema, String element, String qualifier) 546 { 547 if (qualifier == null) 548 { 549 return schema + "_" + element; 550 } 551 else 552 { 553 return schema + "_" + element + "_" + qualifier; 554 } 555 } 556 557 568 public static MetadataField find(Context context, int id) 569 throws SQLException 570 { 571 initCache(context); 572 573 Integer iid = new Integer (id); 575 if (!id2field.containsKey(iid)) 576 return null; 577 578 return (MetadataField) id2field.get(iid); 579 } 580 581 private static void decache() 583 { 584 id2field = null; 585 } 586 587 private static void initCache(Context context) throws SQLException 589 { 590 if (id2field != null) 591 return; 592 id2field = new HashMap (); 593 log.info("Loading MetadataField elements into cache."); 594 595 TableRowIterator tri = DatabaseManager.queryTable(context,"MetadataFieldRegistry", 597 "SELECT * from MetadataFieldRegistry"); 598 599 while (tri.hasNext()) 600 { 601 TableRow row = tri.next(); 602 int fieldID = row.getIntColumn("metadata_field_id"); 603 id2field.put(new Integer (fieldID), new MetadataField(row)); 604 } 605 606 tri.close(); 608 } 609 } 610 | Popular Tags |