From 4be39791b1d997ea71bb2ca401f73f122ae65ae9 Mon Sep 17 00:00:00 2001 From: Matija Obreza Date: Thu, 26 Oct 2017 23:59:51 +0200 Subject: [PATCH] Register Genesys as target for passport data for accessions with DOI - when upserted through the API --- pom.xml | 5 ++ .../service/impl/BatchRESTServiceImpl.java | 29 ++++++++- .../spring/config/ApplicationConfig.java | 4 +- .../genesys2/spring/config/GLISConfig.java | 62 +++++++++++++++++++ src/main/resources/application.properties | 6 ++ .../tests/resttests/AbstractRestTest.java | 6 ++ 6 files changed, 109 insertions(+), 3 deletions(-) create mode 100644 src/main/java/org/genesys2/spring/config/GLISConfig.java diff --git a/pom.xml b/pom.xml index d7dda06ca..30d15b548 100644 --- a/pom.xml +++ b/pom.xml @@ -532,6 +532,11 @@ liquibase-core ${liquibase.version} + + org.genesys-pgr + glis-client-resttemplate + 1.0-SNAPSHOT + diff --git a/src/main/java/org/genesys2/server/service/impl/BatchRESTServiceImpl.java b/src/main/java/org/genesys2/server/service/impl/BatchRESTServiceImpl.java index ad62a82a2..0f456c7bd 100644 --- a/src/main/java/org/genesys2/server/service/impl/BatchRESTServiceImpl.java +++ b/src/main/java/org/genesys2/server/service/impl/BatchRESTServiceImpl.java @@ -33,6 +33,10 @@ import com.fasterxml.jackson.databind.node.ObjectNode; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.Predicate; import org.apache.commons.lang.StringUtils; +import org.genesys.glis.v1.TermConstants; +import org.genesys.glis.v1.api.GenesysApi; +import org.genesys.glis.v1.model.UpdateTargets; +import org.genesys.glis.v1.model.UpdatedTarget; import org.genesys2.server.model.genesys.Accession; import org.genesys2.server.model.genesys.AccessionAlias; import org.genesys2.server.model.genesys.AccessionAlias.AliasType; @@ -99,6 +103,9 @@ public class BatchRESTServiceImpl implements BatchRESTService { @Autowired private TaxonomyManager taxonomyManager; + @Autowired + private GenesysApi glisGenesysApi; + @Override // Read-only, everything happens in manager @PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#institute, 'WRITE') or hasPermission(#institute, 'CREATE')") @@ -264,7 +271,7 @@ public class BatchRESTServiceImpl implements BatchRESTService { } updateDoi(accession, accnJson.get(Api1Constants.Accession.DOI)); - if (accession.getDoi()!=null) { + if (accession.getDoi() != null) { // Update ACCENUMB if DOI is provided accession.setAccessionName(dataJson.acceNumb); } @@ -586,6 +593,26 @@ public class BatchRESTServiceImpl implements BatchRESTService { // Update PDCI genesysService.updatePDCI(savedData.stream().map(a -> a.getId()).collect(Collectors.toSet())); + // Update GLIS + UpdateTargets targets = new UpdateTargets(); + targets.addKwsItem(TermConstants.PASSPORT_DATA); + savedData.stream().map(a -> a.getDoi()).filter(doi -> doi != null).forEach(doi -> targets.addDoisItem(doi)); + if (targets.getDois().size() > 0) { + try { + LOG.debug("Updating GLIS for {} accessions with DOIs", targets.getDois().size()); + List glisResponse = glisGenesysApi.registerGenesysAsTarget(targets); + glisResponse.forEach(updatedTarget -> { + if (StringUtils.equals("OK", updatedTarget.getResult())) { + LOG.trace("GLIS for {} result={}", updatedTarget.getDoi(), updatedTarget.getResult()); + } else if (StringUtils.equals("KO", updatedTarget.getResult())) { + LOG.info("GLIS for {} result={} msg={}", updatedTarget.getDoi(), updatedTarget.getResult()); + } + }); + } catch (Throwable e) { + LOG.error("Error updating GLIS targets: {}", e.getMessage(), e); + } + } + return upsertResponses; } diff --git a/src/main/java/org/genesys2/spring/config/ApplicationConfig.java b/src/main/java/org/genesys2/spring/config/ApplicationConfig.java index 005ebaacd..b73304be9 100644 --- a/src/main/java/org/genesys2/spring/config/ApplicationConfig.java +++ b/src/main/java/org/genesys2/spring/config/ApplicationConfig.java @@ -40,10 +40,10 @@ import org.springframework.core.io.Resource; @Configuration @Import({ CommonConfig.class, SchedulerConfig.class, DatabaseConfig.class, MailConfig.class, OAuth2ServerConfig.class, SecurityConfig.class, CacheConfig.class, - ElasticsearchConfig.class, FileRepositoryConfig.class, WebConfiguration.class, AuditConfig.class }) + ElasticsearchConfig.class, FileRepositoryConfig.class, WebConfiguration.class, AuditConfig.class, GLISConfig.class }) public class ApplicationConfig { public static final Logger LOG = LoggerFactory.getLogger(ApplicationConfig.class); - + @Bean public TransifexService transifexService() { return new TransifexServiceImpl(); diff --git a/src/main/java/org/genesys2/spring/config/GLISConfig.java b/src/main/java/org/genesys2/spring/config/GLISConfig.java new file mode 100644 index 000000000..374db76ba --- /dev/null +++ b/src/main/java/org/genesys2/spring/config/GLISConfig.java @@ -0,0 +1,62 @@ +/* + * Copyright 2017 Global Crop Diversity Trust + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.genesys2.spring.config; + +import org.genesys.glis.v1.GlisRateLimiter; +import org.genesys.glis.v1.invoker.ApiClient; +import org.genesys.glis.v1.invoker.auth.HttpBasicAuth; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +@Configuration +// Get the beans! +@ComponentScan(basePackages = { "org.genesys.glis.v1.api" }) +public class GLISConfig { + + @Value("${itpgrfa.glis.basepath}") + private String basePath; + + @Value("${itpgrfa.glis.username}") + private String username; + + @Value("${itpgrfa.glis.password}") + private String password; + + @Value("${itpgrfa.glis.ratelimit}") + private double glisRateLimit; + + @Bean + public GlisRateLimiter glisRateLimiter() { + return new GlisRateLimiter(glisRateLimit); + } + + @Bean + public ApiClient glisClient() { + ApiClient client = new ApiClient(); + client.setDebugging("true".equals(System.getenv("GLIS_DEBUG"))); + client.setBasePath(basePath); + + // Configure HTTP basic authorization: glis_auth + HttpBasicAuth easySmtaAuth = (HttpBasicAuth) client.getAuthentication("easySmta"); + easySmtaAuth.setUsername(username); + easySmtaAuth.setPassword(password); + + System.err.println("GLIS at " + client.getBasePath() + " with username=" + username); + return client; + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 745d00a75..14c07b3a2 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -191,3 +191,9 @@ repository.ftp.keystore.password=changeit! repository.ftp.port=8021 repository.ftp.passivePorts=2300-2350 repository.ftp.externalAddress= + +# GLIS +itpgrfa.glis.basepath=https://glistest.planttreaty.org +itpgrfa.glis.username= +itpgrfa.glis.password= +itpgrfa.glis.ratelimit=20 diff --git a/src/test/java/org/genesys2/tests/resttests/AbstractRestTest.java b/src/test/java/org/genesys2/tests/resttests/AbstractRestTest.java index af3a52a18..11f59dd0c 100644 --- a/src/test/java/org/genesys2/tests/resttests/AbstractRestTest.java +++ b/src/test/java/org/genesys2/tests/resttests/AbstractRestTest.java @@ -50,6 +50,7 @@ import org.genesys.filerepository.service.impl.FilesystemStorageServiceImpl; import org.genesys.filerepository.service.impl.ImageGalleryServiceImpl; import org.genesys.filerepository.service.impl.RepositoryServiceImpl; import org.genesys.filerepository.service.impl.ThumbnailGenerator1; +import org.genesys.glis.v1.api.GenesysApi; import org.genesys2.brapi.service.impl.BrAPIServiceImpl; import org.genesys2.server.aspect.AsAdminAspect; import org.genesys2.server.aspect.GenesysImageGalleryAspects; @@ -396,6 +397,11 @@ public abstract class AbstractRestTest extends BaseSpringTest { public BatchRESTService batchRESTService() { return new BatchRESTServiceImpl(); } + + @Bean + public GenesysApi glisGenesysApi() { + return new GenesysApi(); + } @Bean public TaxonomyManager taxonomyManager() { -- GitLab