Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pull] main from DSpace:main #283

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
package org.dspace.app.mediafilter;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -37,8 +38,9 @@
* MFM: -v verbose outputs all extracted text to STDOUT; -f force forces all
* bitstreams to be processed, even if they have been before; -n noindex does not
* recreate index after processing bitstreams; -i [identifier] limits processing
* scope to a community, collection or item; and -m [max] limits processing to a
* maximum number of items.
* scope to a community, collection or item; -m [max] limits processing to a
* maximum number of items; -fd [fromdate] takes only items starting from this date,
* filtering by last_modified in the item table.
*/
public class MediaFilterScript extends DSpaceRunnable<MediaFilterScriptConfiguration> {

Expand All @@ -60,6 +62,7 @@ public class MediaFilterScript extends DSpaceRunnable<MediaFilterScriptConfigura
private String[] filterNames;
private String[] skipIds = null;
private Map<String, List<String>> filterFormats = new HashMap<>();
private LocalDate fromDate = null;

public MediaFilterScriptConfiguration getScriptConfiguration() {
return new DSpace().getServiceManager()
Expand Down Expand Up @@ -112,6 +115,10 @@ public void setup() throws ParseException {
skipIds = commandLine.getOptionValues('s');
}

if (commandLine.hasOption('d')) {
fromDate = LocalDate.parse(commandLine.getOptionValue('d'));
}


}

Expand Down Expand Up @@ -215,6 +222,10 @@ public void internalRun() throws Exception {
mediaFilterService.setSkipList(Arrays.asList(skipIds));
}

if (fromDate != null) {
mediaFilterService.setFromDate(fromDate);
}

Context c = null;

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public Options getOptions() {
.build();
options.addOption(pluginOption);

options.addOption("d", "fromdate", true, "Process only item from specified last modified date");

Option skipOption = Option.builder("s")
.longOpt("skip")
.hasArg()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@

import java.io.InputStream;
import java.sql.SQLException;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -93,6 +96,7 @@ public class MediaFilterServiceImpl implements MediaFilterService, InitializingB
protected boolean isVerbose = false;
protected boolean isQuiet = false;
protected boolean isForce = false; // default to not forced
protected LocalDate fromDate = null;

protected MediaFilterServiceImpl() {

Expand Down Expand Up @@ -120,6 +124,15 @@ public void applyFiltersAllItems(Context context) throws Exception {
for (Community topLevelCommunity : topLevelCommunities) {
applyFiltersCommunity(context, topLevelCommunity);
}
} else if (fromDate != null) {
Iterator<Item> itemIterator =
itemService.findByLastModifiedSince(
context,
Date.from(fromDate.atStartOfDay(ZoneId.systemDefault()).toInstant())
);
while (itemIterator.hasNext() && processed < max2Process) {
applyFiltersItem(context, itemIterator.next());
}
} else {
//otherwise, just find every item and process
Iterator<Item> itemIterator = itemService.findAll(context);
Expand Down Expand Up @@ -588,4 +601,9 @@ public void setFilterFormats(Map<String, List<String>> filterFormats) {
public void setLogHandler(DSpaceRunnableHandler handler) {
this.handler = handler;
}

@Override
public void setFromDate(LocalDate fromDate) {
this.fromDate = fromDate;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package org.dspace.app.mediafilter.service;

import java.sql.SQLException;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -149,4 +150,6 @@ public void updatePoliciesOfDerivativeBitstreams(Context context, Item item, Bit
* @param handler
*/
public void setLogHandler(DSpaceRunnableHandler handler);

public void setFromDate(LocalDate fromDate);
}
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ public int countItems(Context context, List<Collection> collections, boolean inc
public Iterator<Item> findByLastModifiedSince(Context context, Date since)
throws SQLException {
Query query = createQuery(context,
"SELECT i.id FROM Item i WHERE last_modified > :last_modified ORDER BY id");
"SELECT i.id FROM Item i WHERE lastModified > :last_modified ORDER BY id");
query.setParameter("last_modified", since, TemporalType.TIMESTAMP);
@SuppressWarnings("unchecked")
List<UUID> uuids = query.getResultList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.sql.SQLException;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.validator.routines.EmailValidator;
import org.apache.http.client.utils.URIBuilder;
import org.apache.logging.log4j.LogManager;
Expand Down Expand Up @@ -287,19 +289,24 @@ public Class<RequestItemRest> getDomainClass() {
* Generate a link back to DSpace, to act on a request.
*
* @param token identifies the request.
* @return URL to the item request API, with the token as request parameter
* "token".
* @return URL to the item request API, with /request-a-copy/{token} as the last URL segments
* @throws URISyntaxException passed through.
* @throws MalformedURLException passed through.
*/
private String getLinkTokenEmail(String token)
public String getLinkTokenEmail(String token)
throws URISyntaxException, MalformedURLException {
final String base = configurationService.getProperty("dspace.ui.url");

URI link = new URIBuilder(base)
.setPathSegments("request-a-copy", token)
.build();
// Construct the link, making sure to support sub-paths
URIBuilder uriBuilder = new URIBuilder(base);
List<String> segments = new LinkedList<>();
if (StringUtils.isNotBlank(uriBuilder.getPath())) {
segments.add(StringUtils.strip(uriBuilder.getPath(), "/"));
}
segments.add("request-a-copy");
segments.add(token);

return link.toURL().toExternalForm();
// Build and return the URL from segments (or throw exception)
return uriBuilder.setPathSegments(segments).build().toURL().toExternalForm();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.sql.SQLException;
import java.time.temporal.ChronoUnit;
import java.util.Date;
Expand Down Expand Up @@ -55,10 +57,12 @@
import org.dspace.content.Bitstream;
import org.dspace.content.Collection;
import org.dspace.content.Item;
import org.dspace.services.ConfigurationService;
import org.hamcrest.Matchers;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;

/**
*
Expand All @@ -81,6 +85,12 @@ public class RequestItemRepositoryIT
@Autowired(required = true)
RequestItemService requestItemService;

@Autowired
ApplicationContext applicationContext;

@Autowired
private ConfigurationService configurationService;

private Collection collection;

private Item item;
Expand Down Expand Up @@ -594,4 +604,39 @@ public void testGetDomainClass() {
Class instanceClass = instance.getDomainClass();
assertEquals("Wrong domain class", RequestItemRest.class, instanceClass);
}

/**
* Test that generated links include the correct base URL, where the UI URL has a subpath like /subdir
*/
@Test
public void testGetLinkTokenEmailWithSubPath() throws MalformedURLException, URISyntaxException {
RequestItemRepository instance = applicationContext.getBean(
RequestItemRest.CATEGORY + '.' + RequestItemRest.PLURAL_NAME,
RequestItemRepository.class);
String currentDspaceUrl = configurationService.getProperty("dspace.ui.url");
String newDspaceUrl = currentDspaceUrl + "/subdir";
// Add a /subdir to the url for this test
configurationService.setProperty("dspace.ui.url", newDspaceUrl);
String expectedUrl = newDspaceUrl + "/request-a-copy/token";
String generatedLink = instance.getLinkTokenEmail("token");
// The URLs should match
assertEquals(expectedUrl, generatedLink);
configurationService.reloadConfig();
}

/**
* Test that generated links include the correct base URL, with NO subpath elements
*/
@Test
public void testGetLinkTokenEmailWithoutSubPath() throws MalformedURLException, URISyntaxException {
RequestItemRepository instance = applicationContext.getBean(
RequestItemRest.CATEGORY + '.' + RequestItemRest.PLURAL_NAME,
RequestItemRepository.class);
String currentDspaceUrl = configurationService.getProperty("dspace.ui.url");
String expectedUrl = currentDspaceUrl + "/request-a-copy/token";
String generatedLink = instance.getLinkTokenEmail("token");
// The URLs should match
assertEquals(expectedUrl, generatedLink);
configurationService.reloadConfig();
}
}