-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Bitstream.java
756 lines (667 loc) · 21.8 KB
/
Bitstream.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.content;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.dspace.authorize.AuthorizeException;
import org.dspace.authorize.AuthorizeManager;
import org.dspace.core.ConfigurationManager;
import org.dspace.core.Constants;
import org.dspace.core.Context;
import org.dspace.core.LogManager;
import org.dspace.event.Event;
import org.dspace.storage.bitstore.BitstreamStorageManager;
import org.dspace.storage.rdbms.DatabaseManager;
import org.dspace.storage.rdbms.TableRow;
import org.dspace.storage.rdbms.TableRowIterator;
/**
* Class representing bitstreams stored in the DSpace system.
* <P>
* When modifying the bitstream metadata, changes are not reflected in the
* database until <code>update</code> is called. Note that you cannot alter
* the contents of a bitstream; you need to create a new bitstream.
*
* @author Robert Tansley
* @version $Revision$
*/
public class Bitstream extends DSpaceObject
{
/** log4j logger */
private static Logger log = Logger.getLogger(Bitstream.class);
/** Our context */
private Context bContext;
/** The row in the table representing this bitstream */
private TableRow bRow;
/** The bitstream format corresponding to this bitstream */
private BitstreamFormat bitstreamFormat;
/** Flag set when data is modified, for events */
private boolean modified;
/** Flag set when metadata is modified, for events */
private boolean modifiedMetadata;
/**
* Private constructor for creating a Bitstream object based on the contents
* of a DB table row.
*
* @param context
* the context this object exists in
* @param row
* the corresponding row in the table
* @throws SQLException
*/
Bitstream(Context context, TableRow row) throws SQLException
{
bContext = context;
bRow = row;
// Get the bitstream format
bitstreamFormat = BitstreamFormat.find(context, row
.getIntColumn("bitstream_format_id"));
if (bitstreamFormat == null)
{
// No format: use "Unknown"
bitstreamFormat = BitstreamFormat.findUnknown(context);
// Panic if we can't find it
if (bitstreamFormat == null)
{
throw new IllegalStateException("No Unknown bitstream format");
}
}
// Cache ourselves
context.cache(this, row.getIntColumn("bitstream_id"));
modified = false;
modifiedMetadata = false;
clearDetails();
}
/**
* Get a bitstream from the database. The bitstream metadata is loaded into
* memory.
*
* @param context
* DSpace context object
* @param id
* ID of the bitstream
*
* @return the bitstream, or null if the ID is invalid.
* @throws SQLException
*/
public static Bitstream find(Context context, int id) throws SQLException
{
// First check the cache
Bitstream fromCache = (Bitstream) context
.fromCache(Bitstream.class, id);
if (fromCache != null)
{
return fromCache;
}
TableRow row = DatabaseManager.find(context, "bitstream", id);
if (row == null)
{
if (log.isDebugEnabled())
{
log.debug(LogManager.getHeader(context, "find_bitstream",
"not_found,bitstream_id=" + id));
}
return null;
}
// not null, return Bitstream
if (log.isDebugEnabled())
{
log.debug(LogManager.getHeader(context, "find_bitstream",
"bitstream_id=" + id));
}
return new Bitstream(context, row);
}
public static Bitstream[] findAll(Context context) throws SQLException
{
TableRowIterator tri = DatabaseManager.queryTable(context, "bitstream",
"SELECT * FROM bitstream");
List<Bitstream> bitstreams = new ArrayList<Bitstream>();
try
{
while (tri.hasNext())
{
TableRow row = tri.next();
// First check the cache
Bitstream fromCache = (Bitstream) context.fromCache(
Bitstream.class, row.getIntColumn("bitstream_id"));
if (fromCache != null)
{
bitstreams.add(fromCache);
}
else
{
bitstreams.add(new Bitstream(context, row));
}
}
}
finally
{
// close the TableRowIterator to free up resources
if (tri != null)
{
tri.close();
}
}
Bitstream[] bitstreamArray = new Bitstream[bitstreams.size()];
bitstreamArray = bitstreams.toArray(bitstreamArray);
return bitstreamArray;
}
/**
* Create a new bitstream, with a new ID. The checksum and file size are
* calculated. This method is not public, and does not check authorisation;
* other methods such as Bundle.createBitstream() will check authorisation.
* The newly created bitstream has the "unknown" format.
*
* @param context
* DSpace context object
* @param is
* the bits to put in the bitstream
*
* @return the newly created bitstream
* @throws IOException
* @throws SQLException
*/
static Bitstream create(Context context, InputStream is)
throws IOException, SQLException
{
// Store the bits
int bitstreamID = BitstreamStorageManager.store(context, is);
log.info(LogManager.getHeader(context, "create_bitstream",
"bitstream_id=" + bitstreamID));
// Set the format to "unknown"
Bitstream bitstream = find(context, bitstreamID);
bitstream.setFormat(null);
context.addEvent(new Event(Event.CREATE, Constants.BITSTREAM,
bitstreamID, null, bitstream.getIdentifiers(context)));
return bitstream;
}
/**
* Register a new bitstream, with a new ID. The checksum and file size
* are calculated. This method is not public, and does not check
* authorisation; other methods such as Bundle.createBitstream() will
* check authorisation. The newly created bitstream has the "unknown"
* format.
*
* @param context DSpace context object
* @param assetstore corresponds to an assetstore in dspace.cfg
* @param bitstreamPath the path and filename relative to the assetstore
* @return the newly registered bitstream
* @throws IOException
* @throws SQLException
*/
static Bitstream register(Context context,
int assetstore, String bitstreamPath)
throws IOException, SQLException
{
// Store the bits
int bitstreamID = BitstreamStorageManager.register(
context, assetstore, bitstreamPath);
log.info(LogManager.getHeader(context,
"create_bitstream",
"bitstream_id=" + bitstreamID));
// Set the format to "unknown"
Bitstream bitstream = find(context, bitstreamID);
bitstream.setFormat(null);
context.addEvent(new Event(Event.CREATE, Constants.BITSTREAM,
bitstreamID, "REGISTER", bitstream.getIdentifiers(context)));
return bitstream;
}
/**
* Get the internal identifier of this bitstream
*
* @return the internal identifier
*/
public int getID()
{
return bRow.getIntColumn("bitstream_id");
}
public String getHandle()
{
// No Handles for bitstreams
return null;
}
/**
* Get the sequence ID of this bitstream
*
* @return the sequence ID
*/
public int getSequenceID()
{
return bRow.getIntColumn("sequence_id");
}
/**
* Set the sequence ID of this bitstream
*
* @param sid
* the ID
*/
public void setSequenceID(int sid)
{
bRow.setColumn("sequence_id", sid);
modifiedMetadata = true;
addDetails("SequenceID");
}
/**
* Get the name of this bitstream - typically the filename, without any path
* information
*
* @return the name of the bitstream
*/
public String getName()
{
return bRow.getStringColumn("name");
}
/**
* Set the name of the bitstream
*
* @param n
* the new name of the bitstream
*/
public void setName(String n)
{
bRow.setColumn("name", n);
modifiedMetadata = true;
addDetails("Name");
}
/**
* Get the source of this bitstream - typically the filename with path
* information (if originally provided) or the name of the tool that
* generated this bitstream
*
* @return the source of the bitstream
*/
public String getSource()
{
return bRow.getStringColumn("source");
}
/**
* Set the source of the bitstream
*
* @param n
* the new source of the bitstream
*/
public void setSource(String n)
{
bRow.setColumn("source", n);
modifiedMetadata = true;
addDetails("Source");
}
/**
* Get the description of this bitstream - optional free text, typically
* provided by a user at submission time
*
* @return the description of the bitstream
*/
public String getDescription()
{
return bRow.getStringColumn("description");
}
/**
* Set the description of the bitstream
*
* @param n
* the new description of the bitstream
*/
public void setDescription(String n)
{
bRow.setColumn("description", n);
modifiedMetadata = true;
addDetails("Description");
}
/**
* Get the checksum of the content of the bitstream, for integrity checking
*
* @return the checksum
*/
public String getChecksum()
{
return bRow.getStringColumn("checksum");
}
/**
* Get the algorithm used to calculate the checksum
*
* @return the algorithm, e.g. "MD5"
*/
public String getChecksumAlgorithm()
{
return bRow.getStringColumn("checksum_algorithm");
}
/**
* Get the size of the bitstream
*
* @return the size in bytes
*/
public long getSize()
{
return bRow.getLongColumn("size_bytes");
}
/**
* Set the user's format description. This implies that the format of the
* bitstream is uncertain, and the format is set to "unknown."
*
* @param desc
* the user's description of the format
* @throws SQLException
*/
public void setUserFormatDescription(String desc) throws SQLException
{
// FIXME: Would be better if this didn't throw an SQLException,
// but we need to find the unknown format!
setFormat(null);
bRow.setColumn("user_format_description", desc);
modifiedMetadata = true;
addDetails("UserFormatDescription");
}
/**
* Get the user's format description. Returns null if the format is known by
* the system.
*
* @return the user's format description.
*/
public String getUserFormatDescription()
{
return bRow.getStringColumn("user_format_description");
}
/**
* Get the description of the format - either the user's or the description
* of the format defined by the system.
*
* @return a description of the format.
*/
public String getFormatDescription()
{
if (bitstreamFormat.getShortDescription().equals("Unknown"))
{
// Get user description if there is one
String desc = bRow.getStringColumn("user_format_description");
if (desc == null)
{
return "Unknown";
}
return desc;
}
// not null or Unknown
return bitstreamFormat.getShortDescription();
}
/**
* Get the format of the bitstream
*
* @return the format of this bitstream
*/
public BitstreamFormat getFormat()
{
return bitstreamFormat;
}
/**
* Set the format of the bitstream. If the user has supplied a type
* description, it is cleared. Passing in <code>null</code> sets the type
* of this bitstream to "unknown".
*
* @param f
* the format of this bitstream, or <code>null</code> for
* unknown
* @throws SQLException
*/
public void setFormat(BitstreamFormat f) throws SQLException
{
// FIXME: Would be better if this didn't throw an SQLException,
// but we need to find the unknown format!
if (f == null)
{
// Use "Unknown" format
bitstreamFormat = BitstreamFormat.findUnknown(bContext);
}
else
{
bitstreamFormat = f;
}
// Remove user type description
bRow.setColumnNull("user_format_description");
// Update the ID in the table row
bRow.setColumn("bitstream_format_id", bitstreamFormat.getID());
modified = true;
}
/**
* Update the bitstream metadata. Note that the content of the bitstream
* cannot be changed - for that you need to create a new bitstream.
*
* @throws SQLException
* @throws AuthorizeException
*/
public void update() throws SQLException, AuthorizeException
{
// Check authorisation
AuthorizeManager.authorizeAction(bContext, this, Constants.WRITE);
log.info(LogManager.getHeader(bContext, "update_bitstream",
"bitstream_id=" + getID()));
if (modified)
{
bContext.addEvent(new Event(Event.MODIFY, Constants.BITSTREAM,
getID(), null, getIdentifiers(bContext)));
modified = false;
}
if (modifiedMetadata)
{
bContext.addEvent(new Event(Event.MODIFY_METADATA,
Constants.BITSTREAM, getID(), getDetails(),
getIdentifiers(bContext)));
modifiedMetadata = false;
clearDetails();
}
DatabaseManager.update(bContext, bRow);
}
/**
* Delete the bitstream, including any mappings to bundles
*
* @throws SQLException
*/
void delete() throws SQLException
{
boolean oracle = false;
if ("oracle".equals(ConfigurationManager.getProperty("db.name")))
{
oracle = true;
}
// changed to a check on remove
// Check authorisation
//AuthorizeManager.authorizeAction(bContext, this, Constants.DELETE);
log.info(LogManager.getHeader(bContext, "delete_bitstream",
"bitstream_id=" + getID()));
bContext.addEvent(new Event(Event.DELETE, Constants.BITSTREAM, getID(),
String.valueOf(getSequenceID()), getIdentifiers(bContext)));
// Remove from cache
bContext.removeCached(this, getID());
// Remove policies
AuthorizeManager.removeAllPolicies(bContext, this);
// Remove references to primary bitstreams in bundle
String query = "update bundle set primary_bitstream_id = ";
query += (oracle ? "''" : "Null") + " where primary_bitstream_id = ? ";
DatabaseManager.updateQuery(bContext,
query, bRow.getIntColumn("bitstream_id"));
// Remove bitstream itself
BitstreamStorageManager.delete(bContext, bRow
.getIntColumn("bitstream_id"));
}
/**
* Bitstreams are only logically deleted (via a flag in the database).
* This method allows us to verify is the bitstream is still valid
*
* @return true if the bitstream has been deleted
*/
boolean isDeleted() throws SQLException
{
String query = "select count(*) as mycount from Bitstream where deleted = '1' and bitstream_id = ? ";
TableRowIterator tri = DatabaseManager.query(bContext, query, bRow.getIntColumn("bitstream_id"));
long count = 0;
try
{
TableRow r = tri.next();
count = r.getLongColumn("mycount");
}
finally
{
// close the TableRowIterator to free up resources
if (tri != null)
{
tri.close();
}
}
return count == 1;
}
/**
* Retrieve the contents of the bitstream
*
* @return a stream from which the bitstream can be read.
* @throws IOException
* @throws SQLException
* @throws AuthorizeException
*/
public InputStream retrieve() throws IOException, SQLException,
AuthorizeException
{
// Maybe should return AuthorizeException??
AuthorizeManager.authorizeAction(bContext, this, Constants.READ);
return BitstreamStorageManager.retrieve(bContext, bRow
.getIntColumn("bitstream_id"));
}
/**
* Get the bundles this bitstream appears in
*
* @return array of <code>Bundle</code> s this bitstream appears in
* @throws SQLException
*/
public Bundle[] getBundles() throws SQLException
{
// Get the bundle table rows
TableRowIterator tri = DatabaseManager.queryTable(bContext, "bundle",
"SELECT bundle.* FROM bundle, bundle2bitstream WHERE " +
"bundle.bundle_id=bundle2bitstream.bundle_id AND " +
"bundle2bitstream.bitstream_id= ? ",
bRow.getIntColumn("bitstream_id"));
// Build a list of Bundle objects
List<Bundle> bundles = new ArrayList<Bundle>();
try
{
while (tri.hasNext())
{
TableRow r = tri.next();
// First check the cache
Bundle fromCache = (Bundle) bContext.fromCache(Bundle.class, r
.getIntColumn("bundle_id"));
if (fromCache != null)
{
bundles.add(fromCache);
}
else
{
bundles.add(new Bundle(bContext, r));
}
}
}
finally
{
// close the TableRowIterator to free up resources
if (tri != null)
{
tri.close();
}
}
Bundle[] bundleArray = new Bundle[bundles.size()];
bundleArray = (Bundle[]) bundles.toArray(bundleArray);
return bundleArray;
}
/**
* return type found in Constants
*
* @return int Constants.BITSTREAM
*/
public int getType()
{
return Constants.BITSTREAM;
}
/**
* Determine if this bitstream is registered (available elsewhere on
* filesystem than in assetstore). More about registered items:
* https://wiki.duraspace.org/display/DSDOC3x/Registering+(not+Importing)+Bitstreams+via+Simple+Archive+Format
*
* @return true if the bitstream is registered, false otherwise
*/
public boolean isRegisteredBitstream() {
return BitstreamStorageManager
.isRegisteredBitstream(bRow.getStringColumn("internal_id"));
}
/**
* Get the asset store number where this bitstream is stored
*
* @return the asset store number of the bitstream
*/
public int getStoreNumber() {
return bRow.getIntColumn("store_number");
}
/**
* Get the parent object of a bitstream. The parent can be an item if this
* is a normal bitstream, or it could be a collection or a community if the
* bitstream is a logo.
*
* @return this bitstream's parent.
* @throws SQLException
*/
public DSpaceObject getParentObject() throws SQLException
{
Bundle[] bundles = getBundles();
if (bundles != null && (bundles.length > 0 && bundles[0] != null))
{
// the ADMIN action is not allowed on Bundle object so skip to the item
Item[] items = bundles[0].getItems();
if (items != null && items.length > 0)
{
return items[0];
}
else
{
return null;
}
}
else
{
// is the bitstream a logo for a community or a collection?
TableRow qResult = DatabaseManager.querySingle(bContext,
"SELECT collection_id FROM collection " +
"WHERE logo_bitstream_id = ?",getID());
if (qResult != null)
{
return Collection.find(bContext,qResult.getIntColumn("collection_id"));
}
else
{
// is the bitstream related to a community?
qResult = DatabaseManager.querySingle(bContext,
"SELECT community_id FROM community " +
"WHERE logo_bitstream_id = ?",getID());
if (qResult != null)
{
return Community.find(bContext,qResult.getIntColumn("community_id"));
}
else
{
return null;
}
}
}
}
@Override
public void updateLastModified()
{
//Also fire a modified event since the bitstream HAS been modified
bContext.addEvent(new Event(Event.MODIFY, Constants.BITSTREAM, getID(),
null, getIdentifiers(bContext)));
}
}