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 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;
@ -16,7 +18,6 @@ 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.exceptions.ResourceNotFoundException;
import eu.dnetlib.openaire.exporter.model.community.CommunityType;
import eu.dnetlib.openaire.exporter.model.community.SubCommunity;
import eu.dnetlib.openaire.exporter.model.context.CategorySummary;
@ -38,6 +39,8 @@ 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)
@ -71,22 +74,27 @@ public class ContextApiController {
})
public List<CategorySummary> listCategories(
@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)
.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)"); }
final CategorySummary cat = new CategorySummary();
cat.setId(parts[0] + "::" + parts[1]);
cat.setLabel(sc.getLabel());
cat.setHasConcept(true);
return cat;
})
.distinct()
.collect(Collectors.toList());
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 = {
@ -101,8 +109,11 @@ public class ContextApiController {
@PathVariable final String categoryId,
@RequestParam(required = false, defaultValue = "false") final boolean all) throws CommunityException {
final String[] parts = StringUtils.split(categoryId);
if (parts.length != 2) { throw new CommunityException("Invalid category id (it should have 2 parts"); }
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];
@ -122,16 +133,19 @@ public class ContextApiController {
})
public List<ConceptSummary> listSubConcepts(
@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);
if (parts.length < 3) { throw new CommunityException("Invalid concept id (it should have 3 (or more) parts"); }
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<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 {