Project

General

Profile

1
package eu.dnetlib.data.bulktag;
2

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

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

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

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

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

    
28
    private Map<String,Community> communities;
29

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

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

    
42
    public void init() {
43

    
44
        if (subjectMap == null) {
45
            subjectMap = Maps.newHashMap();
46
        }
47
        if (datasourceMap == null) {
48
            datasourceMap = Maps.newHashMap();
49
        }
50
        if (zenodocommunityMap == null) {
51
            zenodocommunityMap = Maps.newHashMap();
52
        }
53

    
54
        for(Community c : getCommunities().values()) {
55
            //get subjects
56
            final String id = c.getId();
57
            for(String sbj : c.getSubjects()){
58
                Pair<String,SelectionCriteria> p = new Pair<>(id,new SelectionCriteria(null));
59
                add(sbj.toLowerCase().trim() , p, subjectMap);
60
            }
61
            //get datasources
62
            for(Datasource d: c.getDatasources()){
63
                add(d.getOpenaireId(),new Pair<>(id,d.getSelCriteria()),datasourceMap);
64
            }
65
            //get zenodo communities
66
            for(ZenodoCommunity zc : c.getZenodoCommunities()){
67
                add(zc.getZenodoCommunityId(),new Pair<>(id,zc.getSelCriteria()),zenodocommunityMap);
68
            }
69
        }
70
    }
71

    
72
    private void add(String key,Pair<String,SelectionCriteria> value, Map<String,List<Pair<String,SelectionCriteria>>> map){
73
        List<Pair<String,SelectionCriteria>> values = map.get(key);
74

    
75
        if (values == null){
76
            values = new ArrayList<>();
77
            map.put(key,values);
78
        }
79
        values.add(value);
80
    }
81

    
82
    public List<Pair<String,SelectionCriteria>> getCommunityForSubject(String sbj){
83
        return subjectMap.get(sbj);
84
    }
85

    
86
    public List<Pair<String,SelectionCriteria>> getCommunityForDatasource(String dts){
87
        return datasourceMap.get(dts);
88
    }
89

    
90
    public List<Pair<String,SelectionCriteria>> getCommunityForZenodoCommunity(String zc){
91
        return zenodocommunityMap.get(zc);
92
    }
93

    
94
    public List<String> getCommunityForSubjectValue(String value) {
95

    
96
        return getContextIds(subjectMap.get(value));
97
    }
98

    
99
    public List<String> getCommunityForDatasourceValue(String value) {
100

    
101
        return getContextIds(datasourceMap.get(value.toLowerCase()));
102
    }
103

    
104
    public List<String> getCommunityForZenodoCommunityValue(String value){
105

    
106
        return getContextIds(zenodocommunityMap.get(value.toLowerCase()));
107
    }
108

    
109
    private List<String> getContextIds(List<Pair<String, SelectionCriteria>> list) {
110
        if (list != null) {
111
            return list.stream().map(p -> p.getFst()).collect(Collectors.toList());
112
        }
113
        return Lists.newArrayList();
114
    }
115

    
116
    /*
117
    public SelectionCriteria getSelCriteria(String value, String community, MapModes map_mode){
118

    
119
        Map<String,List<Pair<String,SelectionCriteria>>> map = null;
120
        if(map_mode == MapModes.DATASOURCE_MAP)
121
            map = datasourceMap;
122
        else
123
        if(map_mode == MapModes.ZENODO_COMMUNITY_MAP)
124
            map = zenodocommunityMap;
125
        else
126
            new Throwable("Impossible to have Selection Criteria over subjects");
127

    
128
        List<Pair<String, SelectionCriteria>> lst = map.get(value);
129
        List<SelectionCriteria> selectionList = lst.stream().map(p -> {
130
            if (p.fst == community)
131
                return p.snd;
132
            return null;
133
        }).collect(Collectors.toList());//for each community there will be only one Selection Criteria per datasource or zenodo community
134
        if(selectionList != null)
135
            if (selectionList.size()>0)
136
                return selectionList.get(0);
137
        return null;
138
    }
139
    */
140

    
141
    public Map<String, Community> getCommunities() {
142
        return communities;
143
    }
144

    
145
    public void setCommunities(Map<String, Community> communities) {
146
        this.communities = communities;
147
    }
148

    
149
    public String toJson() {
150
        final Gson g = new Gson();
151
        return g.toJson(this);
152
    }
153

    
154
    public int size() {
155
        return communities.keySet().size();
156
    }
157

    
158
    public Community getCommunityById(String id){
159
        return communities.get(id);
160
    }
161

    
162
    public List<Community> getCommunityList() {
163
        return Lists.newLinkedList(communities.values());
164
    }
165
}
(2-2/7)