forked from DSpace/dspace-replicate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveAIP.java
More file actions
199 lines (176 loc) · 7.71 KB
/
Copy pathRemoveAIP.java
File metadata and controls
199 lines (176 loc) · 7.71 KB
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
/**
* 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.ctask.replicate;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.dspace.content.Collection;
import org.dspace.content.Community;
import org.dspace.content.DSpaceObject;
import org.dspace.content.Item;
import org.dspace.content.Site;
import org.dspace.content.factory.ContentServiceFactory;
import org.dspace.content.service.CommunityService;
import org.dspace.content.service.ItemService;
import org.dspace.core.Context;
import org.dspace.curate.AbstractCurationTask;
import org.dspace.curate.Curator;
import org.dspace.curate.Distributive;
import org.dspace.pack.bagit.CatalogPacker;
/**
* RemoveAIP task will remove requested objects from the replica store. If the
* object is a container, all its children (members) will also be removed.
*
* @author richardrodgers
* @see TransmitAIP
*/
@Distributive
public class RemoveAIP extends AbstractCurationTask {
private static final Logger log = LogManager.getLogger();
private final CommunityService communityService = ContentServiceFactory.getInstance().getCommunityService();
private final ItemService itemService = ContentServiceFactory.getInstance().getItemService();
private String archFmt;
// Group where all AIPs are stored
private String storeGroupName;
// Group where object deletion catalog/records are stored
private String deleteGroupName;
@Override
public void init(Curator curator, String taskId) throws IOException {
super.init(curator, taskId);
archFmt = configurationService.getProperty("replicate.packer.archfmt");
storeGroupName = configurationService.getProperty("replicate.group.aip.name");
deleteGroupName = configurationService.getProperty("replicate.group.delete.name");
}
/**
* Removes replicas of passed object from the replica store.
* If a container, removes all the member replicas, in addition
* to the replica of the container object. No change is made to
* the DSPace object itself.
*
* @param dso the DSpace object
* @return integer which represents Curator return status
* @throws IOException if I/O error
*/
@Override
public int perform(DSpaceObject dso) throws IOException {
ReplicaManager repMan = ReplicaManager.instance();
try {
remove(Curator.curationContext(), repMan, dso);
} catch (SQLException e) {
throw new IOException(e);
}
setResult("AIP for '" + dso.getHandle() + "' has been removed");
return Curator.CURATE_SUCCESS;
}
/**
* Remove replica(s) of the passed in DSpace object from a particular
* replica ObjectStore.
*
* @param context the context to use
* @param repMan ReplicaManager (used to access ObjectStore)
* @param dso the DSpace object whose replicas we will remove
* @throws IOException if I/O error
* @throws SQLException if database error
*/
private void remove(Context context, ReplicaManager repMan, DSpaceObject dso) throws IOException, SQLException {
// Remove object from AIP storage
String objId = repMan.storageId(context, dso.getHandle(), archFmt);
repMan.removeObject(storeGroupName, objId);
report("Removing AIP for: " + objId);
// If it is a Collection, also remove all Items from AIP storage
if (dso instanceof Collection) {
Collection collection = (Collection) dso;
Iterator<Item> iter = itemService.findByCollection(context, collection);
while (iter.hasNext()) {
remove(context, repMan, iter.next());
}
} else if (dso instanceof Community) {
// else if it is a Community, also remove all sub-communities, collections (and items) from AIP storage
Community community = (Community) dso;
for (Community subCommunity : community.getSubcommunities()) {
remove(context, repMan, subCommunity);
}
for (Collection coll : community.getCollections()) {
remove(context, repMan, coll);
}
} else if (dso instanceof Site) {
// else if it is a Site object, remove all top-level communities (and everything else) from AIP storage
List<Community> topCommunities = communityService.findAllTop(context);
for (Community subCommunity : topCommunities) {
remove(context, repMan, subCommunity);
}
}
}
/**
* Removes replicas of passed id from the replica store. This can act in
* one of two ways: either there is an existing DSpace Object with
* this id, in which case it behaves like the previous method, or there
* is no DSpace Object, in which case we assume that the object has been
* deleted. In this case, the replica store is purged of the deleted
* object, or objects, if the id is (was) a container.
*
* @param ctx current DSpace Context
* @param id Identifier of the object to be removed.
* @return integer which represents Curator return status
* @throws IOException if I/O error
*/
@Override
public int perform(Context ctx, String id) throws IOException {
ReplicaManager repMan = ReplicaManager.instance();
// If the object is still in DSpace, call perform(dso) instead.
DSpaceObject dso;
try {
dso = dspaceObjectUtils.findDSpaceObject(ctx,id);
if (dso != null) {
return perform(dso);
}
} catch (SQLException sqlE) {
throw new IOException(sqlE.getMessage(), sqlE);
}
// Otherwise, this object was already previously deleted from DSpace.
// So, we'll treat this as a deletion "garbage clean"
// Locate the deletion catalog associated with this object
// (This catalog should exist, as the object was previously deleted)
String catId = repMan.deletionCatalogId(id, archFmt);
int status = Curator.CURATE_FAIL;
String result;
File catFile = repMan.fetchObject(ctx, deleteGroupName, catId);
if (catFile != null) {
CatalogPacker cpack = new CatalogPacker(ctx, id);
cpack.unpack(catFile);
// remove the object AIP itself
String objId = repMan.storageId(ctx, id, archFmt);
repMan.removeObject(storeGroupName, objId);
report("Removing AIP for: " + objId);
// remove all member/child object's AIPs
for (String mem : cpack.getMembers()) {
String memId = repMan.storageId(ctx, mem, archFmt);
repMan.removeObject(storeGroupName, memId);
report("Removing AIP for: " + memId);
}
// remove local deletion catalog
boolean successful = catFile.delete();
if (!successful) {
log.warn("Unable to delete AIP: {}", catFile);
}
// remove remote deletion catalog
repMan.removeObject(deleteGroupName, catId);
result = "AIP for '" + id + "' has been removed (along with any child object AIPs)";
status = Curator.CURATE_SUCCESS;
} else {
result = "Deletion record for '" + id + "' could not be found in Replica Store. Perhaps " +
"this object's AIP was already removed?";
}
setResult(result);
return status;
}
}