1 40 package org.dspace.content; 41 42 import java.io.IOException ; 43 import java.io.InputStream ; 44 import java.sql.SQLException ; 45 import java.util.ArrayList ; 46 import java.util.List ; 47 48 import org.apache.log4j.Logger; 49 import org.dspace.authorize.AuthorizeException; 50 import org.dspace.authorize.AuthorizeManager; 51 import org.dspace.core.ConfigurationManager; 52 import org.dspace.core.Constants; 53 import org.dspace.core.Context; 54 import org.dspace.core.LogManager; 55 import org.dspace.storage.bitstore.BitstreamStorageManager; 56 import org.dspace.storage.rdbms.DatabaseManager; 57 import org.dspace.storage.rdbms.TableRow; 58 import org.dspace.storage.rdbms.TableRowIterator; 59 60 70 public class Bitstream extends DSpaceObject 71 { 72 73 private static Logger log = Logger.getLogger(Bitstream.class); 74 75 76 private Context bContext; 77 78 79 private TableRow bRow; 80 81 82 private BitstreamFormat bitstreamFormat; 83 84 94 Bitstream(Context context, TableRow row) throws SQLException 95 { 96 bContext = context; 97 bRow = row; 98 99 bitstreamFormat = BitstreamFormat.find(context, row 101 .getIntColumn("bitstream_format_id")); 102 103 if (bitstreamFormat == null) 104 { 105 bitstreamFormat = BitstreamFormat.findUnknown(context); 107 108 if (bitstreamFormat == null) 110 { 111 throw new IllegalStateException ("No Unknown bitsream format"); 112 } 113 } 114 115 context.cache(this, row.getIntColumn("bitstream_id")); 117 } 118 119 131 public static Bitstream find(Context context, int id) throws SQLException 132 { 133 Bitstream fromCache = (Bitstream) context 135 .fromCache(Bitstream.class, id); 136 137 if (fromCache != null) 138 { 139 return fromCache; 140 } 141 142 TableRow row = DatabaseManager.find(context, "bitstream", id); 143 144 if (row == null) 145 { 146 if (log.isDebugEnabled()) 147 { 148 log.debug(LogManager.getHeader(context, "find_bitstream", 149 "not_found,bitstream_id=" + id)); 150 } 151 152 return null; 153 } 154 155 if (log.isDebugEnabled()) 157 { 158 log.debug(LogManager.getHeader(context, "find_bitstream", 159 "bitstream_id=" + id)); 160 } 161 162 return new Bitstream(context, row); 163 } 164 165 180 static Bitstream create(Context context, InputStream is) 181 throws IOException , SQLException 182 { 183 int bitstreamID = BitstreamStorageManager.store(context, is); 185 186 log.info(LogManager.getHeader(context, "create_bitstream", 187 "bitstream_id=" + bitstreamID)); 188 189 Bitstream bitstream = find(context, bitstreamID); 191 bitstream.setFormat(null); 192 193 return bitstream; 194 } 195 196 210 static Bitstream register(Context context, 211 int assetstore, String bitstreamPath) 212 throws IOException , SQLException 213 { 214 int bitstreamID = BitstreamStorageManager.register( 216 context, assetstore, bitstreamPath); 217 218 log.info(LogManager.getHeader(context, 219 "create_bitstream", 220 "bitstream_id=" + bitstreamID)); 221 222 Bitstream bitstream = find(context, bitstreamID); 224 bitstream.setFormat(null); 225 226 return bitstream; 227 } 228 229 234 public int getID() 235 { 236 return bRow.getIntColumn("bitstream_id"); 237 } 238 239 public String getHandle() 240 { 241 return null; 243 } 244 245 250 public int getSequenceID() 251 { 252 return bRow.getIntColumn("sequence_id"); 253 } 254 255 261 public void setSequenceID(int sid) 262 { 263 bRow.setColumn("sequence_id", sid); 264 } 265 266 272 public String getName() 273 { 274 return bRow.getStringColumn("name"); 275 } 276 277 283 public void setName(String n) 284 { 285 bRow.setColumn("name", n); 286 } 287 288 295 public String getSource() 296 { 297 return bRow.getStringColumn("source"); 298 } 299 300 306 public void setSource(String n) 307 { 308 bRow.setColumn("source", n); 309 } 310 311 317 public String getDescription() 318 { 319 return bRow.getStringColumn("description"); 320 } 321 322 328 public void setDescription(String n) 329 { 330 bRow.setColumn("description", n); 331 } 332 333 338 public String getChecksum() 339 { 340 return bRow.getStringColumn("checksum"); 341 } 342 343 348 public String getChecksumAlgorithm() 349 { 350 return bRow.getStringColumn("checksum_algorithm"); 351 } 352 353 358 public long getSize() 359 { 360 return bRow.getLongColumn("size_bytes"); 361 } 362 363 371 public void setUserFormatDescription(String desc) throws SQLException 372 { 373 setFormat(null); 376 bRow.setColumn("user_format_description", desc); 377 } 378 379 385 public String getUserFormatDescription() 386 { 387 return bRow.getStringColumn("user_format_description"); 388 } 389 390 396 public String getFormatDescription() 397 { 398 if (bitstreamFormat.getShortDescription().equals("Unknown")) 399 { 400 String desc = bRow.getStringColumn("user_format_description"); 402 403 if (desc == null) 404 { 405 return "Unknown"; 406 } 407 408 return desc; 409 } 410 411 return bitstreamFormat.getShortDescription(); 413 } 414 415 420 public BitstreamFormat getFormat() 421 { 422 return bitstreamFormat; 423 } 424 425 435 public void setFormat(BitstreamFormat f) throws SQLException 436 { 437 if (f == null) 440 { 441 bitstreamFormat = BitstreamFormat.findUnknown(bContext); 443 } 444 else 445 { 446 bitstreamFormat = f; 447 } 448 449 bRow.setColumnNull("user_format_description"); 451 452 bRow.setColumn("bitstream_format_id", bitstreamFormat.getID()); 454 } 455 456 463 public void update() throws SQLException , AuthorizeException 464 { 465 AuthorizeManager.authorizeAction(bContext, this, Constants.WRITE); 467 468 log.info(LogManager.getHeader(bContext, "update_bitstream", 469 "bitstream_id=" + getID())); 470 471 DatabaseManager.update(bContext, bRow); 472 } 473 474 479 void delete() throws SQLException 480 { 481 log.info(LogManager.getHeader(bContext, "delete_bitstream", 485 "bitstream_id=" + getID())); 486 487 bContext.removeCached(this, getID()); 489 490 AuthorizeManager.removeAllPolicies(bContext, this); 492 493 BitstreamStorageManager.delete(bContext, bRow 495 .getIntColumn("bitstream_id")); 496 } 497 498 506 public InputStream retrieve() throws IOException , SQLException , 507 AuthorizeException 508 { 509 AuthorizeManager.authorizeAction(bContext, this, Constants.READ); 511 512 return BitstreamStorageManager.retrieve(bContext, bRow 513 .getIntColumn("bitstream_id")); 514 } 515 516 522 public Bundle[] getBundles() throws SQLException 523 { 524 TableRowIterator tri = DatabaseManager.queryTable(bContext, "bundle", 526 "SELECT bundle.* FROM bundle, bundle2bitstream WHERE " + 527 "bundle.bundle_id=bundle2bitstream.bundle_id AND " + 528 "bundle2bitstream.bitstream_id= ? ", 529 bRow.getIntColumn("bitstream_id")); 530 531 List bundles = new ArrayList (); 533 534 while (tri.hasNext()) 535 { 536 TableRow r = tri.next(); 537 538 Bundle fromCache = (Bundle) bContext.fromCache(Bundle.class, r 540 .getIntColumn("bundle_id")); 541 542 if (fromCache != null) 543 { 544 bundles.add(fromCache); 545 } 546 else 547 { 548 bundles.add(new Bundle(bContext, r)); 549 } 550 } 551 552 tri.close(); 554 555 Bundle[] bundleArray = new Bundle[bundles.size()]; 556 bundleArray = (Bundle[]) bundles.toArray(bundleArray); 557 558 return bundleArray; 559 } 560 561 566 public int getType() 567 { 568 return Constants.BITSTREAM; 569 } 570 571 576 public boolean isRegisteredBitstream() { 577 return BitstreamStorageManager 578 .isRegisteredBitstream(bRow.getStringColumn("internal_id")); 579 } 580 581 586 public int getStoreNumber() { 587 return bRow.getIntColumn("store_number"); 588 } 589 } 590 | Popular Tags |