ItemUtils.java: refactored addEmbargoField

(cherry picked from commit 291afa765d)
This commit is contained in:
Agustina Martinez
2023-08-03 09:54:00 +01:00
committed by github-actions[bot]
parent d154936a3d
commit 08e82ebf28

View File

@@ -12,8 +12,6 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.sql.SQLException; import java.sql.SQLException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
@@ -184,22 +182,28 @@ public class ItemUtils {
bitstream, bitstream,
ResourcePolicy.TYPE_CUSTOM); ResourcePolicy.TYPE_CUSTOM);
List<Date> embargoDates = new ArrayList<>(); Date embargoDate = null;
// Account for cases where there could be more than one embargo policy // Account for cases where there could be more than one embargo policy
for (ResourcePolicy policy : policies) { for (ResourcePolicy policy : policies) {
if (policy.getGroup() == anonymousGroup && policy.getAction() == Constants.READ) { if (policy.getGroup() == anonymousGroup && policy.getAction() == Constants.READ) {
Date startDate = policy.getStartDate(); Date startDate = policy.getStartDate();
if (startDate != null && startDate.after(new Date())) { if (startDate != null && startDate.after(new Date())) {
embargoDates.add(startDate); // There is an active embargo: aim to take the longest embargo
if (embargoDate == null) {
embargoDate = startDate;
} else {
embargoDate = startDate.after(embargoDate) ? startDate : embargoDate;
}
} }
} }
} }
if (embargoDates.size() >= 1) {
if (embargoDate != null) {
// Sort array of dates to extract the longest embargo // Sort array of dates to extract the longest embargo
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Collections.sort(embargoDates, Date::compareTo);
bitstreamEl.getField().add( bitstreamEl.getField().add(
createValue("embargo", formatter.format(embargoDates.get(embargoDates.size() - 1)))); createValue("embargo", formatter.format(embargoDate)));
} }
} }