Project

General

Profile

1
package eu.dnetlib.data.bulktag;
2

    
3
import com.google.common.collect.Lists;
4
import com.google.gson.Gson;
5
import org.apache.commons.logging.Log;
6
import org.apache.commons.logging.LogFactory;
7

    
8
import java.util.ArrayList;
9
import java.util.HashMap;
10
import java.util.List;
11
import java.util.Map;
12
import java.util.stream.Collectors;
13

    
14
/**
15
 * Created by miriam on 02/08/2018.
16
 */
17
public class CommunityConfiguration {
18

    
19
    private static final Log log = LogFactory.getLog(CommunityConfiguration.class);
20

    
21
    enum MapModes{
22
        SUBJECT_MAP,
23
        DATASOURCE_MAP,
24
        ZENODO_COMMUNITY_MAP
25
    }
26

    
27
    private Map<String,Community> communities;
28

    
29
    //map subject -> communityid
30
    private transient final Map<String,List<Pair<String,SelectionCriteria>>> subjectMap = new HashMap<>();
31
    //map datasourceid -> communityid
32
    private transient final Map<String,List<Pair<String,SelectionCriteria>>> datasourceMap = new HashMap<>();
33
    //map zenodocommunityid -> communityid
34
    private transient final Map<String,List<Pair<String,SelectionCriteria>>> zenodocommunityMap = new HashMap<>();
35

    
36
    public CommunityConfiguration(final Map<String, Community> communities) {
37
        this.communities = communities;
38
        init();
39
    }
40

    
41
    private void init() {
42
        for(Community c: getCommunityList()){
43
            //get subjects
44
            final String id = c.getId();
45
            for(String sbj : c.getSubjects()){
46
                Pair<String,SelectionCriteria> p = new Pair<>(id,new SelectionCriteria(null));
47
                add(sbj.toLowerCase(),p,subjectMap);
48
            }
49
            //get datasources
50
            for(Datasource d: c.getDatasources()){
51
                add(d.getOpenaireId(),new Pair<>(id,d.getSelCriteria()),datasourceMap);
52
            }
53
            //get zenodo communities
54
            for(ZenodoCommunity zc : c.getZenodoCommunities()){
55
                add(zc.getZenodoCommunityId(),new Pair<>(id,zc.getSelCriteria()),zenodocommunityMap);
56
            }
57
        }
58
    }
59

    
60
    private void add(String key,Pair<String,SelectionCriteria> value, Map<String,List<Pair<String,SelectionCriteria>>> map){
61
        List<Pair<String,SelectionCriteria>> values = map.get(key);
62

    
63
        if (values == null){
64
            values = new ArrayList<>();
65
            map.put(key,values);
66
        }
67
        values.add(value);
68
    }
69

    
70
    public List<Pair<String,SelectionCriteria>> getCommunityForSubject(String sbj){
71
        return subjectMap.get(sbj);
72
    }
73

    
74
    public List<Pair<String,SelectionCriteria>> getCommunityForDatasource(String dts){
75
        return datasourceMap.get(dts);
76
    }
77

    
78
    public List<Pair<String,SelectionCriteria>> getCommunityForZenodoCommunity(String zc){
79
        return zenodocommunityMap.get(zc);
80
    }
81

    
82
    public List<String> getCommunityForSubjectValue(String value) {
83
        try {
84
            return subjectMap.get(value.toLowerCase()).stream().map(p -> p.getFst()).collect(Collectors.toList());
85
        }catch(Exception e){
86
            return new ArrayList<>();
87
        }
88
    }
89

    
90
    public List<String> getCommunityForDatasourceValue(String value) {
91
        try {
92
            return datasourceMap.get(value.toLowerCase()).stream().map(p -> p.getFst()).collect(Collectors.toList());
93
        }catch(Exception e){
94
            return new ArrayList<>();
95
        }
96
    }
97

    
98
    public List<String> getCommunityForZenodoCommunityValue(String value){
99
        try {
100
            return zenodocommunityMap.get(value.toLowerCase()).stream().map(p -> p.getFst()).collect(Collectors.toList());
101
        }catch(Exception e){
102
            return new ArrayList<>();
103

    
104
        }
105
    }
106

    
107
    /*
108
    public SelectionCriteria getSelCriteria(String value, String community, MapModes map_mode){
109

    
110
        Map<String,List<Pair<String,SelectionCriteria>>> map = null;
111
        if(map_mode == MapModes.DATASOURCE_MAP)
112
            map = datasourceMap;
113
        else
114
        if(map_mode == MapModes.ZENODO_COMMUNITY_MAP)
115
            map = zenodocommunityMap;
116
        else
117
            new Throwable("Impossible to have Selection Criteria over subjects");
118

    
119
        List<Pair<String, SelectionCriteria>> lst = map.get(value);
120
        List<SelectionCriteria> selectionList = lst.stream().map(p -> {
121
            if (p.fst == community)
122
                return p.snd;
123
            return null;
124
        }).collect(Collectors.toList());//for each community there will be only one Selection Criteria per datasource or zenodo community
125
        if(selectionList != null)
126
            if (selectionList.size()>0)
127
                return selectionList.get(0);
128
        return null;
129
    }
130
    */
131

    
132
    public Map<String, Community> getCommunities() {
133
        return communities;
134
    }
135

    
136
    public void setCommunities(Map<String, Community> communities) {
137
        this.communities = communities;
138
    }
139

    
140
    public String toJson() {
141
        final Gson g = new Gson();
142
        return g.toJson(this);
143
    }
144

    
145
    public int size() {
146
        return communities.keySet().size();
147
    }
148

    
149
    public Community getCommunityById(String id){
150
        return communities.get(id);
151
    }
152

    
153
    public List<Community> getCommunityList() {
154
        return Lists.newLinkedList(communities.values());
155
    }
156
}
(2-2/7)