-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
DSpaceObject.java
238 lines (214 loc) · 7.48 KB
/
DSpaceObject.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
/**
* 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.sql.SQLException;
import org.apache.log4j.Logger;
import org.dspace.authorize.AuthorizeException;
import org.dspace.core.Constants;
import org.dspace.core.Context;
import org.dspace.eperson.EPerson;
import org.dspace.eperson.Group;
import org.dspace.handle.HandleManager;
import org.dspace.identifier.IdentifierService;
import org.dspace.utils.DSpace;
/**
* Abstract base class for DSpace objects
*/
public abstract class DSpaceObject
{
private static final Logger log = Logger.getLogger(DSpaceObject.class);
// accumulate information to add to "detail" element of content Event,
// e.g. to document metadata fields touched, etc.
private StringBuffer eventDetails = null;
private String[] identifiers = null;
/**
* Reset the cache of event details.
*/
protected void clearDetails()
{
eventDetails = null;
}
/**
* Add a string to the cache of event details. Automatically
* separates entries with a comma.
* Subclass can just start calling addDetails, since it creates
* the cache if it needs to.
* @param d detail string to add.
*/
protected void addDetails(String d)
{
if (eventDetails == null)
{
eventDetails = new StringBuffer(d);
}
else
{
eventDetails.append(", ").append(d);
}
}
/**
* @return summary of event details, or null if there are none.
*/
protected String getDetails()
{
return (eventDetails == null ? null : eventDetails.toString());
}
/**
* Get the type of this object, found in Constants
*
* @return type of the object
*/
public abstract int getType();
/**
* Provide the text name of the type of this DSpaceObject. It is most likely all uppercase.
* @return Object type as text
*/
public String getTypeText()
{
return Constants.typeText[this.getType()];
}
/**
* Get the internal ID (database primary key) of this object
*
* @return internal ID of object
*/
public abstract int getID();
/**
* Get the Handle of the object. This may return <code>null</code>
*
* @return Handle of the object, or <code>null</code> if it doesn't have
* one
*/
public abstract String getHandle();
/**
* Get a proper name for the object. This may return <code>null</code>.
* Name should be suitable for display in a user interface.
*
* @return Name for the object, or <code>null</code> if it doesn't have
* one
*/
public abstract String getName();
/**
* Tries to lookup all Identifiers of this DSpaceObject.
* @return An array containing all found identifiers or an array with a length of 0.
*/
public String[] getIdentifiers(Context context)
{
if (identifiers == null)
{
log.debug("This DSO's identifiers cache is empty, looking for identifiers...");
identifiers = new String[0];
IdentifierService identifierService =
new DSpace().getSingletonService(IdentifierService.class);
if (identifierService != null)
{
identifiers = identifierService.lookup(context, this);
} else {
log.warn("No IdentifierService found, will return an array containing "
+ "the Handle only.");
if (getHandle() != null)
{
identifiers = new String[] { HandleManager.getCanonicalForm(getHandle()) };
}
}
}
if (log.isDebugEnabled())
{
StringBuilder dbgMsg = new StringBuilder();
for (String id : identifiers)
{
if (dbgMsg.capacity() == 0)
{
dbgMsg.append("This DSO's Identifiers are: ");
} else {
dbgMsg.append(", ");
}
dbgMsg.append(id);
}
dbgMsg.append(".");
log.debug(dbgMsg.toString());
}
return identifiers;
}
public void resetIdentifiersCache()
{
identifiers = null;
}
/**
* Generic find for when the precise type of a DSO is not known, just the
* a pair of type number and database ID.
*
* @param context - the context
* @param type - type number
* @param id - id within table of type'd objects
* @return the object found, or null if it does not exist.
* @throws SQLException only upon failure accessing the database.
*/
public static DSpaceObject find(Context context, int type, int id)
throws SQLException
{
switch (type)
{
case Constants.BITSTREAM : return Bitstream.find(context, id);
case Constants.BUNDLE : return Bundle.find(context, id);
case Constants.ITEM : return Item.find(context, id);
case Constants.COLLECTION: return Collection.find(context, id);
case Constants.COMMUNITY : return Community.find(context, id);
case Constants.GROUP : return Group.find(context, id);
case Constants.EPERSON : return EPerson.find(context, id);
case Constants.SITE : return Site.find(context, id);
}
return null;
}
/**
* Return the dspace object where an ADMIN action right is sufficient to
* grant the initial authorize check.
* <p>
* Default behaviour is ADMIN right on the object grant right on all other
* action on the object itself. Subclass should override this method as
* needed.
*
* @param action
* ID of action being attempted, from
* <code>org.dspace.core.Constants</code>. The ADMIN action is
* not a valid parameter for this method, an
* IllegalArgumentException should be thrown
* @return the dspace object, if any, where an ADMIN action is sufficient to
* grant the original action
* @throws SQLException
* @throws IllegalArgumentException
* if the ADMIN action is supplied as parameter of the method
* call
*/
public DSpaceObject getAdminObject(int action) throws SQLException
{
if (action == Constants.ADMIN)
{
throw new IllegalArgumentException("Illegal call to the DSpaceObject.getAdminObject method");
}
return this;
}
/**
* Return the dspace object that "own" the current object in the hierarchy.
* Note that this method has a meaning slightly different from the
* getAdminObject because it is independent of the action but it is in a way
* related to it. It defines the "first" dspace object <b>OTHER</b> then the
* current one, where allowed ADMIN actions imply allowed ADMIN actions on
* the object self.
*
* @return the dspace object that "own" the current object in
* the hierarchy
* @throws SQLException
*/
public DSpaceObject getParentObject() throws SQLException
{
return null;
}
public abstract void update() throws SQLException, AuthorizeException;
public abstract void updateLastModified();
}