diff --git a/apps/scholexplorer-api/pom.xml b/apps/scholexplorer-api/pom.xml index 58eb06ea..687a6c1e 100644 --- a/apps/scholexplorer-api/pom.xml +++ b/apps/scholexplorer-api/pom.xml @@ -14,12 +14,10 @@ scholexplorer-api - org.springframework.data spring-data-elasticsearch - org.springframework.boot spring-boot-starter-test @@ -29,15 +27,11 @@ eu.dnetlib.dhp dhp-schemas - org.apache.commons commons-pool2 - - - diff --git a/apps/scholexplorer-api/src/main/java/eu/dnetlib/scholix/api/controller/DatasourceV1.java b/apps/scholexplorer-api/src/main/java/eu/dnetlib/scholix/api/controller/DatasourceV1.java index 684ec92f..87616b2b 100644 --- a/apps/scholexplorer-api/src/main/java/eu/dnetlib/scholix/api/controller/DatasourceV1.java +++ b/apps/scholexplorer-api/src/main/java/eu/dnetlib/scholix/api/controller/DatasourceV1.java @@ -1,16 +1,22 @@ package eu.dnetlib.scholix.api.controller; import eu.dnetlib.common.controller.AbstractDnetController; +import eu.dnetlib.scholix.api.ScholixException; +import eu.dnetlib.scholix.api.index.ScholixIndexManager; import eu.dnetlib.scholix.api.model.v1.LinkPublisher; import io.micrometer.core.annotation.Timed; import io.swagger.annotations.Api; import io.swagger.v3.oas.annotations.Operation; +import org.apache.commons.lang3.tuple.Pair; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.stream.Collectors; @RestController @@ -20,16 +26,24 @@ import java.util.List; }) public class DatasourceV1 extends AbstractDnetController { + @Autowired + private ScholixIndexManager manager; + @Timed(value = "scholix.v1.datasources", description = "Time taken to return all datasources on Version 1.0 of Scholix") @Operation( summary = "Get all Datasources", description = "returns a list of all datasources") @GetMapping("/listDatasources") - public List getDatasources() { - return Arrays.asList( - new LinkPublisher().name("pippo").totalRelationships(30), - new LinkPublisher().name("pluto").totalRelationships(30), - new LinkPublisher().name("peppa").totalRelationships(30) - ); + public List getDatasources() throws ScholixException { + + + final List> result = manager.totalLinksByProvider(null); + + if (result == null) + return new ArrayList<>(); + + return result.stream().map(p -> new LinkPublisher().name(p.getKey()).totalRelationships(p.getValue().intValue())).collect(Collectors.toList()); + + } } diff --git a/apps/scholexplorer-api/src/main/java/eu/dnetlib/scholix/api/controller/LinkProviderV2.java b/apps/scholexplorer-api/src/main/java/eu/dnetlib/scholix/api/controller/LinkProviderV2.java index 73014644..286e0eb9 100644 --- a/apps/scholexplorer-api/src/main/java/eu/dnetlib/scholix/api/controller/LinkProviderV2.java +++ b/apps/scholexplorer-api/src/main/java/eu/dnetlib/scholix/api/controller/LinkProviderV2.java @@ -14,7 +14,6 @@ import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; @@ -36,7 +35,7 @@ public class LinkProviderV2 { @Parameter(in = ParameterIn.QUERY, description = "Filter the link provider name") @RequestParam(required = false) String name ) throws ScholixException { - List> result = manager.linksByProvider(null); + List> result = manager.totalLinksByProvider(name); if (result==null) return new ArrayList<>(); diff --git a/apps/scholexplorer-api/src/main/java/eu/dnetlib/scholix/api/controller/LinkPublisherV2.java b/apps/scholexplorer-api/src/main/java/eu/dnetlib/scholix/api/controller/LinkPublisherV2.java index 41883343..88e2e6a6 100644 --- a/apps/scholexplorer-api/src/main/java/eu/dnetlib/scholix/api/controller/LinkPublisherV2.java +++ b/apps/scholexplorer-api/src/main/java/eu/dnetlib/scholix/api/controller/LinkPublisherV2.java @@ -1,16 +1,21 @@ package eu.dnetlib.scholix.api.controller; +import eu.dnetlib.scholix.api.ScholixException; +import eu.dnetlib.scholix.api.index.ScholixIndexManager; import eu.dnetlib.scholix.api.model.v2.LinkProviderType; import io.swagger.annotations.Api; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.enums.ParameterIn; +import org.apache.commons.lang3.tuple.Pair; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; -import java.util.Arrays; +import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; @RestController @RequestMapping("/v2/LinkPublisher") @@ -19,6 +24,9 @@ import java.util.List; }) public class LinkPublisherV2 { + @Autowired + ScholixIndexManager manager; + @Operation( summary = "Get All Publishers that provide source object", description = "Return a List of all Publishers that provide source objects in Scholix " + @@ -26,12 +34,13 @@ public class LinkPublisherV2 { @GetMapping("/inSource") public List getInSource( @Parameter(in = ParameterIn.QUERY, description = "Filter the link publisher name") @RequestParam(required = false) String name - ) { - return Arrays.asList( - new LinkProviderType().name("pippo").totalRelationships(30), - new LinkProviderType().name("pluto").totalRelationships(30), - new LinkProviderType().name("peppa").totalRelationships(30) - ); + ) throws ScholixException { + List> result = manager.totalLinksPublisher(ScholixIndexManager.RelationPrefix.source,name); + + if (result==null) + return new ArrayList<>(); + + return result.stream().map(s -> new LinkProviderType().name(s.getLeft()).totalRelationships(s.getValue().intValue())).collect(Collectors.toList()); } @Operation( @@ -41,11 +50,12 @@ public class LinkPublisherV2 { @GetMapping("/inTarget") public List getInTarget( @Parameter(in = ParameterIn.QUERY, description = "Filter the link publisher name") @RequestParam(required = false) String name - ) { - return Arrays.asList( - new LinkProviderType().name("pippo").totalRelationships(30), - new LinkProviderType().name("pluto").totalRelationships(30), - new LinkProviderType().name("peppa").totalRelationships(30) - ); + ) throws ScholixException { + List> result = manager.totalLinksPublisher(ScholixIndexManager.RelationPrefix.target,name); + + if (result==null) + return new ArrayList<>(); + + return result.stream().map(s -> new LinkProviderType().name(s.getLeft()).totalRelationships(s.getValue().intValue())).collect(Collectors.toList()); } } diff --git a/apps/scholexplorer-api/src/main/java/eu/dnetlib/scholix/api/controller/ScholixControllerV1.java b/apps/scholexplorer-api/src/main/java/eu/dnetlib/scholix/api/controller/ScholixControllerV1.java index b63d7c2f..b81d0e87 100644 --- a/apps/scholexplorer-api/src/main/java/eu/dnetlib/scholix/api/controller/ScholixControllerV1.java +++ b/apps/scholexplorer-api/src/main/java/eu/dnetlib/scholix/api/controller/ScholixControllerV1.java @@ -2,36 +2,42 @@ package eu.dnetlib.scholix.api.controller; import eu.dnetlib.common.controller.AbstractDnetController; +import eu.dnetlib.dhp.schema.sx.scholix.Scholix; +import eu.dnetlib.scholix.api.ScholixException; +import eu.dnetlib.scholix.api.index.ScholixIndexManager; import eu.dnetlib.scholix.api.model.v1.ScholixV1; +import io.micrometer.core.annotation.Timed; import io.swagger.annotations.Api; -import io.swagger.annotations.ApiModelProperty; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.enums.ParameterIn; import io.swagger.v3.oas.annotations.media.Schema; +import org.apache.commons.lang3.tuple.Pair; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; - -import javax.validation.Valid; import javax.validation.constraints.NotNull; import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; @RestController @RequestMapping("/v1") -@Api(tags = { - "Scholix" -}) +@Api(tags = {"Scholix"}) public class ScholixControllerV1 extends AbstractDnetController { + @Autowired + ScholixIndexManager manager; + @Operation( summary = "Get all Scholix relation collected from a publisher", description = "return a list of scholix object published from a specific publisher" ) @GetMapping("/linksFromPublisher") + @Timed(value = "scholix.v1.linksFromPublisher", description = "Time taken to return links on Version 1.0 of Scholix collected from a publisher") public List linksFromPublisher( @Parameter( in = ParameterIn.QUERY, @@ -39,9 +45,17 @@ public class ScholixControllerV1 extends AbstractDnetController { schema = @Schema(), required = true) String publisher, @Parameter(in = ParameterIn.QUERY, description = "The page number") @RequestParam(required = false) Integer page - ) { + ) throws ScholixException { - return new ArrayList<>(); + final int currentPage = page != null ? page : 0; + + Pair> scholixResult =manager.linksFromPid(null,null,null,publisher, + null,null,null,null,null,null, currentPage + ); + List scholixData = scholixResult.getValue(); + if (scholixData== null) + return null; + return scholixData.stream().map(ScholixV1::fromScholix).collect(Collectors.toList()); } @Operation( @@ -49,6 +63,7 @@ public class ScholixControllerV1 extends AbstractDnetController { description = "return a list of scholix object collected from a specific datasource" ) @GetMapping("/linksFromDatasource") + @Timed(value = "scholix.v1.linksFromDatasource", description = "Time taken to return links on Version 1.0 of Scholix collected from a LinkProvider") public List linksFromDatasource( @Parameter( in = ParameterIn.QUERY, @@ -56,9 +71,16 @@ public class ScholixControllerV1 extends AbstractDnetController { schema = @Schema()) @NotNull String datasource, @Parameter(in = ParameterIn.QUERY, description = "The page number") @RequestParam(required = false) Integer page - ) { + ) throws ScholixException { - return new ArrayList<>(); + final int currentPage = page != null ? page : 0; + Pair> scholixResult =manager.linksFromPid(datasource,null,null,null, + null,null,null,null,null,null, currentPage + ); + List scholixData = scholixResult.getValue(); + if (scholixData== null) + return null; + return scholixData.stream().map(ScholixV1::fromScholix).collect(Collectors.toList()); } @@ -67,6 +89,7 @@ public class ScholixControllerV1 extends AbstractDnetController { description = "The linksFromPid endpoint returns a list of scholix object related from a specific persistent identifier" ) @GetMapping("/linksFromPid") + @Timed(value = "scholix.v1.linksFromPid", description = "Time taken to return links on Version 1.0 of Scholix related from a specific persistent identifier") public List linksFromPid( @Parameter(in = ParameterIn.QUERY, description = "persistent Identifier") @NotNull String pid, @Parameter(in = ParameterIn.QUERY, description = "Persistent Identifier Type") @RequestParam(required = false) String pidType, @@ -74,9 +97,16 @@ public class ScholixControllerV1 extends AbstractDnetController { @Parameter(in = ParameterIn.QUERY, description = "a datasource provenance filter of the target relation") @RequestParam(required = false) String datasourceTarget, @Parameter(in = ParameterIn.QUERY, description = "The page number") @RequestParam(required = false) Integer page - ) { + ) throws ScholixException { - return new ArrayList<>(); + final int currentPage = page != null ? page : 0; + Pair> scholixResult =manager.linksFromPid(datasourceTarget,null,null,null, + typologyTarget,pid,pidType,null,null,null, currentPage + ); + List scholixData = scholixResult.getValue(); + if (scholixData== null) + return null; + return scholixData.stream().map(ScholixV1::fromScholix).collect(Collectors.toList()); } diff --git a/apps/scholexplorer-api/src/main/java/eu/dnetlib/scholix/api/controller/ScholixControllerV2.java b/apps/scholexplorer-api/src/main/java/eu/dnetlib/scholix/api/controller/ScholixControllerV2.java index b54a65cb..895d50c2 100644 --- a/apps/scholexplorer-api/src/main/java/eu/dnetlib/scholix/api/controller/ScholixControllerV2.java +++ b/apps/scholexplorer-api/src/main/java/eu/dnetlib/scholix/api/controller/ScholixControllerV2.java @@ -2,7 +2,7 @@ package eu.dnetlib.scholix.api.controller; import eu.dnetlib.common.controller.AbstractDnetController; -import eu.dnetlib.dhp.schema.sx.scholix.ScholixIdentifier; +import eu.dnetlib.dhp.schema.sx.scholix.Scholix; import eu.dnetlib.scholix.api.ScholixAPIVersion; import eu.dnetlib.scholix.api.ScholixException; import eu.dnetlib.scholix.api.index.ScholixIndexManager; @@ -13,29 +13,13 @@ import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.enums.ParameterIn; -import eu.dnetlib.dhp.schema.sx.scholix.Scholix; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.tuple.Pair; -import org.apache.lucene.search.join.ScoreMode; -import org.elasticsearch.index.query.BoolQueryBuilder; -import org.elasticsearch.index.query.NestedQueryBuilder; -import org.elasticsearch.index.query.TermQueryBuilder; -import org.springframework.data.domain.PageRequest; -import org.springframework.data.domain.Pageable; -import org.springframework.data.domain.Sort; -import org.springframework.data.elasticsearch.core.query.NativeSearchQuery; -import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder; -import org.springframework.data.elasticsearch.core.query.Query; -import org.elasticsearch.index.query.QueryBuilders; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.elasticsearch.core.ElasticsearchOperations; -import org.springframework.data.elasticsearch.core.SearchHits; -import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; -import java.util.Collections; import java.util.List; import java.util.stream.Collectors; @@ -49,9 +33,6 @@ public class ScholixControllerV2 extends AbstractDnetController { @Autowired private ScholixIndexManager manager; - - - @Timed(value = "scholix.v2.links", description = "Time taken to return links on Version 2.0 of Scholix") @ApiOperation("Get Scholix Links") @GetMapping("/Links") @@ -84,7 +65,7 @@ public class ScholixControllerV2 extends AbstractDnetController { try { final int currentPage = page != null ? page : 0; - Pair> scholixResult = manager.linksFromPid(ScholixAPIVersion.V2, linkProvider, targetPid, targetPidType, targetPublisher, targetType, sourcePid, sourcePidType, sourcePublisher, sourceType, harvestedAfter, currentPage); + Pair> scholixResult = manager.linksFromPid( linkProvider, targetPid, targetPidType, targetPublisher, targetType, sourcePid, sourcePidType, sourcePublisher, sourceType, harvestedAfter, currentPage); final PageResultType pageResult = new PageResultType(); pageResult.setTotalPages(scholixResult.getLeft().intValue() / 10); pageResult.setTotalLinks(scholixResult.getLeft().intValue()); @@ -94,7 +75,4 @@ public class ScholixControllerV2 extends AbstractDnetController { throw new ScholixException("Error on requesting url ", e); } } - - - } diff --git a/apps/scholexplorer-api/src/main/java/eu/dnetlib/scholix/api/index/ScholixIndexManager.java b/apps/scholexplorer-api/src/main/java/eu/dnetlib/scholix/api/index/ScholixIndexManager.java index f93f8d02..d1f8b54a 100644 --- a/apps/scholexplorer-api/src/main/java/eu/dnetlib/scholix/api/index/ScholixIndexManager.java +++ b/apps/scholexplorer-api/src/main/java/eu/dnetlib/scholix/api/index/ScholixIndexManager.java @@ -61,7 +61,7 @@ public class ScholixIndexManager { /** * The enum Pid type prefix. */ - enum pidTypePrefix { + public enum RelationPrefix { /** * Source pid type prefix. */ @@ -73,7 +73,7 @@ public class ScholixIndexManager { } - private QueryBuilder createObjectTypeQuery(final pidTypePrefix prefix, final String objectType ) throws ScholixException{ + private QueryBuilder createObjectTypeQuery(final RelationPrefix prefix, final String objectType ) throws ScholixException{ if (prefix == null){ throw new ScholixException("prefix cannot be null"); } @@ -81,7 +81,7 @@ public class ScholixIndexManager { } - private QueryBuilder createPidTypeQuery(final pidTypePrefix prefix, final String pidTypeValue ) throws ScholixException{ + private QueryBuilder createPidTypeQuery(final RelationPrefix prefix, final String pidTypeValue ) throws ScholixException{ if (prefix == null){ throw new ScholixException("prefix cannot be null"); } @@ -89,7 +89,21 @@ public class ScholixIndexManager { } - private QueryBuilder createPidValueQuery(final pidTypePrefix prefix, final String pidValue ) throws ScholixException{ + private QueryBuilder createLinkProviderQuery(final String providerName ) throws ScholixException{ + if (providerName == null){ + throw new ScholixException("prefix cannot be null"); + } + return new NestedQueryBuilder("linkprovider", new TermQueryBuilder("linkprovider.name",providerName), ScoreMode.None); + } + + private QueryBuilder createLinkPublisherQuery(final RelationPrefix prefix, final String publisher ) throws ScholixException{ + if (prefix == null){ + throw new ScholixException("prefix cannot be null"); + } + return new NestedQueryBuilder(String.format("%s.publisher", prefix), new TermQueryBuilder(String.format("%s.publisher.name",prefix), publisher), ScoreMode.None); + } + + private QueryBuilder createPidValueQuery(final RelationPrefix prefix, final String pidValue ) throws ScholixException{ if (prefix == null){ throw new ScholixException("prefix cannot be null"); } @@ -116,7 +130,7 @@ public class ScholixIndexManager { } - private void incrementPidCounter(pidTypePrefix prefix, String value) { + private void incrementPidCounter(RelationPrefix prefix, String value) { switch (value.toLowerCase()){ case "doi": { myCounter.increment(String.format("%s_doi", prefix)); @@ -132,10 +146,13 @@ public class ScholixIndexManager { } - public List> linksByProvider(final String filterName) throws ScholixException { + public List> totalLinksByProvider(final String filterName) throws ScholixException { + + + final QueryBuilder query = StringUtils.isNoneBlank(filterName)?createLinkProviderQuery(filterName):QueryBuilders.matchAllQuery(); final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder() - .withQuery(QueryBuilders.matchAllQuery()) + .withQuery(query) .withSearchType(SearchType.DEFAULT) .withPageable(PageRequest.of(0,10)) .addAggregation(AggregationBuilders.nested("nested", "linkprovider") @@ -148,7 +165,41 @@ public class ScholixIndexManager { final SearchHits hits = client.search(searchQuery, Scholix.class, IndexCoordinates.of(elasticSearchProperties.getIndexName())); final Aggregations aggregations = hits.getAggregations(); + connectionPool.returnResource(resource); + if(aggregations == null) + return null; + + final Aggregation aggByMap = ((ParsedNested) aggregations.asMap().get("nested")).getAggregations().asMap().get("by_map"); + + + return ((ParsedStringTerms) aggByMap).getBuckets() + .stream() + .map(b -> new ImmutablePair<>(b.getKeyAsString(), b.getDocCount())) + .collect(Collectors.toList()); + } + + + public List> totalLinksPublisher(final RelationPrefix prefix, final String filterName) throws ScholixException { + + + final QueryBuilder query = StringUtils.isNoneBlank(filterName)?createLinkPublisherQuery(prefix,filterName):QueryBuilders.matchAllQuery(); + + final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder() + .withQuery(query) + .withSearchType(SearchType.DEFAULT) + .withPageable(PageRequest.of(0,10)) + .addAggregation(AggregationBuilders.nested("nested", String.format("%s.publisher", prefix )) + .subAggregation(AggregationBuilders.terms("by_map").field(String.format("%s.publisher.name", prefix )).size(100).minDocCount(1))) + .build(); + + + Pair resource = connectionPool.getResource(); + ElasticsearchRestTemplate client = resource.getValue(); + final SearchHits hits = client.search(searchQuery, Scholix.class, IndexCoordinates.of(elasticSearchProperties.getIndexName())); + + final Aggregations aggregations = hits.getAggregations(); + connectionPool.returnResource(resource); if(aggregations == null) return null; @@ -171,7 +222,6 @@ public class ScholixIndexManager { /** * Links from pid pair. * - * @param outputVersion the output version * @param linkProvider the link provider * @param targetPid the target pid * @param targetPidType the target pid type @@ -187,51 +237,61 @@ public class ScholixIndexManager { * @throws ScholixException the scholix exception */ @Timed(value = "scholix.index.request.links", description = "Time taken to request index") - public Pair> linksFromPid (final ScholixAPIVersion outputVersion, final String linkProvider, + public Pair> linksFromPid ( final String linkProvider, final String targetPid, final String targetPidType, final String targetPublisher, final String targetType, final String sourcePid, final String sourcePidType, final String sourcePublisher, final String sourceType, final String harvestedAfter, final Integer page) throws ScholixException { - if(outputVersion == null) - throw new ScholixException("Error outputVersion not be empty"); + if (sourcePid==null && sourcePidType==null && targetPid==null && targetPidType==null && sourcePublisher==null && targetPublisher==null && linkProvider==null) throw new ScholixException("One of sourcePid, targetPid, sourcePublisher, targetPublisher, linkProvider should be not null"); final List queries = new ArrayList<>(); + if (StringUtils.isNoneBlank(linkProvider)) { + myCounter.increment("linkProvider"); + queries.add(createLinkProviderQuery(linkProvider)); + } + if (StringUtils.isNoneBlank(targetPid)) { myCounter.increment("targetPid"); - queries.add(createPidValueQuery(pidTypePrefix.target, targetPid)); + queries.add(createPidValueQuery(RelationPrefix.target, targetPid)); } if (StringUtils.isNoneBlank(sourcePid)) { myCounter.increment("sourcePid"); - queries.add(createPidValueQuery(pidTypePrefix.source, sourcePid)); + queries.add(createPidValueQuery(RelationPrefix.source, sourcePid)); } if (StringUtils.isNoneBlank(targetPidType)) { assert targetPidType != null; - incrementPidCounter(pidTypePrefix.target,targetPidType); - queries.add(createPidTypeQuery(pidTypePrefix.target, targetPidType)); + incrementPidCounter(RelationPrefix.target,targetPidType); + queries.add(createPidTypeQuery(RelationPrefix.target, targetPidType)); } if (StringUtils.isNoneBlank(sourcePidType)) { assert sourcePidType != null; - incrementPidCounter(pidTypePrefix.source,sourcePidType); - queries.add(createPidTypeQuery(pidTypePrefix.source, sourcePidType)); + incrementPidCounter(RelationPrefix.source,sourcePidType); + queries.add(createPidTypeQuery(RelationPrefix.source, sourcePidType)); } if (StringUtils.isNoneBlank(targetType)) { if ("dataset".equalsIgnoreCase(targetType) || "publication".equalsIgnoreCase(targetType)) myCounter.increment(String.format("targetType_%s", targetType)); - queries.add(createObjectTypeQuery(pidTypePrefix.target, targetType)); + queries.add(createObjectTypeQuery(RelationPrefix.target, targetType)); } if (StringUtils.isNoneBlank(sourceType)) { if ("dataset".equalsIgnoreCase(sourceType) || "publication".equalsIgnoreCase(sourceType)) { myCounter.increment(String.format("sourceType_%s", sourceType)); } - queries.add(createObjectTypeQuery(pidTypePrefix.source, sourceType)); + queries.add(createObjectTypeQuery(RelationPrefix.source, sourceType)); + } + + if (StringUtils.isNoneBlank(targetPublisher)) { + myCounter.increment("targetPublisher"); + + queries.add(createLinkPublisherQuery(RelationPrefix.target,targetPublisher)); } QueryBuilder result = createFinalQuery(queries); @@ -254,8 +314,4 @@ public class ScholixIndexManager { return new ImmutablePair<>(tt,scholixRes.stream().map(SearchHit::getContent).collect(Collectors.toList())); } - - - - } diff --git a/apps/scholexplorer-api/src/main/java/eu/dnetlib/scholix/api/model/v1/ScholixCreator.java b/apps/scholexplorer-api/src/main/java/eu/dnetlib/scholix/api/model/v1/ScholixCreator.java index 97f571b1..53c39cf4 100644 --- a/apps/scholexplorer-api/src/main/java/eu/dnetlib/scholix/api/model/v1/ScholixCreator.java +++ b/apps/scholexplorer-api/src/main/java/eu/dnetlib/scholix/api/model/v1/ScholixCreator.java @@ -1,11 +1,11 @@ package eu.dnetlib.scholix.api.model.v1; import com.fasterxml.jackson.annotation.JsonProperty; +import eu.dnetlib.dhp.schema.sx.scholix.ScholixEntityId; import io.swagger.v3.oas.annotations.media.Schema; -import java.util.ArrayList; import java.util.List; -import java.util.Objects; +import java.util.stream.Collectors; public class ScholixCreator { @@ -29,22 +29,11 @@ public class ScholixCreator { return name; } - public void setName(String name) { + public ScholixCreator setName(String name) { this.name = name; - } - - public ScholixCreator identifier(List identifier) { - this.identifier = identifier; return this; } - public ScholixCreator addIdentifierItem(ScholixIdentifier identifierItem) { - if (this.identifier == null) { - this.identifier = new ArrayList(); - } - this.identifier.add(identifierItem); - return this; - } /** * Get identifier @@ -55,48 +44,26 @@ public class ScholixCreator { return identifier; } - public void setIdentifier(List identifier) { + public ScholixCreator setIdentifier(List identifier) { this.identifier = identifier; + return this; } + public static ScholixCreator fromScholixEntityId(final ScholixEntityId provider) { + if (provider == null) + return null; + ScholixCreator instance = new ScholixCreator().setName(provider.getName()); - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - ScholixCreator ScholixCreator = (ScholixCreator) o; - return Objects.equals(this.name, ScholixCreator.name) && - Objects.equals(this.identifier, ScholixCreator.identifier); - } - @Override - public int hashCode() { - return Objects.hash(name, identifier); - } + if (provider.getIdentifiers()!= null && provider.getIdentifiers().size()>0) + instance.setIdentifier(provider.getIdentifiers() + .stream() + .map(i ->new ScholixIdentifier() + .setIdentifier(i.getIdentifier()) + .setSchema(i.getSchema())) + .collect(Collectors.toList())); + return instance; - @Override - public String toString() { - StringBuilder sb = new StringBuilder(); - sb.append("class ScholixCreator {\n"); - sb.append(" name: ").append(toIndentedString(name)).append("\n"); - sb.append(" identifier: ").append(toIndentedString(identifier)).append("\n"); - sb.append("}"); - return sb.toString(); - } - - /** - * Convert the given object to string with each line indented by 4 spaces - * (except the first line). - */ - private String toIndentedString(Object o) { - if (o == null) { - return "null"; - } - return o.toString().replace("\n", "\n "); } } diff --git a/apps/scholexplorer-api/src/main/java/eu/dnetlib/scholix/api/model/v1/ScholixIdentifier.java b/apps/scholexplorer-api/src/main/java/eu/dnetlib/scholix/api/model/v1/ScholixIdentifier.java index 49fb0be6..70f7c50f 100644 --- a/apps/scholexplorer-api/src/main/java/eu/dnetlib/scholix/api/model/v1/ScholixIdentifier.java +++ b/apps/scholexplorer-api/src/main/java/eu/dnetlib/scholix/api/model/v1/ScholixIdentifier.java @@ -29,8 +29,9 @@ public class ScholixIdentifier { return identifier; } - public void setIdentifier(String identifier) { + public ScholixIdentifier setIdentifier(String identifier) { this.identifier = identifier; + return this; } public ScholixIdentifier Schema(String idScheme) { @@ -48,51 +49,23 @@ public class ScholixIdentifier { return schema; } - public void setSchema(String schema) { + public ScholixIdentifier setSchema(String schema) { this.schema = schema; + return this; + } + + public static ScholixIdentifier fromScholixIdentifier(eu.dnetlib.dhp.schema.sx.scholix.ScholixIdentifier input) { + if (input == null) + return null; + + final ScholixIdentifier result = new ScholixIdentifier(); + result.setSchema(input.getSchema()); + result.setIdentifier(input.getIdentifier()); + return result; + + + } - - - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - ScholixIdentifier identifierType = (ScholixIdentifier) o; - return Objects.equals(this.identifier, identifierType.identifier) && - Objects.equals(this.schema, identifierType.schema); - } - - @Override - public int hashCode() { - return Objects.hash(identifier, schema); - } - - @Override - public String toString() { - StringBuilder sb = new StringBuilder(); - sb.append("class IdentifierType {\n"); - - sb.append(" identifier: ").append(toIndentedString(identifier)).append("\n"); - sb.append(" schema: ").append(toIndentedString(schema)).append("\n"); - sb.append("}"); - return sb.toString(); - } - - /** - * Convert the given object to string with each line indented by 4 spaces - * (except the first line). - */ - private String toIndentedString(Object o) { - if (o == null) { - return "null"; - } - return o.toString().replace("\n", "\n "); - } } diff --git a/apps/scholexplorer-api/src/main/java/eu/dnetlib/scholix/api/model/v1/ScholixItem.java b/apps/scholexplorer-api/src/main/java/eu/dnetlib/scholix/api/model/v1/ScholixItem.java index c81b3bd1..fb0af52b 100644 --- a/apps/scholexplorer-api/src/main/java/eu/dnetlib/scholix/api/model/v1/ScholixItem.java +++ b/apps/scholexplorer-api/src/main/java/eu/dnetlib/scholix/api/model/v1/ScholixItem.java @@ -1,16 +1,18 @@ package eu.dnetlib.scholix.api.model.v1; import com.fasterxml.jackson.annotation.JsonProperty; +import eu.dnetlib.dhp.schema.sx.scholix.ScholixResource; import io.swagger.v3.oas.annotations.media.Schema; import java.util.ArrayList; import java.util.List; import java.util.Objects; +import java.util.stream.Collectors; public class ScholixItem { - @JsonProperty("identifier") - private ScholixIdentifier identifier; + @JsonProperty("identifiers") + private List identifiers; @JsonProperty("objectType") private String objectType; @@ -28,22 +30,18 @@ public class ScholixItem { private String publicationDate; @JsonProperty("publisher") - private ScholixProvider publisher; + private List publisher; - public ScholixItem identifier(ScholixIdentifier identifier) { - this.identifier = identifier; + @Schema(description = "The list of identifiers") + public List getIdentifiers() { + return identifiers; + } + + public ScholixItem setIdentifiers(List identifiers) { + this.identifiers = identifiers; return this; } - @Schema(description = "The identifier string") - public ScholixIdentifier getIdentifier() { - return identifier; - } - - public void setIdentifier(ScholixIdentifier identifier) { - this.identifier = identifier; - } - public ScholixItem objectType(String objectType) { this.objectType = objectType; return this; @@ -116,63 +114,48 @@ public class ScholixItem { this.publicationDate = publicationDate; } - @Schema(description = "The name of the publisher of the object") - public ScholixProvider getPublisher() { + @Schema(description = "The list name of the publisher of the object") + public List getPublisher() { return publisher; } - public void setPublisher(ScholixProvider publisher) { + public ScholixItem setPublisher(List publisher) { this.publisher = publisher; + return this; } + public static ScholixItem fromScholixResource(final ScholixResource input) { + if (input == null) + return null; - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - ScholixItem scholixItemType = (ScholixItem) o; - return Objects.equals(this.objectType, scholixItemType.objectType) && - Objects.equals(this.objectSubType, scholixItemType.objectSubType) && - Objects.equals(this.identifier, scholixItemType.identifier) && - Objects.equals(this.title, scholixItemType.title) && - Objects.equals(this.creator, scholixItemType.creator) && - Objects.equals(this.publicationDate, scholixItemType.publicationDate) && - Objects.equals(this.publisher, scholixItemType.publisher); - } - @Override - public int hashCode() { - return Objects.hash(objectType, objectSubType, identifier, title, creator, publicationDate, publisher); - } + final ScholixItem result = new ScholixItem(); - @Override - public String toString() { - StringBuilder sb = new StringBuilder(); - sb.append("class ScholixItemType {\n"); + if (input.getIdentifier()!= null) + result.setIdentifiers( + input.getIdentifier().stream() + .map(ScholixIdentifier::fromScholixIdentifier) + .collect(Collectors.toList()) + ); - sb.append(" objectType: ").append(toIndentedString(objectType)).append("\n"); - sb.append(" objectSubType: ").append(toIndentedString(objectSubType)).append("\n"); - sb.append(" identifier: ").append(toIndentedString(identifier)).append("\n"); - sb.append(" title: ").append(toIndentedString(title)).append("\n"); - sb.append(" creator: ").append(toIndentedString(creator)).append("\n"); - sb.append(" publicationDate: ").append(toIndentedString(publicationDate)).append("\n"); - sb.append(" publisher: ").append(toIndentedString(publisher)).append("\n"); - sb.append("}"); - return sb.toString(); - } + result.setTitle(input.getTitle()); + result.setObjectType(input.getObjectType()); + result.setObjectSubType(input.getObjectSubType()); + result.setPublicationDate(input.getPublicationDate()); + if(input.getPublisher()!= null) + result.setPublisher(input.getPublisher().stream() + .map(ScholixProvider::fromScholixEntityId) + .collect(Collectors.toList()) + ); + + + if (input.getCreator()!= null) + result.setCreator(input.getCreator().stream() + .map(ScholixCreator::fromScholixEntityId) + .collect(Collectors.toList()) + ); + + return result; - /** - * Convert the given object to string with each line indented by 4 spaces - * (except the first line). - */ - private String toIndentedString(Object o) { - if (o == null) { - return "null"; - } - return o.toString().replace("\n", "\n "); } } diff --git a/apps/scholexplorer-api/src/main/java/eu/dnetlib/scholix/api/model/v1/ScholixProvider.java b/apps/scholexplorer-api/src/main/java/eu/dnetlib/scholix/api/model/v1/ScholixProvider.java index 1436c36d..21a50ce2 100644 --- a/apps/scholexplorer-api/src/main/java/eu/dnetlib/scholix/api/model/v1/ScholixProvider.java +++ b/apps/scholexplorer-api/src/main/java/eu/dnetlib/scholix/api/model/v1/ScholixProvider.java @@ -1,15 +1,36 @@ package eu.dnetlib.scholix.api.model.v1; import com.fasterxml.jackson.annotation.JsonProperty; +import eu.dnetlib.dhp.schema.sx.scholix.ScholixEntityId; import io.swagger.v3.oas.annotations.media.Schema; import javax.validation.Valid; import java.util.ArrayList; import java.util.List; import java.util.Objects; +import java.util.stream.Collectors; public class ScholixProvider { + public static ScholixProvider fromScholixEntityId(final ScholixEntityId provider) { + if (provider == null) + return null; + ScholixProvider instance = new ScholixProvider().setName(provider.getName()); + + + if (provider.getIdentifiers()!= null && provider.getIdentifiers().size()>0) + instance.setIdentifier(provider.getIdentifiers() + .stream() + .map(i ->new ScholixIdentifier() + .setIdentifier(i.getIdentifier()) + .setSchema(i.getSchema())) + .collect(Collectors.toList())); + return instance; + + + } + + @JsonProperty("name") private String name = null; @@ -32,8 +53,9 @@ public class ScholixProvider { return name; } - public void setName(String name) { + public ScholixProvider setName(String name) { this.name = name; + return this; } public ScholixProvider identifier(List identifier) { @@ -59,8 +81,9 @@ public class ScholixProvider { return identifier; } - public void setIdentifier(List identifier) { + public ScholixProvider setIdentifier(List identifier) { this.identifier = identifier; + return this; } diff --git a/apps/scholexplorer-api/src/main/java/eu/dnetlib/scholix/api/model/v1/ScholixRelationship.java b/apps/scholexplorer-api/src/main/java/eu/dnetlib/scholix/api/model/v1/ScholixRelationship.java index 82242d39..da58c956 100644 --- a/apps/scholexplorer-api/src/main/java/eu/dnetlib/scholix/api/model/v1/ScholixRelationship.java +++ b/apps/scholexplorer-api/src/main/java/eu/dnetlib/scholix/api/model/v1/ScholixRelationship.java @@ -6,6 +6,8 @@ import io.swagger.v3.oas.annotations.media.Schema; import javax.validation.constraints.NotBlank; import java.util.Objects; +import static eu.dnetlib.scholix.api.model.v2.RelationshipType.relationMapping; + public class ScholixRelationship { @JsonProperty("name") @@ -116,4 +118,17 @@ public class ScholixRelationship { return o.toString().replace("\n", "\n "); } + public static ScholixRelationship fromScholixIndexRelationship(eu.dnetlib.dhp.schema.sx.scholix.ScholixRelationship inputRel) { + if(inputRel==null) + return null; + + ScholixRelationship result = new ScholixRelationship(); + + result.setName(relationMapping.getOrDefault(inputRel.getName(), "IsRelatedTo")); + result.setInverseRelationship(relationMapping.getOrDefault(inputRel.getInverse(), "IsRelatedTo")); + result.setSchema(inputRel.getSchema()); + return result; + + } + } diff --git a/apps/scholexplorer-api/src/main/java/eu/dnetlib/scholix/api/model/v1/ScholixV1.java b/apps/scholexplorer-api/src/main/java/eu/dnetlib/scholix/api/model/v1/ScholixV1.java index 3e9c5978..42f30acd 100644 --- a/apps/scholexplorer-api/src/main/java/eu/dnetlib/scholix/api/model/v1/ScholixV1.java +++ b/apps/scholexplorer-api/src/main/java/eu/dnetlib/scholix/api/model/v1/ScholixV1.java @@ -9,6 +9,7 @@ import java.io.Serializable; import java.util.ArrayList; import java.util.List; import java.util.Objects; +import java.util.stream.Collectors; public class ScholixV1 implements Serializable { @@ -161,6 +162,23 @@ public class ScholixV1 implements Serializable { public static ScholixV1 fromScholix(Scholix input) { - return new ScholixV1(); + if (input == null) + return null; + final ScholixV1 result = new ScholixV1(); + + + if (input.getLinkprovider()!= null) + result.setLinkProvider(input.getLinkprovider() + .stream() + .map(ScholixProvider::fromScholixEntityId) + .collect(Collectors.toList())); + + result.setPublicationDate(input.getPublicationDate()); + + result.setRelationship(ScholixRelationship.fromScholixIndexRelationship(input.getRelationship())); + + result.setSource(ScholixItem.fromScholixResource(input.getSource())); + result.setTarget(ScholixItem.fromScholixResource(input.getTarget())); + return result; } } diff --git a/apps/scholexplorer-api/src/main/java/eu/dnetlib/scholix/api/model/v2/RelationshipType.java b/apps/scholexplorer-api/src/main/java/eu/dnetlib/scholix/api/model/v2/RelationshipType.java index 6651c1b9..807b3e3f 100644 --- a/apps/scholexplorer-api/src/main/java/eu/dnetlib/scholix/api/model/v2/RelationshipType.java +++ b/apps/scholexplorer-api/src/main/java/eu/dnetlib/scholix/api/model/v2/RelationshipType.java @@ -27,7 +27,7 @@ public class RelationshipType { private String subTypeSchema; - private static Map relationMapping = Stream.of(new String[][] { + public static Map relationMapping = Stream.of(new String[][] { { "issupplementto", "IsSupplementTo" }, { "issupplementedby", "IsSupplementedBy" }, { "references", "References" }, diff --git a/apps/scholexplorer-api/src/test/java/eu/dnetlib/scholix/test/QueryTest.java b/apps/scholexplorer-api/src/test/java/eu/dnetlib/scholix/test/QueryTest.java new file mode 100644 index 00000000..f0e3bd1b --- /dev/null +++ b/apps/scholexplorer-api/src/test/java/eu/dnetlib/scholix/test/QueryTest.java @@ -0,0 +1,18 @@ +package eu.dnetlib.scholix.test; + + + +import org.junit.jupiter.api.Test; + + +public class QueryTest { + + + @Test + public void featureToTest() { + + + + + } +}