This commit is contained in:
Michele Artini 2023-07-05 10:25:56 +02:00
parent 653e925cff
commit 7d91da1099
1 changed files with 36 additions and 22 deletions

View File

@ -6,6 +6,8 @@ import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils; 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.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.CrossOrigin;
@ -16,7 +18,6 @@ import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import eu.dnetlib.openaire.exporter.exceptions.CommunityException; import eu.dnetlib.openaire.exporter.exceptions.CommunityException;
import eu.dnetlib.openaire.exporter.exceptions.ResourceNotFoundException;
import eu.dnetlib.openaire.exporter.model.community.CommunityType; import eu.dnetlib.openaire.exporter.model.community.CommunityType;
import eu.dnetlib.openaire.exporter.model.community.SubCommunity; import eu.dnetlib.openaire.exporter.model.community.SubCommunity;
import eu.dnetlib.openaire.exporter.model.context.CategorySummary; import eu.dnetlib.openaire.exporter.model.context.CategorySummary;
@ -38,6 +39,8 @@ public class ContextApiController {
@Autowired @Autowired
private CommunityService communityService; private CommunityService communityService;
private static final Log log = LogFactory.getLog(ContextApiController.class);
@RequestMapping(value = "/contexts", produces = { @RequestMapping(value = "/contexts", produces = {
"application/json" "application/json"
}, method = RequestMethod.GET) }, method = RequestMethod.GET)
@ -71,22 +74,27 @@ public class ContextApiController {
}) })
public List<CategorySummary> listCategories( public List<CategorySummary> listCategories(
@PathVariable final String contextId, @PathVariable final String contextId,
@RequestParam(required = false, defaultValue = "false") final boolean all) throws ResourceNotFoundException, CommunityException { @RequestParam(required = false, defaultValue = "false") final boolean all) throws CommunityException {
return communityService.getSubCommunities(contextId) try {
.stream() return communityService.getSubCommunities(contextId)
.filter(sc -> all || sc.isClaim()) .stream()
.map(sc -> { .filter(sc -> all || sc.isClaim())
final String[] parts = StringUtils.split(sc.getSubCommunityId()); .map(sc -> {
if (parts.length < 3) { throw new RuntimeException("Invalid conceptId (It should have 3 (or more) parts)"); } final String[] parts = StringUtils.split(sc.getSubCommunityId(), "::");
final CategorySummary cat = new CategorySummary(); if (parts.length < 3) { throw new RuntimeException("Invalid conceptId (It should have 3 (or more) parts): " + sc.getSubCommunityId()); }
cat.setId(parts[0] + "::" + parts[1]); final CategorySummary cat = new CategorySummary();
cat.setLabel(sc.getLabel()); cat.setId(parts[0] + "::" + parts[1]);
cat.setHasConcept(true); cat.setLabel(sc.getLabel());
return cat; cat.setHasConcept(true);
}) return cat;
.distinct() })
.collect(Collectors.toList()); .distinct()
.collect(Collectors.toList());
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
} }
@RequestMapping(value = "/context/category/{categoryId}", produces = { @RequestMapping(value = "/context/category/{categoryId}", produces = {
@ -101,8 +109,11 @@ public class ContextApiController {
@PathVariable final String categoryId, @PathVariable final String categoryId,
@RequestParam(required = false, defaultValue = "false") final boolean all) throws CommunityException { @RequestParam(required = false, defaultValue = "false") final boolean all) throws CommunityException {
final String[] parts = StringUtils.split(categoryId); final String[] parts = StringUtils.split(categoryId, "::");
if (parts.length != 2) { throw new CommunityException("Invalid category id (it should have 2 parts"); } 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 String contextId = parts[0];
@ -122,16 +133,19 @@ public class ContextApiController {
}) })
public List<ConceptSummary> listSubConcepts( public List<ConceptSummary> listSubConcepts(
@PathVariable final String conceptId, @PathVariable final String conceptId,
@RequestParam(required = false, defaultValue = "false") final boolean all) throws ResourceNotFoundException, CommunityException { @RequestParam(required = false, defaultValue = "false") final boolean all) throws CommunityException {
final String[] parts = StringUtils.split(conceptId); final String[] parts = StringUtils.split(conceptId, "::");
if (parts.length < 3) { throw new CommunityException("Invalid concept id (it should have 3 (or more) parts"); } 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 String contextId = parts[0];
final List<SubCommunity> list = findSubCommunities(conceptId + "::", all, contextId); final List<SubCommunity> list = findSubCommunities(conceptId + "::", all, contextId);
return processSubCommunities(contextId, list); return processSubCommunities(conceptId, list);
} }
private List<SubCommunity> findSubCommunities(final String prefix, final boolean all, final String contextId) throws CommunityException { private List<SubCommunity> findSubCommunities(final String prefix, final boolean all, final String contextId) throws CommunityException {