1
|
package eu.dnetlib.enabling.datasources;
|
2
|
|
3
|
import java.io.StringReader;
|
4
|
|
5
|
import org.apache.commons.lang3.BooleanUtils;
|
6
|
import org.apache.commons.lang3.math.NumberUtils;
|
7
|
import org.apache.commons.logging.Log;
|
8
|
import org.apache.commons.logging.LogFactory;
|
9
|
import org.dom4j.Document;
|
10
|
import org.dom4j.Element;
|
11
|
import org.dom4j.Node;
|
12
|
import org.dom4j.io.SAXReader;
|
13
|
|
14
|
import com.google.common.base.Function;
|
15
|
|
16
|
import eu.dnetlib.enabling.datasources.rmi.DatasourceDesc;
|
17
|
import eu.dnetlib.enabling.datasources.rmi.IfaceDesc;
|
18
|
import eu.dnetlib.miscutils.datetime.DateUtils;
|
19
|
|
20
|
public class ProfileToDatasourceDesc implements Function<String, DatasourceDesc> {
|
21
|
|
22
|
private static final Log log = LogFactory.getLog(ProfileToDatasourceDesc.class);
|
23
|
|
24
|
@Override
|
25
|
public DatasourceDesc apply(final String profile) {
|
26
|
try {
|
27
|
return ProfileToDatasourceDesc.convert(profile);
|
28
|
} catch (Exception e) {
|
29
|
log.error("Error convering profile", e);
|
30
|
return null;
|
31
|
}
|
32
|
}
|
33
|
|
34
|
public static DatasourceDesc convert(final String profile) throws Exception {
|
35
|
final SAXReader reader = new SAXReader();
|
36
|
final Document doc = reader.read(new StringReader(profile));
|
37
|
|
38
|
final DatasourceDesc ds = new DatasourceDesc();
|
39
|
ds.setId(doc.valueOf("//RESOURCE_IDENTIFIER/@value"));
|
40
|
ds.setDatasourceClass(doc.valueOf("//DATASOURCE_TYPE"));
|
41
|
ds.setTypology(doc.valueOf("//TYPOLOGY"));
|
42
|
ds.setOfficialName(doc.valueOf("//OFFICIAL_NAME"));
|
43
|
ds.setEnglishName(doc.valueOf("//ENGLISH_NAME"));
|
44
|
ds.setCountryCode(doc.valueOf("//COUNTRY"));
|
45
|
ds.setLongitude(NumberUtils.toDouble(doc.valueOf("//LOCATION/LONGITUDE"), 0.0));
|
46
|
ds.setLatitude(NumberUtils.toDouble(doc.valueOf("//LOCATION/LATITUDE"), 0.0));
|
47
|
ds.setTimezone(NumberUtils.toDouble(doc.valueOf("//LOCATION/TIMEZONE"), 0.0));
|
48
|
ds.setWebsiteUrl(doc.valueOf("//REPOSITORY_WEBPAGE"));
|
49
|
ds.setOrganization(doc.valueOf("//REPOSITORY_INSTITUTION"));
|
50
|
ds.setContactEmail(doc.valueOf("//ADMIN_INFO"));
|
51
|
|
52
|
for (Object o : doc.selectNodes("//INTERFACES/INTERFACE")) {
|
53
|
final Element n = (Element) o;
|
54
|
|
55
|
final IfaceDesc ifc = new IfaceDesc();
|
56
|
|
57
|
ifc.setId(n.valueOf("@id"));
|
58
|
ifc.setCompliance(n.valueOf("@compliance"));
|
59
|
ifc.setTypology(n.valueOf("@typology"));
|
60
|
ifc.setContentDescription(n.valueOf("@contentDescription"));
|
61
|
ifc.setActive(BooleanUtils.toBoolean(n.valueOf("@active")));
|
62
|
ifc.setRemovable(BooleanUtils.toBoolean(n.valueOf("@removable")));
|
63
|
ifc.setBaseUrl(n.valueOf("./BASE_URL"));
|
64
|
ifc.setAccessProtocol(n.valueOf("./ACCESS_PROTOCOL"));
|
65
|
|
66
|
for (Object o1 : n.selectNodes("./ACCESS_PROTOCOL/@*")) {
|
67
|
final Node attr = (Node) o1;
|
68
|
ifc.getAccessParams().put(attr.getName(), attr.getText());
|
69
|
}
|
70
|
for (Object o1 : n.selectNodes("./INTERFACE_EXTRA_FIELD")) {
|
71
|
final Node f = (Node) o1;
|
72
|
ifc.getExtraFields().put(f.valueOf("@name"), f.getText());
|
73
|
}
|
74
|
|
75
|
ds.getInterfaces().add(ifc);
|
76
|
}
|
77
|
|
78
|
final DateUtils dateUtils = new DateUtils();
|
79
|
for (Object o : doc.selectNodes("//EXTRA_FIELDS/EXTRA_FIELD")) {
|
80
|
final String k = ((Element) o).valueOf("./key").trim();
|
81
|
final String v = ((Element) o).valueOf("./value").trim();
|
82
|
|
83
|
if (k.equalsIgnoreCase("ACTIVATION_ID")) {
|
84
|
ds.setActivationId(v);
|
85
|
} else if (k.equalsIgnoreCase("NamespacePrefix")) {
|
86
|
ds.setNamespacePrefix(v);
|
87
|
} else if (k.equalsIgnoreCase("aggregatorName")) {
|
88
|
ds.setAggregator(v);
|
89
|
} else if (k.equalsIgnoreCase("dateOfCollection")) {
|
90
|
ds.setDateOfCollection(dateUtils.parse(v));
|
91
|
} else if (k.equalsIgnoreCase("dateOfValidation")) {
|
92
|
ds.setDateOfValidation(dateUtils.parse(v));
|
93
|
}
|
94
|
}
|
95
|
|
96
|
return ds;
|
97
|
}
|
98
|
}
|