forked from DSpace/DSpace
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEmbargoInfoCheck.java
More file actions
150 lines (128 loc) · 5.84 KB
/
Copy pathEmbargoInfoCheck.java
File metadata and controls
150 lines (128 loc) · 5.84 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
/**
* 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.health;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;
import org.dspace.authorize.ResourcePolicy;
import org.dspace.content.Bitstream;
import org.dspace.content.Bundle;
import org.dspace.content.Collection;
import org.dspace.content.Community;
import org.dspace.content.Item;
import org.dspace.content.factory.ContentServiceFactory;
import org.dspace.content.service.CollectionService;
import org.dspace.content.service.CommunityService;
import org.dspace.content.service.ItemService;
import org.dspace.core.Context;
/**
* This check identifies DSpace objects that have a start date or end date defined in their resource policies.
* @author Matus Kasak (dspace at dataquest.sk)
*/
public class EmbargoInfoCheck extends Check {
private final List<EmbargoInfo> embItems = new ArrayList<>();
private final List<EmbargoInfo> embBitstreams = new ArrayList<>();
private final List<EmbargoInfo> embBundles = new ArrayList<>();
private final List<EmbargoInfo> embComs = new ArrayList<>();
private final List<EmbargoInfo> embCols = new ArrayList<>();
private static final int DISPLAY_THRESHOLD = 50;
@Override
public String run(ReportInfo ri) {
StringBuilder sb = new StringBuilder();
ItemService itemService = ContentServiceFactory.getInstance().getItemService();
CollectionService collectionService = ContentServiceFactory.getInstance().getCollectionService();
CommunityService communityService = ContentServiceFactory.getInstance().getCommunityService();
try (Context context = new Context()) {
Iterator<Item> items = itemService.findAll(context);
while (items.hasNext()) {
Item item = items.next();
collectEmbargoedObjectInfos(item.getResourcePolicies(), embItems, item.getID(), null);
for (Bundle bundle : item.getBundles()) {
collectEmbargoedObjectInfos(bundle.getResourcePolicies(), embBundles, bundle.getID(), item.getID());
for (Bitstream bitstream : bundle.getBitstreams()) {
collectEmbargoedObjectInfos(
bitstream.getResourcePolicies(), embBitstreams, bitstream.getID(), item.getID());
}
}
}
for (Collection col : collectionService.findAll(context)) {
collectEmbargoedObjectInfos(col.getResourcePolicies(), embCols, col.getID(), null);
}
for (Community com : communityService.findAll(context)) {
collectEmbargoedObjectInfos(com.getResourcePolicies(), embComs, com.getID(), null);
}
} catch (SQLException e) {
throw new RuntimeException("Error while fetching items, collections or communities ", e);
}
appendReport(sb, "Items", embItems, false);
appendReport(sb, "Bitstreams", embBitstreams, true);
appendReport(sb, "Bundles", embBundles, true);
appendReport(sb, "Communities", embComs, false);
appendReport(sb, "Collections", embCols, false);
sb.append("\n");
sb.append(String.format("Items: %d\n", embItems.size()));
sb.append(String.format("Bitstreams: %d\n", embBitstreams.size()));
sb.append(String.format("Bundles: %d\n", embBundles.size()));
sb.append(String.format("Communities: %d\n", embComs.size()));
sb.append(String.format("Collections: %d\n", embCols.size()));
return sb.toString();
}
/**
* Add embargo info to target list of DSpace object
*/
private void collectEmbargoedObjectInfos(
List<ResourcePolicy> policies, List<EmbargoInfo> targetList, UUID id, UUID parentId) {
for (ResourcePolicy policy : policies) {
if (policy.getStartDate() != null || policy.getEndDate() != null) {
targetList.add(new EmbargoInfo(id, policy.getStartDate(), policy.getEndDate(), parentId));
}
}
}
private void appendReport(StringBuilder sb, String label, List<EmbargoInfo> list, boolean includeParent) {
int size = list.size();
if (size == 0) {
return;
}
sb.append(String.format("\n%s (%d):\n", label, size));
if (includeParent) {
sb.append(String.format("%-40s | %-12s | %-12s | %-40s\n",
label + " UUID", "Start Date", "End Date", "Item UUID"));
} else {
sb.append(String.format("%-40s | %-12s | %-12s\n", label + " UUID", "Start Date", "End Date"));
}
sb.append("-".repeat(113)).append("\n");
int limit = Math.min(size, DISPLAY_THRESHOLD);
for (int i = 0; i < limit; ++i) {
EmbargoInfo ei = list.get(i);
if (includeParent) {
sb.append(String.format("%-40s | %-12s | %-12s | %-40s\n",
ei.id, ei.startDate, ei.endDate, ei.parentItemId));
} else {
sb.append(String.format("%-40s | %-12s | %-12s\n", ei.id, ei.startDate, ei.endDate));
}
}
if (size > DISPLAY_THRESHOLD) {
sb.append(String.format("... (%d more rows not shown)\n", size - DISPLAY_THRESHOLD));
}
}
private static class EmbargoInfo {
UUID id;
UUID parentItemId;
Date startDate;
Date endDate;
EmbargoInfo(UUID id, Date startDate, Date endDate, UUID parentItemId) {
this.id = id;
this.startDate = startDate;
this.endDate = endDate;
this.parentItemId = parentItemId;
}
}
}