Skip to content

Commit

Permalink
Merged in task/dspace-cris-2023_02_x/DSC-2014 (pull request DSpace#2990)
Browse files Browse the repository at this point in the history
DSC-2014

Approved-by: Andrea Bollini
  • Loading branch information
Micheleboychuk authored and abollini committed Nov 15, 2024
2 parents 2fa07ba + 5bc0e3e commit 6a78aba
Show file tree
Hide file tree
Showing 2 changed files with 219 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.validator.routines.EmailValidator;
import org.dspace.content.Item;
import org.dspace.content.MetadataValue;
import org.dspace.content.service.ItemService;
import org.dspace.core.Context;
import org.dspace.core.I18nUtil;
import org.dspace.services.ConfigurationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;
Expand Down Expand Up @@ -57,6 +57,7 @@ public List<RequestItemAuthor> getRequestItemAuthor(Context context, Item item)
}
boolean useNames = vals.size() == nameVals.size();
if (!vals.isEmpty()) {
EmailValidator emailValidator = EmailValidator.getInstance(false, false);
authors = new ArrayList<>(vals.size());
for (int authorIndex = 0; authorIndex < vals.size(); authorIndex++) {
String email = vals.get(authorIndex).getValue();
Expand All @@ -66,42 +67,47 @@ public List<RequestItemAuthor> getRequestItemAuthor(Context context, Item item)
}

if (StringUtils.isBlank(fullname)) {
fullname = I18nUtil.getMessage(
"org.dspace.app.requestitem.RequestItemMetadataStrategy.unnamed",
context);
fullname = email;
}

if (emailValidator.isValid(email)) {
RequestItemAuthor author = new RequestItemAuthor(fullname, email);
authors.add(author);
}
RequestItemAuthor author = new RequestItemAuthor(
fullname, email);
authors.add(author);
}
return authors;
return authors.size() > 0 ? authors : fallback(context, item);
} else {
return Collections.EMPTY_LIST;
return fallback(context, item);
}
} else {
// Uses the basic strategy to look for the original submitter
authors = super.getRequestItemAuthor(context, item);
return fallback(context, item);
}
}

