/
BrowseItem.java
412 lines (367 loc) · 11.3 KB
/
BrowseItem.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
/**
* 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.browse;
import org.apache.log4j.Logger;
import org.dspace.authorize.AuthorizeException;
import org.dspace.authorize.AuthorizeManager;
import org.dspace.content.*;
import org.dspace.core.Constants;
import org.dspace.core.Context;
import org.dspace.handle.HandleManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
/**
* Entity class to represent an item that is being used to generate Browse
* results. This behaves in many was similar to the Item object, but its
* metadata handling has been further optimised for performance in both
* reading and writing, and it does not deal with other objects related to
* items
*
* FIXME: this class violates some of the encapsulation of the Item, but there is
* unfortunately no way around this until DAOs and an interface are provided
* for the Item class.
*
* @author Richard Jones
*
*/
public class BrowseItem extends DSpaceObject
{
/** Logger */
private static Logger log = Logger.getLogger(BrowseItem.class);
/** a List of all the metadata */
private List<DCValue> metadata = new ArrayList<DCValue>();
/** database id of the item */
private int id = -1;
/** is the item in the archive */
private boolean in_archive = true;
/** is the item withdrawn */
private boolean withdrawn = false;
/** is the item discoverable */
private boolean discoverable = true;
/** item handle */
private String handle = null;
/**
* Construct a new browse item with the given ourContext and the database id
*
* @param context the DSpace ourContext
* @param id the database id of the item
* @param in_archive
* @param withdrawn
*/
public BrowseItem(Context context, int id, boolean in_archive, boolean withdrawn, boolean discoverable) {
super(context);
this.id = id;
this.in_archive = in_archive;
this.withdrawn = withdrawn;
this.discoverable = discoverable;
}
/**
* Get String array of metadata values matching the given parameters
*
* @param schema metadata schema
* @param element metadata element
* @param qualifier metadata qualifier
* @param lang metadata language
* @return array of matching values
* @throws SQLException
*/
public DCValue[] getMetadata(String schema, String element, String qualifier, String lang) {
try
{
BrowseItemDAO dao = BrowseDAOFactory.getItemInstance(ourContext);
// if the qualifier is a wildcard, we have to get it out of the
// database
if (Item.ANY.equals(qualifier))
{
try {
return dao.queryMetadata(id, schema, element, qualifier, lang);
} catch (SQLException e) {
log.error("caught exception: ", e);
}
}
if (!metadata.isEmpty())
{
List<DCValue> values = new ArrayList<DCValue>();
Iterator<DCValue> i = metadata.iterator();
while (i.hasNext())
{
DCValue dcv = i.next();
if (match(schema, element, qualifier, lang, dcv))
{
values.add(dcv);
}
}
if (values.isEmpty())
{
DCValue[] dcvs = new DCValue[0];
try {
dcvs = dao.queryMetadata(id, schema, element, qualifier, lang);
} catch (SQLException e) {
log.error("caught exception: ", e);
}
if (dcvs != null)
{
Collections.addAll(metadata, dcvs);
}
return dcvs;
}
// else, Create an array of matching values
DCValue[] valueArray = new DCValue[values.size()];
valueArray = (DCValue[]) values.toArray(valueArray);
return valueArray;
}
else
{
DCValue[] dcvs = new DCValue[0];
try {
dcvs = dao.queryMetadata(id, schema, element, qualifier, lang);
} catch (SQLException e) {
log.error("caught exception: ", e);
}
if (dcvs != null)
{
Collections.addAll(metadata, dcvs);
}
return dcvs;
}
}
catch (BrowseException be)
{
log.error("caught exception: ", be);
return null;
}
}
/**
* Get the type of object. This object masquerades as an Item, so this
* returns the value of Constants.ITEM
*
*@return Constants.ITEM
*/
public int getType()
{
return Constants.ITEM;
}
/**
* @deprecated
* @param real
*/
public int getType(boolean real)
{
if (!real)
{
return Constants.ITEM;
}
else
{
return getType();
}
}
/**
* get the database id of the item
*
* @return database id of item
*/
public int getID()
{
return id;
}
/**
* Set the database id of the item
*
* @param id the database id of the item
*/
public void setID(int id)
{
this.id = id;
}
/**
* Utility method for pattern-matching metadata elements. This
* method will return <code>true</code> if the given schema,
* element, qualifier and language match the schema, element,
* qualifier and language of the <code>DCValue</code> object passed
* in. Any or all of the element, qualifier and language passed
* in can be the <code>Item.ANY</code> wildcard.
*
* @param schema
* the schema for the metadata field. <em>Must</em> match
* the <code>name</code> of an existing metadata schema.
* @param element
* the element to match, or <code>Item.ANY</code>
* @param qualifier
* the qualifier to match, or <code>Item.ANY</code>
* @param language
* the language to match, or <code>Item.ANY</code>
* @param dcv
* the Dublin Core value
* @return <code>true</code> if there is a match
*/
private boolean match(String schema, String element, String qualifier,
String language, DCValue dcv)
{
// We will attempt to disprove a match - if we can't we have a match
if (!element.equals(Item.ANY) && !element.equals(dcv.element))
{
// Elements do not match, no wildcard
return false;
}
if (qualifier == null)
{
// Value must be unqualified
if (dcv.qualifier != null)
{
// Value is qualified, so no match
return false;
}
}
else if (!qualifier.equals(Item.ANY))
{
// Not a wildcard, so qualifier must match exactly
if (!qualifier.equals(dcv.qualifier))
{
return false;
}
}
if (language == null)
{
// Value must be null language to match
if (dcv.language != null)
{
// Value is qualified, so no match
return false;
}
}
else if (!language.equals(Item.ANY))
{
// Not a wildcard, so language must match exactly
if (!language.equals(dcv.language))
{
return false;
}
}
else if (!schema.equals(Item.ANY))
{
if (dcv.schema != null && !dcv.schema.equals(schema))
{
// The namespace doesn't match
return false;
}
}
// If we get this far, we have a match
return true;
}
/* (non-Javadoc)
* @see org.dspace.content.DSpaceObject#getHandle()
*/
public String getHandle()
{
// Get our Handle if any
if (this.handle == null)
{
try
{
this.handle = HandleManager.findHandle(ourContext, this);
}
catch (SQLException e)
{
log.error("caught exception: ", e);
}
}
return this.handle;
}
/**
* Get a thumbnail object out of the item.
*
* Warning: using this method actually instantiates an Item, which has a
* corresponding performance hit on the database during browse listing
* rendering. That's your own fault for wanting to put images on your
* browse page!
*
* @throws SQLException
*/
public Thumbnail getThumbnail()
throws SQLException
{
// instantiate an item for this one. Not nice.
Item item = Item.find(ourContext, id);
if (item == null)
{
return null;
}
// now go sort out the thumbnail
// if there's no original, there is no thumbnail
Bundle[] original = item.getBundles("ORIGINAL");
if (original.length == 0)
{
return null;
}
// if multiple bitstreams, check if the primary one is HTML
boolean html = false;
if (original[0].getBitstreams().length > 1)
{
Bitstream[] bitstreams = original[0].getBitstreams();
for (int i = 0; (i < bitstreams.length) && !html; i++)
{
if (bitstreams[i].getID() == original[0].getPrimaryBitstreamID())
{
html = bitstreams[i].getFormat().getMIMEType().equals("text/html");
}
}
}
// now actually pull out the thumbnail (ouch!)
Bundle[] thumbs = item.getBundles("THUMBNAIL");
// if there are thumbs and we're not dealing with an HTML item
// then show the thumbnail
if ((thumbs.length > 0) && !html)
{
Bitstream thumbnailBitstream;
Bitstream originalBitstream;
if ((original[0].getBitstreams().length > 1) && (original[0].getPrimaryBitstreamID() > -1))
{
originalBitstream = Bitstream.find(ourContext, original[0].getPrimaryBitstreamID());
thumbnailBitstream = thumbs[0].getBitstreamByName(originalBitstream.getName() + ".jpg");
}
else
{
originalBitstream = original[0].getBitstreams()[0];
thumbnailBitstream = thumbs[0].getBitstreams()[0];
}
if ((thumbnailBitstream != null)
&& (AuthorizeManager.authorizeActionBoolean(ourContext, thumbnailBitstream, Constants.READ)))
{
return new Thumbnail(thumbnailBitstream, originalBitstream);
}
}
return null;
}
public String getName()
{
return getMetadataFirstValue(MetadataSchema.DC_SCHEMA, "title", null, Item.ANY);
}
@Override
public void update() throws SQLException, AuthorizeException
{
}
@Override
public void updateLastModified()
{
}
public boolean isArchived()
{
return in_archive;
}
public boolean isWithdrawn()
{
return withdrawn;
}
public boolean isDiscoverable() {
return discoverable;
}
}