From 9dbcf19efbd44ebe9681bb5474c8cd3e77ffe503 Mon Sep 17 00:00:00 2001 From: Miriam Baglioni Date: Wed, 20 Nov 2024 09:16:33 +0100 Subject: [PATCH] [SubCommunity] Extention of communityApis to add also the associations between the subcommunities and organization/project/datasources --- .../eu/dnetlib/dhp/api/QueryCommunityAPI.java | 8 + .../main/java/eu/dnetlib/dhp/api/Utils.java | 140 ++++++++++++------ 2 files changed, 104 insertions(+), 44 deletions(-) diff --git a/dhp-workflows/dhp-enrichment/src/main/java/eu/dnetlib/dhp/api/QueryCommunityAPI.java b/dhp-workflows/dhp-enrichment/src/main/java/eu/dnetlib/dhp/api/QueryCommunityAPI.java index d10be5365..4926587d7 100644 --- a/dhp-workflows/dhp-enrichment/src/main/java/eu/dnetlib/dhp/api/QueryCommunityAPI.java +++ b/dhp-workflows/dhp-enrichment/src/main/java/eu/dnetlib/dhp/api/QueryCommunityAPI.java @@ -88,4 +88,12 @@ public class QueryCommunityAPI { public static String subcommunityDatasource(String communityId, String subcommunityId, String baseURL) throws IOException { return get(baseURL + communityId + "/subcommunities/datasources?subCommunityId=" + subcommunityId); } + + public static String subcommunityPropagationOrganization(String communityId, String subcommunityId , String baseURL) throws IOException { + return get(baseURL + communityId + "/subcommunities/propagationOrganizations?subCommunityId=" + subcommunityId); + } + + public static String subcommunityProjects(String communityId, String subcommunityId, String page, String size, String baseURL) throws IOException { + return get(baseURL + communityId + "/subcommunities/projects/" + page + "/" + size + "?subCommunityId=" + subcommunityId); + } } diff --git a/dhp-workflows/dhp-enrichment/src/main/java/eu/dnetlib/dhp/api/Utils.java b/dhp-workflows/dhp-enrichment/src/main/java/eu/dnetlib/dhp/api/Utils.java index 97fafe5f0..6183ae537 100644 --- a/dhp-workflows/dhp-enrichment/src/main/java/eu/dnetlib/dhp/api/Utils.java +++ b/dhp-workflows/dhp-enrichment/src/main/java/eu/dnetlib/dhp/api/Utils.java @@ -170,67 +170,119 @@ public class Utils implements Serializable { return MAPPER.readValue(QueryCommunityAPI.subcommunities(communityId, baseURL), SubCommunitySummary.class); } + private static void getRelatedOrganizations(String communityId, String baseURL, CommunityEntityMap communityEntityMap){ + + try { + List associatedOrgs = MAPPER + .readValue( + QueryCommunityAPI.communityPropagationOrganization(communityId, baseURL), OrganizationList.class); + associatedOrgs.forEach(o -> updateEntityMap(communityId, o, communityEntityMap, ModelSupport.getIdPrefix(Organization.class))); + } catch (IOException e) { + throw new RuntimeException(e); + } + + } + private static void getRelatedOrganizations(String communityId, String subcommunityId, String baseURL, CommunityEntityMap communityEntityMap){ + + try { + List associatedOrgs = MAPPER + .readValue( + QueryCommunityAPI.subcommunityPropagationOrganization(communityId, subcommunityId, baseURL), OrganizationList.class); + associatedOrgs.forEach(o -> updateEntityMap(communityId, o, communityEntityMap, ModelSupport.getIdPrefix(Organization.class))); + } catch (IOException e) { + throw new RuntimeException(e); + } + + } + + private static void updateEntityMap(String communityId, String entityId, CommunityEntityMap communityEntityMap, String entityPrefix){ + + if (!communityEntityMap + .containsKey(entityPrefix + "|" + entityId)) + communityEntityMap.put(entityPrefix + "|" + entityId, new ArrayList<>()); + + communityEntityMap.get(entityPrefix + "|" + entityId).add(communityId); + + } /** * it returns for each organization the list of associated communities */ public static CommunityEntityMap getCommunityOrganization(String baseURL) throws IOException { CommunityEntityMap organizationMap = new CommunityEntityMap(); - String entityPrefix = ModelSupport.getIdPrefix(Organization.class); - getValidCommunities(baseURL) - .forEach(community -> { - String id = community.getId(); - try { - List associatedOrgs = MAPPER - .readValue( - QueryCommunityAPI.communityPropagationOrganization(id, baseURL), OrganizationList.class); - associatedOrgs.forEach(o -> { - if (!organizationMap - .keySet() - .contains( - entityPrefix + "|" + o)) - organizationMap.put(entityPrefix + "|" + o, new ArrayList<>()); - organizationMap.get(entityPrefix + "|" + o).add(community.getId()); - }); - } catch (IOException e) { - throw new RuntimeException(e); - } - }); + List communityList = getValidCommunities(baseURL); + communityList.forEach(community -> { + getRelatedOrganizations(community.getId(), baseURL, organizationMap ); + try { + List subcommunities = getSubcommunities(community.getId(), baseURL); + subcommunities.forEach(sc -> getRelatedOrganizations(community.getId(), sc.getSubCommunityId(), baseURL, organizationMap)); + } catch (IOException e) { + throw new RuntimeException(e); + } + }); return organizationMap; } public static CommunityEntityMap getCommunityProjects(String baseURL) throws IOException { CommunityEntityMap projectMap = new CommunityEntityMap(); - String entityPrefix = ModelSupport.getIdPrefix(Project.class); + getValidCommunities(baseURL) .forEach(community -> { - int page = -1; - int size = 100; - ContentModel cm = new ContentModel(); - do { - page++; - try { - cm = MAPPER - .readValue( - QueryCommunityAPI - .communityProjects( - community.getId(), String.valueOf(page), String.valueOf(size), baseURL), - ContentModel.class); - if (cm.getContent().size() > 0) { - cm.getContent().forEach(p -> { - if (!projectMap.keySet().contains(entityPrefix + "|" + p.getOpenaireId())) - projectMap.put(entityPrefix + "|" + p.getOpenaireId(), new ArrayList<>()); - projectMap.get(entityPrefix + "|" + p.getOpenaireId()).add(community.getId()); - }); - } - } catch (IOException e) { - throw new RuntimeException(e); - } - } while (!cm.getLast()); + addRelevantProjects(community.getId(), baseURL, projectMap); + try { + List subcommunities = getSubcommunities(community.getId(), baseURL); + subcommunities.forEach(sc -> addRelevantProjects(community.getId(), sc.getSubCommunityId(), baseURL, projectMap)); + } catch (IOException e) { + throw new RuntimeException(e); + } }); return projectMap; } + private static void addRelevantProjects(String communityId, String baseURL, CommunityEntityMap communityEntityMap){ + int page = -1; + int size = 100; + ContentModel cm = new ContentModel(); + do { + page++; + try { + cm = MAPPER + .readValue( + QueryCommunityAPI + .communityProjects( + communityId, String.valueOf(page), String.valueOf(size), baseURL), + ContentModel.class); + if (!cm.getContent().isEmpty()) { + cm.getContent().forEach(p -> updateEntityMap(communityId, p.getOpenaireId(),communityEntityMap, ModelSupport.getIdPrefix(Project.class))); + } + } catch (IOException e) { + throw new RuntimeException(e); + } + } while (!cm.getLast()); + } + + private static void addRelevantProjects(String communityId, String subcommunityId, String baseURL, CommunityEntityMap communityEntityMap){ + int page = -1; + int size = 100; + ContentModel cm = new ContentModel(); + do { + page++; + try { + cm = MAPPER + .readValue( + QueryCommunityAPI + .subcommunityProjects( + communityId, subcommunityId , String.valueOf(page), String.valueOf(size), baseURL), + ContentModel.class); + if (!cm.getContent().isEmpty()) { + cm.getContent().forEach(p -> updateEntityMap(communityId, p.getOpenaireId(),communityEntityMap, ModelSupport.getIdPrefix(Project.class))); + } + } catch (IOException e) { + throw new RuntimeException(e); + } + } while (!cm.getLast()); + } + public static List getCommunityIdList(String baseURL) throws IOException { return getValidCommunities(baseURL) .stream()