BrBETA_dnet-hadoop/dhp-workflows/dhp-enrichment/src/main/java/eu/dnetlib/dhp/api/Utils.java

171 lines
5.5 KiB
Java
Raw Normal View History

2023-10-09 14:52:17 +02:00
package eu.dnetlib.dhp.api;
2023-10-09 14:52:17 +02:00
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.management.Query;
2023-10-16 11:26:07 +02:00
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.amazonaws.util.StringUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Maps;
2023-10-09 14:52:17 +02:00
import eu.dnetlib.dhp.api.model.*;
import eu.dnetlib.dhp.bulktag.community.Community;
import eu.dnetlib.dhp.bulktag.community.CommunityConfiguration;
import eu.dnetlib.dhp.bulktag.community.Provider;
import eu.dnetlib.dhp.bulktag.criteria.VerbResolver;
import eu.dnetlib.dhp.bulktag.criteria.VerbResolverFactory;
2023-10-16 11:26:07 +02:00
import eu.dnetlib.dhp.resulttocommunityfromorganization.SparkResultToCommunityFromOrganizationJob;
/**
* @author miriam.baglioni
* @Date 09/10/23
*/
public class Utils implements Serializable {
2023-10-09 14:52:17 +02:00
private static final ObjectMapper MAPPER = new ObjectMapper();
private static final VerbResolver resolver = VerbResolverFactory.newInstance();
2023-10-16 11:26:07 +02:00
private static final Logger log = LoggerFactory.getLogger(Utils.class);
2023-11-14 14:53:34 +01:00
public static CommunityConfiguration getCommunityConfiguration(String baseURL) throws IOException {
2023-10-09 14:52:17 +02:00
final Map<String, Community> communities = Maps.newHashMap();
List<Community> validCommunities = new ArrayList<>();
2023-11-14 14:53:34 +01:00
getValidCommunities(baseURL)
2023-10-09 14:52:17 +02:00
.forEach(community -> {
try {
CommunityModel cm = MAPPER
2023-11-14 14:53:34 +01:00
.readValue(QueryCommunityAPI.community(community.getId(), baseURL), CommunityModel.class);
2023-10-09 14:52:17 +02:00
validCommunities.add(getCommunity(cm));
} catch (IOException e) {
throw new RuntimeException(e);
}
});
validCommunities.forEach(community -> {
try {
DatasourceList dl = MAPPER
.readValue(
2023-11-14 14:53:34 +01:00
QueryCommunityAPI.communityDatasource(community.getId(), baseURL), DatasourceList.class);
2023-10-09 14:52:17 +02:00
community.setProviders(dl.stream().map(d -> {
if (d.getEnabled() == null || Boolean.FALSE.equals(d.getEnabled()))
return null;
2023-10-09 14:52:17 +02:00
Provider p = new Provider();
p.setOpenaireId("10|" + 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);
}
});
validCommunities.forEach(community -> {
if (community.isValid())
communities.put(community.getId(), community);
});
return new CommunityConfiguration(communities);
}
2023-10-09 14:52:17 +02:00
private static Community getCommunity(CommunityModel cm) {
Community c = new Community();
c.setId(cm.getId());
c.setZenodoCommunities(cm.getOtherZenodoCommunities());
if (!StringUtils.isNullOrEmpty(cm.getZenodoCommunity()))
c.getZenodoCommunities().add(cm.getZenodoCommunity());
c.setSubjects(cm.getSubjects());
c.getSubjects().addAll(cm.getFos());
c.getSubjects().addAll(cm.getSdg());
if (cm.getAdvancedConstraints() != null) {
c.setConstraints(cm.getAdvancedConstraints());
2023-10-09 14:52:17 +02:00
c.getConstraints().setSelection(resolver);
}
if (cm.getRemoveConstraints() != null) {
c.setRemoveConstraints(cm.getRemoveConstraints());
2023-10-09 14:52:17 +02:00
c.getRemoveConstraints().setSelection(resolver);
}
2023-10-09 14:52:17 +02:00
return c;
}
2023-11-14 14:53:34 +01:00
public static List<CommunityModel> getValidCommunities(String baseURL) throws IOException {
2023-10-09 14:52:17 +02:00
return MAPPER
2023-11-14 14:53:34 +01:00
.readValue(QueryCommunityAPI.communities(baseURL), CommunitySummary.class)
2023-10-09 14:52:17 +02:00
.stream()
.filter(
community -> !community.getStatus().equals("hidden") &&
(community.getType().equals("ri") || community.getType().equals("community")))
.collect(Collectors.toList());
}
/**
* it returns for each organization the list of associated communities
*/
2023-11-14 14:53:34 +01:00
public static CommunityEntityMap getCommunityOrganization(String baseURL) throws IOException {
2023-10-09 14:52:17 +02:00
CommunityEntityMap organizationMap = new CommunityEntityMap();
2023-11-14 14:53:34 +01:00
getValidCommunities(baseURL)
2023-10-09 14:52:17 +02:00
.forEach(community -> {
String id = community.getId();
try {
List<String> associatedOrgs = MAPPER
.readValue(
2023-11-14 14:53:34 +01:00
QueryCommunityAPI.communityPropagationOrganization(id, baseURL), OrganizationList.class);
associatedOrgs.forEach(o -> {
if (!organizationMap
.keySet()
.contains(
"20|" + o))
organizationMap.put("20|" + o, new ArrayList<>());
organizationMap.get("20|" + o).add(community.getId());
});
2023-10-09 14:52:17 +02:00
} catch (IOException e) {
throw new RuntimeException(e);
}
});
2023-10-16 11:26:07 +02:00
2023-10-09 14:52:17 +02:00
return organizationMap;
}
2023-11-14 14:53:34 +01:00
public static CommunityEntityMap getCommunityProjects(String baseURL) throws IOException {
2023-10-09 14:52:17 +02:00
CommunityEntityMap projectMap = new CommunityEntityMap();
2023-11-14 14:53:34 +01:00
getValidCommunities(baseURL)
2023-10-09 14:52:17 +02:00
.forEach(community -> {
int page = -1;
int size = 100;
ContentModel cm = new ContentModel();
do {
page++;
try {
cm = MAPPER
.readValue(
QueryCommunityAPI
.communityProjects(
2023-11-14 14:53:34 +01:00
community.getId(), String.valueOf(page), String.valueOf(size), baseURL),
2023-10-09 14:52:17 +02:00
ContentModel.class);
if (cm.getContent().size() > 0) {
cm.getContent().forEach(p -> {
if (!projectMap.keySet().contains("40|" + p.getOpenaireId()))
projectMap.put("40|" + p.getOpenaireId(), new ArrayList<>());
projectMap.get("40|" + p.getOpenaireId()).add(community.getId());
});
2023-10-09 14:52:17 +02:00
}
} catch (IOException e) {
throw new RuntimeException(e);
}
} while (!cm.getLast());
});
return projectMap;
}
}