2626import org .dspace .core .Constants ;
2727import org .dspace .core .Context ;
2828import org .dspace .eperson .Group ;
29+ import org .joda .time .LocalDate ;
2930
3031/**
3132 * Default plugin implementation of the access status helper.
3233 * The getAccessStatusFromItem method provides a simple logic to
3334 * calculate the access status of an item based on the policies of
3435 * the primary or the first bitstream in the original bundle.
3536 * Users can override this method for enhanced functionality.
37+ *
38+ * The getEmbargoInformationFromItem method provides a simple logic to
39+ * * retrieve embargo information of bitstreams from an item based on the policies of
40+ * * the primary or the first bitstream in the original bundle.
41+ * * Users can override this method for enhanced functionality.
3642 */
3743public class DefaultAccessStatusHelper implements AccessStatusHelper {
3844 public static final String EMBARGO = "embargo" ;
@@ -54,12 +60,12 @@ public DefaultAccessStatusHelper() {
5460
5561 /**
5662 * Look at the item's policies to determine an access status value.
57- * It is also considering a date threshold for embargos and restrictions.
63+ * It is also considering a date threshold for embargoes and restrictions.
5864 *
5965 * If the item is null, simply returns the "unknown" value.
6066 *
6167 * @param context the DSpace context
62- * @param item the item to embargo
68+ * @param item the item to check for embargoes
6369 * @param threshold the embargo threshold date
6470 * @return an access status value
6571 */
@@ -86,7 +92,7 @@ public String getAccessStatusFromItem(Context context, Item item, Date threshold
8692 .findFirst ()
8793 .orElse (null );
8894 }
89- return caculateAccessStatusForDso (context , bitstream , threshold );
95+ return calculateAccessStatusForDso (context , bitstream , threshold );
9096 }
9197
9298 /**
@@ -104,7 +110,7 @@ public String getAccessStatusFromItem(Context context, Item item, Date threshold
104110 * @param threshold the embargo threshold date
105111 * @return an access status value
106112 */
107- private String caculateAccessStatusForDso (Context context , DSpaceObject dso , Date threshold )
113+ private String calculateAccessStatusForDso (Context context , DSpaceObject dso , Date threshold )
108114 throws SQLException {
109115 if (dso == null ) {
110116 return METADATA_ONLY ;
@@ -156,4 +162,79 @@ private String caculateAccessStatusForDso(Context context, DSpaceObject dso, Dat
156162 }
157163 return RESTRICTED ;
158164 }
165+
166+ /**
167+ * Look at the policies of the primary (or first) bitstream of the item to retrieve its embargo.
168+ *
169+ * If the item is null, simply returns an empty map with no embargo information.
170+ *
171+ * @param context the DSpace context
172+ * @param item the item to embargo
173+ * @return an access status value
174+ */
175+ @ Override
176+ public String getEmbargoFromItem (Context context , Item item )
177+ throws SQLException {
178+ Date embargoDate ;
179+
180+ if (item == null ) {
181+ return null ;
182+ }
183+ // Consider only the original bundles.
184+ List <Bundle > bundles = item .getBundles (Constants .DEFAULT_BUNDLE_NAME );
185+ // Check for primary bitstreams first.
186+ Bitstream bitstream = bundles .stream ()
187+ .map (bundle -> bundle .getPrimaryBitstream ())
188+ .filter (Objects ::nonNull )
189+ .findFirst ()
190+ .orElse (null );
191+ if (bitstream == null ) {
192+ // If there is no primary bitstream,
193+ // take the first bitstream in the bundles.
194+ bitstream = bundles .stream ()
195+ .map (bundle -> bundle .getBitstreams ())
196+ .flatMap (List ::stream )
197+ .findFirst ()
198+ .orElse (null );
199+ }
200+
201+ embargoDate = this .retrieveLongestEmbargo (context , bitstream );
202+
203+ return embargoDate != null ? embargoDate .toString () : null ;
204+ }
205+
206+ /**
207+ *
208+ */
209+ private Date retrieveLongestEmbargo (Context context , Bitstream bitstream ) throws SQLException {
210+ Date embargoDate = null ;
211+ // Only consider read policies.
212+ List <ResourcePolicy > policies = authorizeService
213+ .getPoliciesActionFilter (context , bitstream , Constants .READ );
214+
215+ // Looks at all read policies.
216+ for (ResourcePolicy policy : policies ) {
217+ boolean isValid = resourcePolicyService .isDateValid (policy );
218+ Group group = policy .getGroup ();
219+
220+ if (group != null && StringUtils .equals (group .getName (), Group .ANONYMOUS )) {
221+ // Only calculate the status for the anonymous group.
222+ if (!isValid ) {
223+ // If the policy is not valid there is an active embargo
224+ Date startDate = policy .getStartDate ();
225+
226+ if (startDate != null && !startDate .before (LocalDate .now ().toDate ())) {
227+ // There is an active embargo: aim to take the longest embargo
228+ if (embargoDate == null ) {
229+ embargoDate = startDate ;
230+ } else {
231+ embargoDate = startDate .after (embargoDate ) ? startDate : embargoDate ;
232+ }
233+ }
234+ }
235+ }
236+ }
237+
238+ return embargoDate ;
239+ }
159240}
0 commit comments