1
|
package eu.dnetlib.openaire.dsm.dao.utils;
|
2
|
|
3
|
import java.sql.Date;
|
4
|
import java.util.HashSet;
|
5
|
import java.util.Map;
|
6
|
import java.util.Set;
|
7
|
|
8
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
9
|
import com.google.common.collect.Maps;
|
10
|
import com.google.common.xml.XmlEscapers;
|
11
|
import eu.dnetlib.data.transform.xml.AbstractDNetXsltFunctions;
|
12
|
import eu.dnetlib.miscutils.datetime.DateUtils;
|
13
|
import eu.dnetlib.openaire.dsm.domain.*;
|
14
|
import eu.dnetlib.openaire.dsm.domain.db.ApiDbEntry;
|
15
|
import eu.dnetlib.openaire.dsm.domain.db.DatasourceDbEntry;
|
16
|
import org.apache.commons.lang3.RandomStringUtils;
|
17
|
import org.apache.commons.lang3.StringUtils;
|
18
|
import org.dom4j.DocumentHelper;
|
19
|
import org.dom4j.Element;
|
20
|
import org.springframework.beans.BeanUtils;
|
21
|
import org.springframework.beans.BeanWrapper;
|
22
|
import org.springframework.beans.BeanWrapperImpl;
|
23
|
|
24
|
import static eu.dnetlib.openaire.common.ExporterConstants.*;
|
25
|
|
26
|
public class DsmMappingUtils {
|
27
|
|
28
|
public static final String DATE_FORMAT = "yyyy-MM-dd";
|
29
|
|
30
|
public static final String ID_SEPARATOR = "::";
|
31
|
|
32
|
public static final String ID_PREFIX = "api_________" + ID_SEPARATOR;
|
33
|
|
34
|
public static String createId(final ApiDetails api) {
|
35
|
return ID_PREFIX + api.getDatasource() + ID_SEPARATOR + RandomStringUtils.randomAlphanumeric(8);
|
36
|
}
|
37
|
|
38
|
public static DatasourceDetails asDetails(final DatasourceDbEntry d) {
|
39
|
final DatasourceDetails details = _convert(d, DatasourceDetails.class);
|
40
|
return details.setOpenaireId(asOpenaireId(details.getId()));
|
41
|
}
|
42
|
|
43
|
public static DatasourceSnippet asSnippet(final DatasourceDbEntry d) {
|
44
|
final DatasourceSnippet ds = new DatasourceSnippet();
|
45
|
ds.setId(d.getId());
|
46
|
ds.setOfficialname(d.getOfficialname());
|
47
|
ds.setEnglishname(d.getEnglishname());
|
48
|
ds.setRegisteredby(d.getRegisteredby());
|
49
|
ds.setWebsiteurl(d.getWebsiteurl());
|
50
|
return ds;
|
51
|
}
|
52
|
|
53
|
public static ApiDetails asDetails(final ApiDbEntry d) {
|
54
|
return _convert(d, ApiDetails.class);
|
55
|
}
|
56
|
|
57
|
public static ApiDbEntry asDbEntry(final ApiDetails d) {
|
58
|
final ApiDbEntry apiDbEntry = _convert(d, ApiDbEntry.class);
|
59
|
|
60
|
// Need to complete the references among objects, because you know, referential integrity ...
|
61
|
apiDbEntry.getApiParams().forEach(ap -> ap.getId().setApi(apiDbEntry));
|
62
|
|
63
|
return apiDbEntry;
|
64
|
}
|
65
|
|
66
|
public static DatasourceDbEntry asDbEntry(DatasourceDetails d) {
|
67
|
final DatasourceDbEntry dbe = _convert(d, DatasourceDbEntry.class);
|
68
|
if (dbe.getOrganizations() != null) {
|
69
|
dbe.getOrganizations().forEach(o -> {
|
70
|
o.setId(dbe.getId() + ID_SEPARATOR + o.getLegalname());
|
71
|
if (o.getDateofcollection() == null) {
|
72
|
o.setDateofcollection(new Date(System.currentTimeMillis()));
|
73
|
}
|
74
|
});
|
75
|
}
|
76
|
return dbe;
|
77
|
}
|
78
|
|
79
|
public static DatasourceDbEntry asDbEntry(DatasourceDetailsUpdate d) {
|
80
|
return _convert(d, DatasourceDbEntry.class);
|
81
|
}
|
82
|
|
83
|
public static String asRepositoryProfile(final DatasourceDetails ds) {
|
84
|
final Element root = DocumentHelper.createElement("RESOURCE_PROFILE");
|
85
|
|
86
|
final Element header = root.addElement("HEADER");
|
87
|
header.addElement("RESOURCE_IDENTIFIER").addAttribute("value", "");
|
88
|
header.addElement("RESOURCE_TYPE").addAttribute("value", "RepositoryServiceResourceType");
|
89
|
header.addElement("RESOURCE_KIND").addAttribute("value", "RepositoryServiceResources");
|
90
|
header.addElement("RESOURCE_URI").addAttribute("value", "");
|
91
|
header.addElement("DATE_OF_CREATION").addAttribute("value", DateUtils.now_ISO8601());
|
92
|
header.addElement("PROTOCOL");
|
93
|
|
94
|
final Element body = root.addElement("BODY");
|
95
|
final Element conf = body.addElement("CONFIGURATION");
|
96
|
conf.addElement("DATASOURCE_TYPE").setText(ds.getTypology());
|
97
|
|
98
|
final Element origId = conf.addElement("DATASOURCE_ORIGINAL_ID");
|
99
|
origId.addAttribute("provenance", "D-NET");
|
100
|
origId.setText(ds.getId());
|
101
|
|
102
|
conf.addElement("DATASOURCE_AGGREGATED").setText("false");
|
103
|
conf.addElement("ENVIRONMENTS").addElement("ENVIRONMENT").setText("OPENAIRE");
|
104
|
conf.addElement("TYPOLOGY").setText(""+ds.getTypology());
|
105
|
conf.addElement("MAX_SIZE_OF_DATASTRUCTURE").setText("0");
|
106
|
conf.addElement("AVAILABLE_DISKSPACE").setText("0");
|
107
|
conf.addElement("MAX_NUMBER_OF_DATASTRUCTURE").setText("0");
|
108
|
|
109
|
final String officialName = ds.getOfficialname();
|
110
|
conf.addElement("OFFICIAL_NAME").setText(officialName);
|
111
|
String englishName = ds.getEnglishname();
|
112
|
conf.addElement("ENGLISH_NAME").setText(StringUtils.isNotBlank(englishName) ? englishName : officialName);
|
113
|
conf.addElement("ICON_URI").setText("" + ds.getLogourl());
|
114
|
final OrganizationDetails org = getOrganization(ds);
|
115
|
|
116
|
conf.addElement("COUNTRY").setText(org != null ? org.getCountry() : "");
|
117
|
|
118
|
final Element location = conf.addElement("LOCATION");
|
119
|
location.addElement("LONGITUDE").setText("" + ds.getLongitude());
|
120
|
location.addElement("LATITUDE").setText("" + ds.getLatitude());
|
121
|
location.addElement("TIMEZONE").setText("" + ds.getTimezone());
|
122
|
|
123
|
conf.addElement("REPOSITORY_WEBPAGE").setText(ds.getWebsiteurl());
|
124
|
getOrganization(ds);
|
125
|
conf.addElement("REPOSITORY_INSTITUTION").setText(org != null ? org.getLegalname() : "");
|
126
|
|
127
|
conf.addElement("ADMIN_INFO").setText(ds.getContactemail());
|
128
|
|
129
|
conf.addElement("INTERFACES");
|
130
|
|
131
|
final Element extraFields = conf.addElement("EXTRA_FIELDS");
|
132
|
addExtraField(extraFields, "ACTIVATION_ID", ds.getActivationId());
|
133
|
addExtraField(extraFields, "NamespacePrefix", ds.getNamespaceprefix());
|
134
|
addExtraField(extraFields, "aggregatorName", ds.getAggregator());
|
135
|
addExtraField(extraFields, "dateOfCollection", "" + ds.getDateofcollection());
|
136
|
addExtraField(extraFields, "dateOfValidation", "" + ds.getDateofvalidation());
|
137
|
conf.addElement("REGISTERED_BY").setText(ds.getRegisteredby());
|
138
|
|
139
|
final Element status = body.addElement("STATUS");
|
140
|
status.addElement("NUMBER_OF_OBJECTS").setText("0");
|
141
|
status.addElement("LAST_UPDATE").addAttribute("value", DateUtils.now_ISO8601());
|
142
|
|
143
|
final Element qos = body.addElement("QOS");
|
144
|
qos.addElement("AVAILABILITY").setText("0");
|
145
|
qos.addElement("CAPACITY");
|
146
|
qos.addElement("THROUGHPUT").setText("0");
|
147
|
|
148
|
body.addElement("SECURITY_PARAMETERS");
|
149
|
body.addElement("BLACKBOARD");
|
150
|
|
151
|
return root.asXML();
|
152
|
}
|
153
|
|
154
|
public static String asRepositoryInterfce(final ApiDetails api) {
|
155
|
final Element iface = DocumentHelper.createElement("INTERFACE");
|
156
|
|
157
|
iface.addAttribute("active", String.valueOf(api.getActive()))
|
158
|
.addAttribute("compliance", api.getCompatibility())
|
159
|
.addAttribute("contentDescription", api.getContentdescription())
|
160
|
.addAttribute("id", api.getId())
|
161
|
.addAttribute("label", String.format("%s (%s)", api.getTypology(), api.getCompatibility()))
|
162
|
.addAttribute("removable", String.valueOf(api.getRemovable()))
|
163
|
.addAttribute("typology", api.getTypology());
|
164
|
iface.addElement("ACCESS_PROTOCOL").setText(api.getProtocol());
|
165
|
if (api.getApiParams() != null) {
|
166
|
final Element accessProtocol = (Element) iface.selectSingleNode("./ACCESS_PROTOCOL");
|
167
|
api.getApiParams().forEach(ap -> {
|
168
|
if ("format".equals(ap.getParam())) {
|
169
|
accessProtocol.addAttribute("format", ap.getValue());
|
170
|
}
|
171
|
if ("set".equals(ap.getParam())) {
|
172
|
accessProtocol.addAttribute("set", ap.getValue());
|
173
|
}
|
174
|
});
|
175
|
}
|
176
|
iface.addElement("BASE_URL").setText(api.getBaseurl());
|
177
|
iface.addElement("INTERFACE_EXTRA_FIELD").addAttribute("name", "last_collection_date");
|
178
|
iface.addElement("INTERFACE_EXTRA_FIELD").addAttribute("name", "last_collection_mdId");
|
179
|
iface.addElement("INTERFACE_EXTRA_FIELD").addAttribute("name", "last_collection_total");
|
180
|
iface.addElement("INTERFACE_EXTRA_FIELD").addAttribute("name", "last_aggregation_date");
|
181
|
iface.addElement("INTERFACE_EXTRA_FIELD").addAttribute("name", "last_aggregation_mdId");
|
182
|
iface.addElement("INTERFACE_EXTRA_FIELD").addAttribute("name", "last_aggregation_total");
|
183
|
iface.addElement("INTERFACE_EXTRA_FIELD").addAttribute("name", "metadata_identifier_path");
|
184
|
|
185
|
return iface.asXML();
|
186
|
}
|
187
|
|
188
|
// HELPERS
|
189
|
|
190
|
private static <T> T _convert(final Object o, final Class<T> clazz) {
|
191
|
final ObjectMapper mapper = new ObjectMapper();
|
192
|
return mapper.convertValue(o, clazz);
|
193
|
}
|
194
|
|
195
|
private static OrganizationDetails getOrganization(final DatasourceDetails ds) {
|
196
|
if (ds.getOrganizations() != null && !ds.getOrganizations().isEmpty()) {
|
197
|
return ds.getOrganizations().stream().findFirst().get();
|
198
|
}
|
199
|
return null;
|
200
|
}
|
201
|
|
202
|
private static void addExtraField(final Element extraFields, final String field, final String value) {
|
203
|
final Element f = extraFields.addElement("FIELD");
|
204
|
f.addElement("key").setText(field);
|
205
|
f.addElement("value").setText(value != null ? value : "");
|
206
|
}
|
207
|
|
208
|
private static String asOpenaireId(final String id) {
|
209
|
final String prefix = StringUtils.substringBefore(id, ID_SEPARATOR);
|
210
|
final String md5 = StringUtils.substringAfter(id, ID_SEPARATOR);
|
211
|
|
212
|
return prefix + ID_SEPARATOR + AbstractDNetXsltFunctions.md5(md5);
|
213
|
}
|
214
|
|
215
|
|
216
|
public static void copyNonNullProperties(Object src, Object target) {
|
217
|
BeanUtils.copyProperties(src, target, getNullPropertyNames(src));
|
218
|
}
|
219
|
|
220
|
public static String[] getNullPropertyNames(Object source) {
|
221
|
final BeanWrapper src = new BeanWrapperImpl(source);
|
222
|
java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();
|
223
|
|
224
|
Set<String> emptyNames = new HashSet<>();
|
225
|
for(java.beans.PropertyDescriptor pd : pds) {
|
226
|
Object srcValue = src.getPropertyValue(pd.getName());
|
227
|
if (srcValue == null) emptyNames.add(pd.getName());
|
228
|
}
|
229
|
String[] result = new String[emptyNames.size()];
|
230
|
return emptyNames.toArray(result);
|
231
|
}
|
232
|
|
233
|
public static Map<String,String> asMapOfChanges(final DatasourceDetailsUpdate d) {
|
234
|
final Map<String, String> changes = Maps.newHashMap();
|
235
|
|
236
|
if (d.getContactemail() != null) {
|
237
|
changes.put(ADMIN_INFO, XmlEscapers.xmlContentEscaper().escape(d.getContactemail()));
|
238
|
}
|
239
|
if (d.getEnglishname() != null) {
|
240
|
changes.put(ENGLISH_NAME, XmlEscapers.xmlContentEscaper().escape(d.getEnglishname()));
|
241
|
}
|
242
|
if (d.getOfficialname() != null) {
|
243
|
changes.put(OFFICIAL_NAME, XmlEscapers.xmlContentEscaper().escape(d.getOfficialname()));
|
244
|
}
|
245
|
if (d.getTimezone() != null) {
|
246
|
changes.put(TIMEZONE, XmlEscapers.xmlContentEscaper().escape(d.getTimezone()));
|
247
|
}
|
248
|
if (d.getLatitude() != null) {
|
249
|
changes.put(LATITUDE, XmlEscapers.xmlContentEscaper().escape(String.valueOf(d.getLatitude())));
|
250
|
}
|
251
|
if (d.getLongitude() != null) {
|
252
|
changes.put(LONGITUDE, XmlEscapers.xmlContentEscaper().escape(String.valueOf(d.getLongitude())));
|
253
|
}
|
254
|
if (d.getPlatform() != null) {
|
255
|
changes.put(PLATFORM, XmlEscapers.xmlContentEscaper().escape(d.getPlatform()));
|
256
|
}
|
257
|
if (d.getTypology() != null) {
|
258
|
changes.put(TYPOLOGY, XmlEscapers.xmlContentEscaper().escape(d.getTypology()));
|
259
|
}
|
260
|
if (d.getWebsiteurl() != null) {
|
261
|
changes.put(WEBSITE_URL, XmlEscapers.xmlContentEscaper().escape(d.getWebsiteurl()));
|
262
|
}
|
263
|
return changes;
|
264
|
}
|
265
|
}
|