Skip to content

Commit 52fdcf3

Browse files
committed
Updated DatashareItemDataset to check if embargo date is passed, as it
seems to be retailed in the metadata in DSpace 8.
1 parent 9ffea53 commit 52fdcf3

2 files changed

Lines changed: 78 additions & 3 deletions

File tree

dspace-api/src/main/java/org/dspace/content/datashare/DatashareItemDataset.java

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public class DatashareItemDataset {
5353
private static final String DATASHARE_SCHEMA = "ds";
5454
private static final String TOMBSTONE_ELEMENT = "withdrawn";
5555
private static final String TOMBSTONE_SHOW_QUALIFIER = "showtombstone";
56+
private static final String DC_DATE_EMBARGO = "dc.date.embargo";
5657

5758
// Static variables
5859
private static String dir = null;
@@ -341,12 +342,27 @@ public static String getShowTombstone(Item item) {
341342
public static boolean hasEmbargo(Context context, Item item) {
342343
boolean hasEmbargo = true;
343344
ItemService itemService = ContentServiceFactory.getInstance().getItemService();
344-
ConfigurationService configurationService = DSpaceServicesFactory.getInstance().getConfigurationService();
345345

346-
List<MetadataValue> embargoList = itemService.getMetadataByMetadataString(item,
347-
configurationService.getProperty("embargo.field.lift"));
346+
// List<MetadataValue> embargoList = itemService.getMetadataByMetadataString(item,
347+
// configurationService.getProperty("embargo.field.lift"));
348+
List<MetadataValue> embargoList = itemService.getMetadataByMetadataString(item, DC_DATE_EMBARGO);
348349
if (embargoList == null || embargoList.size() == 0) {
349350
hasEmbargo = false;
351+
} else {
352+
// Check if embargo date is in the past
353+
try {
354+
String embargoDateStr = embargoList.get(0).getValue();
355+
log.info("embargoDateStr: " + embargoDateStr);
356+
357+
Date embargoDate = parseDate(embargoDateStr);
358+
Date now = new Date();
359+
log.info("embargoDate: " + embargoDate + ", now: " + now);
360+
if (embargoDate != null && embargoDate.after(now)) {
361+
hasEmbargo = false;
362+
}
363+
} catch (Exception ex) {
364+
log.error("Problem parsing embargo date: " + ex.getMessage());
365+
}
350366
}
351367

352368
log.info(item.getID() + " hasEmbargo: " + hasEmbargo);
@@ -384,6 +400,30 @@ private static boolean isTombstoned(Context context, Item item) {
384400
log.info("isTombstoned(): " + show);
385401
return show;
386402
}
403+
404+
/**
405+
* Parse a date string in ISO format yyyy-MM-dd and return a Date
406+
* representing the start of that day in the system default timezone.
407+
* Returns null when the input is null, empty or not in the expected format.
408+
*
409+
* @param dateStr date string expected in yyyy-MM-dd
410+
* @return parsed Date or null
411+
*/
412+
public static Date parseDate(String dateStr) {
413+
if (dateStr == null || dateStr.trim().isEmpty()) {
414+
return null;
415+
}
416+
try {
417+
// Use Java 8 date time API to parse date
418+
java.time.LocalDate ld = java.time.LocalDate.parse(dateStr,
419+
java.time.format.DateTimeFormatter.ISO_LOCAL_DATE);
420+
java.time.ZonedDateTime zdt = ld.atStartOfDay(java.time.ZoneId.systemDefault());
421+
return Date.from(zdt.toInstant());
422+
} catch (java.time.format.DateTimeParseException ex) {
423+
log.error("Problem parsing date: " + ex.getMessage());
424+
return null;
425+
}
426+
}
387427

388428
/**
389429
* Create dataset zip file in a seperate thread.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.dspace.content.datashare;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNotNull;
5+
import static org.junit.Assert.assertNull;
6+
7+
import java.time.LocalDate;
8+
import java.time.ZoneId;
9+
import java.util.Date;
10+
11+
import org.junit.Test;
12+
13+
public class DatashareItemDatasetTest {
14+
15+
@Test
16+
public void testParseDateValidYMD() {
17+
String s = "2015-05-07";
18+
Date d = DatashareItemDataset.parseDate(s);
19+
assertNotNull(d);
20+
LocalDate ld = d.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
21+
assertEquals(LocalDate.of(2015, 5, 7), ld);
22+
}
23+
24+
@Test
25+
public void testParseDateInvalidFormatReturnsNull() {
26+
String s = "05/07/2015";
27+
Date d = DatashareItemDataset.parseDate(s);
28+
assertNull(d);
29+
}
30+
31+
@Test
32+
public void testParseDateNullReturnsNull() {
33+
assertNull(DatashareItemDataset.parseDate(null));
34+
}
35+
}

0 commit comments

Comments
 (0)