1
|
package eu.dnetlib.openaire.exporter.funders.model;
|
2
|
|
3
|
import java.io.StringReader;
|
4
|
import java.util.stream.Collectors;
|
5
|
|
6
|
import eu.dnetlib.openaire.exporter.funders.model.db.FunderDbEntry;
|
7
|
import eu.dnetlib.openaire.exporter.funders.model.db.FundingPathDbEntry;
|
8
|
import org.dom4j.Document;
|
9
|
import org.dom4j.DocumentException;
|
10
|
import org.dom4j.io.SAXReader;
|
11
|
|
12
|
import static org.apache.commons.lang3.StringUtils.*;
|
13
|
|
14
|
public class ConversionUtils {
|
15
|
|
16
|
public static final String SEPARATOR = "::";
|
17
|
|
18
|
public static FunderDetails asFunderDetails(final FunderDbEntry fdb) {
|
19
|
final FunderDetails f = new FunderDetails();
|
20
|
|
21
|
f.setId(fdb.getId());
|
22
|
f.setName(fdb.getName());
|
23
|
f.setShortname(fdb.getShortname());
|
24
|
f.setJurisdiction(fdb.getJurisdiction());
|
25
|
f.setRegistrationDate(fdb.getRegistrationdate());
|
26
|
f.setLastUpdateDate(fdb.getLastupdatedate());
|
27
|
|
28
|
return f;
|
29
|
}
|
30
|
|
31
|
public static ExtendedFunderDetails asExtendedFunderDetails(final FunderDbEntry fdb) {
|
32
|
final ExtendedFunderDetails f = new ExtendedFunderDetails(asFunderDetails(fdb));
|
33
|
|
34
|
if (fdb.getFundingpaths() != null) {
|
35
|
f.setFundingStreams(
|
36
|
fdb.getFundingpaths().stream()
|
37
|
.map(ConversionUtils::asFundingStream)
|
38
|
.collect(Collectors.toList()));
|
39
|
}
|
40
|
|
41
|
return f;
|
42
|
}
|
43
|
|
44
|
private static FundingStream asFundingStream(final FundingPathDbEntry pathDbEntry) {
|
45
|
final FundingStream f = new FundingStream();
|
46
|
|
47
|
try {
|
48
|
final Document xml = new SAXReader().read(new StringReader(pathDbEntry.getPath()));
|
49
|
|
50
|
for(int i=2;i>=0;i--) {
|
51
|
if (hasFundingLevel(i, xml)) {
|
52
|
f.setId(getId(i, xml));
|
53
|
f.setName(getName(i, xml));
|
54
|
break;
|
55
|
}
|
56
|
}
|
57
|
|
58
|
if (isBlank(f.getId()) && isNoneBlank(xml.valueOf("//funder/id/text()"))) {
|
59
|
f.setId(xml.valueOf("//funder/shortname/text()"));
|
60
|
f.setName(xml.valueOf("//funder/name/text()"));
|
61
|
}
|
62
|
|
63
|
if (isBlank(f.getId())) {
|
64
|
throw new IllegalStateException("invalid funding path:\n" + xml.asXML());
|
65
|
}
|
66
|
|
67
|
return f;
|
68
|
|
69
|
} catch (DocumentException e) {
|
70
|
throw new IllegalStateException("unable to parse funding path:\n" + pathDbEntry.getPath());
|
71
|
}
|
72
|
}
|
73
|
|
74
|
private static String getName(int level, final Document d) {
|
75
|
return d.valueOf(String.format("//funding_level_%s/description/text()", level));
|
76
|
}
|
77
|
|
78
|
private static String getId(int level, final Document d) {
|
79
|
return substringAfter(
|
80
|
d.valueOf(
|
81
|
String.format("//funding_level_%s/id/text()", level)
|
82
|
), SEPARATOR);
|
83
|
}
|
84
|
|
85
|
private static boolean hasFundingLevel(int level, Document d) {
|
86
|
return isNotBlank(getId(level, d));
|
87
|
}
|
88
|
}
|