package eu.dnetlib.dhp.community; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import eu.dnetlib.dhp.selectioncriteria.InterfaceAdapter; import eu.dnetlib.dhp.selectioncriteria.Selection; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; /** * Created by miriam on 02/08/2018. */ public class CommunityConfiguration { private static final Log log = LogFactory.getLog(CommunityConfiguration.class); private Map communities; //map subject -> communityid private transient Map>> subjectMap = new HashMap<>(); //map datasourceid -> communityid private transient Map>> datasourceMap = new HashMap<>(); //map zenodocommunityid -> communityid private transient Map>> zenodocommunityMap = new HashMap<>(); CommunityConfiguration(final Map communities) { this.communities = communities; init(); } void init() { if (subjectMap == null) { subjectMap = Maps.newHashMap(); } if (datasourceMap == null) { datasourceMap = Maps.newHashMap(); } if (zenodocommunityMap == null) { zenodocommunityMap = Maps.newHashMap(); } for(Community c : getCommunities().values()) { //get subjects final String id = c.getId(); for(String sbj : c.getSubjects()){ Pair p = new Pair<>(id,new SelectionConstraints()); add(sbj.toLowerCase().trim() , p, subjectMap); } //get datasources for(Datasource d: c.getDatasources()){ add(d.getOpenaireId(),new Pair<>(id,d.getSelectionConstraints()),datasourceMap); } //get zenodo communities for(ZenodoCommunity zc : c.getZenodoCommunities()){ add(zc.getZenodoCommunityId(),new Pair<>(id,zc.getSelCriteria()),zenodocommunityMap); } } } private void add(String key, Pair value, Map>> map){ List> values = map.get(key); if (values == null){ values = new ArrayList<>(); map.put(key,values); } values.add(value); } public List> getCommunityForSubject(String sbj){ return subjectMap.get(sbj); } public List> getCommunityForDatasource(String dts){ return datasourceMap.get(dts); } public List getCommunityForDatasource(final String dts, final Map> param) { List> lp = datasourceMap.get(dts); if (lp==null) return Lists.newArrayList(); return lp.stream().map(p -> { if (p.getSnd() == null) return p.getFst(); if (((SelectionConstraints) p.getSnd()).verifyCriteria(param)) return p.getFst(); else return null; }).filter(st->(st!=null)).collect(Collectors.toList()); } public List> getCommunityForZenodoCommunity(String zc){ return zenodocommunityMap.get(zc); } public List getCommunityForSubjectValue(String value) { return getContextIds(subjectMap.get(value)); } public List getCommunityForDatasourceValue(String value) { return getContextIds(datasourceMap.get(value.toLowerCase())); } public List getCommunityForZenodoCommunityValue(String value){ return getContextIds(zenodocommunityMap.get(value.toLowerCase())); } private List getContextIds(List> list) { if (list != null) { return list.stream().map(p -> p.getFst()).collect(Collectors.toList()); } return Lists.newArrayList(); } public Map getCommunities() { return communities; } public void setCommunities(Map communities) { this.communities = communities; } public String toJson() { GsonBuilder builder = new GsonBuilder(); builder.registerTypeAdapter(Selection.class, new InterfaceAdapter()); Gson gson = builder.create(); return gson.toJson(this); } public int size() { return communities.keySet().size(); } public Community getCommunityById(String id){ return communities.get(id); } public List getCommunityList() { return Lists.newLinkedList(communities.values()); } }