1
|
package eu.dnetlib.data.bulktag;
|
2
|
|
3
|
import com.google.common.collect.Lists;
|
4
|
import com.google.gson.Gson;
|
5
|
import com.sun.tools.javac.util.Pair;
|
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 final Map<String,List<Pair<String,SelectionCriteria>>> subjectMap = new HashMap<>();
|
32
|
//map datasourceid -> communityid
|
33
|
private transient final Map<String,List<Pair<String,SelectionCriteria>>> datasourceMap = new HashMap<>();
|
34
|
//map zenodocommunityid -> communityid
|
35
|
private transient final 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
|
private void init() {
|
43
|
for(Community c: getCommunityList()){
|
44
|
//get subjects
|
45
|
final String id = c.getId();
|
46
|
for(String sbj : c.getSubjects()){
|
47
|
Pair<String,SelectionCriteria> p = new Pair<>(id,new SelectionCriteria(null));
|
48
|
add(sbj.toLowerCase(),p,subjectMap);
|
49
|
}
|
50
|
//get datasources
|
51
|
for(Datasource d: c.getDatasources()){
|
52
|
add(d.getOpenaireId(),new Pair<>(id,d.getSelCriteria()),datasourceMap);
|
53
|
}
|
54
|
//get zenodo communities
|
55
|
for(ZenodoCommunity zc : c.getZenodoCommunities()){
|
56
|
add(zc.getZenodoCommunityId(),new Pair<>(id,zc.getSelCriteria()),zenodocommunityMap);
|
57
|
}
|
58
|
}
|
59
|
}
|
60
|
|
61
|
private void add(String key,Pair<String,SelectionCriteria> value, Map<String,List<Pair<String,SelectionCriteria>>> map){
|
62
|
List<Pair<String,SelectionCriteria>> values = map.get(key);
|
63
|
|
64
|
if (values == null){
|
65
|
values = new ArrayList<>();
|
66
|
map.put(key,values);
|
67
|
}
|
68
|
values.add(value);
|
69
|
}
|
70
|
|
71
|
public List<Pair<String,SelectionCriteria>> getCommunityForSubject(String sbj){
|
72
|
return subjectMap.get(sbj);
|
73
|
}
|
74
|
|
75
|
public List<Pair<String,SelectionCriteria>> getCommunityForDatasource(String dts){
|
76
|
return datasourceMap.get(dts);
|
77
|
}
|
78
|
|
79
|
public List<Pair<String,SelectionCriteria>> getCommunityForZenodoCommunity(String zc){
|
80
|
return zenodocommunityMap.get(zc);
|
81
|
}
|
82
|
|
83
|
public List<String> getCommunityForSubjectValue(String value) {
|
84
|
try {
|
85
|
return subjectMap.get(value.toLowerCase()).stream().map(p -> p.fst).collect(Collectors.toList());
|
86
|
}catch(Exception e){
|
87
|
return new ArrayList<>();
|
88
|
}
|
89
|
}
|
90
|
|
91
|
public List<String> getCommunityForDatasourceValue(String value) {
|
92
|
try {
|
93
|
return datasourceMap.get(value.toLowerCase()).stream().map(p -> p.fst).collect(Collectors.toList());
|
94
|
}catch(Exception e){
|
95
|
return new ArrayList<>();
|
96
|
}
|
97
|
}
|
98
|
|
99
|
public List<String> getCommunityForZenodoCommunityValue(String value){
|
100
|
try {
|
101
|
return zenodocommunityMap.get(value.toLowerCase()).stream().map(p -> p.fst).collect(Collectors.toList());
|
102
|
}catch(Exception e){
|
103
|
return new ArrayList<>();
|
104
|
|
105
|
}
|
106
|
}
|
107
|
|
108
|
/*
|
109
|
public SelectionCriteria getSelCriteria(String value, String community, MapModes map_mode){
|
110
|
|
111
|
Map<String,List<Pair<String,SelectionCriteria>>> map = null;
|
112
|
if(map_mode == MapModes.DATASOURCE_MAP)
|
113
|
map = datasourceMap;
|
114
|
else
|
115
|
if(map_mode == MapModes.ZENODO_COMMUNITY_MAP)
|
116
|
map = zenodocommunityMap;
|
117
|
else
|
118
|
new Throwable("Impossible to have Selection Criteria over subjects");
|
119
|
|
120
|
List<Pair<String, SelectionCriteria>> lst = map.get(value);
|
121
|
List<SelectionCriteria> selectionList = lst.stream().map(p -> {
|
122
|
if (p.fst == community)
|
123
|
return p.snd;
|
124
|
return null;
|
125
|
}).collect(Collectors.toList());//for each community there will be only one Selection Criteria per datasource or zenodo community
|
126
|
if(selectionList != null)
|
127
|
if (selectionList.size()>0)
|
128
|
return selectionList.get(0);
|
129
|
return null;
|
130
|
}
|
131
|
*/
|
132
|
|
133
|
public Map<String, Community> getCommunities() {
|
134
|
return communities;
|
135
|
}
|
136
|
|
137
|
public void setCommunities(Map<String, Community> communities) {
|
138
|
this.communities = communities;
|
139
|
}
|
140
|
|
141
|
public String toJson() {
|
142
|
final Gson g = new Gson();
|
143
|
return g.toJson(this);
|
144
|
}
|
145
|
|
146
|
public int size() {
|
147
|
return communities.keySet().size();
|
148
|
}
|
149
|
|
150
|
public Community getCommunityById(String id){
|
151
|
return communities.get(id);
|
152
|
}
|
153
|
|
154
|
public List<Community> getCommunityList() {
|
155
|
return Lists.newLinkedList(communities.values());
|
156
|
}
|
157
|
}
|