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.Iterator ; 47 import java.util.List ; 48 import java.util.ListIterator ; 49 50 import org.apache.log4j.Logger; 51 import org.dspace.authorize.AuthorizeException; 52 import org.dspace.authorize.AuthorizeManager; 53 import org.dspace.core.Constants; 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 71 public class Bundle extends DSpaceObject 72 { 73 74 private static Logger log = Logger.getLogger(Bundle.class); 75 76 77 private Context ourContext; 78 79 80 private TableRow bundleRow; 81 82 83 private List bitstreams; 84 85 93 Bundle(Context context, TableRow row) throws SQLException 94 { 95 ourContext = context; 96 bundleRow = row; 97 bitstreams = new ArrayList (); 98 99 TableRowIterator tri = DatabaseManager.queryTable( 101 ourContext, "bitstream", 102 "SELECT bitstream.* FROM bitstream, bundle2bitstream WHERE " 103 + "bundle2bitstream.bitstream_id=bitstream.bitstream_id AND " 104 + "bundle2bitstream.bundle_id= ? ", 105 bundleRow.getIntColumn("bundle_id")); 106 107 while (tri.hasNext()) 108 { 109 TableRow r = (TableRow) tri.next(); 110 111 Bitstream fromCache = (Bitstream) context.fromCache( 113 Bitstream.class, r.getIntColumn("bitstream_id")); 114 115 if (fromCache != null) 116 { 117 bitstreams.add(fromCache); 118 } 119 else 120 { 121 bitstreams.add(new Bitstream(ourContext, r)); 122 } 123 } 124 tri.close(); 126 127 context.cache(this, row.getIntColumn("bundle_id")); 129 } 130 131 142 public static Bundle find(Context context, int id) throws SQLException 143 { 144 Bundle fromCache = (Bundle) context.fromCache(Bundle.class, id); 146 147 if (fromCache != null) 148 { 149 return fromCache; 150 } 151 152 TableRow row = DatabaseManager.find(context, "bundle", id); 153 154 if (row == null) 155 { 156 if (log.isDebugEnabled()) 157 { 158 log.debug(LogManager.getHeader(context, "find_bundle", 159 "not_found,bundle_id=" + id)); 160 } 161 162 return null; 163 } 164 else 165 { 166 if (log.isDebugEnabled()) 167 { 168 log.debug(LogManager.getHeader(context, "find_bundle", 169 "bundle_id=" + id)); 170 } 171 172 return new Bundle(context, row); 173 } 174 } 175 176 187 static Bundle create(Context context) throws SQLException 188 { 189 TableRow row = DatabaseManager.create(context, "bundle"); 191 192 log.info(LogManager.getHeader(context, "create_bundle", "bundle_id=" 193 + row.getIntColumn("bundle_id"))); 194 195 return new Bundle(context, row); 196 } 197 198 203 public int getID() 204 { 205 return bundleRow.getIntColumn("bundle_id"); 206 } 207 208 213 public String getName() 214 { 215 return bundleRow.getStringColumn("name"); 216 } 217 218 225 public void setName(String name) 226 { 227 bundleRow.setColumn("name", name); 228 } 229 230 235 public int getPrimaryBitstreamID() 236 { 237 return bundleRow.getIntColumn("primary_bitstream_id"); 238 } 239 240 246 public void setPrimaryBitstreamID(int bitstreamID) 247 { 248 bundleRow.setColumn("primary_bitstream_id", bitstreamID); 249 } 250 251 254 public void unsetPrimaryBitstreamID() 255 { 256 bundleRow.setColumnNull("primary_bitstream_id"); 257 } 258 259 public String getHandle() 260 { 261 return null; 263 } 264 265 271 public Bitstream getBitstreamByName(String name) 272 { 273 Bitstream target = null; 274 275 Iterator i = bitstreams.iterator(); 276 277 while (i.hasNext()) 278 { 279 Bitstream b = (Bitstream) i.next(); 280 281 if (name.equals(b.getName())) 282 { 283 target = b; 284 285 break; 286 } 287 } 288 289 return target; 290 } 291 292 297 public Bitstream[] getBitstreams() 298 { 299 Bitstream[] bitstreamArray = new Bitstream[bitstreams.size()]; 300 bitstreamArray = (Bitstream[]) bitstreams.toArray(bitstreamArray); 301 302 return bitstreamArray; 303 } 304 305 310 public Item[] getItems() throws SQLException 311 { 312 List items = new ArrayList (); 313 314 TableRowIterator tri = DatabaseManager.queryTable( 316 ourContext, "item", 317 "SELECT item.* FROM item, item2bundle WHERE " + 318 "item2bundle.item_id=item.item_id AND " + 319 "item2bundle.bundle_id= ? ", 320 bundleRow.getIntColumn("bundle_id")); 321 322 while (tri.hasNext()) 323 { 324 TableRow r = (TableRow) tri.next(); 325 326 Item fromCache = (Item) ourContext.fromCache(Item.class, r 328 .getIntColumn("item_id")); 329 330 if (fromCache != null) 331 { 332 items.add(fromCache); 333 } 334 else 335 { 336 items.add(new Item(ourContext, r)); 337 } 338 } 339 tri.close(); 341 342 Item[] itemArray = new Item[items.size()]; 343 itemArray = (Item[]) items.toArray(itemArray); 344 345 return itemArray; 346 } 347 348 356 public Bitstream createBitstream(InputStream is) throws AuthorizeException, 357 IOException , SQLException 358 { 359 AuthorizeManager.authorizeAction(ourContext, this, Constants.ADD); 361 362 Bitstream b = Bitstream.create(ourContext, is); 363 364 addBitstream(b); 366 367 return b; 368 } 369 370 380 public Bitstream registerBitstream(int assetstore, String bitstreamPath) 381 throws AuthorizeException, IOException , SQLException 382 { 383 AuthorizeManager.authorizeAction(ourContext, this, Constants.ADD); 385 386 Bitstream b = Bitstream.register(ourContext, assetstore, bitstreamPath); 387 388 390 addBitstream(b); 391 return b; 392 } 393 394 400 public void addBitstream(Bitstream b) throws SQLException , 401 AuthorizeException 402 { 403 AuthorizeManager.authorizeAction(ourContext, this, Constants.ADD); 405 406 log.info(LogManager.getHeader(ourContext, "add_bitstream", "bundle_id=" 407 + getID() + ",bitstream_id=" + b.getID())); 408 409 for (int i = 0; i < bitstreams.size(); i++) 411 { 412 Bitstream existing = (Bitstream) bitstreams.get(i); 413 414 if (b.getID() == existing.getID()) 415 { 416 return; 418 } 419 } 420 421 bitstreams.add(b); 423 424 AuthorizeManager.inheritPolicies(ourContext, this, b); 427 428 TableRow mappingRow = DatabaseManager.create(ourContext, 430 "bundle2bitstream"); 431 mappingRow.setColumn("bundle_id", getID()); 432 mappingRow.setColumn("bitstream_id", b.getID()); 433 DatabaseManager.update(ourContext, mappingRow); 434 } 435 436 448 public void removeBitstream(Bitstream b) throws AuthorizeException, 449 SQLException , IOException 450 { 451 AuthorizeManager.authorizeAction(ourContext, this, Constants.REMOVE); 453 454 log.info(LogManager.getHeader(ourContext, "remove_bitstream", 455 "bundle_id=" + getID() + ",bitstream_id=" + b.getID())); 456 457 ListIterator li = bitstreams.listIterator(); 459 460 while (li.hasNext()) 461 { 462 Bitstream existing = (Bitstream) li.next(); 463 464 if (b.getID() == existing.getID()) 465 { 466 li.remove(); 468 469 if (b.getID() == getPrimaryBitstreamID()) { 473 unsetPrimaryBitstreamID(); 474 } 475 } 476 } 477 478 DatabaseManager.updateQuery(ourContext, 480 "DELETE FROM bundle2bitstream WHERE bundle_id= ? "+ 481 "AND bitstream_id= ? ", 482 getID(), b.getID()); 483 484 TableRowIterator tri = DatabaseManager.query(ourContext, 486 "SELECT * FROM bundle2bitstream WHERE bitstream_id= ? ", 487 b.getID()); 488 489 if (!tri.hasNext()) 490 { 491 b.delete(); 493 } 494 tri.close(); 496 } 497 498 501 public void update() throws SQLException , AuthorizeException 502 { 503 log.info(LogManager.getHeader(ourContext, "update_bundle", "bundle_id=" 506 + getID())); 507 508 DatabaseManager.update(ourContext, bundleRow); 509 } 510 511 516 void delete() throws SQLException , AuthorizeException, IOException 517 { 518 log.info(LogManager.getHeader(ourContext, "delete_bundle", "bundle_id=" 519 + getID())); 520 521 ourContext.removeCached(this, getID()); 523 524 Bitstream[] bs = getBitstreams(); 526 527 for (int i = 0; i < bs.length; i++) 528 { 529 removeBitstream(bs[i]); 530 } 531 532 AuthorizeManager.removeAllPolicies(ourContext, this); 534 535 DatabaseManager.delete(ourContext, bundleRow); 537 } 538 539 542 public int getType() 543 { 544 return Constants.BUNDLE; 545 } 546 } 547 | Popular Tags |