forked from DSpace/dspace-replicate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBagItReplaceWithAIP.java
More file actions
111 lines (103 loc) · 4.37 KB
/
Copy pathBagItReplaceWithAIP.java
File metadata and controls
111 lines (103 loc) · 4.37 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
/**
* 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 org.dspace.authorize.AuthorizeException;
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.packager.PackageUtils;
import org.dspace.content.service.CollectionService;
import org.dspace.content.service.CommunityService;
import org.dspace.content.service.ItemService;
import org.dspace.content.service.SiteService;
import org.dspace.core.Constants;
import org.dspace.core.Context;
import org.dspace.curate.AbstractCurationTask;
import org.dspace.curate.Curator;
import org.dspace.curate.Mutative;
import org.dspace.pack.Packer;
import org.dspace.pack.PackerFactory;
/**
* BagItReplaceWithAIP task will instate the replica representation of the object in
* place of the current (repository) one.
*
* @author richardrodgers
* @see TransmitAIP
*/
@Mutative
public class BagItReplaceWithAIP extends AbstractCurationTask {
private final CommunityService communityService = ContentServiceFactory.getInstance().getCommunityService();
private final CollectionService collectionService = ContentServiceFactory.getInstance().getCollectionService();
private final ItemService itemService = ContentServiceFactory.getInstance().getItemService();
private final SiteService siteService = ContentServiceFactory.getInstance().getSiteService();
private String archFmt;
// Group where all AIPs are stored
private String storeGroupName;
@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");
}
/**
* Perform the 'Replace with AIP' task.
* <P>
* Actually overwrite any existing object data in the repository with
* whatever information is contained in the AIP.
* @param dso the DSpace object to replace
* @return integer which represents Curator return status
* @throws IOException if I/O error
*/
@Override
public int perform(DSpaceObject dso) throws IOException {
final ReplicaManager repMan = ReplicaManager.instance();
// overwrite with AIP data
try {
Context context = Curator.curationContext();
final Packer packer = PackerFactory.instance(context, dso);
int status = Curator.CURATE_FAIL;
String result = null;
String objId = repMan.storageId(context, dso.getHandle(), archFmt);
File archive = repMan.fetchObject(context, storeGroupName, objId);
if (archive != null) {
// clear object where necessary
if (dso.getType() == Constants.ITEM) {
PackageUtils.removeAllBitstreams(context, dso);
PackageUtils.clearAllMetadata(context, dso);
}
packer.unpack(archive);
// now update the dso
int type = dso.getType();
if (type == Constants.ITEM) {
itemService.update(context, (Item) dso);
} else if (type == Constants.COLLECTION) {
collectionService.update(context, (Collection) dso);
} else if (type == Constants.COMMUNITY) {
communityService.update(context, (Community) dso);
} else if (type == Constants.SITE) {
siteService.update(context, (Site) dso);
}
status = Curator.CURATE_SUCCESS;
result = "Object: " + dso.getHandle() + " replaced from AIP";
} else {
result = "Failed to replace Object. AIP could not be found in Replica Store.";
}
report(result);
setResult(result);
return status;
} catch (AuthorizeException | SQLException e) {
throw new IOException(e);
}
}
}