From 51cbe18abf357ee5858c977eb9e7bd55a4fb6153 Mon Sep 17 00:00:00 2001 From: "michele.artini" Date: Tue, 11 Jul 2023 07:52:00 +0200 Subject: [PATCH] moved context API controller in the original package --- .../community/ContextApiController.java | 174 ------------------ .../context/ContextApiController.java | 124 +++++++++++-- .../openaire/context/ContextMappingUtils.java | 1 + 3 files changed, 105 insertions(+), 194 deletions(-) delete mode 100644 apps/dnet-exporter-api/src/main/java/eu/dnetlib/openaire/community/ContextApiController.java diff --git a/apps/dnet-exporter-api/src/main/java/eu/dnetlib/openaire/community/ContextApiController.java b/apps/dnet-exporter-api/src/main/java/eu/dnetlib/openaire/community/ContextApiController.java deleted file mode 100644 index 145d4b86..00000000 --- a/apps/dnet-exporter-api/src/main/java/eu/dnetlib/openaire/community/ContextApiController.java +++ /dev/null @@ -1,174 +0,0 @@ -package eu.dnetlib.openaire.community; - -import java.util.List; -import java.util.Objects; -import java.util.Set; -import java.util.stream.Collectors; - -import org.apache.commons.lang3.StringUtils; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; -import org.springframework.web.bind.annotation.CrossOrigin; -import org.springframework.web.bind.annotation.PathVariable; -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.RestController; - -import eu.dnetlib.openaire.exporter.exceptions.CommunityException; -import eu.dnetlib.openaire.exporter.model.community.CommunityType; -import eu.dnetlib.openaire.exporter.model.community.SubCommunity; -import eu.dnetlib.openaire.exporter.model.context.CategorySummary; -import eu.dnetlib.openaire.exporter.model.context.ConceptSummary; -import eu.dnetlib.openaire.exporter.model.context.ContextSummary; -import io.swagger.v3.oas.annotations.Operation; -import io.swagger.v3.oas.annotations.responses.ApiResponse; -import io.swagger.v3.oas.annotations.responses.ApiResponses; -import io.swagger.v3.oas.annotations.tags.Tag; - -@RestController -@CrossOrigin(origins = { - "*" -}) -@ConditionalOnProperty(value = "openaire.exporter.enable.community", havingValue = "true") -@Tag(name = "OpenAIRE Context API", description = "the OpenAIRE Context API") -public class ContextApiController { - - @Autowired - private CommunityService communityService; - - private static final Log log = LogFactory.getLog(ContextApiController.class); - - @RequestMapping(value = "/contexts", produces = { - "application/json" - }, method = RequestMethod.GET) - @Operation(summary = "list brief information about all the context profiles", description = "list brief information about all the context profiles") - @ApiResponses(value = { - @ApiResponse(responseCode = "200", description = "OK"), - @ApiResponse(responseCode = "500", description = "unexpected error") - }) - public List listContexts(@RequestParam(required = false) final Set type) throws CommunityException { - return communityService.listCommunities() - .stream() - .filter(c -> type == null || type.contains(c.getType())) - .map(c -> { - final ContextSummary ctx = new ContextSummary(); - ctx.setId(c.getId()); - ctx.setLabel(c.getName()); - ctx.setStatus(c.getStatus().toString()); - ctx.setType(c.getType().toString()); - return ctx; - }) - .collect(Collectors.toList()); - } - - @RequestMapping(value = "/context/{contextId}", produces = { - "application/json" - }, method = RequestMethod.GET) - @Operation(summary = "list the categories defined within a context", description = "list the categories defined within a context") - @ApiResponses(value = { - @ApiResponse(responseCode = "200", description = "OK"), - @ApiResponse(responseCode = "500", description = "unexpected error") - }) - public List listCategories( - @PathVariable final String contextId, - @RequestParam(required = false, defaultValue = "false") final boolean all) throws CommunityException { - - try { - return communityService.getSubCommunities(contextId) - .stream() - .filter(sc -> all || sc.isClaim()) - .map(sc -> { - final String[] parts = StringUtils.split(sc.getSubCommunityId(), "::"); - if (parts.length < 3) { throw new RuntimeException("Invalid conceptId (It should have 3 (or more) parts): " + sc.getSubCommunityId()); } - final CategorySummary cat = new CategorySummary(); - cat.setId(parts[0] + "::" + parts[1]); - cat.setLabel(sc.getLabel()); - cat.setHasConcept(true); - return cat; - }) - .distinct() - .collect(Collectors.toList()); - } catch (final Throwable e) { - log.error(e); - throw new CommunityException(e); - } - } - - @RequestMapping(value = "/context/category/{categoryId}", produces = { - "application/json" - }, method = RequestMethod.GET) - @Operation(summary = "list the concepts defined within a category", description = "list the concepts defined within a category") - @ApiResponses(value = { - @ApiResponse(responseCode = "200", description = "OK"), - @ApiResponse(responseCode = "500", description = "unexpected error") - }) - public List listConcepts( - @PathVariable final String categoryId, - @RequestParam(required = false, defaultValue = "false") final boolean all) throws CommunityException { - - final String[] parts = StringUtils.split(categoryId, "::"); - if (parts.length != 2) { - log.error("Invalid category id (it should have 2 parts): " + categoryId); - throw new CommunityException("Invalid category id (it should have 2 parts): " + categoryId); - } - - final String contextId = parts[0]; - - final List list = findSubCommunities(categoryId + "::", all, contextId); - - return processSubCommunities(null, list); - - } - - @RequestMapping(value = "/context/category/concept/{conceptId}", produces = { - "application/json" - }, method = RequestMethod.GET) - @Operation(summary = "list the concepts defined within a category", description = "list the concepts defined within a category") - @ApiResponses(value = { - @ApiResponse(responseCode = "200", description = "OK"), - @ApiResponse(responseCode = "500", description = "unexpected error") - }) - public List listSubConcepts( - @PathVariable final String conceptId, - @RequestParam(required = false, defaultValue = "false") final boolean all) throws CommunityException { - - final String[] parts = StringUtils.split(conceptId, "::"); - if (parts.length < 3) { - log.error("Invalid concept id (it should have 3 (or more) parts): " + conceptId); - throw new CommunityException("Invalid concept id (it should have 3 (or more) parts): " + conceptId); - } - - final String contextId = parts[0]; - - final List list = findSubCommunities(conceptId + "::", all, contextId); - - return processSubCommunities(conceptId, list); - } - - private List findSubCommunities(final String prefix, final boolean all, final String contextId) throws CommunityException { - return communityService.getSubCommunities(contextId) - .stream() - .filter(sc -> all || sc.isClaim()) - .filter(sc -> sc.getSubCommunityId().startsWith(prefix)) - .collect(Collectors.toList()); - } - - private List processSubCommunities(final String parent, final List list) { - return list.stream() - .filter(sc -> Objects.equals(sc.getParent(), parent)) - .map(sc -> { - final List childs = processSubCommunities(sc.getSubCommunityId(), list); - final ConceptSummary concept = new ConceptSummary(); - concept.setId(sc.getSubCommunityId()); - concept.setLabel(sc.getLabel()); - concept.setHasSubConcept(!childs.isEmpty()); - concept.setConcept(childs); - return concept; - }) - .collect(Collectors.toList()); - } - -} diff --git a/apps/dnet-exporter-api/src/main/java/eu/dnetlib/openaire/context/ContextApiController.java b/apps/dnet-exporter-api/src/main/java/eu/dnetlib/openaire/context/ContextApiController.java index 899bcfe0..101b9948 100644 --- a/apps/dnet-exporter-api/src/main/java/eu/dnetlib/openaire/context/ContextApiController.java +++ b/apps/dnet-exporter-api/src/main/java/eu/dnetlib/openaire/context/ContextApiController.java @@ -1,32 +1,46 @@ package eu.dnetlib.openaire.context; import java.util.List; -import java.util.Optional; +import java.util.Objects; +import java.util.Set; +import java.util.stream.Collectors; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.PathVariable; 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.RestController; -import eu.dnetlib.openaire.exporter.exceptions.ContextException; +import eu.dnetlib.openaire.community.CommunityService; +import eu.dnetlib.openaire.exporter.exceptions.CommunityException; +import eu.dnetlib.openaire.exporter.model.community.CommunityType; +import eu.dnetlib.openaire.exporter.model.community.SubCommunity; import eu.dnetlib.openaire.exporter.model.context.CategorySummary; import eu.dnetlib.openaire.exporter.model.context.ConceptSummary; import eu.dnetlib.openaire.exporter.model.context.ContextSummary; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.responses.ApiResponse; import io.swagger.v3.oas.annotations.responses.ApiResponses; +import io.swagger.v3.oas.annotations.tags.Tag; -// @RestController -// @CrossOrigin(origins = { "*" }) -// @ConditionalOnProperty(value = "openaire.exporter.enable.context", havingValue = "true") -// @Tag(name = "OpenAIRE Context API", description = "the OpenAIRE Context API") -@Deprecated -// THIS APIs HAS BEEN REIMPLEMENTED UNDER THE COMMUNITY APIs +@RestController +@CrossOrigin(origins = { + "*" +}) +@ConditionalOnProperty(value = "openaire.exporter.enable.community", havingValue = "true") +@Tag(name = "OpenAIRE Context API", description = "the OpenAIRE Context API") public class ContextApiController { @Autowired - private ContextApiCore contextApiCore; + private CommunityService communityService; + + private static final Log log = LogFactory.getLog(ContextApiController.class); @RequestMapping(value = "/contexts", produces = { "application/json" @@ -36,8 +50,19 @@ public class ContextApiController { @ApiResponse(responseCode = "200", description = "OK"), @ApiResponse(responseCode = "500", description = "unexpected error") }) - public List listContexts(@RequestParam(required = false, defaultValue = "") final List type) throws ContextException { - return contextApiCore.listContexts(type); + public List listContexts(@RequestParam(required = false) final Set type) throws CommunityException { + return communityService.listCommunities() + .stream() + .filter(c -> type == null || type.contains(c.getType())) + .map(c -> { + final ContextSummary ctx = new ContextSummary(); + ctx.setId(c.getId()); + ctx.setLabel(c.getName()); + ctx.setStatus(c.getStatus().toString()); + ctx.setType(c.getType().toString()); + return ctx; + }) + .collect(Collectors.toList()); } @RequestMapping(value = "/context/{contextId}", produces = { @@ -50,10 +75,27 @@ public class ContextApiController { }) public List listCategories( @PathVariable final String contextId, - @RequestParam(required = false, defaultValue = "false") final Boolean all) throws ContextException { + @RequestParam(required = false, defaultValue = "false") final boolean all) throws CommunityException { - final Boolean allFilter = Optional.ofNullable(all).orElse(false); - return contextApiCore.listCategories(contextId, allFilter); + try { + return communityService.getSubCommunities(contextId) + .stream() + .filter(sc -> all || sc.isClaim()) + .map(sc -> { + final String[] parts = StringUtils.split(sc.getSubCommunityId(), "::"); + if (parts.length < 3) { throw new RuntimeException("Invalid conceptId (It should have 3 (or more) parts): " + sc.getSubCommunityId()); } + final CategorySummary cat = new CategorySummary(); + cat.setId(parts[0] + "::" + parts[1]); + cat.setLabel(sc.getLabel()); + cat.setHasConcept(true); + return cat; + }) + .distinct() + .collect(Collectors.toList()); + } catch (final Throwable e) { + log.error(e); + throw new CommunityException(e); + } } @RequestMapping(value = "/context/category/{categoryId}", produces = { @@ -66,10 +108,20 @@ public class ContextApiController { }) public List listConcepts( @PathVariable final String categoryId, - @RequestParam(required = false, defaultValue = "false") final Boolean all) throws ContextException { + @RequestParam(required = false, defaultValue = "false") final boolean all) throws CommunityException { + + final String[] parts = StringUtils.split(categoryId, "::"); + if (parts.length != 2) { + log.error("Invalid category id (it should have 2 parts): " + categoryId); + throw new CommunityException("Invalid category id (it should have 2 parts): " + categoryId); + } + + final String contextId = parts[0]; + + final List list = findSubCommunities(categoryId + "::", all, contextId); + + return processSubCommunities(null, list); - final Boolean allFilter = Optional.ofNullable(all).orElse(false); - return contextApiCore.listConcepts(categoryId, allFilter); } @RequestMapping(value = "/context/category/concept/{conceptId}", produces = { @@ -82,10 +134,42 @@ public class ContextApiController { }) public List listSubConcepts( @PathVariable final String conceptId, - @RequestParam(required = false, defaultValue = "false") final Boolean all) throws ContextException { + @RequestParam(required = false, defaultValue = "false") final boolean all) throws CommunityException { - final Boolean allFilter = Optional.ofNullable(all).orElse(false); - return contextApiCore.listSubConcepts(conceptId, allFilter); + final String[] parts = StringUtils.split(conceptId, "::"); + if (parts.length < 3) { + log.error("Invalid concept id (it should have 3 (or more) parts): " + conceptId); + throw new CommunityException("Invalid concept id (it should have 3 (or more) parts): " + conceptId); + } + + final String contextId = parts[0]; + + final List list = findSubCommunities(conceptId + "::", all, contextId); + + return processSubCommunities(conceptId, list); + } + + private List findSubCommunities(final String prefix, final boolean all, final String contextId) throws CommunityException { + return communityService.getSubCommunities(contextId) + .stream() + .filter(sc -> all || sc.isClaim()) + .filter(sc -> sc.getSubCommunityId().startsWith(prefix)) + .collect(Collectors.toList()); + } + + private List processSubCommunities(final String parent, final List list) { + return list.stream() + .filter(sc -> Objects.equals(sc.getParent(), parent)) + .map(sc -> { + final List childs = processSubCommunities(sc.getSubCommunityId(), list); + final ConceptSummary concept = new ConceptSummary(); + concept.setId(sc.getSubCommunityId()); + concept.setLabel(sc.getLabel()); + concept.setHasSubConcept(!childs.isEmpty()); + concept.setConcept(childs); + return concept; + }) + .collect(Collectors.toList()); } } diff --git a/apps/dnet-exporter-api/src/main/java/eu/dnetlib/openaire/context/ContextMappingUtils.java b/apps/dnet-exporter-api/src/main/java/eu/dnetlib/openaire/context/ContextMappingUtils.java index 81dd0e69..7a864621 100644 --- a/apps/dnet-exporter-api/src/main/java/eu/dnetlib/openaire/context/ContextMappingUtils.java +++ b/apps/dnet-exporter-api/src/main/java/eu/dnetlib/openaire/context/ContextMappingUtils.java @@ -28,6 +28,7 @@ import eu.dnetlib.openaire.exporter.model.context.Context; import eu.dnetlib.openaire.exporter.model.context.Param; import eu.dnetlib.openaire.exporter.model.funders.FunderDetails; +@Deprecated public class ContextMappingUtils { private static final List DATE_PATTERN = Lists.newArrayList("yyyy-MM-dd'T'hh:mm:ss", "yyyy-MM-dd'T'hh:mm:ssXXX", "yyyy-MM-dd'T'hh:mm:ss+00:00");