From 8b7cc375fef58e5ab8e579c2c9a07722bde2e89d Mon Sep 17 00:00:00 2001 From: "michele.artini" Date: Wed, 25 Sep 2024 14:55:21 +0200 Subject: [PATCH] partial implementation --- .../community/CommunityApiController.java | 32 +++++++++++++++---- .../openaire/community/CommunityService.java | 29 ++++++----------- .../importer/CommunityImporterService.java | 2 +- .../openaire/community/model/DbCommunity.java | 12 +++---- .../repository/DbCommunityRepository.java | 8 +++-- .../utils/CommunityMappingUtils.java | 23 +++++++------ 6 files changed, 58 insertions(+), 48 deletions(-) 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 6c04fc68..b47cb418 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 @@ -28,6 +28,7 @@ import org.springframework.web.bind.annotation.RestController; import eu.dnetlib.openaire.common.AbstractExporterController; import eu.dnetlib.openaire.community.model.DepositionInfo; +import eu.dnetlib.openaire.community.utils.CommunityMappingUtils; import eu.dnetlib.openaire.exporter.exceptions.CommunityException; import eu.dnetlib.openaire.exporter.exceptions.ResourceNotFoundException; import eu.dnetlib.openaire.exporter.model.community.CommunityContentprovider; @@ -141,10 +142,10 @@ public class CommunityApiController extends AbstractExporterController { @ApiResponse(responseCode = "404", description = "not found"), @ApiResponse(responseCode = "500", description = "unexpected error") }) - public void deleteCommunity(@PathVariable final String id, @RequestParam(required = false, defaultValue = "false") final boolean recursive) + public void deleteCommunity(@PathVariable final String id) throws CommunityException { try { - communityService.deleteCommunity(id, recursive); + communityService.removeCommunities(id); } catch (final ResourceNotFoundException e) { throw e; } catch (final Throwable e) { @@ -1008,7 +1009,12 @@ public class CommunityApiController extends AbstractExporterController { @PathVariable final String id, @RequestBody final SubCommunity subcommunity) throws CommunityException { try { - communityService.addSubCommunities(id, subcommunity); + 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; @@ -1030,8 +1036,13 @@ public class CommunityApiController extends AbstractExporterController { @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(id, subcommunities); + communityService.addSubCommunities(subcommunities); return subcommunities; } catch (final ResourceNotFoundException e) { throw e; @@ -1053,7 +1064,11 @@ public class CommunityApiController extends AbstractExporterController { @PathVariable final String id, @RequestParam final String subCommunityId) throws CommunityException { try { - communityService.removeSubCommunities(id, subCommunityId); + 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) { @@ -1074,7 +1089,12 @@ public class CommunityApiController extends AbstractExporterController { @PathVariable final String id, @RequestBody final String[] subCommunityIdList) throws CommunityException { try { - communityService.removeSubCommunities(id, subCommunityIdList); + 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) { 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 78b636ab..5ab1846c 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 @@ -70,7 +70,7 @@ public class CommunityService { private static final Log log = LogFactory.getLog(CommunityService.class); public List listCommunities() { - return dbCommunityRepository.findAll() + return dbCommunityRepository.findByParentIsNull() .stream() .map(CommunityMappingUtils::toCommunitySummary) .collect(Collectors.toList()); @@ -269,25 +269,19 @@ public class CommunityService { dbSupportOrgRepository.saveAll(list); } - @Transactional - public void removeSubCommunities(final String masterId, final String... subCommunityIds) { - for (final String subId : subCommunityIds) { - dbCommunityRepository.deleteByMasterAndId(masterId, subId); - } - } - @Transactional public List getSubCommunities(final String id) { - return dbCommunityRepository.findByMaster(id) + return dbCommunityRepository.findByIdStartsWith(id + CommunityMappingUtils.COMMUNITY_ID_PARTS_SEPARATOR) .stream() .map(CommunityMappingUtils::toSubCommunity) .collect(Collectors.toList()); } @Transactional - public void addSubCommunities(final String id, final SubCommunity... subs) { + public void addSubCommunities(final SubCommunity... subs) { + final List list = Arrays.stream(subs) - .map(s -> CommunityMappingUtils.toDbCommunity(id, s)) + .map(CommunityMappingUtils::toDbCommunity) .collect(Collectors.toList()); dbCommunityRepository.saveAll(list); @@ -443,15 +437,10 @@ public class CommunityService { } @Transactional - public void deleteCommunity(final String id, final boolean recursive) { - if (recursive) { - dbProjectRepository.deleteByCommunity(id); - dbDatasourceRepository.deleteByCommunity(id); - dbOrganizationRepository.deleteByCommunity(id); - dbSupportOrgRepository.deleteByCommunity(id); - dbCommunityRepository.deleteByMaster(id); + public void removeCommunities(final String... ids) { + for (final String subId : ids) { + dbCommunityRepository.deleteById(subId); } - dbCommunityRepository.deleteById(id); } @Transactional @@ -462,7 +451,7 @@ public class CommunityService { .map(CommunityMappingUtils::asIISConfigurationEntry) .orElseThrow(() -> new ResourceNotFoundException("Community not found: " + id))); - for (final DbCommunity subc : dbCommunityRepository.findByMaster(id)) { + for (final DbCommunity subc : dbCommunityRepository.findByIdStartsWith(id + CommunityMappingUtils.COMMUNITY_ID_PARTS_SEPARATOR)) { res.add(CommunityMappingUtils.asIISConfigurationEntry(subc)); } 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 894c835a..9e459ab1 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 @@ -216,7 +216,7 @@ 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(context.getId(), subs.toArray(new SubCommunity[subs.size()])); + service.addSubCommunities(subs.toArray(new SubCommunity[subs.size()])); } catch ( final Exception e) { diff --git a/apps/dnet-exporter-api/src/main/java/eu/dnetlib/openaire/community/model/DbCommunity.java b/apps/dnet-exporter-api/src/main/java/eu/dnetlib/openaire/community/model/DbCommunity.java index e50d45d4..dcfcf43d 100644 --- a/apps/dnet-exporter-api/src/main/java/eu/dnetlib/openaire/community/model/DbCommunity.java +++ b/apps/dnet-exporter-api/src/main/java/eu/dnetlib/openaire/community/model/DbCommunity.java @@ -50,8 +50,8 @@ public class DbCommunity implements Serializable { @Column(name = "id") private String id; - @Column(name = "master") - private String master; + @Column(name = "parent") + private String parent; @Column(name = "name") private String name; @@ -152,12 +152,12 @@ public class DbCommunity implements Serializable { this.id = id; } - public String getMaster() { - return master; + public String getParent() { + return parent; } - public void setMaster(final String master) { - this.master = master; + public void setParent(final String parent) { + this.parent = parent; } public String getName() { diff --git a/apps/dnet-exporter-api/src/main/java/eu/dnetlib/openaire/community/repository/DbCommunityRepository.java b/apps/dnet-exporter-api/src/main/java/eu/dnetlib/openaire/community/repository/DbCommunityRepository.java index 0fa26510..827c0825 100644 --- a/apps/dnet-exporter-api/src/main/java/eu/dnetlib/openaire/community/repository/DbCommunityRepository.java +++ b/apps/dnet-exporter-api/src/main/java/eu/dnetlib/openaire/community/repository/DbCommunityRepository.java @@ -11,13 +11,15 @@ import eu.dnetlib.openaire.community.model.DbCommunity; @ConditionalOnProperty(value = "openaire.exporter.enable.community", havingValue = "true") public interface DbCommunityRepository extends JpaRepository { + List findByParentIsNull(); + @Query(value = "select id from communities where ?1 = ANY(array_append(other_zenodo_communities, main_zenodo_community))", nativeQuery = true) List findByZenodoId(String zenodoId); - void deleteByMaster(String master); + // TODO RICONTROLLARE QUERY - void deleteByMasterAndId(String masterId, String subId); + void deleteByIdStartWith(String main); - List findByMaster(String master); + List findByIdStartsWith(String prefix); } 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 992acc91..2d72e372 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 @@ -32,6 +32,8 @@ import eu.dnetlib.openaire.exporter.model.context.IISConfigurationEntry; public class CommunityMappingUtils { + public static final String COMMUNITY_ID_PARTS_SEPARATOR = "::"; + public final static String PIPE_SEPARATOR = "||"; private static final List DATE_PATTERN = Lists.newArrayList("yyyy-MM-dd'T'hh:mm:ss", "yyyy-MM-dd'T'hh:mm:ssXXX", "yyyy-MM-dd'T'hh:mm:ss+00:00"); @@ -234,15 +236,11 @@ public class CommunityMappingUtils { return dbo; } - public static DbCommunity toDbCommunity(final String master, final SubCommunity sub) { + public static DbCommunity toDbCommunity(final SubCommunity sub) { final DbCommunity dbsc = new DbCommunity(); dbsc.setId(sub.getSubCommunityId()); - dbsc.setMaster(master); - - // TODO - // dbsc.setParent(sub.getParent()); - + dbsc.setParent(sub.getParent()); dbsc.setCategory(sub.getCategory()); dbsc.setName(sub.getLabel()); dbsc.setShortName(sub.getLabel()); @@ -266,11 +264,8 @@ public class CommunityMappingUtils { final SubCommunity sc = new SubCommunity(); sc.setSubCommunityId(sub.getId()); - sc.setCommunityId(sub.getMaster()); - - // TODO - // sc.setParent(sub.getParent()); - + sc.setCommunityId(calculateMainCommunityId(sub.getId())); + sc.setParent(sub.getParent()); sc.setCategory(sub.getCategory()); sc.setLabel(sub.getName()); sc.setParams(sub.getParams()); @@ -288,6 +283,10 @@ public class CommunityMappingUtils { return sc; } + public static String calculateMainCommunityId(final String sub) { + return StringUtils.substringBefore(sub, COMMUNITY_ID_PARTS_SEPARATOR); + } + public static LocalDateTime asLocalDateTime(final String s) { if (StringUtils.isBlank(s)) { return null; } @@ -316,7 +315,7 @@ public class CommunityMappingUtils { conf.setId(c.getId()); conf.setLabel(c.getName()); - if (c.getMaster() == null) { + if (c.getParent() == null) { conf.addParams(CommunityImporterService.CSUMMARY_DESCRIPTION, c.getDescription()); conf.addParams(CommunityImporterService.CSUMMARY_LOGOURL, c.getLogoUrl()); conf.addParams(CommunityImporterService.CSUMMARY_STATUS, c.getStatus().toString());