diff --git a/orcid-core/src/main/java/org/orcid/core/common/manager/EmailDomainManager.java b/orcid-core/src/main/java/org/orcid/core/common/manager/EmailDomainManager.java new file mode 100644 index 00000000000..b6c7e310adb --- /dev/null +++ b/orcid-core/src/main/java/org/orcid/core/common/manager/EmailDomainManager.java @@ -0,0 +1,15 @@ +package org.orcid.core.common.manager; + +import java.util.List; + +import org.orcid.persistence.jpa.entities.EmailDomainEntity; + +public interface EmailDomainManager { + EmailDomainEntity createEmailDomain(String emailDomain, EmailDomainEntity.DomainCategory category); + + boolean updateCategory(long id, EmailDomainEntity.DomainCategory category); + + EmailDomainEntity findByEmailDoman(String emailDomain); + + List findByCategory(EmailDomainEntity.DomainCategory category); +} diff --git a/orcid-core/src/main/java/org/orcid/core/common/manager/impl/EmailDomainManagerImpl.java b/orcid-core/src/main/java/org/orcid/core/common/manager/impl/EmailDomainManagerImpl.java new file mode 100644 index 00000000000..b5950fb9f91 --- /dev/null +++ b/orcid-core/src/main/java/org/orcid/core/common/manager/impl/EmailDomainManagerImpl.java @@ -0,0 +1,60 @@ +package org.orcid.core.common.manager.impl; + +import java.util.List; + +import javax.annotation.Resource; + +import org.orcid.core.common.manager.EmailDomainManager; +import org.orcid.persistence.dao.EmailDomainDao; +import org.orcid.persistence.jpa.entities.EmailDomainEntity; +import org.orcid.persistence.jpa.entities.EmailDomainEntity.DomainCategory; + +import com.google.common.net.InternetDomainName; + +public class EmailDomainManagerImpl implements EmailDomainManager { + + @Resource(name = "emailDomainDao") + private EmailDomainDao emailDomainDao; + + @Resource(name = "emailDomainDaoReadOnly") + private EmailDomainDao emailDomainDaoReadOnly; + + @Override + public EmailDomainEntity createEmailDomain(String emailDomain, DomainCategory category) { + if (emailDomain == null || emailDomain.isBlank()) { + throw new IllegalArgumentException("Email Domain must not be empty"); + } + if(!InternetDomainName.isValid(emailDomain)) { + throw new IllegalArgumentException("Email Domain '" + emailDomain + "' is invalid"); + } + if (category == null) { + throw new IllegalArgumentException("Category must not be empty"); + } + return emailDomainDao.createEmailDomain(emailDomain, category); + } + + @Override + public boolean updateCategory(long id, DomainCategory category) { + if (category == null) { + throw new IllegalArgumentException("Category must not be empty"); + } + return emailDomainDao.updateCategory(id, category); + } + + @Override + public EmailDomainEntity findByEmailDoman(String emailDomain) { + if (emailDomain == null || emailDomain.isBlank()) { + throw new IllegalArgumentException("Email Domain must not be empty"); + } + return emailDomainDaoReadOnly.findByEmailDoman(emailDomain); + } + + @Override + public List findByCategory(DomainCategory category) { + if (category == null) { + throw new IllegalArgumentException("Category must not be empty"); + } + return emailDomainDaoReadOnly.findByCategory(category); + } + +} diff --git a/orcid-core/src/main/java/org/orcid/core/utils/cache/redis/RedisClient.java b/orcid-core/src/main/java/org/orcid/core/utils/cache/redis/RedisClient.java index 99952cfc018..42affcf8f47 100644 --- a/orcid-core/src/main/java/org/orcid/core/utils/cache/redis/RedisClient.java +++ b/orcid-core/src/main/java/org/orcid/core/utils/cache/redis/RedisClient.java @@ -66,6 +66,10 @@ public RedisClient(String redisHost, int redisPort, String password, int cacheEx @PostConstruct private void init() { + if(!enabled) { + LOG.debug("Redis is not enabled, so, it will not be initilized"); + return; + } try { JedisClientConfig config = DefaultJedisClientConfig.builder().connectionTimeoutMillis(this.clientTimeoutInMillis).timeoutMillis(this.clientTimeoutInMillis) .socketTimeoutMillis(this.clientTimeoutInMillis).password(this.redisPassword).ssl(true).build(); diff --git a/orcid-core/src/main/resources/orcid-core-context.xml b/orcid-core/src/main/resources/orcid-core-context.xml index 3479fe83c8a..76f109dc3ca 100644 --- a/orcid-core/src/main/resources/orcid-core-context.xml +++ b/orcid-core/src/main/resources/orcid-core-context.xml @@ -1214,4 +1214,6 @@ + + diff --git a/orcid-core/src/test/java/org/orcid/core/common/manager/EmailDomainManagerTest.java b/orcid-core/src/test/java/org/orcid/core/common/manager/EmailDomainManagerTest.java new file mode 100644 index 00000000000..bc347b9977f --- /dev/null +++ b/orcid-core/src/test/java/org/orcid/core/common/manager/EmailDomainManagerTest.java @@ -0,0 +1,138 @@ +package org.orcid.core.common.manager; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.List; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.orcid.core.common.manager.impl.EmailDomainManagerImpl; +import org.orcid.persistence.dao.EmailDomainDao; +import org.orcid.persistence.jpa.entities.EmailDomainEntity; +import org.orcid.persistence.jpa.entities.EmailDomainEntity.DomainCategory; +import org.orcid.test.TargetProxyHelper; + +public class EmailDomainManagerTest { + @Mock + private EmailDomainDao emailDomainDaoMock; + + @Mock + private EmailDomainDao emailDomainDaoReadOnlyMock; + + EmailDomainManager edm = new EmailDomainManagerImpl(); + + @Before + public void before() { + MockitoAnnotations.initMocks(this); + TargetProxyHelper.injectIntoProxy(edm, "emailDomainDao", emailDomainDaoMock); + TargetProxyHelper.injectIntoProxy(edm, "emailDomainDaoReadOnly", emailDomainDaoReadOnlyMock); + + EmailDomainEntity e1 = new EmailDomainEntity("gmail.com", DomainCategory.PERSONAL); + EmailDomainEntity e2 = new EmailDomainEntity("yahoo.com", DomainCategory.PERSONAL); + EmailDomainEntity e3 = new EmailDomainEntity("orcid.org", DomainCategory.PROFESSIONAL); + + when(emailDomainDaoReadOnlyMock.findByCategory(eq(DomainCategory.PERSONAL))).thenReturn(List.of(e1, e2)); + when(emailDomainDaoReadOnlyMock.findByCategory(eq(DomainCategory.PROFESSIONAL))).thenReturn(List.of(e3)); + + when(emailDomainDaoReadOnlyMock.findByEmailDoman("gmail.com")).thenReturn(e1); + } + + @Test(expected = IllegalArgumentException.class) + public void createEmailDomain_NullDomainTest() { + edm.createEmailDomain(null, DomainCategory.PROFESSIONAL); + } + + @Test(expected = IllegalArgumentException.class) + public void createEmailDomain_EmptyDomainTest() { + edm.createEmailDomain(" ", DomainCategory.PROFESSIONAL); + } + + @Test(expected = IllegalArgumentException.class) + public void createEmailDomain_InvalidDomainTest() { + edm.createEmailDomain("$$$", DomainCategory.PROFESSIONAL); + } + + @Test(expected = IllegalArgumentException.class) + public void createEmailDomain_NullCategoryTest() { + edm.createEmailDomain("orcid.org", null); + } + + @Test + public void createEmailDomainTest() { + edm.createEmailDomain("orcid.org", DomainCategory.PROFESSIONAL); + verify(emailDomainDaoMock, times(1)).createEmailDomain(eq("orcid.org"), eq(DomainCategory.PROFESSIONAL)); + } + + @Test(expected = IllegalArgumentException.class) + public void updateCategory_nullCategoryTest() { + edm.updateCategory(0, null); + } + + @Test + public void updateCategoryTest() { + edm.updateCategory(0, DomainCategory.PERSONAL); + verify(emailDomainDaoMock, times(1)).updateCategory(eq(Long.valueOf(0)), eq(DomainCategory.PERSONAL)); + } + + @Test(expected = IllegalArgumentException.class) + public void findByEmailDoman_NullDomainTest() { + edm.findByEmailDoman(null); + } + + @Test(expected = IllegalArgumentException.class) + public void findByEmailDoman_EmptyDomainTest() { + edm.findByEmailDoman(" "); + } + + @Test + public void findByEmailDoman_NothingFoundTest() { + assertNull(edm.findByEmailDoman("other.com")); + } + + @Test + public void findByEmailDomanTest() { + EmailDomainEntity ede = edm.findByEmailDoman("gmail.com"); + assertNotNull(ede); + assertEquals("gmail.com", ede.getEmailDomain()); + assertEquals(DomainCategory.PERSONAL, ede.getCategory()); + } + + @Test(expected = IllegalArgumentException.class) + public void findByCategory_NullCategoryTest() { + edm.findByEmailDoman(null); + } + + @Test + public void findByCategory_NothingFoundTest() { + assertTrue(edm.findByCategory(DomainCategory.UNDEFINED).isEmpty()); + } + + @Test + public void findByCategory_OneResultTest() { + List personal = edm.findByCategory(DomainCategory.PROFESSIONAL); + assertNotNull(personal); + assertEquals(1, personal.size()); + assertEquals(DomainCategory.PROFESSIONAL, personal.get(0).getCategory()); + assertEquals("orcid.org", personal.get(0).getEmailDomain()); + } + + @Test + public void findByCategory_TwoResultsTest() { + List personal = edm.findByCategory(DomainCategory.PERSONAL); + assertNotNull(personal); + assertEquals(2, personal.size()); + assertEquals(DomainCategory.PERSONAL, personal.get(0).getCategory()); + assertEquals("gmail.com", personal.get(0).getEmailDomain()); + assertEquals(DomainCategory.PERSONAL, personal.get(1).getCategory()); + assertEquals("yahoo.com", personal.get(1).getEmailDomain()); + } +} diff --git a/orcid-persistence/src/main/java/org/orcid/persistence/dao/EmailDomainDao.java b/orcid-persistence/src/main/java/org/orcid/persistence/dao/EmailDomainDao.java new file mode 100644 index 00000000000..bb5c5730a53 --- /dev/null +++ b/orcid-persistence/src/main/java/org/orcid/persistence/dao/EmailDomainDao.java @@ -0,0 +1,15 @@ +package org.orcid.persistence.dao; + +import java.util.List; + +import org.orcid.persistence.jpa.entities.EmailDomainEntity; + +public interface EmailDomainDao extends GenericDao { + EmailDomainEntity createEmailDomain(String emailDomain, EmailDomainEntity.DomainCategory category); + + boolean updateCategory(long id, EmailDomainEntity.DomainCategory category); + + EmailDomainEntity findByEmailDoman(String emailDomain); + + List findByCategory(EmailDomainEntity.DomainCategory category); +} diff --git a/orcid-persistence/src/main/java/org/orcid/persistence/dao/impl/EmailDomainDaoImpl.java b/orcid-persistence/src/main/java/org/orcid/persistence/dao/impl/EmailDomainDaoImpl.java new file mode 100644 index 00000000000..5a8416d1e3c --- /dev/null +++ b/orcid-persistence/src/main/java/org/orcid/persistence/dao/impl/EmailDomainDaoImpl.java @@ -0,0 +1,67 @@ +package org.orcid.persistence.dao.impl; + +import java.util.List; + +import javax.persistence.NoResultException; +import javax.persistence.Query; +import javax.persistence.TypedQuery; + +import org.orcid.persistence.dao.EmailDomainDao; +import org.orcid.persistence.jpa.entities.EmailDomainEntity; +import org.orcid.persistence.jpa.entities.EmailDomainEntity.DomainCategory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.transaction.annotation.Transactional; + +public class EmailDomainDaoImpl extends GenericDaoImpl implements EmailDomainDao { + + private static final Logger LOG = LoggerFactory.getLogger(EmailDomainDaoImpl.class); + + public EmailDomainDaoImpl() { + super(EmailDomainEntity.class); + } + + @Override + @Transactional + public EmailDomainEntity createEmailDomain(String emailDomain, DomainCategory category) { + LOG.debug("Creating domain {} with category {}", emailDomain, category); + EmailDomainEntity e = new EmailDomainEntity(); + e.setEmailDomain(emailDomain); + e.setCategory(category); + entityManager.persist(e); + return e; + } + + @Override + @Transactional + public boolean updateCategory(long id, DomainCategory category) { + LOG.debug("Updating domain with id {} with category {}", id, category); + Query query = entityManager.createNativeQuery("UPDATE email_domain SET category=:category WHERE id = :id"); + query.setParameter("id", id); + query.setParameter("category", category.toString()); + return query.executeUpdate() > 0; + } + + @Override + public EmailDomainEntity findByEmailDoman(String emailDomain) { + TypedQuery query = entityManager.createQuery("from EmailDomainEntity where emailDomain = :emailDomain", EmailDomainEntity.class); + query.setParameter("emailDomain", emailDomain); + try { + return query.getSingleResult(); + } catch(NoResultException nre) { + // Ignore this exception + } catch(Exception e) { + // Propagate any other exception + throw e; + } + return null; + } + + @Override + public List findByCategory(DomainCategory category) { + TypedQuery query = entityManager.createQuery("from EmailDomainEntity where category = :category", EmailDomainEntity.class); + query.setParameter("category", category); + List results = query.getResultList(); + return results.isEmpty() ? null : results; + } +} diff --git a/orcid-persistence/src/main/java/org/orcid/persistence/jpa/entities/EmailDomainEntity.java b/orcid-persistence/src/main/java/org/orcid/persistence/jpa/entities/EmailDomainEntity.java new file mode 100644 index 00000000000..06ea938e449 --- /dev/null +++ b/orcid-persistence/src/main/java/org/orcid/persistence/jpa/entities/EmailDomainEntity.java @@ -0,0 +1,89 @@ +package org.orcid.persistence.jpa.entities; + +import java.util.Objects; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.EnumType; +import javax.persistence.Enumerated; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.SequenceGenerator; +import javax.persistence.Table; + +/** + * + * @author Will Simpson + * + */ +@Table(name = "email_domain") +@Entity +public class EmailDomainEntity extends BaseEntity { + + private static final long serialVersionUID = 7138838021634315502L; + + public static enum DomainCategory {PERSONAL, PROFESSIONAL, UNDEFINED} + + private Long id; + private String emailDomain; + private DomainCategory category; + + public EmailDomainEntity() { + + } + + public EmailDomainEntity(String emailDomain, DomainCategory category) { + this.emailDomain = emailDomain; + this.category = category; + } + + @Override + @Id + @GeneratedValue(strategy = GenerationType.AUTO, generator = "email_domain_seq") + @SequenceGenerator(name = "email_domain_seq", sequenceName = "email_domain_seq", allocationSize = 1) + @Column(name = "id") + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + @Column(name = "email_domain") + public String getEmailDomain() { + return emailDomain; + } + + public void setCategory(DomainCategory category) { + this.category = category; + } + + @Enumerated(EnumType.STRING) + @Column(name = "category") + public DomainCategory getCategory() { + return category; + } + + public void setEmailDomain(String emailDomain) { + this.emailDomain = emailDomain; + } + + @Override + public int hashCode() { + return Objects.hash(category, emailDomain, id); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + EmailDomainEntity other = (EmailDomainEntity) obj; + return category == other.category && Objects.equals(emailDomain, other.emailDomain) && Objects.equals(id, other.id); + } +} diff --git a/orcid-persistence/src/main/java/org/orcid/persistence/jpa/entities/EmailDomainToOrgIdEntity.java b/orcid-persistence/src/main/java/org/orcid/persistence/jpa/entities/EmailDomainToOrgIdEntity.java new file mode 100644 index 00000000000..09596b84f6e --- /dev/null +++ b/orcid-persistence/src/main/java/org/orcid/persistence/jpa/entities/EmailDomainToOrgIdEntity.java @@ -0,0 +1,66 @@ +package org.orcid.persistence.jpa.entities; + +import java.util.Objects; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.SequenceGenerator; +import javax.persistence.Table; + +/** + * + * @author Will Simpson + * + */ +@Table(name = "email_domain_to_org_id") +@Entity +public class EmailDomainToOrgIdEntity extends BaseEntity { + + private static final long serialVersionUID = 7138838021634315502L; + + public enum DomainCategory {PERSONAL, PROFESSIONAL, UNDEFINED} + + private Long id; + private EmailDomainEntity emailDomian; + private OrgDisambiguatedEntity orgDisambiguated; + + + @Override + @Id + @GeneratedValue(strategy = GenerationType.AUTO, generator = "email_domain_to_org_id_seq") + @SequenceGenerator(name = "email_domain_to_org_id_seq", sequenceName = "email_domain_to_org_id_seq", allocationSize = 1) + @Column(name = "id") + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + @ManyToOne(fetch = FetchType.EAGER) + @JoinColumn(name = "email_domian_id") + public EmailDomainEntity getEmailDomian() { + return emailDomian; + } + + public void setEmailDomian(EmailDomainEntity emailDomian) { + this.emailDomian = emailDomian; + } + + @ManyToOne(fetch = FetchType.EAGER) + @JoinColumn(name = "org_disambiguated_id") + public OrgDisambiguatedEntity getOrgDisambiguated() { + return orgDisambiguated; + } + + public void setOrgDisambiguated(OrgDisambiguatedEntity orgDisambiguated) { + this.orgDisambiguated = orgDisambiguated; + } +} diff --git a/orcid-persistence/src/main/resources/META-INF/persistence.xml b/orcid-persistence/src/main/resources/META-INF/persistence.xml index a07674d680a..97491923958 100644 --- a/orcid-persistence/src/main/resources/META-INF/persistence.xml +++ b/orcid-persistence/src/main/resources/META-INF/persistence.xml @@ -60,6 +60,7 @@ org.orcid.persistence.jpa.entities.ResearchResourceItemEntity org.orcid.persistence.jpa.entities.FindMyStuffHistoryEntity org.orcid.persistence.jpa.entities.SpamEntity + org.orcid.persistence.jpa.entities.EmailDomainEntity org.orcid.persistence.jpa.entities.ClientDetailsEntity diff --git a/orcid-persistence/src/main/resources/db-master.xml b/orcid-persistence/src/main/resources/db-master.xml index d995b2a5b58..7949aeaa856 100644 --- a/orcid-persistence/src/main/resources/db-master.xml +++ b/orcid-persistence/src/main/resources/db-master.xml @@ -373,4 +373,5 @@ + \ No newline at end of file diff --git a/orcid-persistence/src/main/resources/db/updates/create_email_domain_mapping_tables.xml b/orcid-persistence/src/main/resources/db/updates/create_email_domain_mapping_tables.xml new file mode 100644 index 00000000000..02d6c9d413a --- /dev/null +++ b/orcid-persistence/src/main/resources/db/updates/create_email_domain_mapping_tables.xml @@ -0,0 +1,86 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + ALTER TABLE email_domain_to_org_id ADD CONSTRAINT email_domain_fk FOREIGN KEY (email_domian_id) REFERENCES email_domain (id); + ALTER TABLE email_domain_to_org_id ADD CONSTRAINT org_disambiguated_id_fk FOREIGN KEY (org_disambiguated_id) REFERENCES org_disambiguated (id); + create index email_domain_to_org_id_domain_index on email_domain_to_org_id(email_domian_id); + create index email_domain_to_org_id_org_index on email_domain_to_org_id(org_disambiguated_id); + + + + + + + + + + + + + + + + + + + + + + + + + create index email_domain_domain_index on email_domain(email_domain); + + + + GRANT SELECT ON email_domain to orcidro; + GRANT SELECT ON email_domain_to_org_id to orcidro; + + + \ No newline at end of file diff --git a/orcid-persistence/src/main/resources/orcid-persistence-context.xml b/orcid-persistence/src/main/resources/orcid-persistence-context.xml index 187ba4314b8..d55dba5c1c4 100644 --- a/orcid-persistence/src/main/resources/orcid-persistence-context.xml +++ b/orcid-persistence/src/main/resources/orcid-persistence-context.xml @@ -458,6 +458,12 @@ + + + + + + diff --git a/orcid-scheduler-web/src/main/java/org/orcid/scheduler/loader/cli/EmailDomainLoader.java b/orcid-scheduler-web/src/main/java/org/orcid/scheduler/loader/cli/EmailDomainLoader.java new file mode 100644 index 00000000000..30977c1abfe --- /dev/null +++ b/orcid-scheduler-web/src/main/java/org/orcid/scheduler/loader/cli/EmailDomainLoader.java @@ -0,0 +1,112 @@ +package org.orcid.scheduler.loader.cli; + +import java.io.FileReader; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.List; + +import org.orcid.core.common.manager.EmailDomainManager; +import org.orcid.persistence.jpa.entities.EmailDomainEntity; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +import com.fasterxml.jackson.databind.MappingIterator; +import com.fasterxml.jackson.dataformat.csv.CsvMapper; +import com.fasterxml.jackson.dataformat.csv.CsvParser; + +public class EmailDomainLoader { + + private static final Logger LOG = LoggerFactory.getLogger(EmailDomainLoader.class); + + private String filePath; + private EmailDomainManager emailDomainManager; + List> emailDomainData; + + List invalidDomains = new ArrayList(); + + public EmailDomainLoader(String filePath) { + this.filePath = filePath; + init(filePath); + } + + public void execute() throws IOException { + load(this.filePath); + process(); + } + + private void init(String filePath) { + Path path = Paths.get(filePath); + if(!Files.exists(path)) { + LOG.error("File does not exists: '{}'", filePath); + System.exit(1); + } + + @SuppressWarnings("resource") + ApplicationContext context = new ClassPathXmlApplicationContext("orcid-core-context.xml"); + emailDomainManager = (EmailDomainManager) context.getBean("emailDomainManager"); + } + + private void load(String filePath) throws IOException { + LOG.info("Reading file {}", filePath); + FileReader fileReader = new FileReader(filePath); + CsvMapper csvMapper = new CsvMapper(); + csvMapper.enable(CsvParser.Feature.WRAP_AS_ARRAY); + MappingIterator> it = csvMapper.readerForListOf(String.class).readValues(fileReader); + + if (it != null) { + emailDomainData = new ArrayList>(); + while(it.hasNext()) { + List r = it.next(); + emailDomainData.add(r); + } + } + } + + private void process() { + int total = 0; + int newEntities = 0; + int updatedEntities = 0; + LOG.info("Process started"); + if (emailDomainData != null) { + for (List row : emailDomainData) { + String elementDomain = row.get(0); + String elementCategory = row.get(1); + EmailDomainEntity ede = emailDomainManager.findByEmailDoman(elementDomain); + EmailDomainEntity.DomainCategory category = EmailDomainEntity.DomainCategory.valueOf(elementCategory.toUpperCase()); + if(ede == null) { + try { + EmailDomainEntity newEde = emailDomainManager.createEmailDomain(elementDomain, category); + newEntities += 1; + LOG.info("New EmailDomainEntity created for domain {} with id {}", elementDomain, newEde.getId()); + } catch(IllegalArgumentException iae) { + LOG.error("Invalid domain: {}", elementDomain); + invalidDomains.add(elementDomain); + } + } else if(!elementCategory.equalsIgnoreCase(ede.getCategory().toString())) { + boolean updated = emailDomainManager.updateCategory(ede.getId(), category); + if(updated) { + LOG.info("Email category has been update for email domain {} from {} to {}", elementDomain, ede.getCategory(), elementCategory); + updatedEntities += 1; + } + } + total += 1; + } + } + LOG.warn("List of invalid domains:"); + for(String invalidDomain : invalidDomains) { + LOG.warn(invalidDomain); + } + LOG.info("Process done, total: {}, new entities: {}, updated entities: {}", total, newEntities, updatedEntities); + } + + public static void main(String[] args) throws IOException { + String filePath = args[0]; + EmailDomainLoader edl = new EmailDomainLoader(filePath); + edl.execute(); + } +} diff --git a/orcid-web/src/main/java/org/orcid/frontend/web/controllers/EmailDomainController.java b/orcid-web/src/main/java/org/orcid/frontend/web/controllers/EmailDomainController.java new file mode 100644 index 00000000000..e42dd146931 --- /dev/null +++ b/orcid-web/src/main/java/org/orcid/frontend/web/controllers/EmailDomainController.java @@ -0,0 +1,45 @@ +package org.orcid.frontend.web.controllers; + +import javax.annotation.Resource; +import javax.ws.rs.core.MediaType; + +import org.orcid.core.common.manager.EmailDomainManager; +import org.orcid.core.utils.OrcidStringUtils; +import org.orcid.persistence.jpa.entities.EmailDomainEntity; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; + +@Controller("emailDomainController") +@RequestMapping(value = { "/email-domain" }) +public class EmailDomainController { + + @Resource + private EmailDomainManager emailDomainManager; + + @RequestMapping(value = "/find-category", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON) + public @ResponseBody ObjectNode findCategory(@RequestParam("domain") String domain) { + ObjectMapper mapper = new ObjectMapper(); + if(domain == null || domain.isBlank() || domain.length() > 254) { + ObjectNode response = mapper.createObjectNode(); + response.put("error", "domain lenght too long or invalid"); + return response; + } + domain = OrcidStringUtils.stripHtml(domain); + EmailDomainEntity ede = emailDomainManager.findByEmailDoman(domain); + if(ede == null) { + ObjectNode response = mapper.createObjectNode(); + response.put("category", EmailDomainEntity.DomainCategory.UNDEFINED.name()); + return response; + } else { + ObjectNode response = mapper.createObjectNode(); + response.put("category", ede.getCategory().name()); + return response; + } + } +} diff --git a/orcid-web/src/main/resources/orcid-frontend-security.xml b/orcid-web/src/main/resources/orcid-frontend-security.xml index 5cd19bc8878..d264b73f05e 100644 --- a/orcid-web/src/main/resources/orcid-frontend-security.xml +++ b/orcid-web/src/main/resources/orcid-frontend-security.xml @@ -419,7 +419,9 @@ + access="IS_AUTHENTICATED_ANONYMOUSLY" /> +