fixed some methods

This commit is contained in:
Michele Artini 2024-10-03 10:20:57 +02:00
parent 33aedaa530
commit 24c4ed3b8d
4 changed files with 188 additions and 160 deletions

View File

@ -9,6 +9,7 @@ import static eu.dnetlib.openaire.common.ExporterConstants.C_ZC;
import static eu.dnetlib.openaire.common.ExporterConstants.R;
import static eu.dnetlib.openaire.common.ExporterConstants.W;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
@ -83,7 +84,7 @@ public class CommunityApiController extends AbstractExporterController {
@ApiResponse(responseCode = "404", description = "not found"),
@ApiResponse(responseCode = "500", description = "unexpected error")
})
public CommunityDetails getCommunity(@RequestBody final CommunityDetails details) throws CommunityException {
public CommunityDetails addCommunity(@RequestBody final CommunityDetails details) throws CommunityException {
try {
return communityService.newCommunity(details);
} catch (final ResourceNotFoundException e) {
@ -121,11 +122,11 @@ public class CommunityApiController extends AbstractExporterController {
@ApiResponse(responseCode = "404", description = "not found"),
@ApiResponse(responseCode = "500", description = "unexpected error")
})
public void setCommunity(
public void updateCommunity(
@PathVariable final String id,
@RequestBody final CommunityWritableProperties properties) throws CommunityException {
try {
communityService.setCommunity(id, properties);
communityService.updateCommunity(id, properties);
} catch (final ResourceNotFoundException e) {
throw e;
} catch (final Throwable e) {
@ -145,7 +146,128 @@ public class CommunityApiController extends AbstractExporterController {
public void deleteCommunity(@PathVariable final String id)
throws CommunityException {
try {
communityService.removeCommunities(id);
communityService.removeCommunity(id);
} catch (final ResourceNotFoundException e) {
throw e;
} catch (final Throwable e) {
throw new CommunityException(e);
}
}
// APIs to manage the sub communities
@GetMapping("/community/{id}/subcommunities")
@Operation(summary = "get the list of subcommunities for a given community", description = "get the list of subcommunities for a given community", tags = {
C_SUB, R
})
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "404", description = "not found"),
@ApiResponse(responseCode = "500", description = "unexpected error")
})
public List<SubCommunity> getSubCommunities(@PathVariable final String id, @RequestParam(required = false, defaultValue = "false") final boolean all)
throws CommunityException {
try {
return communityService.getSubCommunities(id)
.stream()
.filter(sc -> all || sc.isBrowsable())
.collect(Collectors.toList());
} catch (final ResourceNotFoundException e) {
throw e;
} catch (final Throwable e) {
throw new CommunityException(e);
}
}
@PostMapping("/community/{id}/subcommunities")
@Operation(summary = "associate a subcommunity to the community, provide all the fields or the method will overwrite with nulls the fields that are missing", description = "associate a subcommunity to the community, provide all the fields or the method will overwrite with nulls the fields that are missing", tags = {
C_SUB, W
})
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "404", description = "not found"),
@ApiResponse(responseCode = "500", description = "unexpected error")
})
public SubCommunity addSubCommunity(
@PathVariable final String id,
@RequestBody final SubCommunity subcommunity) throws CommunityException {
try {
return communityService.addSubCommunity(id, subcommunity);
} catch (final ResourceNotFoundException e) {
throw e;
} catch (final Throwable e) {
throw new CommunityException(e);
}
}
@PostMapping("/community/{id}/subcommunitiesList")
@Operation(summary = "associate a list of subcommunities to the community, provide all the fields or the method will overwrite with nulls the fields that are missing", description = "associate a list of subcommunities to the community, provide all the fields or the method will overwrite with nulls the fields that are missing", tags = {
C_SUB, W
})
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "404", description = "not found"),
@ApiResponse(responseCode = "500", description = "unexpected error")
})
public List<SubCommunity> addSubCommunityList(
@PathVariable final String id,
@RequestBody final SubCommunity[] subcommunities) throws CommunityException {
for (final SubCommunity sub : subcommunities) {
if (!id.equals(CommunityMappingUtils.calculateMainCommunityId(sub.getSubCommunityId()))) {
throw new CommunityException("The sub-collection id does not start with " + id);
}
}
try {
final List<SubCommunity> res = new ArrayList<SubCommunity>();
for (final SubCommunity subc : subcommunities) {
res.add(communityService.addSubCommunity(id, subc));
}
return res;
} catch (final ResourceNotFoundException e) {
throw e;
} catch (final Throwable e) {
throw new CommunityException(e);
}
}
@DeleteMapping("/community/{id}/subcommunities")
@Operation(summary = "remove the association between a subcommunity and the community", description = "remove the association between a subcommunity and the community", tags = {
C_SUB, W
})
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "404", description = "not found"),
@ApiResponse(responseCode = "500", description = "unexpected error")
})
public void removeSubCommunity(
@PathVariable final String id,
@RequestParam final String subCommunityId) throws CommunityException {
try {
communityService.removeSubCommunity(id, subCommunityId);
} catch (final ResourceNotFoundException e) {
throw e;
} catch (final Throwable e) {
throw new CommunityException(e);
}
}
@DeleteMapping("/community/{id}/subcommunitiesList")
@Operation(summary = "remove a list of associations between some subcommunities and the community", description = "remove a list of associations between some subcommunities and the community", tags = {
C_SUB, W
})
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "404", description = "not found"),
@ApiResponse(responseCode = "500", description = "unexpected error")
})
public void removeSubcommunities(
@PathVariable final String id,
@RequestBody final String[] subCommunityIdList) throws CommunityException {
try {
for (final String subId : subCommunityIdList) {
communityService.removeSubCommunity(id, subId);
}
} catch (final ResourceNotFoundException e) {
throw e;
} catch (final Throwable e) {
@ -178,6 +300,8 @@ public class CommunityApiController extends AbstractExporterController {
}
}
// PROJECTS
@PostMapping("/community/{id}/projects")
@Operation(summary = "associate a project to the community, provide all the fields or the method will overwrite with nulls the fields that are missing", description = "associate a project to the community, provide all the fields or the method will overwrite with nulls the fields that are missing", tags = {
C_PJ, W
@ -971,135 +1095,4 @@ public class CommunityApiController extends AbstractExporterController {
}
}
// APIs to manage the sub communities
@GetMapping("/community/{id}/subcommunities")
@Operation(summary = "get the list of subcommunities for a given community", description = "get the list of subcommunities for a given community", tags = {
C_SUB, R
})
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "404", description = "not found"),
@ApiResponse(responseCode = "500", description = "unexpected error")
})
public List<SubCommunity> getSubCommunities(@PathVariable final String id, @RequestParam(required = false, defaultValue = "false") final boolean all)
throws CommunityException {
try {
return communityService.getSubCommunities(id)
.stream()
.filter(sc -> all || sc.isBrowsable())
.collect(Collectors.toList());
} catch (final ResourceNotFoundException e) {
throw e;
} catch (final Throwable e) {
throw new CommunityException(e);
}
}
@PostMapping("/community/{id}/subcommunities")
@Operation(summary = "associate a subcommunity to the community, provide all the fields or the method will overwrite with nulls the fields that are missing", description = "associate a subcommunity to the community, provide all the fields or the method will overwrite with nulls the fields that are missing", tags = {
C_SUB, W
})
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "404", description = "not found"),
@ApiResponse(responseCode = "500", description = "unexpected error")
})
public SubCommunity addSubCommunity(
@PathVariable final String id,
@RequestBody final SubCommunity subcommunity) throws CommunityException {
try {
if (!id.equals(CommunityMappingUtils.calculateMainCommunityId(subcommunity.getSubCommunityId()))) {
throw new CommunityException("The sub-collection id does not start with " + id);
}
communityService.addSubCommunities(subcommunity);
return subcommunity;
} catch (final ResourceNotFoundException e) {
throw e;
} catch (final Throwable e) {
throw new CommunityException(e);
}
}
@PostMapping("/community/{id}/subcommunitiesList")
@Operation(summary = "associate a list of subcommunities to the community, provide all the fields or the method will overwrite with nulls the fields that are missing", description = "associate a list of subcommunities to the community, provide all the fields or the method will overwrite with nulls the fields that are missing", tags = {
C_SUB, W
})
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "404", description = "not found"),
@ApiResponse(responseCode = "500", description = "unexpected error")
})
public SubCommunity[] addSubCommunityList(
@PathVariable final String id,
@RequestBody final SubCommunity[] subcommunities) throws CommunityException {
for (final SubCommunity sub : subcommunities) {
if (!id.equals(CommunityMappingUtils.calculateMainCommunityId(sub.getSubCommunityId()))) {
throw new CommunityException("The sub-collection id does not start with " + id);
}
}
try {
communityService.addSubCommunities(subcommunities);
return subcommunities;
} catch (final ResourceNotFoundException e) {
throw e;
} catch (final Throwable e) {
throw new CommunityException(e);
}
}
@DeleteMapping("/community/{id}/subcommunities")
@Operation(summary = "remove the association between a subcommunity and the community", description = "remove the association between a subcommunity and the community", tags = {
C_SUB, W
})
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "404", description = "not found"),
@ApiResponse(responseCode = "500", description = "unexpected error")
})
public void removeSubCommunity(
@PathVariable final String id,
@RequestParam final String subCommunityId) throws CommunityException {
try {
if (!id.equals(CommunityMappingUtils.calculateMainCommunityId(subCommunityId))) {
throw new CommunityException("The sub-collection id does not start with " + id);
}
communityService.removeCommunities(subCommunityId);
} catch (final ResourceNotFoundException e) {
throw e;
} catch (final Throwable e) {
throw new CommunityException(e);
}
}
@DeleteMapping("/community/{id}/subcommunitiesList")
@Operation(summary = "remove a list of associations between some subcommunities and the community", description = "remove a list of associations between some subcommunities and the community", tags = {
C_SUB, W
})
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "404", description = "not found"),
@ApiResponse(responseCode = "500", description = "unexpected error")
})
public void removeSubcommunities(
@PathVariable final String id,
@RequestBody final String[] subCommunityIdList) throws CommunityException {
try {
for (final String sub : subCommunityIdList) {
if (!id.equals(CommunityMappingUtils.calculateMainCommunityId(sub))) {
throw new CommunityException("The sub-collection id does not start with " + id);
}
}
communityService.removeCommunities(subCommunityIdList);
} catch (final ResourceNotFoundException e) {
throw e;
} catch (final Throwable e) {
throw new CommunityException(e);
}
}
}

