1
|
package eu.dnetlib.openaire.directindex.api;
|
2
|
|
3
|
import java.io.StringWriter;
|
4
|
import java.util.*;
|
5
|
import java.util.stream.Collectors;
|
6
|
import java.util.stream.Collectors;
|
7
|
|
8
|
import com.google.common.base.Function;
|
9
|
import com.google.common.collect.Lists;
|
10
|
import eu.dnetlib.miscutils.functional.hash.Hashing;
|
11
|
import eu.dnetlib.openaire.directindex.objects.ZenodoContextList;
|
12
|
import org.apache.commons.lang.StringUtils;
|
13
|
import org.springframework.web.client.RestTemplate;
|
14
|
|
15
|
/**
|
16
|
* Created by michele on 15/01/16.
|
17
|
*/
|
18
|
public class OpenAIRESubmitterUtils {
|
19
|
|
20
|
private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(OpenAIRESubmitterUtils.class);
|
21
|
private static final String ZENODO_COMMUNITY = "zenodo.org/communities/";
|
22
|
private String community_api;
|
23
|
public OpenAIRESubmitterUtils(String community_api){
|
24
|
this.community_api = community_api;
|
25
|
}
|
26
|
|
27
|
public Map<String, String> calculateProjectInfo(final String link) {
|
28
|
final Map<String, String> info = new HashMap<String, String>();
|
29
|
final String[] arr = link.split("/");
|
30
|
// info:eu-repo/grantAgreement/EC/FP7/244909/EU/Making Capabilities Work/WorkAble
|
31
|
|
32
|
if (arr.length > 4) {
|
33
|
final String acronym = arr.length > 7 ? arr[7] : "";
|
34
|
final String title = arr.length > 6 ? StringUtils.isNotBlank(arr[6]) ? arr[6] : acronym : "";
|
35
|
final String jurisdiction = arr.length > 5 ? arr[5] : "";
|
36
|
final String funderId = calculateFunderId(arr[2], arr[3]);
|
37
|
info.put("id", calculateProjectId(arr[2], arr[3], arr[4]));
|
38
|
info.put("funderShortName", arr[2]);
|
39
|
info.put("fundingName", arr[3]);
|
40
|
info.put("code", arr[4]);
|
41
|
info.put("jurisdiction", jurisdiction);
|
42
|
info.put("title", title);
|
43
|
info.put("acronym", acronym);
|
44
|
info.put("funderId", funderId);
|
45
|
info.put("funderName", calculateFunderName(arr[2]));
|
46
|
info.put("fundingId", funderId + "::" + arr[3]);
|
47
|
}
|
48
|
return info;
|
49
|
}
|
50
|
|
51
|
protected String calculateFunderPrefix(final String funderShortName, final String funding){
|
52
|
switch(funderShortName.toLowerCase()){
|
53
|
case "conicyt":
|
54
|
return "conicytf____::";
|
55
|
case "ec":
|
56
|
if (funding.equalsIgnoreCase("fp7")) {
|
57
|
return "corda_______::";
|
58
|
} else {
|
59
|
return "corda__h2020::";
|
60
|
}
|
61
|
case "hrzz":
|
62
|
case "mzos":
|
63
|
return "irb_hr______::";
|
64
|
case "tara":
|
65
|
return "taraexp_____::";
|
66
|
case "tubitak":
|
67
|
return "tubitakf____::";
|
68
|
default:
|
69
|
String prefix = funderShortName.toLowerCase();
|
70
|
//ensure we have 12 chars
|
71
|
while(prefix.length() < 12) prefix += "_";
|
72
|
return prefix +"::";
|
73
|
}
|
74
|
}
|
75
|
|
76
|
protected String calculateProjectId(final String funderShortName, final String funding, final String code) {
|
77
|
final String suffix = Hashing.md5(code);
|
78
|
final String funderPrefix = calculateFunderPrefix(funderShortName, funding);
|
79
|
return funderPrefix + suffix;
|
80
|
}
|
81
|
|
82
|
protected String calculateFunderId(final String funderShortName, final String funding) {
|
83
|
switch (funderShortName.toLowerCase()) {
|
84
|
case "ec":
|
85
|
return "ec__________::EC";
|
86
|
default:
|
87
|
String prefix = calculateFunderPrefix(funderShortName, funding);
|
88
|
return prefix + funderShortName.toUpperCase();
|
89
|
}
|
90
|
}
|
91
|
|
92
|
|
93
|
protected String calculateFunderName(final String funderShortName) {
|
94
|
|
95
|
switch (funderShortName.toLowerCase()) {
|
96
|
case "aff":
|
97
|
case "aka":
|
98
|
return "Academy of Finland";
|
99
|
case "arc":
|
100
|
return "Australian Research Council (ARC)";
|
101
|
case "conicyt":
|
102
|
return "Comisión Nacional de Investigación Científica y Tecnológica";
|
103
|
case "ec":
|
104
|
return "European Commission";
|
105
|
case "fct":
|
106
|
return "Fundação para a Ciência e a Tecnologia, I.P.";
|
107
|
case "fwf":
|
108
|
return "Austrian Science Fund (FWF)";
|
109
|
case "hrzz":
|
110
|
return "Croatian Science Foundation (CSF)";
|
111
|
case "mestd":
|
112
|
return "Ministry of Education, Science and Technological Development of Republic of Serbia";
|
113
|
case "miur":
|
114
|
return "Ministero dell'Istruzione dell'Università e della Ricerca";
|
115
|
case "mzos":
|
116
|
return "Ministry of Science, Education and Sports of the Republic of Croatia (MSES)";
|
117
|
case "nhmrc":
|
118
|
return "National Health and Medical Research Council (NHMRC)";
|
119
|
case "nih":
|
120
|
return "National Institutes of Health";
|
121
|
case "nsf":
|
122
|
return "National Science Foundation";
|
123
|
case "nwo":
|
124
|
return "Netherlands Organisation for Scientific Research (NWO)";
|
125
|
case "rcuk":
|
126
|
return "Research Council UK";
|
127
|
case "sfi":
|
128
|
return "Science Foundation Ireland";
|
129
|
case "sgov":
|
130
|
return "Gobierno de España";
|
131
|
case "snsf":
|
132
|
return "Swiss National Science Foundation";
|
133
|
case "tara":
|
134
|
return "Tara Expeditions Foundation";
|
135
|
case "tubitak":
|
136
|
return "Türkiye Bilimsel ve Teknolojik Araştırma Kurumu";
|
137
|
case "wt":
|
138
|
return "Wellcome Trust";
|
139
|
case "rpf":
|
140
|
return "Research Promotion Foundation";
|
141
|
case "gsrt":
|
142
|
return "General Secretariat for Research and Technology";
|
143
|
default:
|
144
|
log.error("Funder short name '"+funderShortName+"' not managed");
|
145
|
return "";
|
146
|
}
|
147
|
}
|
148
|
|
149
|
private List<String> filterContexts(List<String> contexts){
|
150
|
|
151
|
List<String> zenodoContexts
|
152
|
= contexts.stream()
|
153
|
.map(c -> {
|
154
|
if(c.contains(ZENODO_COMMUNITY))
|
155
|
return c.substring(c.lastIndexOf("/")+1);
|
156
|
return null;
|
157
|
}).collect(Collectors.toList());
|
158
|
|
159
|
if (zenodoContexts.size()>0){
|
160
|
contexts = contexts.stream().filter(c -> !c.contains(ZENODO_COMMUNITY)).collect(Collectors.toList());
|
161
|
RestTemplate rt = new RestTemplate();
|
162
|
Set<String> zenodoOpenAIRE = new HashSet<>();
|
163
|
for (String context: zenodoContexts){
|
164
|
//String ct = context.substring(context.lastIndexOf("/")+1);
|
165
|
zenodoOpenAIRE.addAll(rt.getForObject(community_api +context+"/openairecommunities", ZenodoContextList.class).getOpenAirecommunitylist());
|
166
|
|
167
|
}
|
168
|
contexts.addAll(zenodoOpenAIRE);
|
169
|
}
|
170
|
|
171
|
return contexts;
|
172
|
|
173
|
}
|
174
|
public List<ContextInfo> processContexts(final List<String> list) {
|
175
|
filterContexts(list);
|
176
|
return Lists.newArrayList(Lists.transform(filterContexts(list), new Function<String, ContextInfo>() {
|
177
|
|
178
|
@Override
|
179
|
public ContextInfo apply(final String s) {
|
180
|
return createContextInfo(s.split("::"), 0);
|
181
|
}
|
182
|
|
183
|
private ContextInfo createContextInfo(final String[] arr, final int pos) {
|
184
|
final StringWriter id = new StringWriter();
|
185
|
id.write(arr[0]);
|
186
|
for (int i = 0; i < pos; i++) {
|
187
|
id.write("::");
|
188
|
id.write(arr[i + 1]);
|
189
|
}
|
190
|
final String elem = (pos == 0) ? "context" : (pos == 1) ? "category" : "concept";
|
191
|
final ContextInfo info = new ContextInfo(elem, id.toString());
|
192
|
if ((pos + 1) < arr.length) {
|
193
|
info.getChildren().add(createContextInfo(arr, pos + 1));
|
194
|
}
|
195
|
return info;
|
196
|
}
|
197
|
}));
|
198
|
|
199
|
}
|
200
|
|
201
|
public class ContextInfo {
|
202
|
|
203
|
private String elem;
|
204
|
private String id;
|
205
|
private List<ContextInfo> children = new ArrayList<ContextInfo>();
|
206
|
|
207
|
public ContextInfo(final String elem,
|
208
|
final String id) {
|
209
|
this.elem = elem;
|
210
|
this.id = id;
|
211
|
}
|
212
|
|
213
|
public String getElem() {
|
214
|
return elem;
|
215
|
}
|
216
|
|
217
|
public void setElem(final String elem) {
|
218
|
this.elem = elem;
|
219
|
}
|
220
|
|
221
|
public String getId() {
|
222
|
return id;
|
223
|
}
|
224
|
|
225
|
public void setId(final String id) {
|
226
|
this.id = id;
|
227
|
}
|
228
|
|
229
|
public List<ContextInfo> getChildren() {
|
230
|
return children;
|
231
|
}
|
232
|
|
233
|
public void setChildren(final List<ContextInfo> children) {
|
234
|
this.children = children;
|
235
|
}
|
236
|
|
237
|
public boolean isRoot() {
|
238
|
return elem.equals("context");
|
239
|
}
|
240
|
}
|
241
|
}
|