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 f338edf4..793ac847 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 @@ -24,7 +24,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.CommunityContentprovider; import eu.dnetlib.openaire.exporter.model.community.CommunityDetails; import eu.dnetlib.openaire.exporter.model.community.CommunityOpenAIRECommunities; @@ -74,7 +73,7 @@ public class CommunityApiController { @ApiResponse(responseCode = "404", description = "not found"), @ApiResponse(responseCode = "500", description = "unexpected error") }) - public CommunityDetails getCommunity(@PathVariable final String id) throws CommunityException, ResourceNotFoundException { + public CommunityDetails getCommunity(@PathVariable final String id) throws CommunityException { return communityService.getCommunity(id); } @@ -91,7 +90,7 @@ public class CommunityApiController { }) public void setCommunity( @PathVariable final String id, - @RequestBody final CommunityWritableProperties properties) throws CommunityException, ResourceNotFoundException { + @RequestBody final CommunityWritableProperties properties) throws CommunityException { communityService.setCommunity(id, properties); } @@ -108,7 +107,7 @@ public class CommunityApiController { @ApiResponse(responseCode = "500", description = "unexpected error") }) public Page getCommunityProjects(@PathVariable final String id, @PathVariable final Integer page, @PathVariable final Integer size) - throws CommunityException, ResourceNotFoundException { + throws CommunityException { return communityService.getCommunityProjects(id, page, size); } @@ -125,7 +124,7 @@ public class CommunityApiController { }) public CommunityProject addCommunityProject( @PathVariable final String id, - @RequestBody final CommunityProject project) throws CommunityException, ResourceNotFoundException { + @RequestBody final CommunityProject project) throws CommunityException { return communityService.addCommunityProject(id, project); } @@ -143,9 +142,9 @@ public class CommunityApiController { }) public void deleteCommunityProject( @PathVariable final String id, - @RequestBody final String projectId) throws CommunityException, ResourceNotFoundException { + @RequestBody final String projectId) throws CommunityException { - communityService.removeCommunityProject(id, projectId); + communityService.removeCommunityProjects(id, projectId); } @RequestMapping(value = "/community/{id}/projectList", produces = { @@ -159,11 +158,13 @@ public class CommunityApiController { @ApiResponse(responseCode = "404", description = "not found"), @ApiResponse(responseCode = "500", description = "unexpected error") }) - public List addCommunityProjectList( + public CommunityProject[] addCommunityProjectList( @PathVariable final String id, - @RequestBody final List projectList) throws CommunityException, ResourceNotFoundException { + @RequestBody final CommunityProject[] projects) throws CommunityException { - return communityService.addCommunityProjectList(id, projectList); + communityService.addCommunityProjects(id, projects); + + return projects; } @RequestMapping(value = "/community/{id}/projectList", produces = { @@ -179,9 +180,9 @@ public class CommunityApiController { }) public void deleteCommunityProjectList( @PathVariable final String id, - @RequestBody final List projectIdList) throws CommunityException, ResourceNotFoundException { + @RequestBody final String[] projectIdList) throws CommunityException { - communityService.removeCommunityProjectList(id, projectIdList); + communityService.removeCommunityProjects(id, projectIdList); } @RequestMapping(value = "/community/{id}/contentproviders", produces = { @@ -195,7 +196,7 @@ public class CommunityApiController { @ApiResponse(responseCode = "404", description = "not found"), @ApiResponse(responseCode = "500", description = "unexpected error") }) - public List getCommunityContentproviders(@PathVariable final String id) throws CommunityException, ResourceNotFoundException { + public List getCommunityContentproviders(@PathVariable final String id) throws CommunityException { return communityService.getCommunityContentproviders(id); } @@ -212,9 +213,11 @@ public class CommunityApiController { }) public CommunityContentprovider addCommunityContentprovider( @PathVariable final String id, - @RequestBody final CommunityContentprovider contentprovider) throws CommunityException, ResourceNotFoundException { + @RequestBody final CommunityContentprovider contentprovider) throws CommunityException { - return communityService.addCommunityContentprovider(id, contentprovider); + communityService.addCommunityContentProviders(id, contentprovider); + + return contentprovider; } @RequestMapping(value = "/community/{id}/contentproviders", produces = { @@ -230,34 +233,36 @@ public class CommunityApiController { }) public void removeCommunityContentprovider( @PathVariable final String id, - @RequestBody final String contentproviderId) throws CommunityException, ResourceNotFoundException { + @RequestBody final String contentproviderId) throws CommunityException { - communityService.removeCommunityContentProvider(id, contentproviderId); + communityService.removeCommunityContentProviders(id, contentproviderId); } @RequestMapping(value = "/community/{id}/contentprovidersList", produces = { "application/json" }, method = RequestMethod.POST) @Operation(summary = "associate a list of content providers to the community", description = "associate a list of content providers to the community", tags = { - C_PJ, W + C_CP, W }) @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "OK"), @ApiResponse(responseCode = "404", description = "not found"), @ApiResponse(responseCode = "500", description = "unexpected error") }) - public List addCommunityContentProvidersList( + public CommunityContentprovider[] addCommunityContentProvidersList( @PathVariable final String id, - @RequestBody final List contentprovidersList) throws CommunityException, ResourceNotFoundException { + @RequestBody final CommunityContentprovider[] contentprovidersList) throws CommunityException { - return communityService.addCommunityContentProvidersList(id, contentprovidersList); + communityService.addCommunityContentProviders(id, contentprovidersList); + + return contentprovidersList; } @RequestMapping(value = "/community/{id}/contentprovidersList", produces = { "application/json" }, method = RequestMethod.DELETE) @Operation(summary = "remove a list of content providers from the community", description = "remove a list of content providers from the community", tags = { - C_PJ, W + C_CP, W }) @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "OK"), @@ -266,9 +271,9 @@ public class CommunityApiController { }) public void deleteCommunityContentProvidersList( @PathVariable final String id, - @RequestBody final List contentProviderIdList) throws CommunityException, ResourceNotFoundException { + @RequestBody final String[] contentProviderIdList) throws CommunityException { - communityService.removeCommunityContentProviderList(id, contentProviderIdList); + communityService.removeCommunityContentProviders(id, contentProviderIdList); } // ADDING CODE FOR COMMUNITY ORGANIZATIONS @@ -284,7 +289,7 @@ public class CommunityApiController { @ApiResponse(responseCode = "404", description = "not found"), @ApiResponse(responseCode = "500", description = "unexpected error") }) - public List getCommunityOrganizations(@PathVariable final String id) throws CommunityException, ResourceNotFoundException { + public List getCommunityOrganizations(@PathVariable final String id) throws CommunityException { return communityService.getCommunityOrganizations(id); } @@ -301,9 +306,31 @@ public class CommunityApiController { }) public CommunityOrganization addCommunityOrganization( @PathVariable final String id, - @RequestBody final CommunityOrganization organization) throws CommunityException, ResourceNotFoundException { + @RequestBody final CommunityOrganization organization) throws CommunityException { - return communityService.addCommunityOrganization(id, organization); + communityService.addCommunityOrganizations(id, organization); + + return organization; + } + + @RequestMapping(value = "/community/{id}/organizationList", produces = { + "application/json" + }, method = RequestMethod.POST) + @Operation(summary = "associate a list of organizations to the community", description = "associate a list of organizations to the community", tags = { + C_O, W + }) + @ApiResponses(value = { + @ApiResponse(responseCode = "200", description = "OK"), + @ApiResponse(responseCode = "404", description = "not found"), + @ApiResponse(responseCode = "500", description = "unexpected error") + }) + public CommunityOrganization[] addCommunityOrganizationList( + @PathVariable final String id, + @RequestBody final CommunityOrganization[] orgs) throws CommunityException { + + communityService.addCommunityOrganizations(id, orgs); + + return orgs; } @RequestMapping(value = "/community/{id}/organizations", produces = { @@ -319,10 +346,29 @@ public class CommunityApiController { }) public void removeCommunityOrganization( @PathVariable final String id, - @RequestBody final String organizationId) throws CommunityException, ResourceNotFoundException { + @RequestBody final String organizationId) throws CommunityException { - communityService.removeCommunityOrganization(id, organizationId); + communityService.removeCommunityOrganizations(id, organizationId); } + + @RequestMapping(value = "/community/{id}/organizationList", produces = { + "application/json" + }, method = RequestMethod.DELETE) + @Operation(summary = "remove a list of associations between some organizations and the community", description = "remove a list of associations between some organizations and the community", tags = { + C_O, W + }) + @ApiResponses(value = { + @ApiResponse(responseCode = "200", description = "OK"), + @ApiResponse(responseCode = "404", description = "not found"), + @ApiResponse(responseCode = "500", description = "unexpected error") + }) + public void removeCommunityOrganizationList( + @PathVariable final String id, + @RequestBody final String[] orgsIdList) throws CommunityException { + + communityService.removeCommunityOrganizations(id, orgsIdList); + } + // ********************** @RequestMapping(value = "/community/{id}/subjects", produces = { @@ -338,7 +384,7 @@ public class CommunityApiController { }) public CommunityDetails addCommunitySubjects( @PathVariable final String id, - @RequestBody final String[] subjects) throws CommunityException, ResourceNotFoundException { + @RequestBody final String[] subjects) throws CommunityException { return communityService.addCommunitySubjects(id, subjects); } @@ -356,7 +402,7 @@ public class CommunityApiController { }) public CommunityDetails removeCommunitySubjects( @PathVariable final String id, - @RequestBody final String[] subjects) throws CommunityException, ResourceNotFoundException { + @RequestBody final String[] subjects) throws CommunityException { return communityService.removeCommunitySubjects(id, subjects); } @@ -374,7 +420,7 @@ public class CommunityApiController { }) public CommunityDetails addCommunityFOS( @PathVariable final String id, - @RequestBody final String[] subjects) throws CommunityException, ResourceNotFoundException { + @RequestBody final String[] subjects) throws CommunityException { return communityService.addCommunityFOS(id, subjects); } @@ -392,7 +438,7 @@ public class CommunityApiController { }) public CommunityDetails removeCommunityFOS( @PathVariable final String id, - @RequestBody final String[] subjects) throws CommunityException, ResourceNotFoundException { + @RequestBody final String[] subjects) throws CommunityException { return communityService.removeCommunityFOS(id, subjects); } @@ -410,7 +456,7 @@ public class CommunityApiController { }) public CommunityDetails addCommunitySDG( @PathVariable final String id, - @RequestBody final String[] subjects) throws CommunityException, ResourceNotFoundException { + @RequestBody final String[] subjects) throws CommunityException { return communityService.addCommunitySDG(id, subjects); } @@ -428,7 +474,7 @@ public class CommunityApiController { }) public CommunityDetails removeCommunitySDG( @PathVariable final String id, - @RequestBody final String[] subjects) throws CommunityException, ResourceNotFoundException { + @RequestBody final String[] subjects) throws CommunityException { return communityService.removeCommunitySDG(id, subjects); } @@ -446,7 +492,7 @@ public class CommunityApiController { }) public CommunityDetails addAdvancedConstraint( @PathVariable final String id, - @RequestBody final SelectionCriteria advancedConstraint) throws CommunityException, ResourceNotFoundException { + @RequestBody final SelectionCriteria advancedConstraint) throws CommunityException { return communityService.addCommunityAdvancedConstraint(id, advancedConstraint); } @@ -463,7 +509,7 @@ public class CommunityApiController { @ApiResponse(responseCode = "500", description = "unexpected error") }) public CommunityDetails removeAdvancedConstraint( - @PathVariable final String id) throws CommunityException, ResourceNotFoundException { + @PathVariable final String id) throws CommunityException { return communityService.removeCommunityAdvancedConstraint(id); } @@ -482,7 +528,7 @@ public class CommunityApiController { public CommunityDetails addCommunityZenodoCommunity( @PathVariable final String id, @RequestParam(required = false, defaultValue = "false") final boolean main, - @RequestBody final String zenodocommunity) throws CommunityException, ResourceNotFoundException { + @RequestBody final String zenodocommunity) throws CommunityException { return communityService.addCommunityZenodoCommunity(id, zenodocommunity, main); @@ -502,7 +548,7 @@ public class CommunityApiController { public void removeCommunityZenodoCommunity( @PathVariable final String id, @RequestParam(required = false, defaultValue = "false") final boolean main, - @RequestBody final String zenodoCommId) throws CommunityException, ResourceNotFoundException { + @RequestBody final String zenodoCommId) throws CommunityException { communityService.removeCommunityZenodoCommunity(id, zenodoCommId, main); } @@ -518,7 +564,7 @@ public class CommunityApiController { @ApiResponse(responseCode = "404", description = "not found"), @ApiResponse(responseCode = "500", description = "unexpected error") }) - public CommunityOpenAIRECommunities getOpenAireCommunities(@PathVariable final String zenodoId) throws CommunityException, ResourceNotFoundException { + public CommunityOpenAIRECommunities getOpenAireCommunities(@PathVariable final String zenodoId) throws CommunityException { final CommunityOpenAIRECommunities res = new CommunityOpenAIRECommunities(); res.setZenodoid(zenodoId); res.setOpenAirecommunitylist(communityService.getOpenAIRECommunitiesByZenodoId(zenodoId)); diff --git a/apps/dnet-exporter-api/src/main/java/eu/dnetlib/openaire/community/CommunityService.java b/apps/dnet-exporter-api/src/main/java/eu/dnetlib/openaire/community/CommunityService.java index 99de99cf..2647de48 100644 --- a/apps/dnet-exporter-api/src/main/java/eu/dnetlib/openaire/community/CommunityService.java +++ b/apps/dnet-exporter-api/src/main/java/eu/dnetlib/openaire/community/CommunityService.java @@ -23,6 +23,7 @@ import eu.dnetlib.openaire.community.model.DbCommunity; import eu.dnetlib.openaire.community.model.DbDatasource; import eu.dnetlib.openaire.community.model.DbDatasourcePK; import eu.dnetlib.openaire.community.model.DbOrganization; +import eu.dnetlib.openaire.community.model.DbOrganizationPK; import eu.dnetlib.openaire.community.model.DbProject; import eu.dnetlib.openaire.community.model.DbProjectPK; import eu.dnetlib.openaire.community.model.DbSubCommunity; @@ -49,7 +50,10 @@ import eu.dnetlib.openaire.exporter.model.community.selectioncriteria.SelectionC @ConditionalOnProperty(value = "openaire.exporter.enable.community", havingValue = "true") public class CommunityService { - // TODO: Verificare Tickets: #8835, #8854, #6483, #3259, #3494 + // #8835 reimplementation ot the community API + // #8854 compatibility with the context API + // #6483 post methods to add multiple projects/providers + // #3494 Move context profiles from IS to dedicated DBs @Autowired private DbCommunityRepository dbCommunityRepository; @@ -78,6 +82,7 @@ public class CommunityService { } } + @Transactional public CommunityDetails saveCommunity(final CommunityDetails details) throws CommunityException { try { dbCommunityRepository.save(CommunityMappingUtils.toCommunity(details)); @@ -88,7 +93,8 @@ public class CommunityService { } } - public CommunityDetails getCommunity(final String id) throws CommunityException, ResourceNotFoundException { + @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.CommunityDetails(c); @@ -99,7 +105,7 @@ public class CommunityService { } @Transactional - public void setCommunity(final String id, final CommunityWritableProperties details) throws CommunityException, ResourceNotFoundException { + 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); @@ -110,7 +116,8 @@ public class CommunityService { } } - public Page getCommunityProjects(final String id, final int page, final int size) throws CommunityException, ResourceNotFoundException { + @Transactional + public Page getCommunityProjects(final String id, final int page, final int size) throws CommunityException { try { return dbProjectRepository.findByCommunity(id, PageRequest.of(page, size)).map(CommunityMappingUtils::toCommunityProject); } catch (final Throwable e) { @@ -119,7 +126,8 @@ public class CommunityService { } } - public CommunityProject addCommunityProject(final String id, final CommunityProject project) throws CommunityException, ResourceNotFoundException { + @Transactional + public CommunityProject addCommunityProject(final String id, final CommunityProject project) throws CommunityException { try { final DbProject p = CommunityMappingUtils.toDbProject(id, project); dbProjectRepository.save(p); @@ -130,34 +138,24 @@ public class CommunityService { } } - public List addCommunityProjectList(final String id, final List projectList) - throws CommunityException, ResourceNotFoundException { + @Transactional + public void addCommunityProjects(final String id, final CommunityProject... projects) throws CommunityException { try { - final List list = projectList.stream() + final List list = Arrays.stream(projects) .map(p -> CommunityMappingUtils.toDbProject(id, p)) .collect(Collectors.toList()); dbProjectRepository.saveAll(list); - - return projectList; } catch (final Throwable e) { log.error(e); throw new CommunityException(e); } } - public void removeCommunityProject(final String id, final String projectId) throws CommunityException, ResourceNotFoundException { + @Transactional + public void removeCommunityProjects(final String id, final String... ids) throws CommunityException { try { - dbProjectRepository.deleteById(new DbProjectPK(id, projectId)); - } catch (final Throwable e) { - log.error(e); - throw new CommunityException(e); - } - } - - public void removeCommunityProjectList(final String id, final List projectIdList) throws CommunityException, ResourceNotFoundException { - try { - final List list = projectIdList.stream() + final List list = Arrays.stream(ids) .map(projectId -> new DbProjectPK(id, projectId)) .collect(Collectors.toList()); dbProjectRepository.deleteAllById(list); @@ -167,7 +165,7 @@ public class CommunityService { } } - public List getCommunityContentproviders(final String id) throws CommunityException, ResourceNotFoundException { + public List getCommunityContentproviders(final String id) throws CommunityException { try { return dbDatasourceRepository.findByCommunity(id) .stream() @@ -179,47 +177,25 @@ public class CommunityService { } } - public CommunityContentprovider addCommunityContentprovider(final String id, final CommunityContentprovider cp) - throws CommunityException, ResourceNotFoundException { + @Transactional + public void addCommunityContentProviders(final String id, final CommunityContentprovider... contentproviders) + throws CommunityException { try { - final DbDatasource ds = CommunityMappingUtils.toDbDatasource(id, cp); - dbDatasourceRepository.save(ds); - return cp; - } catch (final Throwable e) { - log.error(e); - throw new CommunityException(e); - } - } - - public List addCommunityContentProvidersList(final String id, final List contentprovidersList) - throws CommunityException, ResourceNotFoundException { - try { - final List list = contentprovidersList.stream() + final List list = Arrays.stream(contentproviders) .map(cp -> CommunityMappingUtils.toDbDatasource(id, cp)) .collect(Collectors.toList()); dbDatasourceRepository.saveAll(list); - - return contentprovidersList; } catch (final Throwable e) { log.error(e); throw new CommunityException(e); } } - public void removeCommunityContentProvider(final String id, final String contentproviderId) throws CommunityException, ResourceNotFoundException { + @Transactional + public void removeCommunityContentProviders(final String id, final String... ids) throws CommunityException { try { - dbDatasourceRepository.deleteById(new DbDatasourcePK(id, contentproviderId)); - } catch (final Throwable e) { - log.error(e); - throw new CommunityException(e); - } - } - - public void removeCommunityContentProviderList(final String id, final List contentProviderIdList) - throws CommunityException, ResourceNotFoundException { - try { - final List list = contentProviderIdList.stream() + final List list = Arrays.stream(ids) .map(dsId -> new DbDatasourcePK(id, dsId)) .collect(Collectors.toList()); dbDatasourceRepository.deleteAllById(list); @@ -229,16 +205,21 @@ public class CommunityService { } } - public void removeCommunityOrganization(final String id, final String organizationId) throws CommunityException, ResourceNotFoundException { + @Transactional + public void removeCommunityOrganizations(final String id, final String... ids) throws CommunityException { try { - dbDatasourceRepository.deleteById(new DbDatasourcePK(id, organizationId)); + final List list = Arrays.stream(ids) + .map(orgId -> new DbOrganizationPK(id, orgId)) + .collect(Collectors.toList()); + dbOrganizationRepository.deleteAllById(list); } catch (final Throwable e) { log.error(e); throw new CommunityException(e); } } - public List getCommunityOrganizations(final String id) throws CommunityException, ResourceNotFoundException { + @Transactional + public List getCommunityOrganizations(final String id) throws CommunityException { try { return dbSupportOrgRepository.findByCommunity(id) .stream() @@ -250,35 +231,22 @@ public class CommunityService { } } - public CommunityOrganization addCommunityOrganization(final String id, final CommunityOrganization organization) - throws CommunityException, ResourceNotFoundException { + @Transactional + public void addCommunityOrganizations(final String id, final CommunityOrganization... orgs) throws CommunityException { try { - final DbSupportOrg o = CommunityMappingUtils.toDbSupportOrg(id, organization); - dbSupportOrgRepository.save(o); - return organization; - } catch (final Throwable e) { - log.error(e); - throw new CommunityException(e); - } - } - - public List addCommunityOrganizationList(final String id, final List orgList) - throws CommunityException, ResourceNotFoundException { - try { - final List list = orgList.stream() + final List list = Arrays.stream(orgs) .map(o -> CommunityMappingUtils.toDbSupportOrg(id, o)) .collect(Collectors.toList()); dbSupportOrgRepository.saveAll(list); - - return orgList; } catch (final Throwable e) { log.error(e); throw new CommunityException(e); } } - public void removeSubCommunity(final String id, final String subCommunityId) throws CommunityException, ResourceNotFoundException { + @Transactional + public void removeSubCommunity(final String id, final String subCommunityId) throws CommunityException { try { dbSubCommunityRepository.deleteById(subCommunityId); } catch (final Throwable e) { @@ -287,7 +255,8 @@ public class CommunityService { } } - public List getSubCommunities(final String id) throws CommunityException, ResourceNotFoundException { + @Transactional + public List getSubCommunities(final String id) throws CommunityException { try { return dbSubCommunityRepository.findByCommunity(id) .stream() @@ -299,33 +268,22 @@ public class CommunityService { } } - public SubCommunity addSubCommunity(final SubCommunity sub) throws CommunityException { + @Transactional + public void addSubCommunities(final SubCommunity... subs) throws CommunityException { try { - final DbSubCommunity sc = CommunityMappingUtils.toDbSubCommunity(sub); - dbSubCommunityRepository.save(sc); - return sub; - } catch (final Throwable e) { - log.error(e); - throw new CommunityException(e); - } - } - - public List addSubCommunityList(final List subs) throws CommunityException { - try { - final List list = subs.stream() + final List list = Arrays.stream(subs) .map(CommunityMappingUtils::toDbSubCommunity) .collect(Collectors.toList()); dbSubCommunityRepository.saveAll(list); - - return subs; } catch (final Throwable e) { log.error(e); throw new CommunityException(e); } } - public CommunityDetails addCommunitySubjects(final String id, final String... subjects) throws CommunityException, ResourceNotFoundException { + @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) { @@ -334,7 +292,7 @@ public class CommunityService { } } - public CommunityDetails removeCommunitySubjects(final String id, final String... subjects) throws CommunityException, ResourceNotFoundException { + 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) { @@ -343,7 +301,7 @@ public class CommunityService { } } - public CommunityDetails addCommunityFOS(final String id, final String... foss) throws CommunityException, ResourceNotFoundException { + 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) { @@ -352,7 +310,7 @@ public class CommunityService { } } - public CommunityDetails removeCommunityFOS(final String id, final String... foss) throws CommunityException, ResourceNotFoundException { + 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) { @@ -361,7 +319,7 @@ public class CommunityService { } } - public CommunityDetails addCommunitySDG(final String id, final String... sdgs) throws CommunityException, ResourceNotFoundException { + 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) { @@ -370,7 +328,7 @@ public class CommunityService { } } - public CommunityDetails removeCommunitySDG(final String id, final String... sdgs) throws CommunityException, ResourceNotFoundException { + 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) { @@ -379,8 +337,9 @@ public class CommunityService { } } + @Transactional public CommunityDetails addCommunityAdvancedConstraint(final String id, final SelectionCriteria advancedCosntraint) - throws CommunityException, ResourceNotFoundException { + throws CommunityException { try { final DbCommunity dbEntry = dbCommunityRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Community not found: " + id)); dbEntry.setAdvancedConstraints(advancedCosntraint); @@ -393,7 +352,8 @@ public class CommunityService { } } - public CommunityDetails removeCommunityAdvancedConstraint(final String id) throws ResourceNotFoundException, CommunityException { + @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); @@ -406,7 +366,7 @@ public class CommunityService { } public CommunityDetails removeCommunityZenodoCommunity(final String id, final String zenodoCommunity, final boolean isMain) - throws CommunityException, ResourceNotFoundException { + throws CommunityException { if (isMain) { return updateElementToSimpleField(id, (c, val) -> c.setMainZenodoCommunity(val), null); } else { @@ -415,7 +375,7 @@ public class CommunityService { } public CommunityDetails addCommunityZenodoCommunity(final String id, final String zenodoCommunity, final boolean isMain) - throws CommunityException, ResourceNotFoundException { + throws CommunityException { try { if (isMain) { return updateElementToSimpleField(id, (c, val) -> c.setMainZenodoCommunity(val), zenodoCommunity); @@ -431,7 +391,7 @@ public class CommunityService { @Transactional private CommunityDetails updateElementToSimpleField(final String id, final BiConsumer setter, - final String value) throws ResourceNotFoundException, CommunityException { + final String value) throws CommunityException { try { final DbCommunity dbEntry = dbCommunityRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Community not found: " + id)); setter.accept(dbEntry, value); @@ -448,7 +408,7 @@ public class CommunityService { final Function getter, final BiConsumer setter, final boolean remove, - final String... values) throws ResourceNotFoundException, CommunityException { + final String... values) throws CommunityException { final DbCommunity dbEntry = dbCommunityRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Community not found: " + id)); @@ -472,6 +432,7 @@ public class CommunityService { return getCommunity(id); } + @Transactional public List getOpenAIRECommunitiesByZenodoId(final String zenodoId) throws CommunityException { try { return dbCommunityRepository.findByZenodoId(zenodoId); @@ -481,6 +442,7 @@ public class CommunityService { } } + @Transactional public Map> getPropagationOrganizationCommunityMap() throws CommunityException { try { return dbOrganizationRepository.findAll() @@ -492,6 +454,7 @@ public class CommunityService { } } + @Transactional public Set getPropagationOrganizationsForCommunity(final String communityId) throws CommunityException { try { return dbOrganizationRepository.findByCommunity(communityId) diff --git a/apps/dnet-exporter-api/src/main/java/eu/dnetlib/openaire/community/importer/CommunityImporterService.java b/apps/dnet-exporter-api/src/main/java/eu/dnetlib/openaire/community/importer/CommunityImporterService.java index 2bdefc4a..de766915 100644 --- a/apps/dnet-exporter-api/src/main/java/eu/dnetlib/openaire/community/importer/CommunityImporterService.java +++ b/apps/dnet-exporter-api/src/main/java/eu/dnetlib/openaire/community/importer/CommunityImporterService.java @@ -209,10 +209,10 @@ public class CommunityImporterService { .collect(Collectors.toList()); service.saveCommunity(community); - service.addCommunityProjectList(context.getId(), projects); - service.addCommunityContentProvidersList(context.getId(), datasources); - service.addCommunityOrganizationList(context.getId(), orgs); - service.addSubCommunityList(subs); + service.addCommunityProjects(context.getId(), projects.toArray(new CommunityProject[projects.size()])); + service.addCommunityContentProviders(context.getId(), datasources.toArray(new CommunityContentprovider[datasources.size()])); + service.addCommunityOrganizations(context.getId(), orgs.toArray(new CommunityOrganization[orgs.size()])); + service.addSubCommunities(subs.toArray(new SubCommunity[subs.size()])); } catch ( final Exception e) { @@ -398,4 +398,12 @@ public class CommunityImporterService { protected void setService(final CommunityService service) { this.service = service; } + + protected JdbcTemplate getJdbcTemplate() { + return jdbcTemplate; + } + + protected void setJdbcTemplate(final JdbcTemplate jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + } } diff --git a/apps/dnet-exporter-api/src/main/java/eu/dnetlib/openaire/community/model/DbOrganizationPK.java b/apps/dnet-exporter-api/src/main/java/eu/dnetlib/openaire/community/model/DbOrganizationPK.java index bf91209d..b23b8a68 100644 --- a/apps/dnet-exporter-api/src/main/java/eu/dnetlib/openaire/community/model/DbOrganizationPK.java +++ b/apps/dnet-exporter-api/src/main/java/eu/dnetlib/openaire/community/model/DbOrganizationPK.java @@ -11,6 +11,13 @@ public class DbOrganizationPK implements Serializable { private String orgId; + public DbOrganizationPK() {} + + public DbOrganizationPK(final String community, final String orgId) { + this.community = community; + this.orgId = orgId; + } + public String getCommunity() { return community; } diff --git a/apps/dnet-exporter-api/src/test/java/eu/dnetlib/openaire/community/importer/CommunityImporterServiceTest.java b/apps/dnet-exporter-api/src/test/java/eu/dnetlib/openaire/community/importer/CommunityImporterServiceTest.java index beea0d82..0d2f4b97 100644 --- a/apps/dnet-exporter-api/src/test/java/eu/dnetlib/openaire/community/importer/CommunityImporterServiceTest.java +++ b/apps/dnet-exporter-api/src/test/java/eu/dnetlib/openaire/community/importer/CommunityImporterServiceTest.java @@ -4,6 +4,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; import java.nio.charset.StandardCharsets; +import java.util.Arrays; import java.util.LinkedList; import java.util.List; import java.util.Queue; @@ -16,6 +17,7 @@ import org.mockito.ArgumentCaptor; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.jdbc.core.JdbcTemplate; import eu.dnetlib.openaire.community.CommunityService; import eu.dnetlib.openaire.community.model.DbOrganization; @@ -40,11 +42,15 @@ class CommunityImporterServiceTest { @Mock private CommunityService service; + @Mock + private JdbcTemplate jdbcTemplate; + @BeforeEach public void setUp() { importer = new CommunityImporterService(); importer.setDbOrganizationRepository(dbOrganizationRepository); importer.setService(service); + importer.setJdbcTemplate(jdbcTemplate); } @Test @@ -54,8 +60,8 @@ class CommunityImporterServiceTest { // list.forEach(System.out::println); assertEquals(245, list.size()); - assertEquals(1, list.stream().filter(o -> o.getOrgId().equals("20|openorgs____::9dd5545aacd3d8019e00c3f837269746")).count()); - assertEquals(2, list.stream().filter(o -> o.getOrgId().equals("20|openorgs____::d11f981828c485cd23d93f7f24f24db1")).count()); + assertEquals(1, list.stream().filter(o -> o.getOrgId().equals("openorgs____::9dd5545aacd3d8019e00c3f837269746")).count()); + assertEquals(2, list.stream().filter(o -> o.getOrgId().equals("openorgs____::d11f981828c485cd23d93f7f24f24db1")).count()); assertEquals(14, list.stream().filter(o -> o.getCommunity().equals("beopen")).count()); } @@ -68,37 +74,38 @@ class CommunityImporterServiceTest { final Context context = ContextMappingUtils.parseContext(profile, errors); assertTrue(errors.isEmpty()); + Mockito.when(jdbcTemplate.queryForList(Mockito.anyString(), Mockito.any(Class.class), Mockito.anyString())).thenReturn(Arrays.asList("corda_______")); importer.importCommunity(context); final ArgumentCaptor detailsCapture = ArgumentCaptor.forClass(CommunityDetails.class); - final ArgumentCaptor> projectsCapture = ArgumentCaptor.forClass(List.class); - final ArgumentCaptor> datasourcesCapture = ArgumentCaptor.forClass(List.class); - final ArgumentCaptor> orgsCapture = ArgumentCaptor.forClass(List.class); - final ArgumentCaptor> subCommunitiesCapture = ArgumentCaptor.forClass(List.class); + final ArgumentCaptor projectsCapture = ArgumentCaptor.forClass(CommunityProject.class); + final ArgumentCaptor datasourcesCapture = ArgumentCaptor.forClass(CommunityContentprovider.class); + final ArgumentCaptor orgsCapture = ArgumentCaptor.forClass(CommunityOrganization.class); + final ArgumentCaptor subCommunitiesCapture = ArgumentCaptor.forClass(SubCommunity.class); Mockito.verify(service, Mockito.times(1)).saveCommunity(detailsCapture.capture()); - Mockito.verify(service, Mockito.times(1)).addCommunityProjectList(Mockito.anyString(), projectsCapture.capture()); - Mockito.verify(service, Mockito.times(1)).addCommunityContentProvidersList(Mockito.anyString(), datasourcesCapture.capture()); - Mockito.verify(service, Mockito.times(1)).addCommunityOrganizationList(Mockito.anyString(), orgsCapture.capture()); - Mockito.verify(service, Mockito.times(1)).addSubCommunityList(subCommunitiesCapture.capture()); + Mockito.verify(service, Mockito.times(1)).addCommunityProjects(Mockito.anyString(), projectsCapture.capture()); + Mockito.verify(service, Mockito.times(1)).addCommunityContentProviders(Mockito.anyString(), datasourcesCapture.capture()); + Mockito.verify(service, Mockito.times(1)).addCommunityOrganizations(Mockito.anyString(), orgsCapture.capture()); + Mockito.verify(service, Mockito.times(1)).addSubCommunities(subCommunitiesCapture.capture()); final CommunityDetails details = detailsCapture.getValue(); assertEquals("egi", details.getId()); // System.out.println(details); - final List projects = projectsCapture.getValue(); + final List projects = projectsCapture.getAllValues(); assertEquals(83, projects.size()); // projects.forEach(System.out::println); - final List datasources = datasourcesCapture.getValue(); + final List datasources = datasourcesCapture.getAllValues(); assertEquals(1, datasources.size()); // datasources.forEach(System.out::println); - final List orgs = orgsCapture.getValue(); + final List orgs = orgsCapture.getAllValues(); assertEquals(1, orgs.size()); // orgs.forEach(System.out::println); - final List subs = subCommunitiesCapture.getValue(); + final List subs = subCommunitiesCapture.getAllValues(); assertEquals(688, subs.size()); // subs.forEach(System.out::println);