diff --git a/apps/dnet-exporter-api/src/main/java/eu/dnetlib/openaire/community/CommunityApiController.java b/apps/dnet-exporter-api/src/main/java/eu/dnetlib/openaire/community/CommunityApiController.java index 6a030010..a80703d8 100644 --- a/apps/dnet-exporter-api/src/main/java/eu/dnetlib/openaire/community/CommunityApiController.java +++ b/apps/dnet-exporter-api/src/main/java/eu/dnetlib/openaire/community/CommunityApiController.java @@ -294,9 +294,7 @@ public class CommunityApiController extends AbstractExporterController { @RequestParam(required = false) final String subCommunityId, @RequestBody final String[] subjects) throws CommunityException { - if (subCommunityId != null && !id.equals(CommunityMappingUtils.calculateMainCommunityId(subCommunityId))) { - throw new CommunityException("The sub-collection id does not start with " + id); - } + verifyIdParameters(id, subCommunityId); try { communityService.addCommunitySubjects(ObjectUtils.firstNonNull(subCommunityId, id), subjects); @@ -323,9 +321,7 @@ public class CommunityApiController extends AbstractExporterController { @RequestParam(required = false) final String subCommunityId, @RequestBody final String[] subjects) throws CommunityException { - if (subCommunityId != null && !id.equals(CommunityMappingUtils.calculateMainCommunityId(subCommunityId))) { - throw new CommunityException("The sub-collection id does not start with " + id); - } + verifyIdParameters(id, subCommunityId); try { communityService.removeCommunitySubjects(ObjectUtils.firstNonNull(subCommunityId, id), subjects); @@ -352,9 +348,7 @@ public class CommunityApiController extends AbstractExporterController { @RequestParam(required = false) final String subCommunityId, @RequestBody final String[] subjects) throws CommunityException { - if (subCommunityId != null && !id.equals(CommunityMappingUtils.calculateMainCommunityId(subCommunityId))) { - throw new CommunityException("The sub-collection id does not start with " + id); - } + verifyIdParameters(id, subCommunityId); try { communityService.addCommunityFOS(ObjectUtils.firstNonNull(subCommunityId, id), subjects); @@ -381,9 +375,7 @@ public class CommunityApiController extends AbstractExporterController { @RequestParam(required = false) final String subCommunityId, @RequestBody final String[] subjects) throws CommunityException { - if (subCommunityId != null && !id.equals(CommunityMappingUtils.calculateMainCommunityId(subCommunityId))) { - throw new CommunityException("The sub-collection id does not start with " + id); - } + verifyIdParameters(id, subCommunityId); try { communityService.removeCommunityFOS(ObjectUtils.firstNonNull(subCommunityId, id), subjects); @@ -410,9 +402,7 @@ public class CommunityApiController extends AbstractExporterController { @RequestParam(required = false) final String subCommunityId, @RequestBody final String[] subjects) throws CommunityException { - if (subCommunityId != null && !id.equals(CommunityMappingUtils.calculateMainCommunityId(subCommunityId))) { - throw new CommunityException("The sub-collection id does not start with " + id); - } + verifyIdParameters(id, subCommunityId); try { communityService.addCommunitySDG(ObjectUtils.firstNonNull(subCommunityId, id), subjects); @@ -439,9 +429,7 @@ public class CommunityApiController extends AbstractExporterController { @RequestParam(required = false) final String subCommunityId, @RequestBody final String[] subjects) throws CommunityException { - if (subCommunityId != null && !id.equals(CommunityMappingUtils.calculateMainCommunityId(subCommunityId))) { - throw new CommunityException("The sub-collection id does not start with " + id); - } + verifyIdParameters(id, subCommunityId); try { communityService.removeCommunitySDG(ObjectUtils.firstNonNull(subCommunityId, id), subjects); @@ -468,9 +456,7 @@ public class CommunityApiController extends AbstractExporterController { @RequestParam(required = false) final String subCommunityId, @RequestBody final SelectionCriteria advancedConstraint) throws CommunityException { - if (subCommunityId != null && !id.equals(CommunityMappingUtils.calculateMainCommunityId(subCommunityId))) { - throw new CommunityException("The sub-collection id does not start with " + id); - } + verifyIdParameters(id, subCommunityId); try { communityService.addCommunityAdvancedConstraint(ObjectUtils.firstNonNull(subCommunityId, id), advancedConstraint); @@ -496,9 +482,7 @@ public class CommunityApiController extends AbstractExporterController { @PathVariable final String id, @RequestParam(required = false) final String subCommunityId) throws CommunityException { - if (subCommunityId != null && !id.equals(CommunityMappingUtils.calculateMainCommunityId(subCommunityId))) { - throw new CommunityException("The sub-collection id does not start with " + id); - } + verifyIdParameters(id, subCommunityId); try { communityService.removeCommunityAdvancedConstraint(ObjectUtils.firstNonNull(subCommunityId, id)); @@ -525,9 +509,7 @@ public class CommunityApiController extends AbstractExporterController { @RequestParam(required = false) final String subCommunityId, @RequestBody final SelectionCriteria removeConstraint) throws CommunityException { - if (subCommunityId != null && !id.equals(CommunityMappingUtils.calculateMainCommunityId(subCommunityId))) { - throw new CommunityException("The sub-collection id does not start with " + id); - } + verifyIdParameters(id, subCommunityId); try { communityService.addCommunityRemoveConstraint(ObjectUtils.firstNonNull(subCommunityId, id), removeConstraint); @@ -552,9 +534,7 @@ public class CommunityApiController extends AbstractExporterController { public Object removeRemoveConstraint(@PathVariable final String id, @RequestParam(required = false) final String subCommunityId) throws CommunityException { - if (subCommunityId != null && !id.equals(CommunityMappingUtils.calculateMainCommunityId(subCommunityId))) { - throw new CommunityException("The sub-collection id does not start with " + id); - } + verifyIdParameters(id, subCommunityId); try { communityService.removeCommunityRemoveConstraint(ObjectUtils.firstNonNull(subCommunityId, id)); @@ -582,9 +562,7 @@ public class CommunityApiController extends AbstractExporterController { @RequestParam(required = false, defaultValue = "false") final boolean main, @RequestParam final String zenodocommunity) throws CommunityException { - if (subCommunityId != null && !id.equals(CommunityMappingUtils.calculateMainCommunityId(subCommunityId))) { - throw new CommunityException("The sub-collection id does not start with " + id); - } + verifyIdParameters(id, subCommunityId); try { communityService.addCommunityZenodoCommunity(ObjectUtils.firstNonNull(subCommunityId, id), zenodocommunity, main); @@ -612,9 +590,7 @@ public class CommunityApiController extends AbstractExporterController { @RequestParam(required = false) final String subCommunityId, @RequestParam(required = false, defaultValue = "false") final boolean main, @RequestParam final String zenodocommunity) throws CommunityException { - if (subCommunityId != null && !id.equals(CommunityMappingUtils.calculateMainCommunityId(subCommunityId))) { - throw new CommunityException("The sub-collection id does not start with " + id); - } + verifyIdParameters(id, subCommunityId); try { communityService.removeCommunityZenodoCommunity(ObjectUtils.firstNonNull(subCommunityId, id), zenodocommunity, main); @@ -641,12 +617,16 @@ public class CommunityApiController extends AbstractExporterController { public Page getCommunityProjects(@PathVariable final String id, @PathVariable final Integer page, @PathVariable final Integer size, + @RequestParam(required = false) final String subCommunityId, @RequestParam(required = false) final String funder, @RequestParam(required = false) final String searchFilter, @RequestParam(required = false) final String orderBy) throws CommunityException { + + verifyIdParameters(id, subCommunityId); + try { - return communityService.getCommunityProjects(id, funder, searchFilter, page, size, orderBy); + return communityService.getCommunityProjects(ObjectUtils.firstNonNull(subCommunityId, id), funder, searchFilter, page, size, orderBy); } catch (final ResourceNotFoundException e) { throw e; } catch (final Throwable e) { @@ -665,10 +645,13 @@ public class CommunityApiController extends AbstractExporterController { }) public CommunityProject addCommunityProject( @PathVariable final String id, + @RequestParam(required = false) final String subCommunityId, @RequestBody final CommunityProject project) throws CommunityException { + verifyIdParameters(id, subCommunityId); + try { - return communityService.addCommunityProject(id, project); + return communityService.addCommunityProject(ObjectUtils.firstNonNull(subCommunityId, id), project); } catch (final ResourceNotFoundException e) { throw e; } catch (final Throwable e) { @@ -687,9 +670,13 @@ public class CommunityApiController extends AbstractExporterController { }) public void deleteCommunityProject( @PathVariable final String id, + @RequestParam(required = false) final String subCommunityId, @RequestParam final String projectId) throws CommunityException { + + verifyIdParameters(id, subCommunityId); + try { - communityService.removeCommunityProjects(id, projectId); + communityService.removeCommunityProjects(ObjectUtils.firstNonNull(subCommunityId, id), projectId); } catch (final ResourceNotFoundException e) { throw e; } catch (final Throwable e) { @@ -708,9 +695,13 @@ public class CommunityApiController extends AbstractExporterController { }) public CommunityProject[] addCommunityProjectList( @PathVariable final String id, + @RequestParam(required = false) final String subCommunityId, @RequestBody final CommunityProject[] projects) throws CommunityException { + + verifyIdParameters(id, subCommunityId); + try { - communityService.addCommunityProjects(id, projects); + communityService.addCommunityProjects(ObjectUtils.firstNonNull(subCommunityId, id), projects); return projects; } catch (final ResourceNotFoundException e) { throw e; @@ -730,9 +721,13 @@ public class CommunityApiController extends AbstractExporterController { }) public void deleteCommunityProjectList( @PathVariable final String id, + @RequestParam(required = false) final String subCommunityId, @RequestBody final String[] projectIdList) throws CommunityException { + + verifyIdParameters(id, subCommunityId); + try { - communityService.removeCommunityProjects(id, projectIdList); + communityService.removeCommunityProjects(ObjectUtils.firstNonNull(subCommunityId, id), projectIdList); } catch (final ResourceNotFoundException e) { throw e; } catch (final Throwable e) { @@ -749,10 +744,13 @@ public class CommunityApiController extends AbstractExporterController { @ApiResponse(responseCode = "404", description = "not found"), @ApiResponse(responseCode = "500", description = "unexpected error") }) - public List getCommunityFunders(@PathVariable final String id) + public List getCommunityFunders(@PathVariable final String id, @RequestParam(required = false) final String subCommunityId) throws CommunityException { + + verifyIdParameters(id, subCommunityId); + try { - return communityService.getCommunityFunders(id); + return communityService.getCommunityFunders(ObjectUtils.firstNonNull(subCommunityId, id)); } catch (final ResourceNotFoundException e) { throw e; } catch (final Throwable e) { @@ -771,10 +769,16 @@ public class CommunityApiController extends AbstractExporterController { @ApiResponse(responseCode = "404", description = "not found"), @ApiResponse(responseCode = "500", description = "unexpected error") }) - public List getCommunityDatasources(@PathVariable final String id, @RequestParam(required = false) final Boolean deposit) + public List getCommunityDatasources(@PathVariable final String id, + @RequestParam(required = false) final String subCommunityId, + @RequestParam(required = false) final Boolean deposit) throws CommunityException { + + verifyIdParameters(id, subCommunityId); + try { - return deposit == null ? communityService.getCommunityDatasources(id) : communityService.getCommunityDatasourcesWithDeposit(id, deposit); + return deposit == null ? communityService.getCommunityDatasources(ObjectUtils.firstNonNull(subCommunityId, id)) + : communityService.getCommunityDatasourcesWithDeposit(id, deposit); } catch (final ResourceNotFoundException e) { throw e; } catch (final Throwable e) { @@ -793,10 +797,13 @@ public class CommunityApiController extends AbstractExporterController { }) public CommunityContentprovider addCommunityDatasource( @PathVariable final String id, + @RequestParam(required = false) final String subCommunityId, @RequestBody final CommunityContentprovider datasource) throws CommunityException { + verifyIdParameters(id, subCommunityId); + try { - communityService.addCommunityDatasources(id, datasource); + communityService.addCommunityDatasources(ObjectUtils.firstNonNull(subCommunityId, id), datasource); return datasource; } catch (final ResourceNotFoundException e) { throw e; @@ -816,9 +823,14 @@ public class CommunityApiController extends AbstractExporterController { }) public CommunityContentprovider addCommunityDatasourceDeposit( @PathVariable final String id, + @RequestParam(required = false) final String subCommunityId, @RequestBody final DepositionInfo info) throws CommunityException { + + verifyIdParameters(id, subCommunityId); + try { - return communityService.updateCommunityDatasourcesDeposit(id, info.getOpenaireId(), info.getDeposit(), info.getMessage()); + return communityService.updateCommunityDatasourcesDeposit(ObjectUtils.firstNonNull(subCommunityId, id), info.getOpenaireId(), info + .getDeposit(), info.getMessage()); } catch (final ResourceNotFoundException e) { throw e; } catch (final Throwable e) { @@ -837,9 +849,13 @@ public class CommunityApiController extends AbstractExporterController { }) public void removeCommunityDatasource( @PathVariable final String id, + @RequestParam(required = false) final String subCommunityId, @RequestParam final String dsId) throws CommunityException { + + verifyIdParameters(id, subCommunityId); + try { - communityService.removeCommunityDatasources(id, dsId); + communityService.removeCommunityDatasources(ObjectUtils.firstNonNull(subCommunityId, id), dsId); } catch (final ResourceNotFoundException e) { throw e; } catch (final Throwable e) { @@ -858,10 +874,13 @@ public class CommunityApiController extends AbstractExporterController { }) public CommunityContentprovider[] addCommunityDatasourcesList( @PathVariable final String id, + @RequestParam(required = false) final String subCommunityId, @RequestBody final CommunityContentprovider[] dsList) throws CommunityException { + verifyIdParameters(id, subCommunityId); + try { - communityService.addCommunityDatasources(id, dsList); + communityService.addCommunityDatasources(ObjectUtils.firstNonNull(subCommunityId, id), dsList); return dsList; } catch (final ResourceNotFoundException e) { throw e; @@ -881,9 +900,13 @@ public class CommunityApiController extends AbstractExporterController { }) public void deleteCommunityDatasourcesList( @PathVariable final String id, + @RequestParam(required = false) final String subCommunityId, @RequestBody final String[] dsIdList) throws CommunityException { + + verifyIdParameters(id, subCommunityId); + try { - communityService.removeCommunityDatasources(id, dsIdList); + communityService.removeCommunityDatasources(ObjectUtils.firstNonNull(subCommunityId, id), dsIdList); } catch (final ResourceNotFoundException e) { throw e; } catch (final Throwable e) { @@ -891,86 +914,6 @@ public class CommunityApiController extends AbstractExporterController { } } - // Content Providers (DEPRECATED) - - @Deprecated - @GetMapping("/community/{id}/contentproviders") - @Operation(summary = "get the list of datasources associated to a given community", description = "get the list of content providers associated to a given community", tags = { - C_CP, R - }) - @ApiResponses(value = { - @ApiResponse(responseCode = "200", description = "OK"), - @ApiResponse(responseCode = "404", description = "not found"), - @ApiResponse(responseCode = "500", description = "unexpected error") - }) - public List getCommunityContentproviders(@PathVariable final String id, @RequestParam(required = false) final Boolean deposit) - throws CommunityException { - return getCommunityDatasources(id, deposit); - } - - @Deprecated - @PostMapping("/community/{id}/contentproviders") - @Operation(summary = "associate a datasource to the community, provide all the fields or the method will overwrite with nulls the fields that are missing", description = "associate a datasource to the community, provide all the fields or the method will overwrite with nulls the fields that are missing", tags = { - C_CP, W - }) - @ApiResponses(value = { - @ApiResponse(responseCode = "200", description = "OK"), - @ApiResponse(responseCode = "404", description = "not found"), - @ApiResponse(responseCode = "500", description = "unexpected error") - }) - public CommunityContentprovider addCommunityContentprovider( - @PathVariable final String id, - @RequestBody final CommunityContentprovider contentprovider) throws CommunityException { - - return addCommunityDatasource(id, contentprovider); - } - - @Deprecated - @DeleteMapping("/community/{id}/contentproviders") - @Operation(summary = "remove the association between a datasource and the community", description = "remove the association between a datasource and the community", tags = { - C_CP, W - }) - @ApiResponses(value = { - @ApiResponse(responseCode = "200", description = "OK"), - @ApiResponse(responseCode = "404", description = "not found"), - @ApiResponse(responseCode = "500", description = "unexpected error") - }) - public void removeCommunityContentprovider(@PathVariable final String id, @RequestParam final String contentproviderId) throws CommunityException { - removeCommunityDatasource(id, contentproviderId); - } - - @Deprecated - @PostMapping("/community/{id}/contentprovidersList") - @Operation(summary = "associate a list of datasources to the community, provide all the fields or the method will overwrite with nulls the fields that are missing", description = "associate a list of datasources to the community, provide all the fields or the method will overwrite with nulls the fields that are missing", tags = { - C_CP, W - }) - @ApiResponses(value = { - @ApiResponse(responseCode = "200", description = "OK"), - @ApiResponse(responseCode = "404", description = "not found"), - @ApiResponse(responseCode = "500", description = "unexpected error") - }) - public CommunityContentprovider[] addCommunityContentProvidersList( - @PathVariable final String id, - @RequestBody final CommunityContentprovider[] contentprovidersList) throws CommunityException { - return addCommunityDatasourcesList(id, contentprovidersList); - } - - @Deprecated - @DeleteMapping("/community/{id}/contentprovidersList") - @Operation(summary = "remove a list of datasources from the community", description = "remove a list of datasources from the community", tags = { - C_CP, W - }) - @ApiResponses(value = { - @ApiResponse(responseCode = "200", description = "OK"), - @ApiResponse(responseCode = "404", description = "not found"), - @ApiResponse(responseCode = "500", description = "unexpected error") - }) - public void deleteCommunityContentProvidersList( - @PathVariable final String id, - @RequestBody final String[] contentProviderIdList) throws CommunityException { - deleteCommunityDatasourcesList(id, contentProviderIdList); - } - // ORGANIZATIONS @GetMapping("/community/{id}/organizations") @@ -982,9 +925,12 @@ public class CommunityApiController extends AbstractExporterController { @ApiResponse(responseCode = "404", description = "not found"), @ApiResponse(responseCode = "500", description = "unexpected error") }) - public List getCommunityOrganizations(@PathVariable final String id) throws CommunityException { + public List getCommunityOrganizations(@PathVariable final String id, @RequestParam(required = false) final String subCommunityId) + throws CommunityException { + verifyIdParameters(id, subCommunityId); + try { - return communityService.getCommunityOrganizations(id); + return communityService.getCommunityOrganizations(ObjectUtils.firstNonNull(subCommunityId, id)); } catch (final ResourceNotFoundException e) { throw e; } catch (final Throwable e) { @@ -1003,9 +949,13 @@ public class CommunityApiController extends AbstractExporterController { }) public CommunityOrganization addCommunityOrganization( @PathVariable final String id, + @RequestParam(required = false) final String subCommunityId, @RequestBody final CommunityOrganization organization) throws CommunityException { + + verifyIdParameters(id, subCommunityId); + try { - communityService.addCommunityOrganizations(id, organization); + communityService.addCommunityOrganizations(ObjectUtils.firstNonNull(subCommunityId, id), organization); return organization; } catch (final ResourceNotFoundException e) { throw e; @@ -1025,10 +975,13 @@ public class CommunityApiController extends AbstractExporterController { }) public CommunityOrganization[] addCommunityOrganizationList( @PathVariable final String id, + @RequestParam(required = false) final String subCommunityId, @RequestBody final CommunityOrganization[] orgs) throws CommunityException { + verifyIdParameters(id, subCommunityId); + try { - communityService.addCommunityOrganizations(id, orgs); + communityService.addCommunityOrganizations(ObjectUtils.firstNonNull(subCommunityId, id), orgs); return orgs; } catch (final ResourceNotFoundException e) { throw e; @@ -1048,9 +1001,13 @@ public class CommunityApiController extends AbstractExporterController { }) public void removeCommunityOrganization( @PathVariable final String id, + @RequestParam(required = false) final String subCommunityId, @RequestParam final String organizationName) throws CommunityException { + + verifyIdParameters(id, subCommunityId); + try { - communityService.removeCommunityOrganizations(id, organizationName); + communityService.removeCommunityOrganizations(ObjectUtils.firstNonNull(subCommunityId, id), organizationName); } catch (final ResourceNotFoundException e) { throw e; } catch (final Throwable e) { @@ -1069,9 +1026,13 @@ public class CommunityApiController extends AbstractExporterController { }) public void removeCommunityOrganizationList( @PathVariable final String id, + @RequestParam(required = false) final String subCommunityId, @RequestBody final String[] orgNames) throws CommunityException { + + verifyIdParameters(id, subCommunityId); + try { - communityService.removeCommunityOrganizations(id, orgNames); + communityService.removeCommunityOrganizations(ObjectUtils.firstNonNull(subCommunityId, id), orgNames); } catch (final ResourceNotFoundException e) { throw e; } catch (final Throwable e) { @@ -1184,4 +1145,16 @@ public class CommunityApiController extends AbstractExporterController { } } + private void verifyIdParameters(final String id, final String subCommunityId) throws CommunityException { + if (!CommunityMappingUtils.isValidCommunityId(id)) { throw new CommunityException("Invalid community id: " + id); } + + if (subCommunityId != null && CommunityMappingUtils.isValidSubCommunityId(subCommunityId)) { + throw new CommunityException("Invalid sub-collection id: " + subCommunityId); + } + + if (subCommunityId != null && !id.equals(CommunityMappingUtils.calculateMainCommunityId(subCommunityId))) { + throw new CommunityException("The sub-collection id does not start with " + id); + } + } + } diff --git a/apps/dnet-exporter-api/src/main/java/eu/dnetlib/openaire/community/utils/CommunityMappingUtils.java b/apps/dnet-exporter-api/src/main/java/eu/dnetlib/openaire/community/utils/CommunityMappingUtils.java index b43cbd57..67152bf6 100644 --- a/apps/dnet-exporter-api/src/main/java/eu/dnetlib/openaire/community/utils/CommunityMappingUtils.java +++ b/apps/dnet-exporter-api/src/main/java/eu/dnetlib/openaire/community/utils/CommunityMappingUtils.java @@ -338,4 +338,11 @@ public class CommunityMappingUtils { return conf; } + public static boolean isValidCommunityId(final String id) { + return StringUtils.isNotBlank(id) && !StringUtils.contains(id, COMMUNITY_ID_PARTS_SEPARATOR); + } + + public static boolean isValidSubCommunityId(final String subcommunityId) { + return StringUtils.isNotBlank(subcommunityId) && StringUtils.split(subcommunityId, COMMUNITY_ID_PARTS_SEPARATOR).length > 1; + } }