[CommunityAPI] refactoring

This commit is contained in:
Miriam Baglioni 2024-11-20 14:03:59 +01:00
parent 6beb94adee
commit 3081cad1d3
1 changed files with 136 additions and 139 deletions

View File

@ -31,195 +31,17 @@ public class Utils implements Serializable {
private static final ObjectMapper MAPPER = new ObjectMapper(); private static final ObjectMapper MAPPER = new ObjectMapper();
private static final VerbResolver resolver = VerbResolverFactory.newInstance(); private static final VerbResolver resolver = VerbResolverFactory.newInstance();
public static CommunityConfiguration getCommunityConfiguration(String baseURL) throws IOException { @FunctionalInterface
final Map<String, Community> communities = Maps.newHashMap(); private interface ProjectQueryFunction {
List<CommunityModel> communityList = getValidCommunities(baseURL); String query(int page, int size);
List<Community> validCommunities = new ArrayList<>();
communityList
.forEach(community -> {
try {
CommunityModel cm = MAPPER
.readValue(QueryCommunityAPI.community(community.getId(), baseURL), CommunityModel.class);
validCommunities.add(getCommunity(cm));
} catch (IOException e) {
throw new RuntimeException(e);
}
});
validCommunities.forEach(community -> {
try {
DatasourceList dl = MAPPER
.readValue(
QueryCommunityAPI.communityDatasource(community.getId(), baseURL), DatasourceList.class);
community.setProviders(dl.stream().map(d -> {
if (d.getEnabled() == null || Boolean.FALSE.equals(d.getEnabled()))
return null;
Provider p = new Provider();
p.setOpenaireId(ModelSupport.getIdPrefix(Datasource.class) + "|" + d.getOpenaireId());
p.setSelectionConstraints(d.getSelectioncriteria());
if (p.getSelectionConstraints() != null)
p.getSelectionConstraints().setSelection(resolver);
return p;
})
.filter(Objects::nonNull)
.collect(Collectors.toList()));
} catch (IOException e) {
throw new RuntimeException(e);
}
});
//add subcommunities information if any
communityList.forEach(community -> {
try {
List<SubCommunityModel> subcommunities = getSubcommunities(community.getId(), baseURL);
subcommunities.forEach(sc ->
validCommunities.add(getSubCommunityConfiguration(baseURL, community.getId(), sc)));
} catch (IOException e) {
throw new RuntimeException(e);
} }
}); @FunctionalInterface
private interface DatasourceQueryFunction{
validCommunities.forEach(community -> { String query();
if (community.isValid())
communities.put(community.getId(), community);
});
return new CommunityConfiguration(communities);
}
private static @NotNull Community getSubCommunityConfiguration(String baseURL, String communityId, SubCommunityModel sc) {
Community c = getCommunity(sc);
c.setProviders(getRelevantDatasources(baseURL, communityId, sc.getSubCommunityId()));
return c;
}
private static List<Provider> getRelevantDatasources(String baseURL, String communityId, String subcommunityId) {
try {
DatasourceList dl = MAPPER
.readValue(
QueryCommunityAPI.subcommunityDatasource(communityId, subcommunityId, baseURL), DatasourceList.class);
return dl.stream().map(d -> {
if (d.getEnabled() == null || Boolean.FALSE.equals(d.getEnabled()))
return null;
Provider p = new Provider();
p.setOpenaireId(ModelSupport.getIdPrefix(Datasource.class) + "|" + d.getOpenaireId());
p.setSelectionConstraints(d.getSelectioncriteria());
if (p.getSelectionConstraints() != null)
p.getSelectionConstraints().setSelection(resolver);
return p;
})
.filter(Objects::nonNull)
.collect(Collectors.toList());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private static <C extends CommonConfigurationModel> Community getCommonConfiguration(C input){
Community c = new Community();
c.setZenodoCommunities(input.getOtherZenodoCommunities());
if (StringUtils.isNotBlank(input.getZenodoCommunity()))
c.getZenodoCommunities().add(input.getZenodoCommunity());
c.setSubjects(input.getSubjects());
c.getSubjects().addAll(input.getFos());
c.getSubjects().addAll(input.getSdg());
if (input.getAdvancedConstraints() != null) {
c.setConstraints(input.getAdvancedConstraints());
c.getConstraints().setSelection(resolver);
}
if (input.getRemoveConstraints() != null) {
c.setRemoveConstraints(input.getRemoveConstraints());
c.getRemoveConstraints().setSelection(resolver);
}
return c;
}
private static Community getCommunity(SubCommunityModel sc) {
Community c = getCommonConfiguration(sc);
c.setId(sc.getSubCommunityId());
return c;
}
private static Community getCommunity(CommunityModel cm) {
Community c = getCommonConfiguration(cm);
c.setId(cm.getId());
return c;
}
public static List<CommunityModel> getValidCommunities(String baseURL) throws IOException {
return MAPPER
.readValue(QueryCommunityAPI.communities(baseURL), CommunitySummary.class)
.stream()
.filter(
community -> !community.getStatus().equals("hidden") &&
(community.getType().equals("ri") || community.getType().equals("community")))
.collect(Collectors.toList());
}
public static List<SubCommunityModel> getSubcommunities(String communityId, String baseURL) throws IOException {
return MAPPER.readValue(QueryCommunityAPI.subcommunities(communityId, baseURL), SubCommunitySummary.class);
}
private static void getRelatedOrganizations(String communityId, String baseURL, CommunityEntityMap communityEntityMap){
try {
List<String> 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<String> 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();
List<CommunityModel> communityList = getValidCommunities(baseURL);
communityList.forEach(community -> {
getRelatedOrganizations(community.getId(), baseURL, organizationMap );
try {
List<SubCommunityModel> subcommunities = getSubcommunities(community.getId(), baseURL);
subcommunities.forEach(sc -> getRelatedOrganizations(community.getId(), sc.getSubCommunityId(), baseURL, organizationMap));
} catch (IOException e) {
throw new RuntimeException(e);
}
});
return organizationMap;
} }
//PROJECT METHODS
public static CommunityEntityMap getCommunityProjects(String baseURL) throws IOException { public static CommunityEntityMap getCommunityProjects(String baseURL) throws IOException {
CommunityEntityMap projectMap = new CommunityEntityMap(); CommunityEntityMap projectMap = new CommunityEntityMap();
@ -273,10 +95,6 @@ public class Utils implements Serializable {
); );
} }
@FunctionalInterface
private interface ProjectQueryFunction {
String query(int page, int size);
}
private static void fetchAndProcessProjects( private static void fetchAndProcessProjects(
ProjectQueryFunction projectQueryFunction, ProjectQueryFunction projectQueryFunction,
String communityId, String communityId,
@ -307,10 +125,195 @@ public class Utils implements Serializable {
page++; page++;
} while (!contentModel.getLast()); } while (!contentModel.getLast());
} }
private static List<Provider> addRelevantDatasources(
DatasourceQueryFunction datasourceQueryFunction
) {
try {
String response = datasourceQueryFunction.query();
DatasourceList datasourceList = MAPPER.readValue(response, DatasourceList.class);
return datasourceList.stream().map(d -> {
if (d.getEnabled() == null || Boolean.FALSE.equals(d.getEnabled()))
return null;
Provider p = new Provider();
p.setOpenaireId(ModelSupport.getIdPrefix(Datasource.class) + "|" + d.getOpenaireId());
p.setSelectionConstraints(d.getSelectioncriteria());
if (p.getSelectionConstraints() != null)
p.getSelectionConstraints().setSelection(resolver);
return p;
})
.filter(Objects::nonNull)
.collect(Collectors.toList());
} catch (IOException e) {
throw new RuntimeException("Error processing datasource information: " + e);
}
}
public static CommunityConfiguration getCommunityConfiguration(String baseURL) throws IOException {
final Map<String, Community> communities = Maps.newHashMap();
List<CommunityModel> communityList = getValidCommunities(baseURL);
List<Community> validCommunities = new ArrayList<>();
communityList
.forEach(community -> {
try {
CommunityModel cm = MAPPER
.readValue(QueryCommunityAPI.community(community.getId(), baseURL), CommunityModel.class);
validCommunities.add(getCommunity(cm));
} catch (IOException e) {
throw new RuntimeException(e);
}
});
validCommunities.forEach(community->community.setProviders(addRelevantDatasources(()->{
try {
return QueryCommunityAPI.communityDatasource(community.getId(),baseURL);
} catch (IOException e) {
throw new RuntimeException(e);
}
})));
//add subcommunities information if any
communityList.forEach(community -> {
try {
List<SubCommunityModel> subcommunities = getSubcommunities(community.getId(), baseURL);
subcommunities.forEach(sc ->
validCommunities.add(getSubCommunityConfiguration(baseURL, community.getId(), sc)));
} catch (IOException e) {
throw new RuntimeException(e);
}
});
validCommunities.forEach(community -> {
if (community.isValid())
communities.put(community.getId(), community);
});
return new CommunityConfiguration(communities);
}
private static @NotNull Community getSubCommunityConfiguration(String baseURL, String communityId, SubCommunityModel sc) {
Community c = getCommunity(sc);
c.setProviders(addRelevantDatasources(()->{
try {
return QueryCommunityAPI.subcommunityDatasource(communityId, sc.getSubCommunityId(), baseURL);
} catch (IOException e) {
throw new RuntimeException(e);
}
}));
return c;
}
private static <C extends CommonConfigurationModel> Community getCommonConfiguration(C input){
Community c = new Community();
c.setZenodoCommunities(input.getOtherZenodoCommunities());
if (StringUtils.isNotBlank(input.getZenodoCommunity()))
c.getZenodoCommunities().add(input.getZenodoCommunity());
c.setSubjects(input.getSubjects());
c.getSubjects().addAll(input.getFos());
c.getSubjects().addAll(input.getSdg());
if (input.getAdvancedConstraints() != null) {
c.setConstraints(input.getAdvancedConstraints());
c.getConstraints().setSelection(resolver);
}
if (input.getRemoveConstraints() != null) {
c.setRemoveConstraints(input.getRemoveConstraints());
c.getRemoveConstraints().setSelection(resolver);
}
return c;
}
private static Community getCommunity(SubCommunityModel sc) {
Community c = getCommonConfiguration(sc);
c.setId(sc.getSubCommunityId());
return c;
}
private static Community getCommunity(CommunityModel cm) {
Community c = getCommonConfiguration(cm);
c.setId(cm.getId());
return c;
}
public static List<CommunityModel> getValidCommunities(String baseURL) throws IOException {
return MAPPER
.readValue(QueryCommunityAPI.communities(baseURL), CommunitySummary.class)
.stream()
.filter(
community -> !community.getStatus().equals("hidden") &&
(community.getType().equals("ri") || community.getType().equals("community")))
.collect(Collectors.toList());
}
public static List<SubCommunityModel> getSubcommunities(String communityId, String baseURL) throws IOException {
return MAPPER.readValue(QueryCommunityAPI.subcommunities(communityId, baseURL), SubCommunitySummary.class);
}
private static void getRelatedOrganizations(String communityId, String baseURL, CommunityEntityMap communityEntityMap){
try {
List<String> 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<String> 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();
List<CommunityModel> communityList = getValidCommunities(baseURL);
communityList.forEach(community -> {
getRelatedOrganizations(community.getId(), baseURL, organizationMap );
try {
List<SubCommunityModel> 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 List<String> getCommunityIdList(String baseURL) throws IOException { public static List<String> getCommunityIdList(String baseURL) throws IOException {
return getValidCommunities(baseURL) return getValidCommunities(baseURL)
.stream() .stream()
.map(community -> community.getId()) .map(CommunityModel::getId)
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
@ -324,7 +327,7 @@ public class Utils implements Serializable {
new ObjectMapper() new ObjectMapper()
.readValue(QueryCommunityAPI.communityDatasource(c.getId(), baseURL), DatasourceList.class) .readValue(QueryCommunityAPI.communityDatasource(c.getId(), baseURL), DatasourceList.class)
.forEach(d -> { .forEach(d -> {
if (!map.keySet().contains(d.getOpenaireId())) if (!map.containsKey(d.getOpenaireId()))
map.put(d.getOpenaireId(), new HashSet<>()); map.put(d.getOpenaireId(), new HashSet<>());
map.get(d.getOpenaireId()).add(c.getId()); map.get(d.getOpenaireId()).add(c.getId());
@ -334,20 +337,14 @@ public class Utils implements Serializable {
} }
}); });
List<EntityCommunities> temp = map return map
.keySet() .keySet()
.stream() .stream()
.map(k -> EntityCommunities.newInstance(entityPrefix + k, getCollect(k, map))) .map(k -> EntityCommunities.newInstance(entityPrefix + k, new ArrayList<>(map.get(k))))
.collect(Collectors.toList()); .collect(Collectors.toList());
return temp;
} }
@NotNull
private static List<String> getCollect(String k, HashMap<String, Set<String>> map) {
List<String> temp = map.get(k).stream().collect(Collectors.toList());
return temp;
}
} }