new apis for subcommunities

This commit is contained in:
Michele Artini 2023-10-18 12:05:56 +02:00
parent 05795a3604
commit 2de5d6e7fb
7 changed files with 136 additions and 9 deletions

View File

@ -10,6 +10,7 @@ public class ExporterConstants {
public final static String C_PJ = "Community projects";
public final static String C_ZC = "Community Zenodo Communities";
public final static String C_O = "Community Organizations";
public final static String C_SUB = "Subcommunities";
public final static String DS = "Datasource";
public final static String API = "Interface";

View File

@ -4,6 +4,7 @@ import static eu.dnetlib.openaire.common.ExporterConstants.C;
import static eu.dnetlib.openaire.common.ExporterConstants.C_CP;
import static eu.dnetlib.openaire.common.ExporterConstants.C_O;
import static eu.dnetlib.openaire.common.ExporterConstants.C_PJ;
import static eu.dnetlib.openaire.common.ExporterConstants.C_SUB;
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;
@ -11,6 +12,7 @@ import static eu.dnetlib.openaire.common.ExporterConstants.W;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
@ -33,6 +35,7 @@ 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.CommunityWritableProperties;
import eu.dnetlib.openaire.exporter.model.community.SubCommunity;
import eu.dnetlib.openaire.exporter.model.community.selectioncriteria.SelectionCriteria;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
@ -937,4 +940,126 @@ public class CommunityApiController extends AbstractExporterController {
}
}
// APIs to manage the sub communities
@RequestMapping(value = "/community/{id}/subcommunities", produces = {
"application/json"
}, method = RequestMethod.GET)
@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);
}
}
@RequestMapping(value = "/community/{id}/subcommunities", produces = {
"application/json"
}, method = RequestMethod.POST)
@Operation(summary = "associate a subcommunity to the community", description = "associate a subcommunity to 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 SubCommunity addSubCommunity(
@PathVariable final String id,
@RequestBody final SubCommunity subcommunity) throws CommunityException {
try {
communityService.addSubCommunities(id, subcommunity);
return subcommunity;
} catch (final ResourceNotFoundException e) {
throw e;
} catch (final Throwable e) {
throw new CommunityException(e);
}
}
@RequestMapping(value = "/community/{id}/subcommunitiesList", produces = {
"application/json"
}, method = RequestMethod.POST)
@Operation(summary = "associate a list of subcommunities to the community", description = "associate a list of subcommunities to 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 SubCommunity[] addSubCommunityList(
@PathVariable final String id,
@RequestBody final SubCommunity[] subcommunities) throws CommunityException {
try {
communityService.addSubCommunities(id, subcommunities);
return subcommunities;
} catch (final ResourceNotFoundException e) {
throw e;
} catch (final Throwable e) {
throw new CommunityException(e);
}
}
@RequestMapping(value = "/community/{id}/subcommunities", produces = {
"application/json"
}, method = RequestMethod.DELETE)
@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.removeSubCommunities(id, subCommunityId);
} catch (final ResourceNotFoundException e) {
throw e;
} catch (final Throwable e) {
throw new CommunityException(e);
}
}
@RequestMapping(value = "/community/{id}/subcommunitiesList", produces = {
"application/json"
}, method = RequestMethod.DELETE)
@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 {
communityService.removeSubCommunities(id, subCommunityIdList);
} catch (final ResourceNotFoundException e) {
throw e;
} catch (final Throwable e) {
throw new CommunityException(e);
}
}
}

View File

@ -259,8 +259,8 @@ public class CommunityService {
}
@Transactional
public void removeSubCommunity(final String id, final String subCommunityId) {
dbSubCommunityRepository.deleteById(subCommunityId);
public void removeSubCommunities(final String id, final String... subCommunityIds) {
dbSubCommunityRepository.deleteAllById(Arrays.asList(subCommunityIds));
}
@Transactional
@ -272,9 +272,9 @@ public class CommunityService {
}
@Transactional
public void addSubCommunities(final SubCommunity... subs) {
public void addSubCommunities(final String id, final SubCommunity... subs) {
final List<DbSubCommunity> list = Arrays.stream(subs)
.map(CommunityMappingUtils::toDbSubCommunity)
.map(s -> CommunityMappingUtils.toDbSubCommunity(id, s))
.collect(Collectors.toList());
dbSubCommunityRepository.saveAll(list);

View File

@ -216,7 +216,7 @@ public class CommunityImporterService {
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()]));
service.addSubCommunities(context.getId(), subs.toArray(new SubCommunity[subs.size()]));
} catch (
final Exception e) {

View File

@ -214,9 +214,9 @@ public class CommunityMappingUtils {
return dbo;
}
public static DbSubCommunity toDbSubCommunity(final SubCommunity sub) {
public static DbSubCommunity toDbSubCommunity(final String id, final SubCommunity sub) {
final DbSubCommunity dbsc = new DbSubCommunity();
dbsc.setCommunity(sub.getCommunityId());
dbsc.setCommunity(id);
dbsc.setId(sub.getSubCommunityId());
dbsc.setCategory(sub.getCategory());
dbsc.setLabel(sub.getLabel());

View File

@ -35,6 +35,7 @@ CREATE TABLE community_projects (
project_name text NOT NULL,
project_acronym text,
project_funder text NOT NULL,
available_since date NOT NULL default now(),
PRIMARY KEY (community, project_id)
);

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)).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());
Mockito.verify(service, Mockito.times(1)).addSubCommunities(Mockito.anyString(), subCommunitiesCapture.capture());
final CommunityDetails details = detailsCapture.getValue();
assertEquals("egi", details.getId());
@ -134,7 +134,7 @@ class CommunityImporterServiceTest {
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());
Mockito.verify(service, Mockito.times(1)).addSubCommunities(Mockito.anyString(), subCommunitiesCapture.capture());
final CommunityDetails details = detailsCapture.getValue();
assertEquals("fet-fp7", details.getId());