From 57e3d8ae54db6121e29e7b0e198b7c5943f45f1e Mon Sep 17 00:00:00 2001 From: "michele.artini" Date: Mon, 21 Oct 2024 11:15:21 +0200 Subject: [PATCH] update method for subcommunities --- .../openaire/community/CommunityService.java | 92 +++++----- .../community/SubCommunityApiController.java | 28 ++++ .../utils/CommunityMappingUtils.java | 53 ++++++ .../SubCommunityWritableProperties.java | 158 ++++++++++++++++++ 4 files changed, 293 insertions(+), 38 deletions(-) create mode 100644 libs/dnet-exporter-model/src/main/java/eu/dnetlib/openaire/exporter/model/community/SubCommunityWritableProperties.java 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 2f18dec5..68efc1a9 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 @@ -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 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 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))) { diff --git a/apps/dnet-exporter-api/src/main/java/eu/dnetlib/openaire/community/SubCommunityApiController.java b/apps/dnet-exporter-api/src/main/java/eu/dnetlib/openaire/community/SubCommunityApiController.java index dc2b068a..774ebec4 100644 --- a/apps/dnet-exporter-api/src/main/java/eu/dnetlib/openaire/community/SubCommunityApiController.java +++ b/apps/dnet-exporter-api/src/main/java/eu/dnetlib/openaire/community/SubCommunityApiController.java @@ -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 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 3394348a..d2a33772 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 @@ -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; } + } diff --git a/libs/dnet-exporter-model/src/main/java/eu/dnetlib/openaire/exporter/model/community/SubCommunityWritableProperties.java b/libs/dnet-exporter-model/src/main/java/eu/dnetlib/openaire/exporter/model/community/SubCommunityWritableProperties.java new file mode 100644 index 00000000..d2c554d8 --- /dev/null +++ b/libs/dnet-exporter-model/src/main/java/eu/dnetlib/openaire/exporter/model/community/SubCommunityWritableProperties.java @@ -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 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 subjects; + + @Schema(description = "list of fos that characterise this sub-community") + private List fos; + + @Schema(description = "list of sdg that characterise this sub-community") + private List 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 otherZenodoCommunities; + + @Schema(description = "Suggested Acknowledgements for this sub-community") + private List 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 getParams() { + return params; + } + + public void setParams(final List 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 getSubjects() { + return subjects; + } + + public void setSubjects(final List subjects) { + this.subjects = subjects; + } + + public List getFos() { + return fos; + } + + public void setFos(final List fos) { + this.fos = fos; + } + + public List getSdg() { + return sdg; + } + + public void setSdg(final List 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 getOtherZenodoCommunities() { + return otherZenodoCommunities; + } + + public void setOtherZenodoCommunities(final List otherZenodoCommunities) { + this.otherZenodoCommunities = otherZenodoCommunities; + } + + public List getSuggestedAcknowledgements() { + return suggestedAcknowledgements; + } + + public void setSuggestedAcknowledgements(final List suggestedAcknowledgements) { + this.suggestedAcknowledgements = suggestedAcknowledgements; + } + +}