Skip to content

Commit

Permalink
ItemUtils.java: refactored addEmbargoField
Browse files Browse the repository at this point in the history
  • Loading branch information
amgciadev committed Aug 3, 2023
1 parent 0de4c39 commit 291afa7
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions dspace-oai/src/main/java/org/dspace/xoai/util/ItemUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
import java.io.InputStream;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

Expand Down Expand Up @@ -184,22 +182,28 @@ private static void addEmbargoField(Context context, Bitstream bitstream, Elemen
bitstream,
ResourcePolicy.TYPE_CUSTOM);

List<Date> embargoDates = new ArrayList<>();
Date embargoDate = null;

// Account for cases where there could be more than one embargo policy
for (ResourcePolicy policy : policies) {
if (policy.getGroup() == anonymousGroup && policy.getAction() == Constants.READ) {
Date startDate = policy.getStartDate();
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
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Collections.sort(embargoDates, Date::compareTo);
bitstreamEl.getField().add(
createValue("embargo", formatter.format(embargoDates.get(embargoDates.size() - 1))));
createValue("embargo", formatter.format(embargoDate)));
}
}

Expand Down

0 comments on commit 291afa7

Please sign in to comment.