package eu.dnetlib.dhp.oa.graph.dump; import java.io.IOException; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.fasterxml.jackson.databind.ObjectMapper; import eu.dnetlib.dhp.communityapi.model.*; import eu.dnetlib.dhp.oa.graph.dump.eosc.CommunityMap; public class UtilCommunityAPI { private static final Logger log = LoggerFactory.getLogger(UtilCommunityAPI.class); public CommunityMap getCommunityMap(boolean singleCommunity, String communityId) throws IOException { if (singleCommunity) return getMap(Arrays.asList(getCommunity(communityId))); return getMap(getValidCommunities()); } private CommunityMap getMap(List communities) { final CommunityMap map = new CommunityMap(); communities.forEach(c -> map.put(c.getId(), c.getName())); return map; } private List getValidCommunities() throws IOException { ObjectMapper mapper = new ObjectMapper(); return mapper .readValue(eu.dnetlib.dhp.communityapi.QueryCommunityAPI.communities(), CommunitySummary.class) .stream() .filter( community -> (community.getStatus().equals("all") || community.getStatus().equalsIgnoreCase("public")) && (community.getType().equals("ri") || community.getType().equals("community"))) .collect(Collectors.toList()); } private CommunityModel getCommunity(String id) throws IOException { ObjectMapper mapper = new ObjectMapper(); return mapper .readValue(eu.dnetlib.dhp.communityapi.QueryCommunityAPI.community(id), CommunityModel.class); } }