Skip to content

Commit

Permalink
Process the email domains in a single file
Browse files Browse the repository at this point in the history
  • Loading branch information
amontenegro committed Dec 11, 2024
1 parent 2f55433 commit 3224dc4
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -251,15 +251,13 @@ public Response viewRecord(String orcid) {
Record record = recordManagerReadOnly.getRecord(orcid);
orcidSecurityManager.checkAndFilter(orcid, record);
if (record.getPerson() != null) {
emailDomainManager.processProfessionalEmailsForV2API(record.getPerson().getEmails());
sourceUtils.setSourceName(record.getPerson());
}
if (record.getActivitiesSummary() != null) {
ActivityUtils.cleanEmptyFields(record.getActivitiesSummary());
sourceUtils.setSourceName(record.getActivitiesSummary());
}
if(record.getPerson() != null && record.getPerson().getEmails() != null) {
processProfessionalEmails(record.getPerson().getEmails());
}
ElementUtils.setPathToRecord(record, orcid);
Api2_0_LastModifiedDatesHelper.calculateLastModified(record);
return Response.ok(record).build();
Expand Down Expand Up @@ -800,8 +798,7 @@ public Response viewEmails(String orcid) {
orcidSecurityManager.checkAndFilter(orcid, emails.getEmails(), ScopePathType.ORCID_BIO_READ_LIMITED);
}

processProfessionalEmails(emails);

emailDomainManager.processProfessionalEmailsForV2API(emails);
ElementUtils.setPathToEmail(emails, orcid);
Api2_0_LastModifiedDatesHelper.calculateLastModified(emails);
sourceUtils.setSourceName(emails);
Expand Down Expand Up @@ -1069,7 +1066,7 @@ public Response viewPersonalDetails(String orcid) {
public Response viewPerson(String orcid) {
Person person = personDetailsManagerReadOnly.getPersonDetails(orcid);
orcidSecurityManager.checkAndFilter(orcid, person);
processProfessionalEmails(person.getEmails());
emailDomainManager.processProfessionalEmailsForV2API(person.getEmails());
ElementUtils.setPathToPerson(person, orcid);
Api2_0_LastModifiedDatesHelper.calculateLastModified(person);
sourceUtils.setSourceName(person);
Expand Down Expand Up @@ -1117,28 +1114,6 @@ private void validateSearchParams(Map<String, List<String>> queryMap) {
}
}

private void processProfessionalEmails(Emails emails) {
if(emails == null || emails.getEmails() == null) {
return;
}
for (Email email : emails.getEmails()) {
if (email.isVerified()) {
String domain = email.getEmail().split("@")[1];
List<EmailDomainEntity> domainInfos = emailDomainManager.findByEmailDomain(domain);
// Set appropriate source name and source id for professional emails
for(EmailDomainEntity domainInfo: domainInfos) {
if (domainInfo != null && domainInfo.getCategory().equals(EmailDomainEntity.DomainCategory.PROFESSIONAL)) {
if (email.getSource() == null) {
email.setSource(new Source());
}
email.setSource(sourceEntityUtils.convertEmailSourceToOrcidValidator(email.getSource()));
break;
}
}
}
}
}

@Override
public Response viewClient(String clientId) {
orcidSecurityManager.checkScopes(ScopePathType.READ_PUBLIC);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public void testViewRecordWrongScope() {
Utils.assertIsPublicOrSource(record.getActivitiesSummary(), SecurityContextTestUtils.DEFAULT_CLIENT_ID);
assertEquals("/0000-0000-0000-0003/person", record.getPerson().getPath());
Utils.assertIsPublicOrSource(record.getPerson(), SecurityContextTestUtils.DEFAULT_CLIENT_ID);

}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public void testViewPersonReadPublic() {
assertNotNull(element);
assertEquals("/0000-0000-0000-0003/person", element.getPath());
Utils.assertIsPublicOrSource(element, "APP-5555555555555555");
fail("Process emails");

Check failure on line 93 in orcid-api-web/src/test/java/org/orcid/api/memberV3/server/delegator/MemberV3ApiServiceDelegator_ReadPersonTest.java

View workflow job for this annotation

GitHub Actions / Unit test report for orcid-api-web

org.orcid.api.memberV3.server.delegator.MemberV3ApiServiceDelegator_ReadPersonTest ► testViewPersonReadPublic

Failed test found in: orcid-api-web/target/surefire-reports/TEST-org.orcid.api.memberV3.server.delegator.MemberV3ApiServiceDelegator_ReadPersonTest.xml Error: java.lang.AssertionError: Process emails
Raw output
java.lang.AssertionError: Process emails
	at org.orcid.api.memberV3.server.delegator.MemberV3ApiServiceDelegator_ReadPersonTest.testViewPersonReadPublic(MemberV3ApiServiceDelegator_ReadPersonTest.java:93)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public void testViewRecordWrongScope() {
Utils.assertIsPublicOrSource(record.getActivitiesSummary(), SecurityContextTestUtils.DEFAULT_CLIENT_ID);
assertEquals("/0000-0000-0000-0003/person", record.getPerson().getPath());
Utils.assertIsPublicOrSource(record.getPerson(), SecurityContextTestUtils.DEFAULT_CLIENT_ID);
fail("Process emails");

Check failure on line 117 in orcid-api-web/src/test/java/org/orcid/api/memberV3/server/delegator/MemberV3ApiServiceDelegator_ReadRecordTest.java

View workflow job for this annotation

GitHub Actions / Unit test report for orcid-api-web

org.orcid.api.memberV3.server.delegator.MemberV3ApiServiceDelegator_ReadRecordTest ► testViewRecordWrongScope

Failed test found in: orcid-api-web/target/surefire-reports/TEST-org.orcid.api.memberV3.server.delegator.MemberV3ApiServiceDelegator_ReadRecordTest.xml Error: java.lang.AssertionError: Process emails
Raw output
java.lang.AssertionError: Process emails
	at org.orcid.api.memberV3.server.delegator.MemberV3ApiServiceDelegator_ReadRecordTest.testViewRecordWrongScope(MemberV3ApiServiceDelegator_ReadRecordTest.java:117)
}

@Test
Expand All @@ -127,6 +128,7 @@ public void testViewRecordReadPublic() {
Utils.assertIsPublicOrSource(record.getActivitiesSummary(), "APP-5555555555555555");
assertEquals("/0000-0000-0000-0003/person", record.getPerson().getPath());
Utils.assertIsPublicOrSource(record.getPerson(), "APP-5555555555555555");
fail("Process emails");

Check failure on line 131 in orcid-api-web/src/test/java/org/orcid/api/memberV3/server/delegator/MemberV3ApiServiceDelegator_ReadRecordTest.java

View workflow job for this annotation

GitHub Actions / Unit test report for orcid-api-web

org.orcid.api.memberV3.server.delegator.MemberV3ApiServiceDelegator_ReadRecordTest ► testViewRecordReadPublic

Failed test found in: orcid-api-web/target/surefire-reports/TEST-org.orcid.api.memberV3.server.delegator.MemberV3ApiServiceDelegator_ReadRecordTest.xml Error: java.lang.AssertionError: Process emails
Raw output
java.lang.AssertionError: Process emails
	at org.orcid.api.memberV3.server.delegator.MemberV3ApiServiceDelegator_ReadRecordTest.testViewRecordReadPublic(MemberV3ApiServiceDelegator_ReadRecordTest.java:131)
}

@Test(expected = OrcidUnauthorizedException.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,12 @@ public static void assertIsPublicOrSource(Person p, String sourceId) {
}
assertIsPublicOrSource(p.getAddresses(), sourceId);
assertIsPublicOrSource(p.getBiography(), sourceId);
assertIsPublicOrSource(p.getEmails(), sourceId);
assertIsPublicOrSource(p.getExternalIdentifiers(), sourceId);
assertIsPublicOrSource(p.getKeywords(), sourceId);
assertIsPublicOrSource(p.getName(), sourceId);
assertIsPublicOrSource(p.getOtherNames(), sourceId);
assertIsPublicOrSource(p.getResearcherUrls(), sourceId);
// EMAILS ARE NOT TESTED! Source on professional emails is overwritten, so, you must verify the email on each test!
}

public static void verifyLastModified(LastModifiedDate l) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@ public interface EmailDomainManager {
List<EmailDomainEntity> findByCategory(EmailDomainEntity.DomainCategory category);

STATUS createOrUpdateEmailDomain(String emailDomain, String rorId);

void processProfessionalEmailsForV2API(org.orcid.jaxb.model.record_v2.Emails emails);

void processProfessionalEmailsForV3API(org.orcid.jaxb.model.v3.release.record.Emails emails);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@

import javax.annotation.Resource;

import liquibase.repackaged.org.apache.commons.lang3.StringUtils;
import org.orcid.core.common.manager.EmailDomainManager;
import org.orcid.core.utils.SourceEntityUtils;
import org.orcid.core.utils.emailDomain.EmailDomainValidator;
import org.orcid.jaxb.model.common_v2.Source;
import org.orcid.jaxb.model.record_v2.Email;
import org.orcid.jaxb.model.record_v2.Emails;
import org.orcid.persistence.dao.EmailDomainDao;
import org.orcid.persistence.jpa.entities.EmailDomainEntity;
import org.orcid.persistence.jpa.entities.EmailDomainEntity.DomainCategory;
Expand All @@ -22,6 +27,9 @@ public enum STATUS {CREATED, UPDATED};
@Resource(name = "emailDomainDaoReadOnly")
private EmailDomainDao emailDomainDaoReadOnly;

@Resource
private SourceEntityUtils sourceEntityUtils;

private void validateEmailDomain(String emailDomain) {
if (emailDomain == null || emailDomain.isBlank()) {
throw new IllegalArgumentException("Email Domain must not be empty");
Expand Down Expand Up @@ -97,4 +105,67 @@ public STATUS createOrUpdateEmailDomain(String emailDomain, String rorId) {
return null;
}

@Override
public void processProfessionalEmailsForV2API(org.orcid.jaxb.model.record_v2.Emails emails) {
if(emails == null || emails.getEmails() == null) {
return;
}
for (org.orcid.jaxb.model.record_v2.Email email : emails.getEmails()) {
if (email.isVerified()) {
String domain = email.getEmail().split("@")[1];
List<EmailDomainEntity> domainsInfo = findByEmailDomain(domain);
String category = EmailDomainEntity.DomainCategory.UNDEFINED.name();
// Set appropriate source name and source id for professional
// emails
if (domainsInfo != null) {
for (EmailDomainEntity domainInfo : domainsInfo) {
category = domainInfo.getCategory().name();
if (StringUtils.equalsIgnoreCase(category, EmailDomainEntity.DomainCategory.PROFESSIONAL.name())) {
break;
}
}
if (StringUtils.equalsIgnoreCase(category, EmailDomainEntity.DomainCategory.PROFESSIONAL.name())) {
if(email.getSource() == null) {
email.setSource(new org.orcid.jaxb.model.common_v2.Source());
}
email.setSource(sourceEntityUtils.convertEmailSourceToOrcidValidator(email.getSource()));
}
}
}
}
}

@Override
public void processProfessionalEmailsForV3API(org.orcid.jaxb.model.v3.release.record.Emails emails) {
if(emails == null || emails.getEmails() == null) {
return;
}
for (org.orcid.jaxb.model.v3.release.record.Email email : emails.getEmails()) {
if (email.isVerified()) {
String domain = email.getEmail().split("@")[1];
List<EmailDomainEntity> domainsInfo = findByEmailDomain(domain);
String category = EmailDomainEntity.DomainCategory.UNDEFINED.name();
// Set appropriate source name and source id for professional
// emails
if (domainsInfo != null) {
for (EmailDomainEntity domainInfo : domainsInfo) {
category = domainInfo.getCategory().name();
if (StringUtils.equalsIgnoreCase(category, EmailDomainEntity.DomainCategory.PROFESSIONAL.name())) {
break;
}
}
if (StringUtils.equalsIgnoreCase(category, EmailDomainEntity.DomainCategory.PROFESSIONAL.name())) {
if(email.getSource() == null) {
email.setSource(new org.orcid.jaxb.model.v3.release.common.Source());
}
email.setSource(sourceEntityUtils.convertEmailSourceToOrcidValidator(email.getSource()));
}
}
}
}
}

// TODO: processProfessionalEmailsForV2API and processProfessionalEmailsForV3API can be merged if we make
// org.orcid.jaxb.model.record_v2.Emails and org.orcid.jaxb.model.v3.release.record.Emails implement from an
// interface that we can call EmailDomainsHolder
}
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ public Person getPersonDetails(String orcid, boolean includeUnverifiedEmails) {
} else {
filteredEmails.setEmails(new ArrayList<Email>(emails.getEmails().stream().filter(e -> e.isVerified()).collect(Collectors.toList())));
}
processProfessionalEmails(filteredEmails);
person.setEmails(filteredEmails);
}
return person;
Expand Down Expand Up @@ -205,32 +204,9 @@ public Person getPublicPersonDetails(String orcid) {
if (emails.getEmails() != null) {
Emails filteredEmails = new Emails();
filteredEmails.setEmails(new ArrayList<Email>(emails.getEmails()));
processProfessionalEmails(filteredEmails);
person.setEmails(filteredEmails);
}

return person;
}

private void processProfessionalEmails(Emails emails) {
for (Email email : emails.getEmails()) {
if (email.isVerified()) {
String domain = email.getEmail().split("@")[1];
List<EmailDomainEntity> domainsInfo = emailDomainManager.findByEmailDomain(domain);
String category = EmailDomainEntity.DomainCategory.UNDEFINED.name();
// Set appropriate source name and source id for professional emails
if (domainsInfo != null) {
for(EmailDomainEntity domainInfo: domainsInfo) {
category = domainInfo.getCategory().name();
if(StringUtils.equalsIgnoreCase(category, EmailDomainEntity.DomainCategory.PROFESSIONAL.name())) {
break;
}
}
if(StringUtils.equalsIgnoreCase(category, EmailDomainEntity.DomainCategory.PROFESSIONAL.name())) {
email.setSource(sourceEntityUtils.convertEmailSourceToOrcidValidator(email.getSource()));
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,6 @@ public class PublicV2ApiServiceDelegatorImpl
@Resource
private EmailDomainManager emailDomainManager;

@Resource
private SourceEntityUtils sourceEntityUtils;

@Value("${org.orcid.core.baseUri}")
private String baseUrl;

Expand Down Expand Up @@ -428,7 +425,7 @@ public Response viewResearcherUrl(String orcid, Long putCode) {
public Response viewEmails(String orcid) {
Emails emails = emailManagerReadOnly.getPublicEmails(orcid);
publicAPISecurityManagerV2.filter(emails);
processProfessionalEmails(emails);
emailDomainManager.processProfessionalEmailsForV2API(emails);
ElementUtils.setPathToEmail(emails, orcid);
Api2_0_LastModifiedDatesHelper.calculateLastModified(emails);
sourceUtilsReadOnly.setSourceName(emails);
Expand Down Expand Up @@ -534,6 +531,7 @@ public Response viewAddress(String orcid, Long putCode) {
public Response viewPerson(String orcid) {
Person person = personDetailsManagerReadOnly.getPublicPersonDetails(orcid);
publicAPISecurityManagerV2.filter(person);
emailDomainManager.processProfessionalEmailsForV2API(person.getEmails());
ElementUtils.setPathToPerson(person, orcid);
Api2_0_LastModifiedDatesHelper.calculateLastModified(person);
sourceUtilsReadOnly.setSourceName(person);
Expand All @@ -545,6 +543,7 @@ public Response viewRecord(String orcid) {
Record record = recordManagerReadOnly.getPublicRecord(orcid);
publicAPISecurityManagerV2.filter(record);
if (record.getPerson() != null) {
emailDomainManager.processProfessionalEmailsForV2API(record.getPerson().getEmails());
sourceUtilsReadOnly.setSourceName(record.getPerson());
}
if (record.getActivitiesSummary() != null) {
Expand Down Expand Up @@ -623,32 +622,6 @@ private void validateRows(Map<String, List<String>> queryMap) {
}
}

private void processProfessionalEmails(Emails emails) {
for (Email email : emails.getEmails()) {
if (email.isVerified()) {
String domain = email.getEmail().split("@")[1];
List<EmailDomainEntity> domainsInfo = emailDomainManager.findByEmailDomain(domain);
String category = EmailDomainEntity.DomainCategory.UNDEFINED.name();
// Set appropriate source name and source id for professional
// emails
if (domainsInfo != null) {
for (EmailDomainEntity domainInfo : domainsInfo) {
category = domainInfo.getCategory().name();
if (StringUtils.equalsIgnoreCase(category, EmailDomainEntity.DomainCategory.PROFESSIONAL.name())) {
break;
}
}
if (StringUtils.equalsIgnoreCase(category, EmailDomainEntity.DomainCategory.PROFESSIONAL.name())) {
if(email.getSource() == null) {
email.setSource(new Source());
}
email.setSource(sourceEntityUtils.convertEmailSourceToOrcidValidator(email.getSource()));
}
}
}
}
}

@Override
public Response viewClient(String clientId) {
ClientSummary client = clientManagerReadOnly.getSummary(clientId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,7 @@ public Response viewPerson(String orcid) {
checkProfileStatus(orcid);
Person person = personDetailsManagerReadOnly.getPublicPersonDetails(orcid);
publicAPISecurityManagerV3.filter(person);
emailDomainManager.processProfessionalEmailsForV3API(person.getEmails());
ElementUtils.setPathToPerson(person, orcid);
Api3_0LastModifiedDatesHelper.calculateLastModified(person);
sourceUtilsReadOnly.setSourceName(person);
Expand All @@ -625,6 +626,7 @@ public Response viewRecord(String orcid) {
Record record = recordManagerReadOnly.getPublicRecord(orcid, filterVersionOfIdentifiers);
publicAPISecurityManagerV3.filter(record);
if (record.getPerson() != null) {
emailDomainManager.processProfessionalEmailsForV3API(record.getPerson().getEmails());
sourceUtilsReadOnly.setSourceName(record.getPerson());
}
if (record.getActivitiesSummary() != null) {
Expand Down

0 comments on commit 3224dc4

Please sign in to comment.