404 errors in community and context API

This commit is contained in:
Michele Artini 2023-10-10 15:51:00 +02:00
parent a81f007dcb
commit b3e3d676ba
7 changed files with 514 additions and 466 deletions

View File

@ -16,7 +16,9 @@ import org.springframework.web.bind.annotation.ResponseStatus;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import eu.dnetlib.openaire.exporter.exceptions.CommunityException;
import eu.dnetlib.openaire.exporter.exceptions.DsmApiException;
import eu.dnetlib.openaire.exporter.exceptions.ResourceNotFoundException;
import eu.dnetlib.openaire.exporter.model.dsm.Response;
/**
@ -28,17 +30,26 @@ public abstract class AbstractExporterController {
@ResponseBody
@ExceptionHandler({
DsmApiException.class
DsmApiException.class, CommunityException.class, Exception.class
})
@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
public ErrorMessage handleDSMException(final Exception e) {
public ErrorMessage handle500(final Exception e) {
return _handleError(e);
}
@ResponseBody
@ExceptionHandler({
ResourceNotFoundException.class
})
@ResponseStatus(value = HttpStatus.NOT_FOUND)
public ErrorMessage handle404(final Exception e) {
return _handleError(e);
}
@ResponseBody
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public List<ErrorMessage> processValidationError(final MethodArgumentNotValidException e) {
public List<ErrorMessage> handle400(final MethodArgumentNotValidException e) {
return e.getBindingResult()
.getFieldErrors()
.stream()

View File

@ -23,8 +23,9 @@ import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import eu.dnetlib.common.controller.AbstractDnetController;
import eu.dnetlib.openaire.common.AbstractExporterController;
import eu.dnetlib.openaire.exporter.exceptions.CommunityException;
import eu.dnetlib.openaire.exporter.exceptions.ResourceNotFoundException;
import eu.dnetlib.openaire.exporter.model.community.CommunityContentprovider;
import eu.dnetlib.openaire.exporter.model.community.CommunityDetails;
import eu.dnetlib.openaire.exporter.model.community.CommunityOpenAIRECommunities;
@ -44,7 +45,7 @@ import io.swagger.v3.oas.annotations.tags.Tag;
})
@ConditionalOnProperty(value = "openaire.exporter.enable.community", havingValue = "true")
@Tag(name = "OpenAIRE Communities API", description = "the OpenAIRE Community API")
public class CommunityApiController extends AbstractDnetController {
public class CommunityApiController extends AbstractExporterController {
@Autowired
private CommunityService communityService;
@ -60,7 +61,13 @@ public class CommunityApiController extends AbstractDnetController {
@ApiResponse(responseCode = "500", description = "unexpected error")
})
public List<CommunitySummary> listCommunities() throws CommunityException {
return communityService.listCommunities();
try {
return communityService.listCommunities();
} catch (final ResourceNotFoundException e) {
throw e;
} catch (final Throwable e) {
throw new CommunityException(e);
}
}
@RequestMapping(value = "/community/", produces = {
@ -75,7 +82,13 @@ public class CommunityApiController extends AbstractDnetController {
@ApiResponse(responseCode = "500", description = "unexpected error")
})
public CommunityDetails getCommunity(@RequestBody final CommunityDetails details) throws CommunityException {
return communityService.newCommunity(details);
try {
return communityService.newCommunity(details);
} catch (final ResourceNotFoundException e) {
throw e;
} catch (final Throwable e) {
throw new CommunityException(e);
}
}
@RequestMapping(value = "/community/{id}", produces = {
@ -90,7 +103,13 @@ public class CommunityApiController extends AbstractDnetController {
@ApiResponse(responseCode = "500", description = "unexpected error")
})
public CommunityDetails getCommunity(@PathVariable final String id) throws CommunityException {
return communityService.getCommunity(id);
try {
return communityService.getCommunity(id);
} catch (final ResourceNotFoundException e) {
throw e;
} catch (final Throwable e) {
throw new CommunityException(e);
}
}
@RequestMapping(value = "/community/{id}", produces = {
@ -107,8 +126,13 @@ public class CommunityApiController extends AbstractDnetController {
public void setCommunity(
@PathVariable final String id,
@RequestBody final CommunityWritableProperties properties) throws CommunityException {
communityService.setCommunity(id, properties);
try {
communityService.setCommunity(id, properties);
} catch (final ResourceNotFoundException e) {
throw e;
} catch (final Throwable e) {
throw new CommunityException(e);
}
}
@RequestMapping(value = "/community/{id}", produces = {
@ -124,7 +148,13 @@ public class CommunityApiController extends AbstractDnetController {
})
public void deleteCommunity(@PathVariable final String id, @RequestParam(required = false, defaultValue = "false") final boolean recursive)
throws CommunityException {
communityService.deleteCommunity(id, recursive);
try {
communityService.deleteCommunity(id, recursive);
} catch (final ResourceNotFoundException e) {
throw e;
} catch (final Throwable e) {
throw new CommunityException(e);
}
}
@RequestMapping(value = "/community/{id}/projects/{page}/{size}", produces = {
@ -145,7 +175,13 @@ public class CommunityApiController extends AbstractDnetController {
@RequestParam(required = false) final String searchFilter,
@RequestParam(required = false) final String orderBy)
throws CommunityException {
return communityService.getCommunityProjects(id, funder, searchFilter, page, size, orderBy);
try {
return communityService.getCommunityProjects(id, funder, searchFilter, page, size, orderBy);
} catch (final ResourceNotFoundException e) {
throw e;
} catch (final Throwable e) {
throw new CommunityException(e);
}
}
@RequestMapping(value = "/community/{id}/projects", produces = {
@ -163,7 +199,13 @@ public class CommunityApiController extends AbstractDnetController {
@PathVariable final String id,
@RequestBody final CommunityProject project) throws CommunityException {
return communityService.addCommunityProject(id, project);
try {
return communityService.addCommunityProject(id, project);
} catch (final ResourceNotFoundException e) {
throw e;
} catch (final Throwable e) {
throw new CommunityException(e);
}
}
@RequestMapping(value = "/community/{id}/projects", produces = {
@ -180,8 +222,13 @@ public class CommunityApiController extends AbstractDnetController {
public void deleteCommunityProject(
@PathVariable final String id,
@RequestParam final String projectId) throws CommunityException {
communityService.removeCommunityProjects(id, projectId);
try {
communityService.removeCommunityProjects(id, projectId);
} catch (final ResourceNotFoundException e) {
throw e;
} catch (final Throwable e) {
throw new CommunityException(e);
}
}
@RequestMapping(value = "/community/{id}/projectList", produces = {
@ -198,10 +245,14 @@ public class CommunityApiController extends AbstractDnetController {
public CommunityProject[] addCommunityProjectList(
@PathVariable final String id,
@RequestBody final CommunityProject[] projects) throws CommunityException {
communityService.addCommunityProjects(id, projects);
return projects;
try {
communityService.addCommunityProjects(id, projects);
return projects;
} catch (final ResourceNotFoundException e) {
throw e;
} catch (final Throwable e) {
throw new CommunityException(e);
}
}
@RequestMapping(value = "/community/{id}/projectList", produces = {
@ -218,8 +269,13 @@ public class CommunityApiController extends AbstractDnetController {
public void deleteCommunityProjectList(
@PathVariable final String id,
@RequestBody final String[] projectIdList) throws CommunityException {
communityService.removeCommunityProjects(id, projectIdList);
try {
communityService.removeCommunityProjects(id, projectIdList);
} catch (final ResourceNotFoundException e) {
throw e;
} catch (final Throwable e) {
throw new CommunityException(e);
}
}
@RequestMapping(value = "/community/{id}/funders", produces = {
@ -235,7 +291,13 @@ public class CommunityApiController extends AbstractDnetController {
})
public List<String> getCommunityFunders(@PathVariable final String id)
throws CommunityException {
return communityService.getCommunityFunders(id);
try {
return communityService.getCommunityFunders(id);
} catch (final ResourceNotFoundException e) {
throw e;
} catch (final Throwable e) {
throw new CommunityException(e);
}
}
@RequestMapping(value = "/community/{id}/contentproviders", produces = {
@ -250,7 +312,13 @@ public class CommunityApiController extends AbstractDnetController {
@ApiResponse(responseCode = "500", description = "unexpected error")
})
public List<CommunityContentprovider> getCommunityContentproviders(@PathVariable final String id) throws CommunityException {
return communityService.getCommunityContentproviders(id);
try {
return communityService.getCommunityContentproviders(id);
} catch (final ResourceNotFoundException e) {
throw e;
} catch (final Throwable e) {
throw new CommunityException(e);
}
}
@RequestMapping(value = "/community/{id}/contentproviders", produces = {
@ -268,9 +336,14 @@ public class CommunityApiController extends AbstractDnetController {
@PathVariable final String id,
@RequestBody final CommunityContentprovider contentprovider) throws CommunityException {
communityService.addCommunityContentProviders(id, contentprovider);
return contentprovider;
try {
communityService.addCommunityContentProviders(id, contentprovider);
return contentprovider;
} catch (final ResourceNotFoundException e) {
throw e;
} catch (final Throwable e) {
throw new CommunityException(e);
}
}
@RequestMapping(value = "/community/{id}/contentproviders", produces = {
@ -287,8 +360,13 @@ public class CommunityApiController extends AbstractDnetController {
public void removeCommunityContentprovider(
@PathVariable final String id,
@RequestParam final String contentproviderId) throws CommunityException {
communityService.removeCommunityContentProviders(id, contentproviderId);
try {
communityService.removeCommunityContentProviders(id, contentproviderId);
} catch (final ResourceNotFoundException e) {
throw e;
} catch (final Throwable e) {
throw new CommunityException(e);
}
}
@RequestMapping(value = "/community/{id}/contentprovidersList", produces = {
@ -306,9 +384,14 @@ public class CommunityApiController extends AbstractDnetController {
@PathVariable final String id,
@RequestBody final CommunityContentprovider[] contentprovidersList) throws CommunityException {
communityService.addCommunityContentProviders(id, contentprovidersList);
return contentprovidersList;
try {
communityService.addCommunityContentProviders(id, contentprovidersList);
return contentprovidersList;
} catch (final ResourceNotFoundException e) {
throw e;
} catch (final Throwable e) {
throw new CommunityException(e);
}
}
@RequestMapping(value = "/community/{id}/contentprovidersList", produces = {
@ -325,8 +408,13 @@ public class CommunityApiController extends AbstractDnetController {
public void deleteCommunityContentProvidersList(
@PathVariable final String id,
@RequestBody final String[] contentProviderIdList) throws CommunityException {
communityService.removeCommunityContentProviders(id, contentProviderIdList);
try {
communityService.removeCommunityContentProviders(id, contentProviderIdList);
} catch (final ResourceNotFoundException e) {
throw e;
} catch (final Throwable e) {
throw new CommunityException(e);
}
}
// ADDING CODE FOR COMMUNITY ORGANIZATIONS
@ -343,7 +431,13 @@ public class CommunityApiController extends AbstractDnetController {
@ApiResponse(responseCode = "500", description = "unexpected error")
})
public List<CommunityOrganization> getCommunityOrganizations(@PathVariable final String id) throws CommunityException {
return communityService.getCommunityOrganizations(id);
try {
return communityService.getCommunityOrganizations(id);
} catch (final ResourceNotFoundException e) {
throw e;
} catch (final Throwable e) {
throw new CommunityException(e);
}
}
@RequestMapping(value = "/community/{id}/organizations", produces = {
@ -360,10 +454,14 @@ public class CommunityApiController extends AbstractDnetController {
public CommunityOrganization addCommunityOrganization(
@PathVariable final String id,
@RequestBody final CommunityOrganization organization) throws CommunityException {
communityService.addCommunityOrganizations(id, organization);
return organization;
try {
communityService.addCommunityOrganizations(id, organization);
return organization;
} catch (final ResourceNotFoundException e) {
throw e;
} catch (final Throwable e) {
throw new CommunityException(e);
}
}
@RequestMapping(value = "/community/{id}/organizationList", produces = {
@ -381,9 +479,14 @@ public class CommunityApiController extends AbstractDnetController {
@PathVariable final String id,
@RequestBody final CommunityOrganization[] orgs) throws CommunityException {
communityService.addCommunityOrganizations(id, orgs);
return orgs;
try {
communityService.addCommunityOrganizations(id, orgs);
return orgs;
} catch (final ResourceNotFoundException e) {
throw e;
} catch (final Throwable e) {
throw new CommunityException(e);
}
}
@RequestMapping(value = "/community/{id}/organizations", produces = {
@ -400,8 +503,13 @@ public class CommunityApiController extends AbstractDnetController {
public void removeCommunityOrganization(
@PathVariable final String id,
@RequestParam final String organizationName) throws CommunityException {
communityService.removeCommunityOrganizations(id, organizationName);
try {
communityService.removeCommunityOrganizations(id, organizationName);
} catch (final ResourceNotFoundException e) {
throw e;
} catch (final Throwable e) {
throw new CommunityException(e);
}
}
@RequestMapping(value = "/community/{id}/organizationList", produces = {
@ -418,8 +526,13 @@ public class CommunityApiController extends AbstractDnetController {
public void removeCommunityOrganizationList(
@PathVariable final String id,
@RequestBody final String[] orgNames) throws CommunityException {
communityService.removeCommunityOrganizations(id, orgNames);
try {
communityService.removeCommunityOrganizations(id, orgNames);
} catch (final ResourceNotFoundException e) {
throw e;
} catch (final Throwable e) {
throw new CommunityException(e);
}
}
// **********************
@ -439,7 +552,13 @@ public class CommunityApiController extends AbstractDnetController {
@PathVariable final String id,
@RequestBody final String[] subjects) throws CommunityException {
return communityService.addCommunitySubjects(id, subjects);
try {
return communityService.addCommunitySubjects(id, subjects);
} catch (final ResourceNotFoundException e) {
throw e;
} catch (final Throwable e) {
throw new CommunityException(e);
}
}
@RequestMapping(value = "/community/{id}/subjects", produces = {
@ -457,7 +576,13 @@ public class CommunityApiController extends AbstractDnetController {
@PathVariable final String id,
@RequestBody final String[] subjects) throws CommunityException {
return communityService.removeCommunitySubjects(id, subjects);
try {
return communityService.removeCommunitySubjects(id, subjects);
} catch (final ResourceNotFoundException e) {
throw e;
} catch (final Throwable e) {
throw new CommunityException(e);
}
}
@RequestMapping(value = "/community/{id}/fos", produces = {
@ -475,7 +600,13 @@ public class CommunityApiController extends AbstractDnetController {
@PathVariable final String id,
@RequestBody final String[] subjects) throws CommunityException {
return communityService.addCommunityFOS(id, subjects);
try {
return communityService.addCommunityFOS(id, subjects);
} catch (final ResourceNotFoundException e) {
throw e;
} catch (final Throwable e) {
throw new CommunityException(e);
}
}
@RequestMapping(value = "/community/{id}/fos", produces = {
@ -493,7 +624,13 @@ public class CommunityApiController extends AbstractDnetController {
@PathVariable final String id,
@RequestBody final String[] subjects) throws CommunityException {
return communityService.removeCommunityFOS(id, subjects);
try {
return communityService.removeCommunityFOS(id, subjects);
} catch (final ResourceNotFoundException e) {
throw e;
} catch (final Throwable e) {
throw new CommunityException(e);
}
}
@RequestMapping(value = "/community/{id}/sdg", produces = {
@ -511,7 +648,13 @@ public class CommunityApiController extends AbstractDnetController {
@PathVariable final String id,
@RequestBody final String[] subjects) throws CommunityException {
return communityService.addCommunitySDG(id, subjects);
try {
return communityService.addCommunitySDG(id, subjects);
} catch (final ResourceNotFoundException e) {
throw e;
} catch (final Throwable e) {
throw new CommunityException(e);
}
}
@RequestMapping(value = "/community/{id}/sdg", produces = {
@ -529,7 +672,13 @@ public class CommunityApiController extends AbstractDnetController {
@PathVariable final String id,
@RequestBody final String[] subjects) throws CommunityException {
return communityService.removeCommunitySDG(id, subjects);
try {
return communityService.removeCommunitySDG(id, subjects);
} catch (final ResourceNotFoundException e) {
throw e;
} catch (final Throwable e) {
throw new CommunityException(e);
}
}
@RequestMapping(value = "/community/{id}/advancedConstraint", produces = {
@ -547,7 +696,13 @@ public class CommunityApiController extends AbstractDnetController {
@PathVariable final String id,
@RequestBody final SelectionCriteria advancedConstraint) throws CommunityException {
return communityService.addCommunityAdvancedConstraint(id, advancedConstraint);
try {
return communityService.addCommunityAdvancedConstraint(id, advancedConstraint);
} catch (final ResourceNotFoundException e) {
throw e;
} catch (final Throwable e) {
throw new CommunityException(e);
}
}
@RequestMapping(value = "/community/{id}/advancedConstraint", produces = {
@ -564,7 +719,13 @@ public class CommunityApiController extends AbstractDnetController {
public CommunityDetails removeAdvancedConstraint(
@PathVariable final String id) throws CommunityException {
return communityService.removeCommunityAdvancedConstraint(id);
try {
return communityService.removeCommunityAdvancedConstraint(id);
} catch (final ResourceNotFoundException e) {
throw e;
} catch (final Throwable e) {
throw new CommunityException(e);
}
}
@RequestMapping(value = "/community/{id}/removeConstraint", produces = {
@ -582,7 +743,13 @@ public class CommunityApiController extends AbstractDnetController {
@PathVariable final String id,
@RequestBody final SelectionCriteria removeConstraint) throws CommunityException {
return communityService.addCommunityRemoveConstraint(id, removeConstraint);
try {
return communityService.addCommunityRemoveConstraint(id, removeConstraint);
} catch (final ResourceNotFoundException e) {
throw e;
} catch (final Throwable e) {
throw new CommunityException(e);
}
}
@RequestMapping(value = "/community/{id}/removeConstraint", produces = {
@ -598,7 +765,13 @@ public class CommunityApiController extends AbstractDnetController {
})
public CommunityDetails removeRemoveConstraint(@PathVariable final String id) throws CommunityException {
return communityService.removeCommunityRemoveConstraint(id);
try {
return communityService.removeCommunityRemoveConstraint(id);
} catch (final ResourceNotFoundException e) {
throw e;
} catch (final Throwable e) {
throw new CommunityException(e);
}
}
@RequestMapping(value = "/community/{id}/zenodocommunities", produces = {
@ -617,7 +790,13 @@ public class CommunityApiController extends AbstractDnetController {
@RequestParam(required = false, defaultValue = "false") final boolean main,
@RequestParam final String zenodocommunity) throws CommunityException {
return communityService.addCommunityZenodoCommunity(id, zenodocommunity, main);
try {
return communityService.addCommunityZenodoCommunity(id, zenodocommunity, main);
} catch (final ResourceNotFoundException e) {
throw e;
} catch (final Throwable e) {
throw new CommunityException(e);
}
}
@ -636,8 +815,13 @@ public class CommunityApiController extends AbstractDnetController {
@PathVariable final String id,
@RequestParam(required = false, defaultValue = "false") final boolean main,
@RequestParam final String zenodocommunity) throws CommunityException {
communityService.removeCommunityZenodoCommunity(id, zenodocommunity, main);
try {
communityService.removeCommunityZenodoCommunity(id, zenodocommunity, main);
} catch (final ResourceNotFoundException e) {
throw e;
} catch (final Throwable e) {
throw new CommunityException(e);
}
}
@RequestMapping(value = "/community/{zenodoId}/openairecommunities", produces = {
@ -652,10 +836,16 @@ public class CommunityApiController extends AbstractDnetController {
@ApiResponse(responseCode = "500", description = "unexpected error")
})
public CommunityOpenAIRECommunities getOpenAireCommunities(@PathVariable final String zenodoId) throws CommunityException {
final CommunityOpenAIRECommunities res = new CommunityOpenAIRECommunities();
res.setZenodoid(zenodoId);
res.setOpenAirecommunitylist(communityService.getOpenAIRECommunitiesByZenodoId(zenodoId));
return res;
try {
final CommunityOpenAIRECommunities res = new CommunityOpenAIRECommunities();
res.setZenodoid(zenodoId);
res.setOpenAirecommunitylist(communityService.getOpenAIRECommunitiesByZenodoId(zenodoId));
return res;
} catch (final ResourceNotFoundException e) {
throw e;
} catch (final Throwable e) {
throw new CommunityException(e);
}
}
// APIs to manage the propagationOrganizationCommunityMap
@ -671,13 +861,19 @@ public class CommunityApiController extends AbstractDnetController {
@ApiResponse(responseCode = "500", description = "unexpected error")
})
public Map<String, Set<String>> getPropagationOrganizationCommunityMap() throws CommunityException {
return communityService.getPropagationOrganizationCommunityMap();
try {
return communityService.getPropagationOrganizationCommunityMap();
} catch (final ResourceNotFoundException e) {
throw e;
} catch (final Throwable e) {
throw new CommunityException(e);
}
}
@RequestMapping(value = "/community/{id}/propagationOrganizations", produces = {
"application/json"
}, method = RequestMethod.GET)
@Operation(summary = "return the propagation organizations of a community", description = "return the propagation organizations of a community", tags = {
@Operation(summary = "try { return the propagation organizations of a community", description = "try { return the propagation organizations of a community", tags = {
C_O, R
})
@ApiResponses(value = {
@ -686,7 +882,13 @@ public class CommunityApiController extends AbstractDnetController {
@ApiResponse(responseCode = "500", description = "unexpected error")
})
public Set<String> getPropagationOrganizationsForCommunity(@PathVariable final String id) throws CommunityException {
return communityService.getPropagationOrganizationsForCommunity(id);
try {
return communityService.getPropagationOrganizationsForCommunity(id);
} catch (final ResourceNotFoundException e) {
throw e;
} catch (final Throwable e) {
throw new CommunityException(e);
}
}
@RequestMapping(value = "/community/{id}/propagationOrganizations", produces = {
@ -703,7 +905,13 @@ public class CommunityApiController extends AbstractDnetController {
public Set<String> addPropagationOrganizationForCommunity(@PathVariable final String id,
@RequestParam final String organizationId) throws CommunityException {
return communityService.addPropagationOrganizationForCommunity(id, organizationId.split(","));
try {
return communityService.addPropagationOrganizationForCommunity(id, organizationId.split(","));
} catch (final ResourceNotFoundException e) {
throw e;
} catch (final Throwable e) {
throw new CommunityException(e);
}
}
@RequestMapping(value = "/community/{id}/propagationOrganizations", produces = {
@ -720,7 +928,13 @@ public class CommunityApiController extends AbstractDnetController {
public Set<String> removePropagationOrganizationForCommunity(@PathVariable final String id,
@RequestParam final String organizationId) throws CommunityException {
return communityService.removePropagationOrganizationForCommunity(id, organizationId.split(","));
try {
return communityService.removePropagationOrganizationForCommunity(id, organizationId.split(","));
} catch (final ResourceNotFoundException e) {
throw e;
} catch (final Throwable e) {
throw new CommunityException(e);
}
}
}

View File

@ -72,16 +72,11 @@ public class CommunityService {
private static final Log log = LogFactory.getLog(CommunityService.class);
public List<CommunitySummary> listCommunities() throws CommunityException {
try {
return dbCommunityRepository.findAll()
.stream()
.map(CommunityMappingUtils::toCommunitySummary)
.collect(Collectors.toList());
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
public List<CommunitySummary> listCommunities() {
return dbCommunityRepository.findAll()
.stream()
.map(CommunityMappingUtils::toCommunitySummary)
.collect(Collectors.toList());
}
@Transactional
@ -98,39 +93,24 @@ public class CommunityService {
}
@Transactional
public CommunityDetails saveCommunity(final CommunityDetails details) throws CommunityException {
try {
details.setLastUpdateDate(LocalDateTime.now());
dbCommunityRepository.save(CommunityMappingUtils.toCommunity(details));
return getCommunity(details.getId());
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
public CommunityDetails saveCommunity(final CommunityDetails details) {
details.setLastUpdateDate(LocalDateTime.now());
dbCommunityRepository.save(CommunityMappingUtils.toCommunity(details));
return getCommunity(details.getId());
}
@Transactional
public CommunityDetails getCommunity(final String id) throws CommunityException {
try {
final DbCommunity c = dbCommunityRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Community not found: " + id));
return CommunityMappingUtils.toCommunityDetails(c);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
public CommunityDetails getCommunity(final String id) {
final DbCommunity c = dbCommunityRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Community not found: " + id));
return CommunityMappingUtils.toCommunityDetails(c);
}
@Transactional
public void setCommunity(final String id, final CommunityWritableProperties details) throws CommunityException {
try {
final DbCommunity c = dbCommunityRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Community not found: " + id));
CommunityMappingUtils.populateCommunity(c, details);
c.setLastUpdateDate(LocalDateTime.now());
dbCommunityRepository.save(c);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
public void setCommunity(final String id, final CommunityWritableProperties details) {
final DbCommunity c = dbCommunityRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Community not found: " + id));
CommunityMappingUtils.populateCommunity(c, details);
c.setLastUpdateDate(LocalDateTime.now());
dbCommunityRepository.save(c);
}
@Transactional
@ -139,9 +119,7 @@ public class CommunityService {
final String filter,
final int page,
final int size,
final String orderBy)
throws CommunityException {
final String orderBy) throws CommunityException {
if (StringUtils.isBlank(id)) { throw new CommunityException("Empty ID"); }
try {
final Sort sort;
@ -203,15 +181,10 @@ public class CommunityService {
}
@Transactional
public CommunityProject addCommunityProject(final String id, final CommunityProject project) throws CommunityException {
try {
final DbProject p = CommunityMappingUtils.toDbProject(id, project);
dbProjectRepository.save(p);
return project;
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
public CommunityProject addCommunityProject(final String id, final CommunityProject project) {
final DbProject p = CommunityMappingUtils.toDbProject(id, project);
dbProjectRepository.save(p);
return project;
}
@Transactional
@ -229,249 +202,146 @@ public class CommunityService {
}
@Transactional
public void removeCommunityProjects(final String id, final String... ids) throws CommunityException {
try {
final List<DbProjectPK> list = Arrays.stream(ids)
.map(projectId -> new DbProjectPK(id, projectId))
.collect(Collectors.toList());
dbProjectRepository.deleteAllById(list);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
public void removeCommunityProjects(final String id, final String... ids) {
final List<DbProjectPK> list = Arrays.stream(ids)
.map(projectId -> new DbProjectPK(id, projectId))
.collect(Collectors.toList());
dbProjectRepository.deleteAllById(list);
}
public List<CommunityContentprovider> getCommunityContentproviders(final String id) throws CommunityException {
try {
return dbDatasourceRepository.findByCommunity(id)
.stream()
.map(CommunityMappingUtils::toCommunityContentprovider)
.collect(Collectors.toList());
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
public List<CommunityContentprovider> getCommunityContentproviders(final String id) {
return dbDatasourceRepository.findByCommunity(id)
.stream()
.map(CommunityMappingUtils::toCommunityContentprovider)
.collect(Collectors.toList());
}
@Transactional
public void addCommunityContentProviders(final String id, final CommunityContentprovider... contentproviders)
throws CommunityException {
try {
final List<DbDatasource> list = Arrays.stream(contentproviders)
.map(cp -> CommunityMappingUtils.toDbDatasource(id, cp))
.collect(Collectors.toList());
public void addCommunityContentProviders(final String id, final CommunityContentprovider... contentproviders) {
final List<DbDatasource> list = Arrays.stream(contentproviders)
.map(cp -> CommunityMappingUtils.toDbDatasource(id, cp))
.collect(Collectors.toList());
dbDatasourceRepository.saveAll(list);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
dbDatasourceRepository.saveAll(list);
}
@Transactional
public void removeCommunityContentProviders(final String id, final String... ids) throws CommunityException {
try {
final List<DbDatasourcePK> list = Arrays.stream(ids)
.map(dsId -> new DbDatasourcePK(id, dsId))
.collect(Collectors.toList());
dbDatasourceRepository.deleteAllById(list);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
public void removeCommunityContentProviders(final String id, final String... ids) {
final List<DbDatasourcePK> list = Arrays.stream(ids)
.map(dsId -> new DbDatasourcePK(id, dsId))
.collect(Collectors.toList());
dbDatasourceRepository.deleteAllById(list);
}
@Transactional
public void removeCommunityOrganizations(final String id, final String... orgNames) throws CommunityException {
try {
final List<DbSupportOrgPK> list = Arrays.stream(orgNames)
.map(name -> new DbSupportOrgPK(id, name))
.collect(Collectors.toList());
dbSupportOrgRepository.deleteAllById(list);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
public void removeCommunityOrganizations(final String id, final String... orgNames) {
final List<DbSupportOrgPK> list = Arrays.stream(orgNames)
.map(name -> new DbSupportOrgPK(id, name))
.collect(Collectors.toList());
dbSupportOrgRepository.deleteAllById(list);
}
@Transactional
public List<CommunityOrganization> getCommunityOrganizations(final String id) throws CommunityException {
try {
return dbSupportOrgRepository.findByCommunity(id)
.stream()
.map(CommunityMappingUtils::toCommunityOrganization)
.collect(Collectors.toList());
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
public List<CommunityOrganization> getCommunityOrganizations(final String id) {
return dbSupportOrgRepository.findByCommunity(id)
.stream()
.map(CommunityMappingUtils::toCommunityOrganization)
.collect(Collectors.toList());
}
@Transactional
public void addCommunityOrganizations(final String id, final CommunityOrganization... orgs) throws CommunityException {
try {
final List<DbSupportOrg> list = Arrays.stream(orgs)
.map(o -> CommunityMappingUtils.toDbSupportOrg(id, o))
.collect(Collectors.toList());
public void addCommunityOrganizations(final String id, final CommunityOrganization... orgs) {
final List<DbSupportOrg> list = Arrays.stream(orgs)
.map(o -> CommunityMappingUtils.toDbSupportOrg(id, o))
.collect(Collectors.toList());
dbSupportOrgRepository.saveAll(list);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
dbSupportOrgRepository.saveAll(list);
}
@Transactional
public void removeSubCommunity(final String id, final String subCommunityId) throws CommunityException {
try {
dbSubCommunityRepository.deleteById(subCommunityId);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
public void removeSubCommunity(final String id, final String subCommunityId) {
dbSubCommunityRepository.deleteById(subCommunityId);
}
@Transactional
public List<SubCommunity> getSubCommunities(final String id) throws CommunityException {
try {
return dbSubCommunityRepository.findByCommunity(id)
.stream()
.map(CommunityMappingUtils::toSubCommunity)
.collect(Collectors.toList());
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
public List<SubCommunity> getSubCommunities(final String id) {
return dbSubCommunityRepository.findByCommunity(id)
.stream()
.map(CommunityMappingUtils::toSubCommunity)
.collect(Collectors.toList());
}
@Transactional
public void addSubCommunities(final SubCommunity... subs) throws CommunityException {
try {
final List<DbSubCommunity> list = Arrays.stream(subs)
.map(CommunityMappingUtils::toDbSubCommunity)
.collect(Collectors.toList());
public void addSubCommunities(final SubCommunity... subs) {
final List<DbSubCommunity> list = Arrays.stream(subs)
.map(CommunityMappingUtils::toDbSubCommunity)
.collect(Collectors.toList());
dbSubCommunityRepository.saveAll(list);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
dbSubCommunityRepository.saveAll(list);
}
@Transactional
public CommunityDetails addCommunitySubjects(final String id, final String... subjects) throws CommunityException {
try {
return modifyElementToArrayField(id, c -> c.getSubjects(), (c, subs) -> c.setSubjects(subs), false, subjects);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
public CommunityDetails addCommunitySubjects(final String id, final String... subjects) {
return modifyElementToArrayField(id, c -> c.getSubjects(), (c, subs) -> c.setSubjects(subs), false, subjects);
}
public CommunityDetails removeCommunitySubjects(final String id, final String... subjects) throws CommunityException {
try {
return modifyElementToArrayField(id, c -> c.getSubjects(), (c, subs) -> c.setSubjects(subs), true, subjects);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
public CommunityDetails removeCommunitySubjects(final String id, final String... subjects) {
return modifyElementToArrayField(id, c -> c.getSubjects(), (c, subs) -> c.setSubjects(subs), true, subjects);
}
public CommunityDetails addCommunityFOS(final String id, final String... foss) throws CommunityException {
try {
return modifyElementToArrayField(id, c -> c.getFos(), (c, fos) -> c.setFos(fos), false, foss);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
public CommunityDetails addCommunityFOS(final String id, final String... foss) {
return modifyElementToArrayField(id, c -> c.getFos(), (c, fos) -> c.setFos(fos), false, foss);
}
public CommunityDetails removeCommunityFOS(final String id, final String... foss) throws CommunityException {
try {
return modifyElementToArrayField(id, c -> c.getFos(), (c, fos) -> c.setFos(fos), true, foss);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
public CommunityDetails removeCommunityFOS(final String id, final String... foss) {
return modifyElementToArrayField(id, c -> c.getFos(), (c, fos) -> c.setFos(fos), true, foss);
}
public CommunityDetails addCommunitySDG(final String id, final String... sdgs) throws CommunityException {
try {
return modifyElementToArrayField(id, c -> c.getSdg(), (c, sdg) -> c.setSdg(sdg), false, sdgs);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
public CommunityDetails addCommunitySDG(final String id, final String... sdgs) {
return modifyElementToArrayField(id, c -> c.getSdg(), (c, sdg) -> c.setSdg(sdg), false, sdgs);
}
public CommunityDetails removeCommunitySDG(final String id, final String... sdgs) throws CommunityException {
try {
return modifyElementToArrayField(id, c -> c.getSdg(), (c, sdg) -> c.setSdg(sdg), true, sdgs);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
public CommunityDetails removeCommunitySDG(final String id, final String... sdgs) {
return modifyElementToArrayField(id, c -> c.getSdg(), (c, sdg) -> c.setSdg(sdg), true, sdgs);
}
@Transactional
public CommunityDetails addCommunityAdvancedConstraint(final String id, final SelectionCriteria advancedCosntraint)
throws CommunityException {
try {
final DbCommunity dbEntry = dbCommunityRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Community not found: " + id));
dbEntry.setAdvancedConstraints(advancedCosntraint);
dbEntry.setLastUpdateDate(LocalDateTime.now());
dbCommunityRepository.save(dbEntry);
return getCommunity(id);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
public CommunityDetails addCommunityAdvancedConstraint(final String id, final SelectionCriteria advancedCosntraint) {
final DbCommunity dbEntry = dbCommunityRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Community not found: " + id));
dbEntry.setAdvancedConstraints(advancedCosntraint);
dbEntry.setLastUpdateDate(LocalDateTime.now());
dbCommunityRepository.save(dbEntry);
return getCommunity(id);
}
@Transactional
public CommunityDetails removeCommunityAdvancedConstraint(final String id) throws CommunityException {
try {
final DbCommunity dbEntry = dbCommunityRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Community not found: " + id));
dbEntry.setAdvancedConstraints(null);
dbEntry.setLastUpdateDate(LocalDateTime.now());
dbCommunityRepository.save(dbEntry);
return getCommunity(id);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
public CommunityDetails removeCommunityAdvancedConstraint(final String id) {
final DbCommunity dbEntry = dbCommunityRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Community not found: " + id));
dbEntry.setAdvancedConstraints(null);
dbEntry.setLastUpdateDate(LocalDateTime.now());
dbCommunityRepository.save(dbEntry);
return getCommunity(id);
}
@Transactional
public CommunityDetails addCommunityRemoveConstraint(final String id, final SelectionCriteria removeConstraint) throws CommunityException {
try {
final DbCommunity dbEntry = dbCommunityRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Community not found: " + id));
dbEntry.setRemoveConstraints(removeConstraint);
dbEntry.setLastUpdateDate(LocalDateTime.now());
dbCommunityRepository.save(dbEntry);
return getCommunity(id);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
public CommunityDetails addCommunityRemoveConstraint(final String id, final SelectionCriteria removeConstraint) {
final DbCommunity dbEntry = dbCommunityRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Community not found: " + id));
dbEntry.setRemoveConstraints(removeConstraint);
dbEntry.setLastUpdateDate(LocalDateTime.now());
dbCommunityRepository.save(dbEntry);
return getCommunity(id);
}
@Transactional
public CommunityDetails removeCommunityRemoveConstraint(final String id) throws CommunityException {
try {
final DbCommunity dbEntry = dbCommunityRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Community not found: " + id));
dbEntry.setRemoveConstraints(null);
dbEntry.setLastUpdateDate(LocalDateTime.now());
dbCommunityRepository.save(dbEntry);
return getCommunity(id);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
public CommunityDetails removeCommunityRemoveConstraint(final String id) {
final DbCommunity dbEntry = dbCommunityRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Community not found: " + id));
dbEntry.setRemoveConstraints(null);
dbEntry.setLastUpdateDate(LocalDateTime.now());
dbCommunityRepository.save(dbEntry);
return getCommunity(id);
}
public CommunityDetails removeCommunityZenodoCommunity(final String id, final String zenodoCommunity, final boolean isMain)
throws CommunityException {
public CommunityDetails removeCommunityZenodoCommunity(final String id, final String zenodoCommunity, final boolean isMain) {
if (isMain) {
return updateElementToSimpleField(id, (c, val) -> c.setMainZenodoCommunity(val), null);
} else {
@ -479,34 +349,23 @@ public class CommunityService {
}
}
public CommunityDetails addCommunityZenodoCommunity(final String id, final String zenodoCommunity, final boolean isMain)
throws CommunityException {
try {
if (isMain) {
return updateElementToSimpleField(id, (c, val) -> c.setMainZenodoCommunity(val), zenodoCommunity);
} else {
return modifyElementToArrayField(id, c -> c.getOtherZenodoCommunities(), (c, arr) -> c.setOtherZenodoCommunities(arr), false, zenodoCommunity);
}
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
public CommunityDetails addCommunityZenodoCommunity(final String id, final String zenodoCommunity, final boolean isMain) {
if (isMain) {
return updateElementToSimpleField(id, (c, val) -> c.setMainZenodoCommunity(val), zenodoCommunity);
} else {
return modifyElementToArrayField(id, c -> c.getOtherZenodoCommunities(), (c, arr) -> c.setOtherZenodoCommunities(arr), false, zenodoCommunity);
}
}
@Transactional
private CommunityDetails updateElementToSimpleField(final String id,
final BiConsumer<DbCommunity, String> setter,
final String value) throws CommunityException {
try {
final DbCommunity dbEntry = dbCommunityRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Community not found: " + id));
setter.accept(dbEntry, value);
dbEntry.setLastUpdateDate(LocalDateTime.now());
dbCommunityRepository.save(dbEntry);
return getCommunity(id);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
final String value) {
final DbCommunity dbEntry = dbCommunityRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Community not found: " + id));
setter.accept(dbEntry, value);
dbEntry.setLastUpdateDate(LocalDateTime.now());
dbCommunityRepository.save(dbEntry);
return getCommunity(id);
}
@Transactional
@ -514,7 +373,7 @@ public class CommunityService {
final Function<DbCommunity, String[]> getter,
final BiConsumer<DbCommunity, String[]> setter,
final boolean remove,
final String... values) throws CommunityException {
final String... values) {
final DbCommunity dbEntry = dbCommunityRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Community not found: " + id));
@ -541,66 +400,41 @@ public class CommunityService {
}
@Transactional
public List<String> getOpenAIRECommunitiesByZenodoId(final String zenodoId) throws CommunityException {
try {
return dbCommunityRepository.findByZenodoId(zenodoId);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
public List<String> getOpenAIRECommunitiesByZenodoId(final String zenodoId) {
return dbCommunityRepository.findByZenodoId(zenodoId);
}
@Transactional
public Map<String, Set<String>> getPropagationOrganizationCommunityMap() throws CommunityException {
try {
return dbOrganizationRepository.findAll()
.stream()
.collect(Collectors.groupingBy(DbOrganization::getOrgId, Collectors.mapping(DbOrganization::getCommunity, Collectors.toSet())));
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
public Map<String, Set<String>> getPropagationOrganizationCommunityMap() {
return dbOrganizationRepository.findAll()
.stream()
.collect(Collectors.groupingBy(DbOrganization::getOrgId, Collectors.mapping(DbOrganization::getCommunity, Collectors.toSet())));
}
@Transactional
public Set<String> getPropagationOrganizationsForCommunity(final String communityId) throws CommunityException {
try {
return dbOrganizationRepository.findByCommunity(communityId)
.stream()
.map(DbOrganization::getOrgId)
.collect(Collectors.toSet());
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
public Set<String> getPropagationOrganizationsForCommunity(final String communityId) {
return dbOrganizationRepository.findByCommunity(communityId)
.stream()
.map(DbOrganization::getOrgId)
.collect(Collectors.toSet());
}
@Transactional
public Set<String> addPropagationOrganizationForCommunity(final String communityId, final String... organizationIds) throws CommunityException {
try {
for (final String orgId : organizationIds) {
final DbOrganization o = new DbOrganization(communityId.trim(), orgId.trim());
dbOrganizationRepository.save(o);
}
return getPropagationOrganizationsForCommunity(communityId);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
public Set<String> addPropagationOrganizationForCommunity(final String communityId, final String... organizationIds) {
for (final String orgId : organizationIds) {
final DbOrganization o = new DbOrganization(communityId.trim(), orgId.trim());
dbOrganizationRepository.save(o);
}
return getPropagationOrganizationsForCommunity(communityId);
}
@Transactional
public Set<String> removePropagationOrganizationForCommunity(final String communityId, final String... organizationIds) throws CommunityException {
try {
for (final String orgId : organizationIds) {
final DbOrganization o = new DbOrganization(communityId.trim(), orgId.trim());
dbOrganizationRepository.delete(o);
}
return getPropagationOrganizationsForCommunity(communityId);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
public Set<String> removePropagationOrganizationForCommunity(final String communityId, final String... organizationIds) {
for (final String orgId : organizationIds) {
final DbOrganization o = new DbOrganization(communityId.trim(), orgId.trim());
dbOrganizationRepository.delete(o);
}
return getPropagationOrganizationsForCommunity(communityId);
}
@Transactional
@ -616,23 +450,18 @@ public class CommunityService {
}
@Transactional
public List<IISConfigurationEntry> getIISConfiguration(final String id) throws CommunityException {
try {
final List<IISConfigurationEntry> res = new ArrayList<>();
public List<IISConfigurationEntry> getIISConfiguration(final String id) {
final List<IISConfigurationEntry> res = new ArrayList<>();
res.add(dbCommunityRepository.findById(id)
.map(CommunityMappingUtils::asIISConfigurationEntry)
.orElseThrow(() -> new ResourceNotFoundException("Community not found: " + id)));
res.add(dbCommunityRepository.findById(id)
.map(CommunityMappingUtils::asIISConfigurationEntry)
.orElseThrow(() -> new ResourceNotFoundException("Community not found: " + id)));
for (final DbSubCommunity subc : dbSubCommunityRepository.findByCommunity(id)) {
res.add(CommunityMappingUtils.asIISConfigurationEntry(subc));
}
return res;
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
for (final DbSubCommunity subc : dbSubCommunityRepository.findByCommunity(id)) {
res.add(CommunityMappingUtils.asIISConfigurationEntry(subc));
}
return res;
}
@Transactional

View File

@ -17,9 +17,10 @@ import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import eu.dnetlib.common.controller.AbstractDnetController;
import eu.dnetlib.openaire.common.AbstractExporterController;
import eu.dnetlib.openaire.community.CommunityService;
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;
@ -37,7 +38,7 @@ import io.swagger.v3.oas.annotations.tags.Tag;
})
@ConditionalOnProperty(value = "openaire.exporter.enable.community", havingValue = "true")
@Tag(name = "OpenAIRE Context API", description = "the OpenAIRE Context API")
public class ContextApiController extends AbstractDnetController {
public class ContextApiController extends AbstractExporterController {
@Autowired
private CommunityService communityService;
@ -50,21 +51,28 @@ public class ContextApiController extends AbstractDnetController {
@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 = "404", description = "not found"),
@ApiResponse(responseCode = "500", description = "unexpected error")
})
public List<ContextSummary> listContexts(@RequestParam(required = false) final Set<CommunityType> 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());
try {
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());
} catch (final ResourceNotFoundException e) {
throw e;
} catch (final Throwable e) {
throw new CommunityException(e);
}
}
@RequestMapping(value = "/context/{contextId}", produces = {
@ -73,6 +81,7 @@ public class ContextApiController extends AbstractDnetController {
@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 = "404", description = "not found"),
@ApiResponse(responseCode = "500", description = "unexpected error")
})
public List<CategorySummary> listCategories(
@ -94,8 +103,9 @@ public class ContextApiController extends AbstractDnetController {
})
.distinct()
.collect(Collectors.toList());
} catch (final ResourceNotFoundException e) {
throw e;
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
}
@ -106,10 +116,17 @@ public class ContextApiController extends AbstractDnetController {
@Operation(summary = "return a list of entries for IIS", description = "return a list of entries for IIS")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "404", description = "not found"),
@ApiResponse(responseCode = "500", description = "unexpected error")
})
public List<IISConfigurationEntry> getIISConfiguration(@PathVariable final String contextId) throws CommunityException {
return communityService.getIISConfiguration(contextId);
try {
return communityService.getIISConfiguration(contextId);
} catch (final ResourceNotFoundException e) {
throw e;
} catch (final Throwable e) {
throw new CommunityException(e);
}
}
@RequestMapping(value = "/context/category/{categoryId}", produces = {
@ -118,24 +135,30 @@ public class ContextApiController extends AbstractDnetController {
@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 = "404", description = "not found"),
@ApiResponse(responseCode = "500", description = "unexpected error")
})
public List<ConceptSummary> listConcepts(
@PathVariable final String categoryId,
@RequestParam(required = false, defaultValue = "false") final boolean all) throws CommunityException {
try {
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[] 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<SubCommunity> list = findSubCommunities(categoryId + "::", all, contextId);
return processSubCommunities(null, list);
} catch (final ResourceNotFoundException e) {
throw e;
} catch (final Throwable e) {
throw new CommunityException(e);
}
final String contextId = parts[0];
final List<SubCommunity> list = findSubCommunities(categoryId + "::", all, contextId);
return processSubCommunities(null, list);
}
@RequestMapping(value = "/context/category/concept/{conceptId}", produces = {
@ -144,23 +167,30 @@ public class ContextApiController extends AbstractDnetController {
@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 = "404", description = "not found"),
@ApiResponse(responseCode = "500", description = "unexpected error")
})
public List<ConceptSummary> listSubConcepts(
@PathVariable final String conceptId,
@RequestParam(required = false, defaultValue = "false") final boolean all) throws CommunityException {
try {
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[] 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(conceptId, list);
} catch (final ResourceNotFoundException e) {
throw e;
} catch (final Throwable e) {
throw new CommunityException(e);
}
final String contextId = parts[0];
final List<SubCommunity> list = findSubCommunities(conceptId + "::", all, contextId);
return processSubCommunities(conceptId, list);
}
private List<SubCommunity> findSubCommunities(final String prefix, final boolean all, final String contextId) throws CommunityException {

View File

@ -13,7 +13,7 @@ import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import eu.dnetlib.openaire.common.ISClient;
import eu.dnetlib.openaire.exporter.exceptions.ContextException;
import eu.dnetlib.openaire.exporter.exceptions.CommunityException;
import eu.dnetlib.openaire.exporter.model.context.Category;
import eu.dnetlib.openaire.exporter.model.context.CategorySummary;
import eu.dnetlib.openaire.exporter.model.context.Concept;
@ -31,7 +31,7 @@ public class ContextApiCore {
@Autowired
private ISClient isClient;
public List<ContextSummary> listContexts(final List<String> type) throws ContextException {
public List<ContextSummary> listContexts(final List<String> type) throws CommunityException {
return getContextMap(type).values()
.stream()
@ -48,7 +48,7 @@ public class ContextApiCore {
.collect(Collectors.toList());
}
public List<CategorySummary> listCategories(final String contextId, final Boolean all) throws ContextException {
public List<CategorySummary> listCategories(final String contextId, final Boolean all) throws CommunityException {
final Stream<Category> categories = getContextMap().get(contextId).getCategories().values().stream();
return all ? asCategorySummaries(categories) : asCategorySummaries(categories.filter(Category::isClaim));
}
@ -62,7 +62,7 @@ public class ContextApiCore {
.collect(Collectors.toList());
}
public List<ConceptSummary> listConcepts(final String categoryId, final Boolean all) throws ContextException {
public List<ConceptSummary> listConcepts(final String categoryId, final Boolean all) throws CommunityException {
final String contextId = StringUtils.substringBefore(categoryId, SEPARATOR);
final Stream<Concept> concepts = getContextMap().get(contextId)
.getCategories()
@ -82,9 +82,9 @@ public class ContextApiCore {
.collect(Collectors.toList());
}
public List<ConceptSummary> listSubConcepts(final String conceptId, final Boolean all) throws ContextException {
public List<ConceptSummary> listSubConcepts(final String conceptId, final Boolean all) throws CommunityException {
final List<String> ids = Splitter.on(SEPARATOR).splitToList(conceptId);
if (ids.size() < 3) { throw new ContextException(""); }
if (ids.size() < 3) { throw new CommunityException(""); }
final String contextId = ids.get(0);
final String categoryId = contextId + SEPARATOR + ids.get(1);
@ -110,15 +110,15 @@ public class ContextApiCore {
.collect(Collectors.toList());
}
private Map<String, Context> getContextMap() throws ContextException {
private Map<String, Context> getContextMap() throws CommunityException {
return getContextMap(Lists.newArrayList());
}
private Map<String, Context> getContextMap(final List<String> type) throws ContextException {
private Map<String, Context> getContextMap(final List<String> type) throws CommunityException {
try {
return isClient.getContextMap(type);
} catch (final IOException e) {
throw new ContextException(e);
throw new CommunityException(e);
}
}

View File

@ -1,19 +0,0 @@
package eu.dnetlib.openaire.exporter.exceptions;
import java.io.IOException;
public class ContextException extends Exception {
/**
*
*/
private static final long serialVersionUID = -5489369676370127052L;
public ContextException(final String message) {
super(message);
}
public ContextException(final IOException e) {
super(e);
}
}

View File

@ -1,17 +0,0 @@
package eu.dnetlib.openaire.exporter.exceptions;
public class ContextNotFoundException extends Exception {
/**
*
*/
private static final long serialVersionUID = -2026506752817353752L;
public ContextNotFoundException(final String msg) {
super(msg);
}
public ContextNotFoundException(final Exception e) {
super(e);
}
}