// Remove from the list authors that do not have email addresses.
for (RequestItemAuthor author : authors) {
if (null == author.getEmail()) {
authors.remove(author);
}
private List<RequestItemAuthor> fallback(Context context, Item item) throws SQLException {
List<RequestItemAuthor> authors;
// Uses the basic strategy to look for the original submitter
authors = super.getRequestItemAuthor(context, item);

// Remove from the list authors that do not have email addresses.
for (RequestItemAuthor author : authors) {
if (null == author.getEmail()) {
authors.remove(author);
}
}

if (authors.isEmpty()) { // No author email addresses! Fall back
//First get help desk name and email
String email = configurationService.getProperty("mail.helpdesk");
String name = configurationService.getProperty("mail.helpdesk.name");
// If help desk mail is null get the mail and name of admin
if (email == null) {
email = configurationService.getProperty("mail.admin");
name = configurationService.getProperty("mail.admin.name");
}
authors.add(new RequestItemAuthor(name, email));
if (authors.isEmpty()) { // No author email addresses! Fall back
//First get help desk name and email
String email = configurationService.getProperty("mail.helpdesk");
String name = configurationService.getProperty("mail.helpdesk.name");
// If help desk mail is null get the mail and name of admin
if (email == null) {
email = configurationService.getProperty("mail.admin");
name = configurationService.getProperty("mail.admin.name");
}
return authors;
authors.add(new RequestItemAuthor(name, email));
}
return authors;
}

public void setEmailMetadata(@NonNull String emailMetadata) {
Expand All @@ -112,4 +118,12 @@ public void setFullNameMetadata(@NonNull String fullNameMetadata) {
this.fullNameMetadata = fullNameMetadata;
}

public void setConfigurationService(ConfigurationService configurationService) {
this.configurationService = configurationService;
}

public void setItemService(ItemService itemService) {
this.itemService = itemService;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.app.requestitem;

import static org.junit.Assert.assertEquals;

import java.sql.SQLException;
import java.util.List;

import org.dspace.AbstractUnitTest;
import org.dspace.builder.AbstractBuilder;
import org.dspace.builder.CollectionBuilder;
import org.dspace.builder.CommunityBuilder;
import org.dspace.builder.EPersonBuilder;
import org.dspace.builder.ItemBuilder;
import org.dspace.content.Collection;
import org.dspace.content.Community;
import org.dspace.content.Item;
import org.dspace.content.factory.ContentServiceFactory;
import org.dspace.content.service.ItemService;
import org.dspace.core.Context;
import org.dspace.eperson.EPerson;
import org.dspace.services.ConfigurationService;
import org.dspace.services.factory.DSpaceServicesFactory;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

/**
* Test suite for RequestItemMetadataStrategy
*
* @author Mykhaylo Boychuk (mykhaylo.boychuk at 4science.com)
*/
public class RequestItemMetadataStrategyTest extends AbstractUnitTest {
private static final String AUTHOR_ADDRESS = "[email protected]";

private ItemService itemService;
private ConfigurationService configurationService;

private static EPerson johnDoe;

private Item item;
private Item orphanItem;
private Item notValidEmailItem;

@BeforeClass
public static void setUpClass() throws SQLException {
AbstractBuilder.init();
Context context = new Context();
context.turnOffAuthorisationSystem();
johnDoe = EPersonBuilder.createEPerson(context)
.withEmail(AUTHOR_ADDRESS)
.withNameInMetadata("John", "Doe")
.build();
context.restoreAuthSystemState();
context.complete();
}

@AfterClass
public static void tearDownClass() {
AbstractBuilder.destroy();
}

@Before
public void setUp() {
this.itemService = ContentServiceFactory.getInstance().getItemService();
this.configurationService = DSpaceServicesFactory.getInstance().getConfigurationService();

this.configurationService.setProperty("mail.helpdesk", "[email protected]");
this.configurationService.setProperty("mail.helpdesk.name", "Helpdesk Name");

context = new Context();
context.setCurrentUser(johnDoe);
context.turnOffAuthorisationSystem();
Community community = CommunityBuilder.createCommunity(context).build();
Collection collection = CollectionBuilder.createCollection(context, community).build();
item = ItemBuilder.createItem(context, collection)
.withTitle("Misha Boychuk")
.withPersonEmail("[email protected]")
.build();

orphanItem = ItemBuilder.createItem(context, collection)
.withTitle("Orphan Item")
.withPersonEmail("[email protected]")
.build();
orphanItem.setSubmitter(null);

notValidEmailItem = ItemBuilder.createItem(context, collection)
.withTitle("Not Valid email Item")
.withPersonEmail("test@test")
.build();
context.restoreAuthSystemState();
}

/**
* Test of getRequestItemAuthor method, of class RequestItemMetadataStrategy.
* @throws Exception passed through.
*/
@Test
public void testMetadataStrategy() throws Exception {
RequestItemMetadataStrategy metadataStrategy = new RequestItemMetadataStrategy();
metadataStrategy.setItemService(this.itemService);
metadataStrategy.setConfigurationService(this.configurationService);
metadataStrategy.setEmailMetadata("person.email");
metadataStrategy.setFullNameMetadata("dc.title");

List<RequestItemAuthor> authors = metadataStrategy.getRequestItemAuthor(context, item);
assertEquals("[email protected]", authors.get(0).getEmail());
assertEquals("Misha Boychuk", authors.get(0).getFullName());

List<RequestItemAuthor> authorsOforphanItem = metadataStrategy.getRequestItemAuthor(context, orphanItem);
assertEquals("[email protected]", authorsOforphanItem.get(0).getEmail());
assertEquals("Orphan Item", authorsOforphanItem.get(0).getFullName());

List<RequestItemAuthor> authorsOfnotValidEmailItem =
metadataStrategy.getRequestItemAuthor(context, notValidEmailItem);
assertEquals(AUTHOR_ADDRESS, authorsOfnotValidEmailItem.get(0).getEmail());
assertEquals("John Doe", authorsOfnotValidEmailItem.get(0).getFullName());
}

@Test
public void testMissingMetadataStrategy() throws Exception {
RequestItemMetadataStrategy metadataStrategy = new RequestItemMetadataStrategy();
metadataStrategy.setItemService(this.itemService);
metadataStrategy.setConfigurationService(this.configurationService);
metadataStrategy.setEmailMetadata("person.notExist");
metadataStrategy.setFullNameMetadata("dc.title");

List<RequestItemAuthor> authors = metadataStrategy.getRequestItemAuthor(context, item);
assertEquals(AUTHOR_ADDRESS, authors.get(0).getEmail());
assertEquals("John Doe", authors.get(0).getFullName());

List<RequestItemAuthor> authorsOforphanItem = metadataStrategy.getRequestItemAuthor(context, orphanItem);
assertEquals("[email protected]", authorsOforphanItem.get(0).getEmail());
assertEquals("Helpdesk Name", authorsOforphanItem.get(0).getFullName());

List<RequestItemAuthor> authorsOfnotValidEmailItem =
metadataStrategy.getRequestItemAuthor(context, notValidEmailItem);
assertEquals(AUTHOR_ADDRESS, authorsOfnotValidEmailItem.get(0).getEmail());
assertEquals("John Doe", authorsOfnotValidEmailItem.get(0).getFullName());
}

@Test
public void testNotConfiguredMetadataStrategy() throws Exception {
RequestItemMetadataStrategy metadataStrategy = new RequestItemMetadataStrategy();
metadataStrategy.setItemService(this.itemService);
metadataStrategy.setConfigurationService(this.configurationService);

List<RequestItemAuthor> authors = metadataStrategy.getRequestItemAuthor(context, item);
assertEquals(AUTHOR_ADDRESS, authors.get(0).getEmail());
assertEquals("John Doe", authors.get(0).getFullName());

List<RequestItemAuthor> authorsOforphanItem = metadataStrategy.getRequestItemAuthor(context, orphanItem);
assertEquals("[email protected]", authorsOforphanItem.get(0).getEmail());
assertEquals("Helpdesk Name", authorsOforphanItem.get(0).getFullName());

// set helpdesk to null
this.configurationService.setProperty("mail.helpdesk", null);
this.configurationService.setProperty("mail.admin", "[email protected]");
this.configurationService.setProperty("mail.admin.name", "Admin Name");

List<RequestItemAuthor> authorsOforphanItem2 = metadataStrategy.getRequestItemAuthor(context, orphanItem);
assertEquals("[email protected]", authorsOforphanItem2.get(0).getEmail());
assertEquals("Admin Name", authorsOforphanItem2.get(0).getFullName());

List<RequestItemAuthor> authorsOfnotValidEmailItem =
metadataStrategy.getRequestItemAuthor(context, notValidEmailItem);
assertEquals(AUTHOR_ADDRESS, authorsOfnotValidEmailItem.get(0).getEmail());
assertEquals("John Doe", authorsOfnotValidEmailItem.get(0).getFullName());
}

}

0 comments on commit 6a78aba

Please sign in to comment.