1 40 package org.dspace.content; 41 42 import java.sql.SQLException ; 43 import java.util.ArrayList ; 44 import java.util.List ; 45 46 import org.apache.log4j.Logger; 47 import org.dspace.authorize.AuthorizeException; 48 import org.dspace.authorize.AuthorizeManager; 49 import org.dspace.core.ConfigurationManager; 50 import org.dspace.core.Context; 51 import org.dspace.core.LogManager; 52 import org.dspace.storage.rdbms.DatabaseManager; 53 import org.dspace.storage.rdbms.TableRow; 54 import org.dspace.storage.rdbms.TableRowIterator; 55 56 65 public class BitstreamFormat 66 { 67 68 private static Logger log = Logger.getLogger(BitstreamFormat.class); 69 70 74 public static final int UNKNOWN = 0; 75 76 80 public static final int KNOWN = 1; 81 82 86 public static final int SUPPORTED = 2; 87 88 89 private Context bfContext; 90 91 92 private TableRow bfRow; 93 94 95 private List extensions; 96 97 107 BitstreamFormat(Context context, TableRow row) throws SQLException 108 { 109 bfContext = context; 110 bfRow = row; 111 extensions = new ArrayList (); 112 113 TableRowIterator tri = DatabaseManager.query(context, 114 "SELECT * FROM fileextension WHERE bitstream_format_id= ? ", 115 getID()); 116 117 while (tri.hasNext()) 118 { 119 extensions.add(tri.next().getStringColumn("extension")); 120 } 121 tri.close(); 123 124 context.cache(this, row.getIntColumn("bitstream_format_id")); 126 } 127 128 139 public static BitstreamFormat find(Context context, int id) 140 throws SQLException 141 { 142 BitstreamFormat fromCache = (BitstreamFormat) context.fromCache( 144 BitstreamFormat.class, id); 145 146 if (fromCache != null) 147 { 148 return fromCache; 149 } 150 151 TableRow row = DatabaseManager.find(context, "bitstreamformatregistry", 152 id); 153 154 if (row == null) 155 { 156 if (log.isDebugEnabled()) 157 { 158 log.debug(LogManager.getHeader(context, 159 "find_bitstream_format", 160 "not_found,bitstream_format_id=" + id)); 161 } 162 163 return null; 164 } 165 166 if (log.isDebugEnabled()) 168 { 169 log.debug(LogManager.getHeader(context, "find_bitstream_format", 170 "bitstream_format_id=" + id)); 171 } 172 173 return new BitstreamFormat(context, row); 174 } 175 176 190 public static BitstreamFormat findByMIMEType(Context context, 191 String mimeType) throws SQLException 192 { 193 TableRow formatRow = DatabaseManager.querySingle(context, 196 "SELECT * FROM bitstreamformatregistry "+ 197 "WHERE mimetype LIKE ? AND internal = '0' ", 198 mimeType); 199 200 if (formatRow == null) 201 return null; 202 return findByFinish(context, formatRow); 203 } 204 205 217 public static BitstreamFormat findByShortDescription(Context context, 218 String desc) throws SQLException 219 { 220 TableRow formatRow = DatabaseManager.findByUnique(context, 221 "bitstreamformatregistry", "short_description", desc); 222 223 if (formatRow == null) 224 { 225 return null; 226 } 227 228 return findByFinish(context, formatRow); 229 } 230 231 private static BitstreamFormat findByFinish(Context context, 234 TableRow formatRow) 235 throws SQLException 236 { 237 if (log.isDebugEnabled()) 239 { 240 log.debug(LogManager.getHeader(context, "find_bitstream", 241 "bitstream_format_id=" 242 + formatRow.getIntColumn("bitstream_format_id"))); 243 } 244 245 BitstreamFormat fromCache = (BitstreamFormat) context.fromCache( 247 BitstreamFormat.class, formatRow 248 .getIntColumn("bitstream_format_id")); 249 250 if (fromCache != null) 251 { 252 return fromCache; 253 } 254 255 return new BitstreamFormat(context, formatRow); 256 } 257 258 270 public static BitstreamFormat findUnknown(Context context) 271 throws SQLException 272 { 273 BitstreamFormat bf = findByShortDescription(context, "Unknown"); 274 275 if (bf == null) 276 { 277 throw new IllegalStateException ( 278 "No `Unknown' bitstream format in registry"); 279 } 280 281 return bf; 282 } 283 284 293 public static BitstreamFormat[] findAll(Context context) 294 throws SQLException 295 { 296 List formats = new ArrayList (); 297 298 TableRowIterator tri = DatabaseManager.queryTable(context, "bitstreamformatregistry", 299 "SELECT * FROM bitstreamformatregistry ORDER BY bitstream_format_id"); 300 301 while (tri.hasNext()) 302 { 303 TableRow row = tri.next(); 304 305 BitstreamFormat fromCache = (BitstreamFormat) context.fromCache( 307 BitstreamFormat.class, row 308 .getIntColumn("bitstream_format_id")); 309 310 if (fromCache != null) 311 { 312 formats.add(fromCache); 313 } 314 else 315 { 316 formats.add(new BitstreamFormat(context, row)); 317 } 318 } 319 tri.close(); 321 322 BitstreamFormat[] formatArray = new BitstreamFormat[formats.size()]; 324 formatArray = (BitstreamFormat[]) formats.toArray(formatArray); 325 326 return formatArray; 327 } 328 329 340 public static BitstreamFormat[] findNonInternal(Context context) 341 throws SQLException 342 { 343 List formats = new ArrayList (); 344 345 String myQuery = "SELECT * FROM bitstreamformatregistry WHERE internal='0' " 346 + "AND short_description NOT LIKE 'Unknown' " 347 + "ORDER BY support_level DESC, short_description"; 348 349 TableRowIterator tri = DatabaseManager.queryTable(context, 350 "bitstreamformatregistry", myQuery); 351 352 while (tri.hasNext()) 353 { 354 TableRow row = tri.next(); 355 356 BitstreamFormat fromCache = (BitstreamFormat) context.fromCache( 358 BitstreamFormat.class, row 359 .getIntColumn("bitstream_format_id")); 360 361 if (fromCache != null) 362 { 363 formats.add(fromCache); 364 } 365 else 366 { 367 formats.add(new BitstreamFormat(context, row)); 368 } 369 } 370 tri.close(); 372 373 BitstreamFormat[] formatArray = new BitstreamFormat[formats.size()]; 375 formatArray = (BitstreamFormat[]) formats.toArray(formatArray); 376 377 return formatArray; 378 } 379 380 389 public static BitstreamFormat create(Context context) throws SQLException , 390 AuthorizeException 391 { 392 if (!AuthorizeManager.isAdmin(context)) 394 { 395 throw new AuthorizeException( 396 "Only administrators can create bitstream formats"); 397 } 398 399 TableRow row = DatabaseManager.create(context, 401 "bitstreamformatregistry"); 402 403 log.info(LogManager.getHeader(context, "create_bitstream_format", 404 "bitstream_format_id=" 405 + row.getIntColumn("bitstream_format_id"))); 406 407 return new BitstreamFormat(context, row); 408 } 409 410 415 public int getID() 416 { 417 return bfRow.getIntColumn("bitstream_format_id"); 418 } 419 420 425 public String getShortDescription() 426 { 427 return bfRow.getStringColumn("short_description"); 428 } 429 430 436 public void setShortDescription(String s) 437 throws SQLException 438 { 439 BitstreamFormat unknown = null;; 441 try { 442 unknown = findUnknown(bfContext); 443 } catch (IllegalStateException e) { 444 } 449 450 if (unknown == null || unknown.getID() != getID()) { 454 bfRow.setColumn("short_description", s); 455 } 456 } 457 458 464 public String getDescription() 465 { 466 return bfRow.getStringColumn("description"); 467 } 468 469 475 public void setDescription(String s) 476 { 477 bfRow.setColumn("description", s); 478 } 479 480 486 public String getMIMEType() 487 { 488 return bfRow.getStringColumn("mimetype"); 489 } 490 491 497 public void setMIMEType(String s) 498 { 499 bfRow.setColumn("mimetype", s); 500 } 501 502 508 public int getSupportLevel() 509 { 510 return bfRow.getIntColumn("support_level"); 511 } 512 513 520 public void setSupportLevel(int sl) 521 { 522 if ((sl < 0) || (sl > 2)) 524 { 525 throw new IllegalArgumentException ("Invalid support level"); 526 } 527 528 bfRow.setColumn("support_level", sl); 529 } 530 531 538 public boolean isInternal() 539 { 540 return bfRow.getBooleanColumn("internal"); 541 } 542 543 550 public void setInternal(boolean b) 551 { 552 bfRow.setColumn("internal", b); 553 } 554 555 561 public void update() throws SQLException , AuthorizeException 562 { 563 if (!AuthorizeManager.isAdmin(bfContext)) 565 { 566 throw new AuthorizeException( 567 "Only administrators can modify bitstream formats"); 568 } 569 570 log.info(LogManager.getHeader(bfContext, "update_bitstream_format", 571 "bitstream_format_id=" + getID())); 572 573 DatabaseManager.updateQuery(bfContext, 575 "DELETE FROM fileextension WHERE bitstream_format_id= ? ", 576 getID()); 577 578 for (int i = 0; i < extensions.size(); i++) 580 { 581 String s = (String ) extensions.get(i); 582 TableRow r = DatabaseManager.create(bfContext, "fileextension"); 583 r.setColumn("bitstream_format_id", getID()); 584 r.setColumn("extension", s); 585 DatabaseManager.update(bfContext, r); 586 } 587 588 DatabaseManager.update(bfContext, bfRow); 589 } 590 591 598 public void delete() throws SQLException , AuthorizeException 599 { 600 if (!AuthorizeManager.isAdmin(bfContext)) 602 { 603 throw new AuthorizeException( 604 "Only administrators can delete bitstream formats"); 605 } 606 607 BitstreamFormat unknown = findUnknown(bfContext); 609 610 if (unknown.getID() == getID()) 611 throw new IllegalArgumentException ("The Unknown bitstream format may not be deleted."); 612 613 bfContext.removeCached(this, getID()); 615 616 int numberChanged = DatabaseManager.updateQuery(bfContext, 618 "UPDATE bitstream SET bitstream_format_id= ? " + 619 " WHERE bitstream_format_id= ? ", 620 unknown.getID(),getID()); 621 622 DatabaseManager.updateQuery(bfContext, 624 "DELETE FROM fileextension WHERE bitstream_format_id= ? ", 625 getID()); 626 627 DatabaseManager.delete(bfContext, bfRow); 629 630 log.info(LogManager.getHeader(bfContext, "delete_bitstream_format", 631 "bitstream_format_id=" + getID() + ",bitstreams_changed=" 632 + numberChanged)); 633 } 634 635 640 public String [] getExtensions() 641 { 642 String [] exts = new String [extensions.size()]; 643 exts = (String []) extensions.toArray(exts); 644 645 return exts; 646 } 647 648 654 public void setExtensions(String [] exts) 655 { 656 extensions = new ArrayList (); 657 658 for (int i = 0; i < exts.length; i++) 659 { 660 extensions.add(exts[i]); 661 } 662 } 663 } 664 | Popular Tags |