update method for subcommunities

This commit is contained in:
Michele Artini 2024-10-21 11:15:21 +02:00
parent 78cd3ddca2
commit 57e3d8ae54
4 changed files with 293 additions and 38 deletions

View File

@ -50,6 +50,7 @@ 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.SubCommunityWritableProperties;
import eu.dnetlib.openaire.exporter.model.community.selectioncriteria.SelectionCriteria;
import eu.dnetlib.openaire.exporter.model.context.IISConfigurationEntry;
@ -289,44 +290,6 @@ public class CommunityService {
dbSupportOrgRepository.saveAll(list);
}
@Transactional
public SubCommunity getSubCommunity(final String id, final String subCommunityId) {
return dbCommunityRepository.findById(subCommunityId)
.filter(c -> c.getId().startsWith(id + CommunityMappingUtils.COMMUNITY_ID_PARTS_SEPARATOR))
.filter(c -> c.getType() == CommunityType.subcommunity)
.filter(c -> c.getParent() != null)
.map(CommunityMappingUtils::toSubCommunity)
.orElseThrow(() -> new ResourceNotFoundException("Sub-Community not found: " + subCommunityId));
}
@Transactional
public List<SubCommunity> getSubCommunitiesForCommunity(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 SubCommunity addSubCommunity(final String id, final SubCommunity subcommunity) throws CommunityException {
if (!id.equals(CommunityMappingUtils.calculateMainCommunityId(subcommunity.getSubCommunityId()))) {
throw new CommunityException("The sub-collection id does not start with " + id);
}
if (subcommunity.getParent() == null) {
subcommunity.setParent(id);
}
final DbCommunity dbc = CommunityMappingUtils.toDbCommunity(subcommunity);
dbCommunityRepository.save(dbc);
return subcommunity;
}
@Transactional
public void addCommunitySubjects(final String id, final String... subjects) {
modifyElementToArrayField(id, DbCommunity::getSubjects, DbCommunity::setSubjects, false, subjects);
@ -493,6 +456,59 @@ public class CommunityService {
return dbProjectRepository.findFundersByCommunity(id);
}
// Sub-communities methods
@Transactional
public SubCommunity getSubCommunity(final String id, final String subCommunityId) {
return dbCommunityRepository.findById(subCommunityId)
.filter(c -> c.getId().startsWith(id + CommunityMappingUtils.COMMUNITY_ID_PARTS_SEPARATOR))
.filter(c -> c.getType() == CommunityType.subcommunity)
.filter(c -> c.getParent() != null)
.map(CommunityMappingUtils::toSubCommunity)
.orElseThrow(() -> new ResourceNotFoundException("Sub-Community not found: " + subCommunityId));
}
@Transactional
public List<SubCommunity> getSubCommunitiesForCommunity(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 updateSubCommunity(final String subCommunityId, final SubCommunityWritableProperties details) {
final DbCommunity dbc = dbCommunityRepository.findById(subCommunityId)
.filter(c -> c.getType() == CommunityType.subcommunity)
.orElseThrow(() -> new ResourceNotFoundException("Community not found: " + subCommunityId));
CommunityMappingUtils.populateCommunity(dbc, details);
dbc.setLastUpdateDate(LocalDateTime.now());
dbCommunityRepository.save(dbc);
}
@Transactional
public SubCommunity addSubCommunity(final String id, final SubCommunity subcommunity) throws CommunityException {
if (!id.equals(CommunityMappingUtils.calculateMainCommunityId(subcommunity.getSubCommunityId()))) {
throw new CommunityException("The sub-collection id does not start with " + id);
}
if (subcommunity.getParent() == null) {
subcommunity.setParent(id);
}
final DbCommunity dbc = CommunityMappingUtils.toDbCommunity(subcommunity);
dbCommunityRepository.save(dbc);
return subcommunity;
}
@Transactional
public void removeSubCommunity(final String communityId, final String subCommunityId) throws CommunityException {
if (!communityId.equals(CommunityMappingUtils.calculateMainCommunityId(subCommunityId))) {

View File

@ -33,6 +33,7 @@ import eu.dnetlib.openaire.exporter.model.community.CommunityContentprovider;
import eu.dnetlib.openaire.exporter.model.community.CommunityOrganization;
import eu.dnetlib.openaire.exporter.model.community.CommunityProject;
import eu.dnetlib.openaire.exporter.model.community.SubCommunity;
import eu.dnetlib.openaire.exporter.model.community.SubCommunityWritableProperties;
import eu.dnetlib.openaire.exporter.model.community.selectioncriteria.SelectionCriteria;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
@ -96,6 +97,33 @@ public class SubCommunityApiController extends AbstractExporterController {
}
}
@PostMapping("/community/{id}/subcommunities/update")
@Operation(summary = "update 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 updateSubCommunity(
@PathVariable final String id,
@RequestParam final String subCommunityId,
@RequestBody final SubCommunityWritableProperties details) throws CommunityException {
verifyIdParameters(id, subCommunityId);
try {
communityService.updateSubCommunity(subCommunityId, details);
return communityService.getSubCommunity(id, subCommunityId);
} 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

View File

@ -30,6 +30,7 @@ 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.SubCommunityWritableProperties;
import eu.dnetlib.openaire.exporter.model.context.IISConfigurationEntry;
public class CommunityMappingUtils {
@ -134,6 +135,57 @@ public class CommunityMappingUtils {
c.setLastUpdateDate(LocalDateTime.now());
}
public static void populateCommunity(final DbCommunity c, final SubCommunityWritableProperties details) {
if (StringUtils.isNotBlank(details.getLabel())) {
c.setName(details.getLabel());
c.setShortName(details.getLabel());
}
if (StringUtils.isNotBlank(details.getCategory())) {
c.setCategory(details.getCategory());
}
if (details.getParams() != null) {
c.setParams(details.getParams());
}
if (details.getClaim() != null) {
c.setClaimable(details.getClaim());
}
if (details.getBrowsable() != null) {
c.setBrowsable(details.getBrowsable());
}
if (details.getFos() != null) {
c.setFos(toStringArray(details.getFos()));
}
if (details.getSdg() != null) {
c.setSdg(toStringArray(details.getSdg()));
}
if (details.getSubjects() != null) {
c.setSubjects(toStringArray(details.getSubjects()));
}
if (details.getAdvancedConstraints() != null) {
c.setAdvancedConstraints(details.getAdvancedConstraints());
}
if (details.getRemoveConstraints() != null) {
c.setRemoveConstraints(details.getRemoveConstraints());
}
if (StringUtils.isNotBlank(details.getZenodoCommunity())) {
c.setMainZenodoCommunity(details.getZenodoCommunity());
}
if (details.getOtherZenodoCommunities() != null) {
c.setOtherZenodoCommunities(toStringArray(details.getOtherZenodoCommunities()));
}
if (details.getSuggestedAcknowledgements() != null) {
c.setSuggestedAcknowledgements(toStringArray(details.getSuggestedAcknowledgements()));
}
c.setLastUpdateDate(LocalDateTime.now());
}
public static CommunityDetails toCommunityDetails(final DbCommunity c) {
final CommunityDetails details = new CommunityDetails();
populateSummary(details, c);
@ -349,4 +401,5 @@ public class CommunityMappingUtils {
public static boolean isValidSubCommunityId(final String subcommunityId) {
return StringUtils.isNotBlank(subcommunityId) && StringUtils.split(subcommunityId, COMMUNITY_ID_PARTS_SEPARATOR).length > 1;
}
}

View File

@ -0,0 +1,158 @@
package eu.dnetlib.openaire.exporter.model.community;
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import eu.dnetlib.openaire.exporter.model.community.selectioncriteria.SelectionCriteria;
import eu.dnetlib.openaire.exporter.model.context.Param;
import io.swagger.v3.oas.annotations.media.Schema;
@JsonAutoDetect
public class SubCommunityWritableProperties {
@Schema(description = "the label of the subCommunity", required = true)
private String label;
@Schema(description = "the category of the subCommunity", required = true)
private String category;
@Schema(description = "the parameters of the subCommunity", required = true)
private List<Param> params = new ArrayList<>();
@Schema(description = "it supports the claims", required = true)
private Boolean claim = null;
@Schema(description = "it is browsable", required = true)
private Boolean browsable = null;
@Schema(description = "list of subjects (keywords) that characterise this sub-community")
private List<String> subjects;
@Schema(description = "list of fos that characterise this sub-community")
private List<String> fos;
@Schema(description = "list of sdg that characterise this sub-community")
private List<String> sdg;
@Schema(description = "list of advanced criteria to associate results to this sub-community")
private SelectionCriteria advancedConstraints;
@Schema(description = "list of the remove criteria for this sub-community")
private SelectionCriteria removeConstraints;
@Schema(description = "Zenodo community associated to this sub-community")
protected String zenodoCommunity;
@Schema(description = "other zenodo communities for this sub-community")
private List<String> otherZenodoCommunities;
@Schema(description = "Suggested Acknowledgements for this sub-community")
private List<String> suggestedAcknowledgements;
public String getLabel() {
return label;
}
public void setLabel(final String label) {
this.label = label;
}
public String getCategory() {
return category;
}
public void setCategory(final String category) {
this.category = category;
}
public List<Param> getParams() {
return params;
}
public void setParams(final List<Param> params) {
this.params = params;
}
public Boolean getClaim() {
return claim;
}
public void setClaim(final Boolean claim) {
this.claim = claim;
}
public Boolean getBrowsable() {
return browsable;
}
public void setBrowsable(final Boolean browsable) {
this.browsable = browsable;
}
public List<String> getSubjects() {
return subjects;
}
public void setSubjects(final List<String> subjects) {
this.subjects = subjects;
}
public List<String> getFos() {
return fos;
}
public void setFos(final List<String> fos) {
this.fos = fos;
}
public List<String> getSdg() {
return sdg;
}
public void setSdg(final List<String> sdg) {
this.sdg = sdg;
}
public SelectionCriteria getAdvancedConstraints() {
return advancedConstraints;
}
public void setAdvancedConstraints(final SelectionCriteria advancedConstraints) {
this.advancedConstraints = advancedConstraints;
}
public SelectionCriteria getRemoveConstraints() {
return removeConstraints;
}
public void setRemoveConstraints(final SelectionCriteria removeConstraints) {
this.removeConstraints = removeConstraints;
}
public String getZenodoCommunity() {
return zenodoCommunity;
}
public void setZenodoCommunity(final String zenodoCommunity) {
this.zenodoCommunity = zenodoCommunity;
}
public List<String> getOtherZenodoCommunities() {
return otherZenodoCommunities;
}
public void setOtherZenodoCommunities(final List<String> otherZenodoCommunities) {
this.otherZenodoCommunities = otherZenodoCommunities;
}
public List<String> getSuggestedAcknowledgements() {
return suggestedAcknowledgements;
}
public void setSuggestedAcknowledgements(final List<String> suggestedAcknowledgements) {
this.suggestedAcknowledgements = suggestedAcknowledgements;
}
}