1
|
package eu.dnetlib.openaire.exporter.model;
|
2
|
|
3
|
import java.sql.Date;
|
4
|
|
5
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
6
|
import eu.dnetlib.data.transform.xml.AbstractDNetXsltFunctions;
|
7
|
import eu.dnetlib.miscutils.datetime.DateUtils;
|
8
|
import eu.dnetlib.openaire.exporter.model.datasource.ApiDetails;
|
9
|
import eu.dnetlib.openaire.exporter.model.datasource.DatasourceDetails;
|
10
|
import eu.dnetlib.openaire.exporter.model.datasource.OrganizationDetails;
|
11
|
import eu.dnetlib.openaire.exporter.model.datasource.db.ApiDbEntry;
|
12
|
import eu.dnetlib.openaire.exporter.model.datasource.db.DatasourceDbEntry;
|
13
|
import org.apache.commons.lang3.RandomStringUtils;
|
14
|
import org.apache.commons.lang3.StringUtils;
|
15
|
import org.dom4j.DocumentHelper;
|
16
|
import org.dom4j.Element;
|
17
|
|
18
|
public class ConversionUtils {
|
19
|
|
20
|
public static final String ID_SEPARATOR = "::";
|
21
|
|
22
|
public static final String ID_PREFIX = "api_________" + ID_SEPARATOR;
|
23
|
|
24
|
public static String createId(final ApiDetails api) {
|
25
|
return ID_PREFIX + api.getDatasource() + ID_SEPARATOR + RandomStringUtils.randomAlphanumeric(8);
|
26
|
}
|
27
|
|
28
|
public static DatasourceDetails asDetails(final DatasourceDbEntry d) {
|
29
|
final DatasourceDetails details = _convert(d, DatasourceDetails.class);
|
30
|
return details.setOpenaireId(asOpenaireId(details.getId()));
|
31
|
}
|
32
|
|
33
|
public static ApiDetails asDetails(final ApiDbEntry d) {
|
34
|
return _convert(d, ApiDetails.class);
|
35
|
}
|
36
|
|
37
|
public static ApiDbEntry asDbEntry(final ApiDetails d) {
|
38
|
final ApiDbEntry apiDbEntry = _convert(d, ApiDbEntry.class);
|
39
|
|
40
|
// Need to complete the references among objects, because you know, referencial integrity ...
|
41
|
apiDbEntry.getApiParams().forEach(ap -> ap.getId().setApi(apiDbEntry));
|
42
|
|
43
|
return apiDbEntry;
|
44
|
}
|
45
|
|
46
|
public static DatasourceDbEntry asDbEntry(DatasourceDetails d) {
|
47
|
final DatasourceDbEntry dbe = _convert(d, DatasourceDbEntry.class);
|
48
|
if (dbe.getOrganizations() != null) {
|
49
|
dbe.getOrganizations().forEach(o -> {
|
50
|
o.setId(dbe.getId() + ID_SEPARATOR + o.getLegalname());
|
51
|
if (o.getDateofcollection() == null) {
|
52
|
o.setDateofcollection(new Date(System.currentTimeMillis()));
|
53
|
}
|
54
|
});
|
55
|
}
|
56
|
return dbe;
|
57
|
}
|
58
|
|
59
|
public static String asRepositoryProfile(final DatasourceDetails ds) {
|
60
|
final Element root = DocumentHelper.createElement("RESOURCE_PROFILE");
|
61
|
|
62
|
final Element header = root.addElement("HEADER");
|
63
|
header.addElement("RESOURCE_IDENTIFIER").addAttribute("value", "");
|
64
|
header.addElement("RESOURCE_TYPE").addAttribute("value", "RepositoryServiceResourceType");
|
65
|
header.addElement("RESOURCE_KIND").addAttribute("value", "RepositoryServiceResources");
|
66
|
header.addElement("RESOURCE_URI").addAttribute("value", "");
|
67
|
header.addElement("DATE_OF_CREATION").addAttribute("value", DateUtils.now_ISO8601());
|
68
|
header.addElement("PROTOCOL");
|
69
|
|
70
|
final Element body = root.addElement("BODY");
|
71
|
final Element conf = body.addElement("CONFIGURATION");
|
72
|
conf.addElement("DATASOURCE_TYPE").setText(ds.getTypology());
|
73
|
|
74
|
final Element origId = conf.addElement("DATASOURCE_ORIGINAL_ID");
|
75
|
origId.addAttribute("provenance", "D-NET");
|
76
|
origId.setText(ds.getId());
|
77
|
|
78
|
conf.addElement("DATASOURCE_AGGREGATED").setText("false");
|
79
|
conf.addElement("ENVIRONMENTS").addElement("ENVIRONMENT").setText("OPENAIRE");
|
80
|
conf.addElement("TYPOLOGY").setText(""+ds.getTypology());
|
81
|
conf.addElement("MAX_SIZE_OF_DATASTRUCTURE").setText("0");
|
82
|
conf.addElement("AVAILABLE_DISKSPACE").setText("0");
|
83
|
conf.addElement("MAX_NUMBER_OF_DATASTRUCTURE").setText("0");
|
84
|
|
85
|
final String officialName = ds.getOfficialname();
|
86
|
conf.addElement("OFFICIAL_NAME").setText(officialName);
|
87
|
String englishName = ds.getEnglishname();
|
88
|
conf.addElement("ENGLISH_NAME").setText(StringUtils.isNotBlank(englishName) ? englishName : officialName);
|
89
|
conf.addElement("ICON_URI").setText("" + ds.getLogourl());
|
90
|
final OrganizationDetails org = getOrganization(ds);
|
91
|
|
92
|
conf.addElement("COUNTRY").setText(org != null ? org.getCountry() : "");
|
93
|
|
94
|
final Element location = conf.addElement("LOCATION");
|
95
|
location.addElement("LONGITUDE").setText("" + ds.getLongitude());
|
96
|
location.addElement("LATITUDE").setText("" + ds.getLatitude());
|
97
|
location.addElement("TIMEZONE").setText("" + ds.getTimezone());
|
98
|
|
99
|
conf.addElement("REPOSITORY_WEBPAGE").setText(ds.getWebsiteurl());
|
100
|
getOrganization(ds);
|
101
|
conf.addElement("REPOSITORY_INSTITUTION").setText(org != null ? org.getLegalname() : "");
|
102
|
|
103
|
conf.addElement("ADMIN_INFO").setText(ds.getContactemail());
|
104
|
|
105
|
conf.addElement("INTERFACES");
|
106
|
|
107
|
final Element extraFields = conf.addElement("EXTRA_FIELDS");
|
108
|
addExtraField(extraFields, "ACTIVATION_ID", ds.getActivationId());
|
109
|
addExtraField(extraFields, "NamespacePrefix", ds.getNamespaceprefix());
|
110
|
addExtraField(extraFields, "aggregatorName", ds.getAggregator());
|
111
|
addExtraField(extraFields, "dateOfCollection", "" + ds.getDateofcollection());
|
112
|
addExtraField(extraFields, "dateOfValidation", "" + ds.getDateofvalidation());
|
113
|
conf.addElement("REGISTERED_BY").setText(ds.getRegisteredby());
|
114
|
|
115
|
final Element status = body.addElement("STATUS");
|
116
|
status.addElement("NUMBER_OF_OBJECTS").setText("0");
|
117
|
status.addElement("LAST_UPDATE").addAttribute("value", DateUtils.now_ISO8601());
|
118
|
|
119
|
final Element qos = body.addElement("QOS");
|
120
|
qos.addElement("AVAILABILITY").setText("0");
|
121
|
qos.addElement("CAPACITY");
|
122
|
qos.addElement("THROUGHPUT").setText("0");
|
123
|
|
124
|
body.addElement("SECURITY_PARAMETERS");
|
125
|
body.addElement("BLACKBOARD");
|
126
|
|
127
|
return root.asXML();
|
128
|
}
|
129
|
|
130
|
public static String asRepositoryInterfce(final ApiDetails api) {
|
131
|
final Element iface = DocumentHelper.createElement("INTERFACE");
|
132
|
|
133
|
iface.addAttribute("active", String.valueOf(api.getActive()))
|
134
|
.addAttribute("compliance", api.getCompatibility())
|
135
|
.addAttribute("contentDescription", api.getContentdescription())
|
136
|
.addAttribute("id", api.getId())
|
137
|
.addAttribute("label", String.format("%s (%s)", api.getTypology(), api.getCompatibility()))
|
138
|
.addAttribute("removable", String.valueOf(api.getRemovable()))
|
139
|
.addAttribute("typology", api.getTypology());
|
140
|
iface.addElement("ACCESS_PROTOCOL").setText(api.getProtocol());
|
141
|
if (api.getApiParams() != null) {
|
142
|
final Element accessProtocol = (Element) iface.selectSingleNode("./ACCESS_PROTOCOL");
|
143
|
api.getApiParams().forEach(ap -> {
|
144
|
if ("format".equals(ap.getParam())) {
|
145
|
accessProtocol.addAttribute("format", ap.getValue());
|
146
|
}
|
147
|
if ("set".equals(ap.getParam())) {
|
148
|
accessProtocol.addAttribute("set", ap.getValue());
|
149
|
}
|
150
|
});
|
151
|
}
|
152
|
iface.addElement("BASE_URL").setText(api.getBaseurl());
|
153
|
iface.addElement("INTERFACE_EXTRA_FIELD").addAttribute("name", "last_collection_date");
|
154
|
iface.addElement("INTERFACE_EXTRA_FIELD").addAttribute("name", "last_collection_mdId");
|
155
|
iface.addElement("INTERFACE_EXTRA_FIELD").addAttribute("name", "last_collection_total");
|
156
|
iface.addElement("INTERFACE_EXTRA_FIELD").addAttribute("name", "last_aggregation_date");
|
157
|
iface.addElement("INTERFACE_EXTRA_FIELD").addAttribute("name", "last_aggregation_mdId");
|
158
|
iface.addElement("INTERFACE_EXTRA_FIELD").addAttribute("name", "last_aggregation_total");
|
159
|
iface.addElement("INTERFACE_EXTRA_FIELD").addAttribute("name", "metadata_identifier_path");
|
160
|
|
161
|
return iface.asXML();
|
162
|
}
|
163
|
|
164
|
// HELPERS
|
165
|
|
166
|
private static <T> T _convert(final Object o, final Class<T> clazz) {
|
167
|
final ObjectMapper mapper = new ObjectMapper();
|
168
|
return mapper.convertValue(o, clazz);
|
169
|
}
|
170
|
|
171
|
private static OrganizationDetails getOrganization(final DatasourceDetails ds) {
|
172
|
if (ds.getOrganizations() != null && !ds.getOrganizations().isEmpty()) {
|
173
|
return ds.getOrganizations().stream().findFirst().get();
|
174
|
}
|
175
|
return null;
|
176
|
}
|
177
|
|
178
|
private static void addExtraField(final Element extraFields, final String field, final String value) {
|
179
|
final Element f = extraFields.addElement("FIELD");
|
180
|
f.addElement("key").setText(field);
|
181
|
f.addElement("value").setText(value != null ? value : "");
|
182
|
}
|
183
|
|
184
|
private static String asOpenaireId(final String id) {
|
185
|
final String prefix = StringUtils.substringBefore(id, ID_SEPARATOR);
|
186
|
final String md5 = StringUtils.substringAfter(id, ID_SEPARATOR);
|
187
|
|
188
|
return prefix + ID_SEPARATOR + AbstractDNetXsltFunctions.md5(md5);
|
189
|
}
|
190
|
|
191
|
}
|