From 5162de286c86e36f3c0106de5a010998ee57b714 Mon Sep 17 00:00:00 2001 From: amontenegro Date: Fri, 20 Oct 2023 14:01:14 -0600 Subject: [PATCH] Endpoint to fetch org info is done, working on unit testing --- .../orcid/core/solr/OrcidSolrOrgsClient.java | 17 ++++++++++- .../controllers/EmailDomainController.java | 29 ++++++++----------- .../resources/orcid-frontend-security.xml | 4 ++- 3 files changed, 31 insertions(+), 19 deletions(-) diff --git a/orcid-core/src/main/java/org/orcid/core/solr/OrcidSolrOrgsClient.java b/orcid-core/src/main/java/org/orcid/core/solr/OrcidSolrOrgsClient.java index e30eaf806fb..be4c57fa21a 100644 --- a/orcid-core/src/main/java/org/orcid/core/solr/OrcidSolrOrgsClient.java +++ b/orcid-core/src/main/java/org/orcid/core/solr/OrcidSolrOrgsClient.java @@ -33,7 +33,7 @@ public class OrcidSolrOrgsClient { private static final String SOLR_SELF_SERVICE_ORGS_QUERY = "(org-disambiguated-id-from-source:%s)^50.0 (org-disambiguated-name%s)^50.0 (org-disambiguated-name-string:%s)^25.0"; - + private static final String SOLR_ORG_BY_ROR_ID_QUERY = "org-disambiguated-id-from-source:%s"; public OrgDisambiguatedSolrDocument findById(Long id) { SolrQuery query = new SolrQuery(); @@ -93,4 +93,19 @@ public List getOrgsForSelfService(String searchTer throw new NonTransientDataAccessResourceException(errorMessage, se); } } + + public OrgDisambiguatedSolrDocument getOrgByRorId(String rorId) { + SolrQuery query = new SolrQuery(); + // Escape the : on the email domain to be able to search in solr + query.setQuery(SOLR_ORG_BY_ROR_ID_QUERY.replace("%s", rorId.replace(":", "\\:"))); + query.addOrUpdateSort("score", ORDER.desc); + try { + QueryResponse queryResponse = solrReadOnlyOrgsClient.query(query); + List result = queryResponse.getBeans(OrgDisambiguatedSolrDocument.class); + return (result == null || result.isEmpty()) ? null : result.get(0); + } catch (SolrServerException | IOException se) { + String errorMessage = MessageFormat.format("Error when attempting to search for orgs by ror id, with ror id {0}", new Object[] { rorId }); + throw new NonTransientDataAccessResourceException(errorMessage, se); + } + } } 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 9a79e493f88..b4626ba8551 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 @@ -1,15 +1,13 @@ package org.orcid.frontend.web.controllers; -import java.util.List; - import javax.annotation.Resource; import javax.ws.rs.core.MediaType; import org.orcid.core.common.manager.EmailDomainManager; -import org.orcid.core.manager.OrgDisambiguatedManager; +import org.orcid.core.solr.OrcidSolrOrgsClient; import org.orcid.core.utils.OrcidStringUtils; import org.orcid.persistence.jpa.entities.EmailDomainEntity; -import org.orcid.pojo.OrgDisambiguated; +import org.orcid.utils.solr.entities.OrgDisambiguatedSolrDocument; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @@ -27,7 +25,7 @@ public class EmailDomainController { private EmailDomainManager emailDomainManager; @Resource - private OrgDisambiguatedManager orgDisambiguatedManager; + private OrcidSolrOrgsClient orcidSolrOrgsClient; @RequestMapping(value = "/find-category", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON) public @ResponseBody ObjectNode findCategory(@RequestParam("domain") String domain) { @@ -62,19 +60,16 @@ public class EmailDomainController { EmailDomainEntity ede = emailDomainManager.findByEmailDoman(domain); if(ede != null) { - String emailDomain = ede.getEmailDomain(); - if(emailDomain != null && !emailDomain.isBlank()) { - // Escape the : on the email domain to be able to search in solr - emailDomain = emailDomain.replace(":", "\\:"); - String searchTerm = "org-disambiguated-id-from-source:" + emailDomain; - List orgsInfo = orgDisambiguatedManager.searchOrgsFromSolr(searchTerm, 0, 1, false); - if(orgsInfo != null && !orgsInfo.isEmpty()) { + String rorId = ede.getRorId(); + if(rorId != null && !rorId.isBlank()) { + OrgDisambiguatedSolrDocument orgInfo = orcidSolrOrgsClient.getOrgByRorId(rorId); + if(orgInfo != null) { // Pick the first result - OrgDisambiguated firstOrg = orgsInfo.get(0); - response.put("ROR", domain); - response.put("Org Name", firstOrg.getValue()); - response.put("Country", firstOrg.getCountry()); - response.put("City", firstOrg.getCity()); + response.put("Domain", domain); + response.put("ROR", rorId); + response.put("Org Name", orgInfo.getOrgDisambiguatedName()); + response.put("Country", orgInfo.getOrgDisambiguatedCountry()); + response.put("City", orgInfo.getOrgDisambiguatedCity()); } } } diff --git a/orcid-web/src/main/resources/orcid-frontend-security.xml b/orcid-web/src/main/resources/orcid-frontend-security.xml index d264b73f05e..42e2fd62ab0 100644 --- a/orcid-web/src/main/resources/orcid-frontend-security.xml +++ b/orcid-web/src/main/resources/orcid-frontend-security.xml @@ -421,7 +421,9 @@ + access="IS_AUTHENTICATED_ANONYMOUSLY" /> +