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
|
|
7
|
import eu.dnetlib.data.bulktag.selectioncriteria.VerbResolver;
|
8
|
import org.apache.commons.logging.Log;
|
9
|
import org.apache.commons.logging.LogFactory;
|
10
|
|
11
|
|
12
|
import java.util.ArrayList;
|
13
|
import java.util.HashMap;
|
14
|
import java.util.List;
|
15
|
import java.util.Map;
|
16
|
import java.util.stream.Collectors;
|
17
|
import java.util.stream.Stream;
|
18
|
|
19
|
/**
|
20
|
* Created by miriam on 02/08/2018.
|
21
|
*/
|
22
|
public class CommunityConfiguration {
|
23
|
|
24
|
private static final Log log = LogFactory.getLog(CommunityConfiguration.class);
|
25
|
|
26
|
|
27
|
enum MapModes{
|
28
|
SUBJECT_MAP,
|
29
|
DATASOURCE_MAP,
|
30
|
ZENODO_COMMUNITY_MAP
|
31
|
}
|
32
|
|
33
|
private Map<String,Community> communities;
|
34
|
|
35
|
|
36
|
//map subject -> communityid
|
37
|
private transient Map<String,List<Pair<String,SelectionConstraints>>> subjectMap = new HashMap<>();
|
38
|
//map datasourceid -> communityid
|
39
|
private transient Map<String,List<Pair<String,SelectionConstraints>>> datasourceMap = new HashMap<>();
|
40
|
//map zenodocommunityid -> communityid
|
41
|
private transient Map<String,List<Pair<String,SelectionConstraints>>> zenodocommunityMap = new HashMap<>();
|
42
|
|
43
|
CommunityConfiguration(final Map<String, Community> communities) {
|
44
|
this.communities = communities;
|
45
|
init();
|
46
|
}
|
47
|
|
48
|
void init() {
|
49
|
|
50
|
if (subjectMap == null) {
|
51
|
subjectMap = Maps.newHashMap();
|
52
|
}
|
53
|
if (datasourceMap == null) {
|
54
|
datasourceMap = Maps.newHashMap();
|
55
|
}
|
56
|
if (zenodocommunityMap == null) {
|
57
|
zenodocommunityMap = Maps.newHashMap();
|
58
|
}
|
59
|
|
60
|
for(Community c : getCommunities().values()) {
|
61
|
//get subjects
|
62
|
final String id = c.getId();
|
63
|
for(String sbj : c.getSubjects()){
|
64
|
Pair<String,SelectionConstraints> p = new Pair<>(id,new SelectionConstraints());
|
65
|
add(sbj.toLowerCase().trim() , p, subjectMap);
|
66
|
}
|
67
|
//get datasources
|
68
|
for(Datasource d: c.getDatasources()){
|
69
|
|
70
|
add(d.getOpenaireId(),new Pair<>(id,d.getSelCriteria()),datasourceMap);
|
71
|
}
|
72
|
//get zenodo communities
|
73
|
for(ZenodoCommunity zc : c.getZenodoCommunities()){
|
74
|
add(zc.getZenodoCommunityId(),new Pair<>(id,zc.getSelCriteria()),zenodocommunityMap);
|
75
|
}
|
76
|
|
77
|
}
|
78
|
}
|
79
|
|
80
|
private void add(String key, Pair<String,SelectionConstraints> value, Map<String,List<Pair<String,SelectionConstraints>>> map){
|
81
|
List<Pair<String,SelectionConstraints>> values = map.get(key);
|
82
|
|
83
|
if (values == null){
|
84
|
values = new ArrayList<>();
|
85
|
map.put(key,values);
|
86
|
}
|
87
|
values.add(value);
|
88
|
}
|
89
|
|
90
|
public List<Pair<String,SelectionConstraints>> getCommunityForSubject(String sbj){
|
91
|
return subjectMap.get(sbj);
|
92
|
}
|
93
|
|
94
|
public List<Pair<String,SelectionConstraints>> getCommunityForDatasource(String dts){
|
95
|
return datasourceMap.get(dts);
|
96
|
}
|
97
|
|
98
|
// public List<String> getCommunityForDatasource(String dts, Map<String,String>param){
|
99
|
// List<Pair<String,SelectionConstraints>> lp = datasourceMap.get(dts);
|
100
|
// if (lp==null)
|
101
|
// return Lists.newArrayList();
|
102
|
// return lp.stream().map(p -> {
|
103
|
// if (p.getSnd() == null)
|
104
|
// return p.getFst();
|
105
|
// if (((SelectionConstraints) p.getSnd()).verifyCriteria(param))
|
106
|
// return p.getFst();
|
107
|
// else
|
108
|
// return null;
|
109
|
// }).collect(Collectors.toList());
|
110
|
//
|
111
|
// }
|
112
|
|
113
|
public List<String> getCommunityForDatasource(String dts, Map<String,List<String>>param){
|
114
|
List<Pair<String,SelectionConstraints>> lp = datasourceMap.get(dts);
|
115
|
if (lp==null)
|
116
|
return Lists.newArrayList();
|
117
|
return lp.stream().map(p -> {
|
118
|
if (p.getSnd() == null)
|
119
|
return p.getFst();
|
120
|
if (((SelectionConstraints) p.getSnd()).verifyCriteria(param))
|
121
|
return p.getFst();
|
122
|
else
|
123
|
return null;
|
124
|
}).collect(Collectors.toList());
|
125
|
|
126
|
}
|
127
|
|
128
|
public List<Pair<String,SelectionConstraints>> getCommunityForZenodoCommunity(String zc){
|
129
|
return zenodocommunityMap.get(zc);
|
130
|
}
|
131
|
|
132
|
public List<String> getCommunityForSubjectValue(String value) {
|
133
|
|
134
|
return getContextIds(subjectMap.get(value));
|
135
|
}
|
136
|
|
137
|
public List<String> getCommunityForDatasourceValue(String value) {
|
138
|
|
139
|
return getContextIds(datasourceMap.get(value.toLowerCase()));
|
140
|
}
|
141
|
|
142
|
public List<String> getCommunityForZenodoCommunityValue(String value){
|
143
|
|
144
|
return getContextIds(zenodocommunityMap.get(value.toLowerCase()));
|
145
|
}
|
146
|
|
147
|
private List<String> getContextIds(List<Pair<String, SelectionConstraints>> list) {
|
148
|
if (list != null) {
|
149
|
return list.stream().map(p -> p.getFst()).collect(Collectors.toList());
|
150
|
}
|
151
|
return Lists.newArrayList();
|
152
|
}
|
153
|
|
154
|
|
155
|
/*
|
156
|
public Constraints getSelCriteria(String value, String community, MapModes map_mode){
|
157
|
|
158
|
Map<String,List<Pair<String,Constraints>>> map = null;
|
159
|
if(map_mode == MapModes.DATASOURCE_MAP)
|
160
|
map = datasourceMap;
|
161
|
else
|
162
|
if(map_mode == MapModes.ZENODO_COMMUNITY_MAP)
|
163
|
map = zenodocommunityMap;
|
164
|
else
|
165
|
new Throwable("Impossible to have Selection Criteria over subjects");
|
166
|
|
167
|
List<Pair<String, Constraints>> lst = map.get(value);
|
168
|
List<Constraints> selectionList = lst.stream().map(p -> {
|
169
|
if (p.fst == community)
|
170
|
return p.snd;
|
171
|
return null;
|
172
|
}).collect(Collectors.toList());//for each community there will be only one Selection Criteria per datasource or zenodo community
|
173
|
if(selectionList != null)
|
174
|
if (selectionList.size()>0)
|
175
|
return selectionList.get(0);
|
176
|
return null;
|
177
|
}
|
178
|
*/
|
179
|
|
180
|
public Map<String, Community> getCommunities() {
|
181
|
return communities;
|
182
|
}
|
183
|
|
184
|
public void setCommunities(Map<String, Community> communities) {
|
185
|
this.communities = communities;
|
186
|
}
|
187
|
|
188
|
public String toJson() {
|
189
|
final Gson g = new Gson();
|
190
|
return g.toJson(this);
|
191
|
}
|
192
|
|
193
|
public int size() {
|
194
|
return communities.keySet().size();
|
195
|
}
|
196
|
|
197
|
public Community getCommunityById(String id){
|
198
|
return communities.get(id);
|
199
|
}
|
200
|
|
201
|
public List<Community> getCommunityList() {
|
202
|
return Lists.newLinkedList(communities.values());
|
203
|
}
|
204
|
}
|