View File

@ -47,6 +47,7 @@ import eu.dnetlib.openaire.exporter.model.community.CommunityDetails;
import eu.dnetlib.openaire.exporter.model.community.CommunityOrganization;
import eu.dnetlib.openaire.exporter.model.community.CommunityProject;
import eu.dnetlib.openaire.exporter.model.community.CommunitySummary;
import eu.dnetlib.openaire.exporter.model.community.CommunityType;
import eu.dnetlib.openaire.exporter.model.community.CommunityWritableProperties;
import eu.dnetlib.openaire.exporter.model.community.SubCommunity;
import eu.dnetlib.openaire.exporter.model.community.selectioncriteria.SelectionCriteria;
@ -94,16 +95,35 @@ public class CommunityService {
@Transactional
public CommunityDetails getCommunity(final String id) {
final DbCommunity c = dbCommunityRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Community not found: " + id));
return CommunityMappingUtils.toCommunityDetails(c);
final DbCommunity dbc = dbCommunityRepository.findById(id)
.filter(c -> c.getType() != CommunityType.subcommunity)
.filter(c -> c.getParent() == null)
.orElseThrow(() -> new ResourceNotFoundException("Community not found: " + id));
return CommunityMappingUtils.toCommunityDetails(dbc);
}
@Transactional
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);
public void updateCommunity(final String id, final CommunityWritableProperties details) {
final DbCommunity dbc = dbCommunityRepository.findById(id)
.filter(c -> c.getType() != CommunityType.subcommunity)
.filter(c -> c.getParent() == null)
.orElseThrow(() -> new ResourceNotFoundException("Community not found: " + id));
CommunityMappingUtils.populateCommunity(dbc, details);
dbc.setLastUpdateDate(LocalDateTime.now());
dbCommunityRepository.save(dbc);
}
@Transactional
public void removeCommunity(final String id) {
final DbCommunity dbc = dbCommunityRepository.findById(id)
.filter(c -> c.getType() != CommunityType.subcommunity)
.filter(c -> c.getParent() == null)
.orElseThrow(() -> new ResourceNotFoundException("Community not found: " + id));
dbCommunityRepository.delete(dbc);
}
@Transactional
@ -273,18 +293,27 @@ public class CommunityService {
public List<SubCommunity> getSubCommunities(final String id) {
return dbCommunityRepository.findByIdStartsWith(id + CommunityMappingUtils.COMMUNITY_ID_PARTS_SEPARATOR)
.stream()
.filter(c -> c.getType() == CommunityType.subcommunity)
.filter(c -> c.getParent() != null)
.map(CommunityMappingUtils::toSubCommunity)
.collect(Collectors.toList());
}
@Transactional
public void addSubCommunities(final SubCommunity... subs) {
public SubCommunity addSubCommunity(final String id, final SubCommunity subcommunity) throws CommunityException {
final List<DbCommunity> list = Arrays.stream(subs)
.map(CommunityMappingUtils::toDbCommunity)
.collect(Collectors.toList());
if (!id.equals(CommunityMappingUtils.calculateMainCommunityId(subcommunity.getSubCommunityId()))) {
throw new CommunityException("The sub-collection id does not start with " + id);
}
dbCommunityRepository.saveAll(list);
if (subcommunity.getParent() == null) {
subcommunity.setParent(id);
}
final DbCommunity dbc = CommunityMappingUtils.toDbCommunity(subcommunity);
dbCommunityRepository.save(dbc);
return subcommunity;
}
@Transactional
@ -436,13 +465,6 @@ public class CommunityService {
return getPropagationOrganizationsForCommunity(communityId);
}
@Transactional
public void removeCommunities(final String... ids) {
for (final String subId : ids) {
dbCommunityRepository.deleteById(subId);
}
}
@Transactional
public List<IISConfigurationEntry> getIISConfiguration(final String id) {
final List<IISConfigurationEntry> res = new ArrayList<>();
@ -463,4 +485,17 @@ public class CommunityService {
return dbProjectRepository.findFundersByCommunity(id);
}
@Transactional
public void removeSubCommunity(final String communityId, final String subCommunityId) throws CommunityException {
if (!communityId.equals(CommunityMappingUtils.calculateMainCommunityId(subCommunityId))) {
throw new CommunityException("The sub-collection id does not start with " + communityId);
}
final DbCommunity dbc = dbCommunityRepository.findById(subCommunityId)
.filter(c -> c.getType() == CommunityType.subcommunity)
.filter(c -> c.getParent() != null)
.orElseThrow(() -> new ResourceNotFoundException("SubCommunity not found: " + subCommunityId));
dbCommunityRepository.delete(dbc);
}
}

View File

@ -216,10 +216,11 @@ public class CommunityImporterService {
service.addCommunityProjects(context.getId(), projects.toArray(new CommunityProject[projects.size()]));
service.addCommunityDatasources(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) {
for (final SubCommunity sub : subs) {
service.addSubCommunity(community.getId(), sub);
}
} catch (final Exception e) {
throw new RuntimeException("Error importing community: " + context.getId(), e);
}
}

View File

@ -87,7 +87,7 @@ class CommunityImporterServiceTest {
Mockito.verify(service, Mockito.times(1)).addCommunityProjects(Mockito.anyString(), projectsCapture.capture());
Mockito.verify(service, Mockito.times(1)).addCommunityDatasources(Mockito.anyString(), datasourcesCapture.capture());
Mockito.verify(service, Mockito.times(1)).addCommunityOrganizations(Mockito.anyString(), orgsCapture.capture());
Mockito.verify(service, Mockito.times(1)).addSubCommunities(subCommunitiesCapture.capture());
Mockito.verify(service, Mockito.times(688)).addSubCommunity(Mockito.anyString(), subCommunitiesCapture.capture());
final CommunityDetails details = detailsCapture.getValue();
assertEquals("egi", details.getId());
@ -111,7 +111,6 @@ class CommunityImporterServiceTest {
}
@SuppressWarnings("unchecked")
@Test
public void testImportCommunityFetFp7() throws Exception {
final String profile = IOUtils.toString(getClass().getResourceAsStream("old_community_profile_fet-fp7.xml"), StandardCharsets.UTF_8.toString());
@ -134,7 +133,7 @@ class CommunityImporterServiceTest {
Mockito.verify(service, Mockito.times(1)).addCommunityProjects(Mockito.anyString(), projectsCapture.capture());
Mockito.verify(service, Mockito.times(1)).addCommunityDatasources(Mockito.anyString(), datasourcesCapture.capture());
Mockito.verify(service, Mockito.times(1)).addCommunityOrganizations(Mockito.anyString(), orgsCapture.capture());
Mockito.verify(service, Mockito.times(1)).addSubCommunities(subCommunitiesCapture.capture());
Mockito.verify(service, Mockito.times(151)).addSubCommunity(Mockito.anyString(), subCommunitiesCapture.capture());
final CommunityDetails details = detailsCapture.getValue();
assertEquals("fet-fp7", details.getId());