From 529c463279a4c8c06517c747936d2f2b38973928 Mon Sep 17 00:00:00 2001 From: amontenegro Date: Mon, 9 Oct 2023 15:52:43 -0600 Subject: [PATCH 1/7] Working on DB design and setup --- .../jpa/entities/EmailDomainEntity.java | 78 +++++++++++++++++ .../entities/EmailDomainToOrgIdEntity.java | 66 ++++++++++++++ .../src/main/resources/db-master.xml | 1 + .../create_email_domain_mapping_tables.xml | 86 +++++++++++++++++++ 4 files changed, 231 insertions(+) create mode 100644 orcid-persistence/src/main/java/org/orcid/persistence/jpa/entities/EmailDomainEntity.java create mode 100644 orcid-persistence/src/main/java/org/orcid/persistence/jpa/entities/EmailDomainToOrgIdEntity.java create mode 100644 orcid-persistence/src/main/resources/db/updates/create_email_domain_mapping_tables.xml 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..49631bd26a7 --- /dev/null +++ b/orcid-persistence/src/main/java/org/orcid/persistence/jpa/entities/EmailDomainEntity.java @@ -0,0 +1,78 @@ +package org.orcid.persistence.jpa.entities; + +import java.util.Objects; + +import javax.persistence.Column; +import javax.persistence.Entity; +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 enum DomainCategory {PERSONAL, PROFESSIONAL, UNDEFINED} + + private Long id; + private String emailDomain; + private DomainCategory 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; + } + + @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/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..0c5473be75f --- /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 From cd8d94b36a98580a0cbad25507a425bc6ec1ca50 Mon Sep 17 00:00:00 2001 From: amontenegro Date: Tue, 10 Oct 2023 15:02:43 -0600 Subject: [PATCH 2/7] Working on DAO's --- .../common/manager/EmailDomainManager.java | 15 ++++++ .../manager/impl/EmailDomainManagerImpl.java | 35 ++++++++++++ .../orcid/persistence/dao/EmailDomainDao.java | 15 ++++++ .../dao/impl/EmailDomainDaoImpl.java | 53 +++++++++++++++++++ .../jpa/entities/EmailDomainEntity.java | 2 +- 5 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 orcid-core/src/main/java/org/orcid/core/common/manager/EmailDomainManager.java create mode 100644 orcid-core/src/main/java/org/orcid/core/common/manager/impl/EmailDomainManagerImpl.java create mode 100644 orcid-persistence/src/main/java/org/orcid/persistence/dao/EmailDomainDao.java create mode 100644 orcid-persistence/src/main/java/org/orcid/persistence/dao/impl/EmailDomainDaoImpl.java 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..56170b77e14 --- /dev/null +++ b/orcid-core/src/main/java/org/orcid/core/common/manager/impl/EmailDomainManagerImpl.java @@ -0,0 +1,35 @@ +package org.orcid.core.common.manager.impl; + +import java.util.List; + +import org.orcid.core.common.manager.EmailDomainManager; +import org.orcid.persistence.jpa.entities.EmailDomainEntity; +import org.orcid.persistence.jpa.entities.EmailDomainEntity.DomainCategory; + +public class EmailDomainManagerImpl implements EmailDomainManager { + + @Override + public EmailDomainEntity createEmailDomain(String emailDomain, DomainCategory category) { + // TODO Auto-generated method stub + return null; + } + + @Override + public boolean updateCategory(long id, DomainCategory category) { + // TODO Auto-generated method stub + return false; + } + + @Override + public EmailDomainEntity findByEmailDoman(String emailDomain) { + // TODO Auto-generated method stub + return null; + } + + @Override + public List findByCategory(DomainCategory category) { + // TODO Auto-generated method stub + return null; + } + +} 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..b39aeae666d --- /dev/null +++ b/orcid-persistence/src/main/java/org/orcid/persistence/dao/impl/EmailDomainDaoImpl.java @@ -0,0 +1,53 @@ +package org.orcid.persistence.dao.impl; + +import java.util.List; + +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.EmailEntity; +import org.orcid.persistence.jpa.entities.EmailDomainEntity.DomainCategory; + +public class EmailDomainDaoImpl extends GenericDaoImpl implements EmailDomainDao { + + + public EmailDomainDaoImpl() { + super(EmailDomainEntity.class); + } + + @Override + public EmailDomainEntity createEmailDomain(String emailDomain, DomainCategory category) { + EmailDomainEntity e = new EmailDomainEntity(); + e.setEmailDomain(emailDomain); + e.setCategory(category); + entityManager.persist(e); + return e; + } + + @Override + public boolean updateCategory(long id, DomainCategory category) { + Query query = entityManager.createNativeQuery("UPDATE email_domain SET category=:category WHERE id = :id"); + query.setParameter("id", id); + query.setParameter("category", category); + 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); + List results = query.getResultList(); + return results.isEmpty() ? null : results.get(0); + } + + @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 index 49631bd26a7..79a168ca611 100644 --- 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 @@ -21,7 +21,7 @@ public class EmailDomainEntity extends BaseEntity { private static final long serialVersionUID = 7138838021634315502L; - public enum DomainCategory {PERSONAL, PROFESSIONAL, UNDEFINED} + public static enum DomainCategory {PERSONAL, PROFESSIONAL, UNDEFINED} private Long id; private String emailDomain; From 9fe037b0dc6775a1ab74708169f901c74a0b3582 Mon Sep 17 00:00:00 2001 From: amontenegro Date: Wed, 11 Oct 2023 14:49:06 -0600 Subject: [PATCH 3/7] Data loader now loads domain and category --- .../manager/impl/EmailDomainManagerImpl.java | 36 ++++-- .../src/main/resources/orcid-core-context.xml | 2 + .../dao/impl/EmailDomainDaoImpl.java | 22 ++-- .../jpa/entities/EmailDomainEntity.java | 3 + .../main/resources/META-INF/persistence.xml | 1 + .../resources/orcid-persistence-context.xml | 6 + .../loader/cli/EmailDomainLoader.java | 103 ++++++++++++++++++ 7 files changed, 158 insertions(+), 15 deletions(-) create mode 100644 orcid-scheduler-web/src/main/java/org/orcid/scheduler/loader/cli/EmailDomainLoader.java 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 index 56170b77e14..74942b54e91 100644 --- 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 @@ -2,34 +2,54 @@ 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; 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) { - // TODO Auto-generated method stub - return null; + if (emailDomain == null || emailDomain.isBlank()) { + throw new IllegalArgumentException("Email Domain must not be empty"); + } + if (category == null) { + throw new IllegalArgumentException("Category must not be empty"); + } + return emailDomainDao.createEmailDomain(emailDomain, category); } @Override public boolean updateCategory(long id, DomainCategory category) { - // TODO Auto-generated method stub - return false; + if (category == null) { + throw new IllegalArgumentException("Category must not be empty"); + } + return emailDomainDao.updateCategory(id, category); } @Override public EmailDomainEntity findByEmailDoman(String emailDomain) { - // TODO Auto-generated method stub - return null; + if (emailDomain == null || emailDomain.isBlank()) { + throw new IllegalArgumentException("Email Domain must not be empty"); + } + return emailDomainDaoReadOnly.findByEmailDoman(emailDomain); } @Override public List findByCategory(DomainCategory category) { - // TODO Auto-generated method stub - return null; + if (category == null) { + throw new IllegalArgumentException("Category must not be empty"); + } + return emailDomainDaoReadOnly.findByCategory(category); } } 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-persistence/src/main/java/org/orcid/persistence/dao/impl/EmailDomainDaoImpl.java b/orcid-persistence/src/main/java/org/orcid/persistence/dao/impl/EmailDomainDaoImpl.java index b39aeae666d..dd02050ba9f 100644 --- 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 @@ -2,22 +2,23 @@ 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.EmailEntity; import org.orcid.persistence.jpa.entities.EmailDomainEntity.DomainCategory; +import org.springframework.transaction.annotation.Transactional; public class EmailDomainDaoImpl extends GenericDaoImpl implements EmailDomainDao { - public EmailDomainDaoImpl() { super(EmailDomainEntity.class); } - + @Override + @Transactional public EmailDomainEntity createEmailDomain(String emailDomain, DomainCategory category) { EmailDomainEntity e = new EmailDomainEntity(); e.setEmailDomain(emailDomain); @@ -27,6 +28,7 @@ public EmailDomainEntity createEmailDomain(String emailDomain, DomainCategory ca } @Override + @Transactional public boolean updateCategory(long id, DomainCategory category) { Query query = entityManager.createNativeQuery("UPDATE email_domain SET category=:category WHERE id = :id"); query.setParameter("id", id); @@ -38,8 +40,15 @@ public boolean updateCategory(long id, DomainCategory category) { public EmailDomainEntity findByEmailDoman(String emailDomain) { TypedQuery query = entityManager.createQuery("from EmailDomainEntity where emailDomain = :emailDomain", EmailDomainEntity.class); query.setParameter("emailDomain", emailDomain); - List results = query.getResultList(); - return results.isEmpty() ? null : results.get(0); + try { + return query.getSingleResult(); + } catch(NoResultException nre) { + // Ignore this exception + } catch(Exception e) { + // Propagate any other exception + throw e; + } + return null; } @Override @@ -48,6 +57,5 @@ public List findByCategory(DomainCategory category) { 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 index 79a168ca611..628a1836b81 100644 --- 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 @@ -4,6 +4,8 @@ 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; @@ -50,6 +52,7 @@ public void setCategory(DomainCategory category) { this.category = category; } + @Enumerated(EnumType.STRING) @Column(name = "category") public DomainCategory getCategory() { return category; 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/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..10d9322e3fd --- /dev/null +++ b/orcid-scheduler-web/src/main/java/org/orcid/scheduler/loader/cli/EmailDomainLoader.java @@ -0,0 +1,103 @@ +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.annotation.JsonPropertyOrder; +import com.fasterxml.jackson.databind.MappingIterator; +import com.fasterxml.jackson.dataformat.csv.CsvMapper; +import com.fasterxml.jackson.dataformat.csv.CsvParser; +import com.fasterxml.jackson.dataformat.csv.CsvSchema; + +public class EmailDomainLoader { + + private static final Logger LOG = LoggerFactory.getLogger(EmailDomainLoader.class); + + private String filePath; + private EmailDomainManager emailDomainManager; + List> emailDomainData; + + 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) { + EmailDomainEntity newEde = emailDomainManager.createEmailDomain(elementDomain, category); + newEntities += 1; + LOG.info("New EmailDomainEntity created for domain {} with id {}", elementDomain, newEde.getId()); + } else if(!elementDomain.equalsIgnoreCase(ede.getCategory().name())) { + 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.info("Process done, total: {}, new entities: {}, updated entities: {}", total, newEntities, updatedEntities); + } + + public static void main(String[] args) throws IOException { + String filePath = "C:/Users/angel/Documents/ORCID/development/tmp/deleteme/email_domains/file.csv"; + EmailDomainLoader edl = new EmailDomainLoader(filePath); + edl.execute(); + } +} From 85d55766ae8b93a1116399453fd8a958db7ab7f2 Mon Sep 17 00:00:00 2001 From: amontenegro Date: Wed, 11 Oct 2023 15:50:08 -0600 Subject: [PATCH 4/7] Domain endpoing --- .../controllers/EmailDomainController.java | 37 +++++++++++++++++++ .../resources/orcid-frontend-security.xml | 4 +- 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 orcid-web/src/main/java/org/orcid/frontend/web/controllers/EmailDomainController.java 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..fefadda63dd --- /dev/null +++ b/orcid-web/src/main/java/org/orcid/frontend/web/controllers/EmailDomainController.java @@ -0,0 +1,37 @@ +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.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.google.common.net.InternetDomainName; + +@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 String findCategory(@RequestParam("domain") String domain) { + + if(domain == null || domain.length() > 64 || !InternetDomainName.isValid(domain)) { + return "{'error':'domain lenght too long or invalid'}"; + } + + EmailDomainEntity ede = emailDomainManager.findByEmailDoman(domain); + if(ede == null) { + return "{'category':'" + EmailDomainEntity.DomainCategory.UNDEFINED.name() + "'}"; + } else { + return "{'category':'" + ede.getCategory().name() + "'}"; + } + } +} 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" /> + Date: Thu, 12 Oct 2023 11:35:59 -0600 Subject: [PATCH 5/7] Improvements and unit testing --- .../manager/impl/EmailDomainManagerImpl.java | 5 ++ .../manager/EmailDomainManagerTest.java | 47 +++++++++++++++++++ .../dao/impl/EmailDomainDaoImpl.java | 8 +++- .../loader/cli/EmailDomainLoader.java | 6 +-- .../controllers/EmailDomainController.java | 4 +- 5 files changed, 62 insertions(+), 8 deletions(-) create mode 100644 orcid-core/src/test/java/org/orcid/core/common/manager/EmailDomainManagerTest.java 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 index 74942b54e91..b5950fb9f91 100644 --- 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 @@ -9,6 +9,8 @@ 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") @@ -22,6 +24,9 @@ public EmailDomainEntity createEmailDomain(String emailDomain, DomainCategory ca 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"); } 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..1c6b8d86baf --- /dev/null +++ b/orcid-core/src/test/java/org/orcid/core/common/manager/EmailDomainManagerTest.java @@ -0,0 +1,47 @@ +package org.orcid.core.common.manager; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.orcid.core.common.manager.impl.EmailDomainManagerImpl; +import org.orcid.persistence.dao.EmailDomainDao; +import org.orcid.test.TargetProxyHelper; + +public class EmailDomainManagerTest { + @Mock + private EmailDomainDao emailDomainDaoMock; + + @Mock + private EmailDomainDao emailDomainDaoReadOnlyMock; + + EmailDomainManager edm = new EmailDomainManagerImpl(); + + @Before + public void before(){ + TargetProxyHelper.injectIntoProxy(edm, "emailDomainDao", emailDomainDaoMock); + TargetProxyHelper.injectIntoProxy(edm, "emailDomainDaoReadOnly", emailDomainDaoReadOnlyMock); + } + + @Test + public void createEmailDomainTest() { + // Check null domain + // Check empty domain + // Check null category + // Check good value + } + + @Test + public void updateCategory() { + + } + + @Test + public void findByEmailDoman() { + + } + + @Test + public void findByCategory() { + + } +} 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 index dd02050ba9f..5a8416d1e3c 100644 --- 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 @@ -9,10 +9,14 @@ 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); } @@ -20,6 +24,7 @@ public EmailDomainDaoImpl() { @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); @@ -30,9 +35,10 @@ public EmailDomainEntity createEmailDomain(String emailDomain, DomainCategory ca @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); + query.setParameter("category", category.toString()); return query.executeUpdate() > 0; } 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 index 10d9322e3fd..79d2aa5c9e7 100644 --- 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 @@ -15,11 +15,9 @@ import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; -import com.fasterxml.jackson.annotation.JsonPropertyOrder; import com.fasterxml.jackson.databind.MappingIterator; import com.fasterxml.jackson.dataformat.csv.CsvMapper; import com.fasterxml.jackson.dataformat.csv.CsvParser; -import com.fasterxml.jackson.dataformat.csv.CsvSchema; public class EmailDomainLoader { @@ -82,7 +80,7 @@ private void process() { EmailDomainEntity newEde = emailDomainManager.createEmailDomain(elementDomain, category); newEntities += 1; LOG.info("New EmailDomainEntity created for domain {} with id {}", elementDomain, newEde.getId()); - } else if(!elementDomain.equalsIgnoreCase(ede.getCategory().name())) { + } 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); @@ -96,7 +94,7 @@ private void process() { } public static void main(String[] args) throws IOException { - String filePath = "C:/Users/angel/Documents/ORCID/development/tmp/deleteme/email_domains/file.csv"; + 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 index fefadda63dd..6d224f8f490 100644 --- 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 @@ -11,8 +11,6 @@ import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; -import com.google.common.net.InternetDomainName; - @Controller("emailDomainController") @RequestMapping(value = { "/email-domain" }) public class EmailDomainController { @@ -23,7 +21,7 @@ public class EmailDomainController { @RequestMapping(value = "/find-category", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON) public @ResponseBody String findCategory(@RequestParam("domain") String domain) { - if(domain == null || domain.length() > 64 || !InternetDomainName.isValid(domain)) { + if(domain == null || domain.length() > 64) { return "{'error':'domain lenght too long or invalid'}"; } From 47a67654b0fb0712ae192af055b665f551da620a Mon Sep 17 00:00:00 2001 From: amontenegro Date: Thu, 12 Oct 2023 13:51:50 -0600 Subject: [PATCH 6/7] More testing needed --- .../core/utils/cache/redis/RedisClient.java | 4 + .../manager/EmailDomainManagerTest.java | 121 +++++++++++++++--- .../jpa/entities/EmailDomainEntity.java | 8 ++ .../controllers/EmailDomainController.java | 10 +- 4 files changed, 123 insertions(+), 20 deletions(-) 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/test/java/org/orcid/core/common/manager/EmailDomainManagerTest.java b/orcid-core/src/test/java/org/orcid/core/common/manager/EmailDomainManagerTest.java index 1c6b8d86baf..bc347b9977f 100644 --- 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 @@ -1,10 +1,24 @@ 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 { @@ -13,35 +27,112 @@ public class EmailDomainManagerTest { @Mock private EmailDomainDao emailDomainDaoReadOnlyMock; - + EmailDomainManager edm = new EmailDomainManagerImpl(); - + @Before - public void 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() { - // Check null domain - // Check empty domain - // Check null category - // Check good value + 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 updateCategory() { - + public void updateCategoryTest() { + edm.updateCategory(0, DomainCategory.PERSONAL); + verify(emailDomainDaoMock, times(1)).updateCategory(eq(Long.valueOf(0)), eq(DomainCategory.PERSONAL)); } - @Test - public void findByEmailDoman() { - + @Test(expected = IllegalArgumentException.class) + public void findByEmailDoman_NullDomainTest() { + edm.findByEmailDoman(null); } + @Test(expected = IllegalArgumentException.class) + public void findByEmailDoman_EmptyDomainTest() { + edm.findByEmailDoman(" "); + } + @Test - public void findByCategory() { - + 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/jpa/entities/EmailDomainEntity.java b/orcid-persistence/src/main/java/org/orcid/persistence/jpa/entities/EmailDomainEntity.java index 628a1836b81..06ea938e449 100644 --- 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 @@ -29,7 +29,15 @@ public static enum DomainCategory {PERSONAL, PROFESSIONAL, UNDEFINED} 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") 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 index 6d224f8f490..6329bb16a36 100644 --- 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 @@ -4,6 +4,7 @@ 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; @@ -19,12 +20,11 @@ public class EmailDomainController { private EmailDomainManager emailDomainManager; @RequestMapping(value = "/find-category", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON) - public @ResponseBody String findCategory(@RequestParam("domain") String domain) { - - if(domain == null || domain.length() > 64) { + public @ResponseBody String findCategory(@RequestParam("domain") String domain) { + if(domain == null || domain.isBlank() || domain.length() > 64) { return "{'error':'domain lenght too long or invalid'}"; - } - + } + domain = OrcidStringUtils.stripHtml(domain); EmailDomainEntity ede = emailDomainManager.findByEmailDoman(domain); if(ede == null) { return "{'category':'" + EmailDomainEntity.DomainCategory.UNDEFINED.name() + "'}"; From cc97b8e62bbb8e043f75826547bb6cc2a4d4e312 Mon Sep 17 00:00:00 2001 From: amontenegro Date: Fri, 13 Oct 2023 07:40:51 -0600 Subject: [PATCH 7/7] Ready for first round of QA --- .../create_email_domain_mapping_tables.xml | 2 +- .../loader/cli/EmailDomainLoader.java | 17 +++++++++++--- .../controllers/EmailDomainController.java | 22 ++++++++++++++----- 3 files changed, 31 insertions(+), 10 deletions(-) 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 index 0c5473be75f..02d6c9d413a 100644 --- 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 @@ -14,7 +14,7 @@ - + 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 index 79d2aa5c9e7..30977c1abfe 100644 --- 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 @@ -27,6 +27,8 @@ public class EmailDomainLoader { private EmailDomainManager emailDomainManager; List> emailDomainData; + List invalidDomains = new ArrayList(); + public EmailDomainLoader(String filePath) { this.filePath = filePath; init(filePath); @@ -77,9 +79,14 @@ private void process() { EmailDomainEntity ede = emailDomainManager.findByEmailDoman(elementDomain); EmailDomainEntity.DomainCategory category = EmailDomainEntity.DomainCategory.valueOf(elementCategory.toUpperCase()); if(ede == null) { - EmailDomainEntity newEde = emailDomainManager.createEmailDomain(elementDomain, category); - newEntities += 1; - LOG.info("New EmailDomainEntity created for domain {} with id {}", elementDomain, newEde.getId()); + 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) { @@ -90,6 +97,10 @@ private void process() { 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); } 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 index 6329bb16a36..e42dd146931 100644 --- 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 @@ -12,6 +12,9 @@ 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 { @@ -20,16 +23,23 @@ public class EmailDomainController { private EmailDomainManager emailDomainManager; @RequestMapping(value = "/find-category", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON) - public @ResponseBody String findCategory(@RequestParam("domain") String domain) { - if(domain == null || domain.isBlank() || domain.length() > 64) { - return "{'error':'domain lenght too long or invalid'}"; + 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); + EmailDomainEntity ede = emailDomainManager.findByEmailDoman(domain); if(ede == null) { - return "{'category':'" + EmailDomainEntity.DomainCategory.UNDEFINED.name() + "'}"; + ObjectNode response = mapper.createObjectNode(); + response.put("category", EmailDomainEntity.DomainCategory.UNDEFINED.name()); + return response; } else { - return "{'category':'" + ede.getCategory().name() + "'}"; + ObjectNode response = mapper.createObjectNode(); + response.put("category", ede.getCategory().name()); + return response; } } }