1
|
package eu.dnetlib.data.transform;
|
2
|
|
3
|
import java.io.*;
|
4
|
import java.util.*;
|
5
|
import java.util.Map.Entry;
|
6
|
import java.util.zip.GZIPInputStream;
|
7
|
import javax.xml.transform.TransformerConfigurationException;
|
8
|
import javax.xml.transform.TransformerFactoryConfigurationError;
|
9
|
|
10
|
import com.google.common.collect.Lists;
|
11
|
import com.google.common.collect.Maps;
|
12
|
import com.google.common.collect.Sets;
|
13
|
import com.google.protobuf.InvalidProtocolBufferException;
|
14
|
import eu.dnetlib.common.rmi.UnimplementedException;
|
15
|
import eu.dnetlib.data.mapreduce.hbase.index.config.*;
|
16
|
import eu.dnetlib.data.mapreduce.util.*;
|
17
|
import eu.dnetlib.data.proto.KindProtos.Kind;
|
18
|
import eu.dnetlib.data.proto.OafProtos.Oaf;
|
19
|
import eu.dnetlib.data.proto.TypeProtos.Type;
|
20
|
import eu.dnetlib.miscutils.functional.xml.IndentXmlString;
|
21
|
import org.apache.commons.io.IOUtils;
|
22
|
import org.apache.commons.lang.StringUtils;
|
23
|
import org.apache.commons.logging.Log;
|
24
|
import org.apache.commons.logging.LogFactory;
|
25
|
import org.dom4j.Document;
|
26
|
import org.dom4j.DocumentException;
|
27
|
import org.dom4j.io.SAXReader;
|
28
|
import org.json.JSONObject;
|
29
|
import org.junit.Before;
|
30
|
import org.junit.Test;
|
31
|
|
32
|
import static org.junit.Assert.*;
|
33
|
|
34
|
public class XsltRowTransformerFactoryTest {
|
35
|
|
36
|
private static final Log log = LogFactory.getLog(XsltRowTransformerFactoryTest.class);
|
37
|
private static String basePathProfiles = "/eu/dnetlib/test/profiles/TransformationRuleDSResources/TransformationRuleDSResourceType/";
|
38
|
private XsltRowTransformerFactory factory;
|
39
|
private EntityConfigTable entityConfigTable;
|
40
|
|
41
|
@Before
|
42
|
public void setUp() throws Exception {
|
43
|
factory = new XsltRowTransformerFactory();
|
44
|
entityConfigTable = IndexConfig.load(IndexConfigTest.config).getConfigMap();
|
45
|
}
|
46
|
|
47
|
@Test
|
48
|
public void testParseManyAuthors() throws Exception {
|
49
|
|
50
|
final Map<String, Object> xslParams = Maps.newHashMap();
|
51
|
|
52
|
final Map<String, String> m = Maps.newHashMap();
|
53
|
|
54
|
m.put("od______2367", "true"); // Puma
|
55
|
m.put("od______2294", "true"); // UNIBI
|
56
|
m.put("od________18", "false"); // Arxiv
|
57
|
|
58
|
xslParams.put("mergeIdForHomonymsMap", m);
|
59
|
|
60
|
final List<Row> rows = asRows(loadFromTransformationProfile("oaf_person2hbase.xml"), xslParams, load("recordManyAuthors.xml"));
|
61
|
int authorCount = 0;
|
62
|
for (final Row row : rows) {
|
63
|
for (final Column<String, byte[]> col : row.getColumns()) {
|
64
|
|
65
|
authorCount++;
|
66
|
final OafDecoder d = OafDecoder.decode(col.getValue());
|
67
|
assertNotNull(d);
|
68
|
|
69
|
log.debug(d.getEntity().getPerson().getCoauthorList().size());
|
70
|
}
|
71
|
}
|
72
|
log.info("authors' count: ---> " + authorCount);
|
73
|
|
74
|
}
|
75
|
|
76
|
@Test
|
77
|
public void testParseAuthors() throws Exception {
|
78
|
|
79
|
final Map<String, Object> xslParams = Maps.newHashMap();
|
80
|
|
81
|
final Map<String, String> m = Maps.newHashMap();
|
82
|
|
83
|
m.put("od______2367", "true"); // Puma
|
84
|
m.put("od______2294", "true"); // UNIBI
|
85
|
m.put("od________18", "false"); // Arxiv
|
86
|
|
87
|
xslParams.put("mergeIdForHomonymsMap", m);
|
88
|
|
89
|
final List<Row> rows = asRows(loadFromTransformationProfile("oaf_person2hbase.xml"), xslParams, load("recordArxiv.xml"));
|
90
|
|
91
|
for (final Row row : rows) {
|
92
|
for (final Column<String, byte[]> col : row.getColumns()) {
|
93
|
|
94
|
final OafDecoder d = OafDecoder.decode(col.getValue());
|
95
|
log.debug(d.getOaf());
|
96
|
}
|
97
|
}
|
98
|
}
|
99
|
|
100
|
@Test
|
101
|
public void testParseClaimUpdate() throws Exception {
|
102
|
|
103
|
final List<Row> rows = Lists.newArrayList();
|
104
|
rows.addAll(asRows(loadFromTransformationProfile("oaf2hbase.xml"), load("recordClaim.xml")));
|
105
|
rows.addAll(asRows(loadFromTransformationProfile("claimUpdates_2_hbase.xsl"), load("recordClaimUpdate.xml")));
|
106
|
|
107
|
printAll(mapAll(buildTable(rows)));
|
108
|
}
|
109
|
|
110
|
@Test
|
111
|
public void testParseDatasetPUB() throws Exception {
|
112
|
|
113
|
doTest(loadFromTransformationProfile("odf2hbase.xml"), load("recordDatasetPUB.xml"));
|
114
|
}
|
115
|
|
116
|
@Test
|
117
|
public void testParseClaim() throws Exception {
|
118
|
|
119
|
doTest(loadFromTransformationProfile("oaf2hbase.xml"), load("recordClaim.xml"));
|
120
|
}
|
121
|
|
122
|
@Test
|
123
|
public void testParseACM() throws Exception {
|
124
|
|
125
|
doTest(loadFromTransformationProfile("oaf2hbase.xml"), load("recordACM.xml"));
|
126
|
}
|
127
|
|
128
|
@Test
|
129
|
public void testParseASB() throws Exception {
|
130
|
|
131
|
doTest(loadFromTransformationProfile("oaf2hbase.xml"), load("recordASB.xml"));
|
132
|
}
|
133
|
|
134
|
@Test
|
135
|
public void testParseProjectCorda() throws Exception {
|
136
|
|
137
|
doTest(loadFromTransformationProfile("projects_2_hbase.xsl"), load("projectRecordCorda.xml"));
|
138
|
}
|
139
|
|
140
|
@Test
|
141
|
public void testParseProjectFCT() throws Exception {
|
142
|
|
143
|
doTest(loadFromTransformationProfile("projects_2_hbase.xsl"), load("projectRecordFCT.xml"));
|
144
|
}
|
145
|
|
146
|
@Test
|
147
|
public void testParseOaf() throws Exception {
|
148
|
|
149
|
doTest(loadFromTransformationProfile("oaf2hbase.xml"), load("record.xml"));
|
150
|
}
|
151
|
|
152
|
@Test
|
153
|
public void testParseOafPublication() throws Exception {
|
154
|
|
155
|
doTest(loadFromTransformationProfile("oaf_publication2hbase.xml"), load("record.xml"));
|
156
|
}
|
157
|
|
158
|
@Test
|
159
|
public void testParseDatacite() throws Exception {
|
160
|
|
161
|
doTest(loadFromTransformationProfile("odf2hbase.xml"), load("recordDatacite.xml"));
|
162
|
}
|
163
|
|
164
|
@Test
|
165
|
public void testParseDatacite2() throws Exception {
|
166
|
|
167
|
doTest(loadFromTransformationProfile("odf2hbase.xml"), load("recordDatacite2.xml"));
|
168
|
}
|
169
|
|
170
|
@Test
|
171
|
public void testLinkPangaea() throws Exception {
|
172
|
|
173
|
final List<Row> rows = Lists.newArrayList();
|
174
|
rows.addAll(asRows(loadFromTransformationProfile("odf2hbase.xml"), load("pangaeODF1.xml")));
|
175
|
rows.addAll(asRows(loadFromTransformationProfile("odf2hbase.xml"), load("pangaeODF2.xml")));
|
176
|
rows.addAll(asRows(loadFromTransformationProfile("oaf2hbase.xml"), load("pangaeOAF.xml")));
|
177
|
rows.addAll(asRows(loadFromTransformationProfile("projects_2_hbase.xsl"), load("projectRecordCordaPangaea.xml")));
|
178
|
|
179
|
printAll(mapAll(buildTable(rows)));
|
180
|
}
|
181
|
|
182
|
@Test
|
183
|
public void testLinkCorda() throws Exception {
|
184
|
|
185
|
final List<Row> rows = Lists.newArrayList();
|
186
|
rows.addAll(asRows(loadFromTransformationProfile("projects_2_hbase.xsl"), load("projectRecordCorda.xml")));
|
187
|
rows.addAll(asRows(loadFromTransformationProfile("oaf2hbase.xml"), load("recordCorda.xml")));
|
188
|
|
189
|
printAll(mapAll(buildTable(rows)));
|
190
|
}
|
191
|
|
192
|
@Test
|
193
|
public void testLinkFCT() throws Exception {
|
194
|
|
195
|
final List<Row> rows = Lists.newArrayList();
|
196
|
rows.addAll(asRows(loadFromTransformationProfile("projects_2_hbase.xsl"), load("projectRecordFCT.xml")));
|
197
|
rows.addAll(asRows(loadFromTransformationProfile("oaf2hbase.xml"), load("recordFCT.xml")));
|
198
|
|
199
|
printAll(mapAll(buildTable(rows)));
|
200
|
}
|
201
|
|
202
|
@Test
|
203
|
public void testLinkARC() throws Exception {
|
204
|
|
205
|
final List<Row> rows = Lists.newArrayList();
|
206
|
rows.addAll(asRows(loadFromTransformationProfile("projects_2_hbase.xsl"), load("projectRecordARC.xml")));
|
207
|
rows.addAll(asRows(loadFromTransformationProfile("oaf2hbase.xml"), load("recordARC.xml")));
|
208
|
|
209
|
printAll(mapAll(buildTable(rows)));
|
210
|
}
|
211
|
|
212
|
@Test
|
213
|
public void testLinkWT() throws Exception {
|
214
|
|
215
|
final List<Row> rows = Lists.newArrayList();
|
216
|
rows.addAll(asRows(loadFromTransformationProfile("projects_2_hbase.xsl"), load("projectRecordWT.xml")));
|
217
|
rows.addAll(asRows(loadFromTransformationProfile("oaf2hbase.xml"), load("recordWT.xml")));
|
218
|
|
219
|
printAll(mapAll(buildTable(rows)));
|
220
|
}
|
221
|
|
222
|
@Test
|
223
|
public void testLinkOrganization() throws Exception {
|
224
|
|
225
|
final List<Row> rows = Lists.newArrayList();
|
226
|
rows.addAll(asRows(loadFromTransformationProfile("organizations_2_hbase.xsl"), load("organization.xml")));
|
227
|
rows.addAll(asRows(loadFromTransformationProfile("projectorganization_2_hbase.xsl"), load("project_organization.xml")));
|
228
|
rows.addAll(asRows(loadFromTransformationProfile("projects_2_hbase.xsl"), load("projectRecordCorda.xml")));
|
229
|
|
230
|
printAll(mapAll(buildTable(rows)));
|
231
|
}
|
232
|
|
233
|
@Test
|
234
|
public void testUnpackAuthors() throws Exception {
|
235
|
|
236
|
final Map<String, Object> xslParams = Maps.newHashMap();
|
237
|
|
238
|
final Map<String, String> m = Maps.newHashMap();
|
239
|
|
240
|
m.put("od______2367", "true"); // Puma
|
241
|
m.put("od______2294", "true"); // UNIBI
|
242
|
m.put("od________18", "false"); // Arxiv
|
243
|
|
244
|
xslParams.put("mergeIdForHomonymsMap", m);
|
245
|
|
246
|
final List<Row> rows = Lists.newArrayList();
|
247
|
rows.addAll(asRows(loadFromTransformationProfile("oaf2hbase.xml"), xslParams, load("recordArxiv.xml")));
|
248
|
rows.addAll(asRows(loadFromTransformationProfile("oaf2hbase.xml"), xslParams, load("recordArxiv2.xml")));
|
249
|
rows.addAll(asRows(loadFromTransformationProfile("oaf2hbase.xml"), xslParams, load("recordPuma1.xml")));
|
250
|
rows.addAll(asRows(loadFromTransformationProfile("oaf2hbase.xml"), xslParams, load("recordPuma2.xml")));
|
251
|
rows.addAll(asRows(loadFromTransformationProfile("oaf2hbase.xml"), xslParams, load("recordUNIBI.xml")));
|
252
|
|
253
|
printPersonFullnames(mapAll(buildTable(rows)));
|
254
|
}
|
255
|
|
256
|
@Test
|
257
|
public void testParseDoajOAF() throws Exception {
|
258
|
|
259
|
doTest(loadFromTransformationProfile("oaf2hbase.xml"), load("doajUniversityRecord.xml"));
|
260
|
}
|
261
|
|
262
|
@Test
|
263
|
public void testParseDatasource() throws Exception {
|
264
|
|
265
|
doTest(loadFromTransformationProfile("datasources_2_hbase.xsl"), load("datasourceNative.xml"));
|
266
|
}
|
267
|
|
268
|
@Test
|
269
|
public void testParseDataDatasource() throws Exception {
|
270
|
|
271
|
doTest(loadFromTransformationProfile("datasources_2_hbase.xsl"), load("dataDatasource.xml"));
|
272
|
}
|
273
|
|
274
|
@Test
|
275
|
public void testFromMongodbCompressedDump() throws Exception {
|
276
|
doTestJsonGz(loadFromTransformationProfile("oaf2hbase.xml"), load("mdstore_cleaned.json.gz"));
|
277
|
}
|
278
|
|
279
|
@Test
|
280
|
public void testLoadFromTransformationProfile() throws IOException {
|
281
|
InputStream in = loadFromTransformationProfile("oaf2hbase.xml");
|
282
|
log.info(IOUtils.toString(in));
|
283
|
}
|
284
|
|
285
|
@Test
|
286
|
public void test_template() throws Exception {
|
287
|
final String xslt = IOUtils.toString(loadFromTransformationProfile("oaf2hbase.xml"));
|
288
|
final XsltRowTransformer transformer = factory.getTransformer(xslt);
|
289
|
assertNotNull(transformer);
|
290
|
|
291
|
final String record = IOUtils.toString(load("record.xml"));
|
292
|
final List<Row> rows = transformer.apply(record);
|
293
|
|
294
|
System.out.println(rows);
|
295
|
}
|
296
|
|
297
|
@Test
|
298
|
public void testWrongCharsOrganization() throws Exception {
|
299
|
final List<Row> rows = Lists.newArrayList();
|
300
|
rows.addAll(asRows(loadFromTransformationProfile("organizations_2_hbase.xsl"), load("organizationWrongChars.xml")));
|
301
|
printAll(mapAll(buildTable(rows)));
|
302
|
}
|
303
|
|
304
|
private void doTest(final InputStream xsltStream, final InputStream recordStream) throws Exception {
|
305
|
try {
|
306
|
final List<Row> rows = asRows(xsltStream, recordStream);
|
307
|
|
308
|
log.info(rows);
|
309
|
|
310
|
final Map<String, Map<String, Map<String, byte[]>>> table = buildTable(rows);
|
311
|
|
312
|
// System.out.println("\n" + table.toString());
|
313
|
|
314
|
final Map<String, XmlRecordFactory> builders = mapAll(table);
|
315
|
|
316
|
printAll(builders);
|
317
|
} catch (final InvalidProtocolBufferException e) {
|
318
|
throw new Exception(e);
|
319
|
} catch (final TransformerConfigurationException e) {
|
320
|
throw new Exception(e);
|
321
|
} catch (final TransformerFactoryConfigurationError e) {
|
322
|
throw new Exception(e);
|
323
|
} catch (final DocumentException e) {
|
324
|
throw new Exception(e);
|
325
|
}
|
326
|
}
|
327
|
|
328
|
private void doTestJsonGz(final InputStream xsltStream, final InputStream recordStream) throws Exception {
|
329
|
|
330
|
final Iterator<List<Row>> rowsIterator = asRowsJsonGzip(xsltStream, recordStream);
|
331
|
|
332
|
int i = 0;
|
333
|
while (rowsIterator.hasNext()) {
|
334
|
final List<Row> rows = rowsIterator.next();
|
335
|
i++;
|
336
|
|
337
|
if ((i % 10000) == 0) {
|
338
|
System.out.println(i);
|
339
|
}
|
340
|
|
341
|
final Map<String, Map<String, Map<String, byte[]>>> table = buildTableDoaj(rows);
|
342
|
|
343
|
for (final Map<String, Map<String, byte[]>> m : table.values()) {
|
344
|
for (final Map<String, byte[]> mv : m.values()) {
|
345
|
for (final byte[] v : mv.values()) {
|
346
|
final OafDecoder d = OafDecoder.decode(v);
|
347
|
assertNotNull(d);
|
348
|
assertNotNull(d.getOaf());
|
349
|
|
350
|
switch (d.getKind()) {
|
351
|
case entity:
|
352
|
assertNotNull(d.getMetadata());
|
353
|
if (d.getOaf().getEntity().getType().equals(Type.result)) {
|
354
|
System.out.println(d.getOaf());
|
355
|
}
|
356
|
break;
|
357
|
case relation:
|
358
|
assertNotNull(d.getRel());
|
359
|
break;
|
360
|
default:
|
361
|
break;
|
362
|
}
|
363
|
}
|
364
|
}
|
365
|
}
|
366
|
}
|
367
|
}
|
368
|
|
369
|
private List<Row> asRows(final InputStream xsltStream, final InputStream recordStream) throws Exception {
|
370
|
return asRows(xsltStream, new HashMap<String, Object>(), recordStream);
|
371
|
}
|
372
|
|
373
|
private List<Row> asRows(final InputStream xsltStream, final Map<String, Object> params, final InputStream recordStream) throws Exception {
|
374
|
final String xslt = IOUtils.toString(xsltStream);
|
375
|
final XsltRowTransformer transformer = factory.getTransformer(xslt, params);
|
376
|
assertNotNull(transformer);
|
377
|
|
378
|
final String record = IOUtils.toString(recordStream);
|
379
|
final List<Row> rows = transformer.apply(record);
|
380
|
|
381
|
assertNotNull(rows);
|
382
|
assertFalse(rows.isEmpty());
|
383
|
return rows;
|
384
|
}
|
385
|
|
386
|
private Iterator<List<Row>> asRowsJsonGzip(final InputStream xsltStream, final InputStream recordStreamJsonGzip) throws Exception {
|
387
|
final String xslt = IOUtils.toString(xsltStream);
|
388
|
final XsltRowTransformer transformer = factory.getTransformer(xslt);
|
389
|
assertNotNull(transformer);
|
390
|
assertNotNull(recordStreamJsonGzip);
|
391
|
|
392
|
final GZIPInputStream stream = new GZIPInputStream(recordStreamJsonGzip);
|
393
|
assertNotNull(stream);
|
394
|
final BufferedReader inStream = new BufferedReader(new InputStreamReader(stream));
|
395
|
assertNotNull(inStream);
|
396
|
return new Iterator<List<Row>>() {
|
397
|
|
398
|
String jsonRecord = null;
|
399
|
|
400
|
@Override
|
401
|
public boolean hasNext() {
|
402
|
try {
|
403
|
return (jsonRecord = inStream.readLine()) != null;
|
404
|
} catch (final IOException e) {
|
405
|
throw new RuntimeException(e);
|
406
|
}
|
407
|
}
|
408
|
|
409
|
@Override
|
410
|
public List<Row> next() {
|
411
|
|
412
|
final JSONObject jsonObj = new JSONObject(jsonRecord);
|
413
|
final String body = jsonObj.getString("body");
|
414
|
try {
|
415
|
assertTrue(StringUtils.isNotBlank(body));
|
416
|
// System.out.println(body);
|
417
|
final List<Row> rows = transformer.apply(body);
|
418
|
assertNotNull(rows);
|
419
|
assertFalse(rows.isEmpty());
|
420
|
return rows;
|
421
|
} catch (final Throwable e) {
|
422
|
System.err.println("error transforming document: " + body);
|
423
|
throw new RuntimeException(e);
|
424
|
}
|
425
|
}
|
426
|
|
427
|
@Override
|
428
|
public void remove() {
|
429
|
throw new UnimplementedException();
|
430
|
}
|
431
|
|
432
|
};
|
433
|
|
434
|
}
|
435
|
|
436
|
private Map<String, Map<String, Map<String, byte[]>>> buildTableDoaj(final List<Row> rows) throws UnsupportedEncodingException {
|
437
|
final Map<String, Map<String, Map<String, byte[]>>> table = Maps.newHashMap();
|
438
|
|
439
|
for (final Row row : rows) {
|
440
|
final String rowKey = row.getKey();
|
441
|
final String cf = row.getColumnFamily();
|
442
|
if (!table.containsKey(rowKey)) {
|
443
|
table.put(rowKey, new HashMap<String, Map<String, byte[]>>());
|
444
|
}
|
445
|
if (!table.get(rowKey).containsKey(cf)) {
|
446
|
table.get(rowKey).put(row.getColumnFamily(), new HashMap<String, byte[]>());
|
447
|
}
|
448
|
for (final Column<String, byte[]> c : row.getColumns()) {
|
449
|
// System.out.println(String.format("ADDING K:%s CF:%s Q:%s", rowKey, cf, c.getName()));
|
450
|
table.get(rowKey).get(cf).put(c.getName(), c.getValue());
|
451
|
if (cf.equals("result") && c.getName().equals("body")) {
|
452
|
// System.out.println(String.format("ADDING K:%s CF:%s Q:%s", rowKey, cf, c.getName()));
|
453
|
assertTrue(StringUtils.isNotBlank(new String(c.getValue(), "UTF-8")));
|
454
|
}
|
455
|
}
|
456
|
}
|
457
|
return table;
|
458
|
|
459
|
}
|
460
|
|
461
|
private Map<String, Map<String, Map<String, byte[]>>> buildTable(final List<Row> rows) throws UnsupportedEncodingException {
|
462
|
final Map<String, Map<String, Map<String, byte[]>>> table = Maps.newHashMap();
|
463
|
|
464
|
for (final Row row : rows) {
|
465
|
final String rowKey = row.getKey();
|
466
|
final String cf = row.getColumnFamily();
|
467
|
if (!table.containsKey(rowKey)) {
|
468
|
table.put(rowKey, new HashMap<String, Map<String, byte[]>>());
|
469
|
}
|
470
|
if (!table.get(rowKey).containsKey(cf)) {
|
471
|
table.get(rowKey).put(row.getColumnFamily(), new HashMap<String, byte[]>());
|
472
|
}
|
473
|
for (final Column<String, byte[]> c : row.getColumns()) {
|
474
|
System.out.println(String.format("ADDING K:%s CF:%s Q:%s", rowKey, cf, c.getName()));
|
475
|
table.get(rowKey).get(cf).put(c.getName(), c.getValue());
|
476
|
if (c.getName().equals("body")) {
|
477
|
final String theBody = new String(c.getValue(), "UTF-8");
|
478
|
assertTrue(StringUtils.isNotBlank(theBody));
|
479
|
// System.out.println(theBody);
|
480
|
}
|
481
|
}
|
482
|
}
|
483
|
return table;
|
484
|
|
485
|
}
|
486
|
|
487
|
private Map<String, XmlRecordFactory> mapAll(final Map<String, Map<String, Map<String, byte[]>>> table) throws Exception {
|
488
|
|
489
|
final Map<String, XmlRecordFactory> builders = Maps.newHashMap();
|
490
|
for (final Entry<String, Map<String, Map<String, byte[]>>> e : table.entrySet()) {
|
491
|
map(builders, e.getKey(), e.getValue());
|
492
|
}
|
493
|
return builders;
|
494
|
}
|
495
|
|
496
|
// private Map<String, XmlRecordFactory> mapResultsOnly(final Map<String, Map<String, Map<String, byte[]>>> table) throws Exception {
|
497
|
//
|
498
|
// final Map<String, XmlRecordFactory> builders = Maps.newHashMap();
|
499
|
// for (final Entry<String, Map<String, Map<String, byte[]>>> e : table.entrySet()) {
|
500
|
// final Type type = OafRowKeyDecoder.decode(e.getKey()).getType();
|
501
|
// if (type == Type.result) {
|
502
|
// map(builders, e.getKey(), e.getValue());
|
503
|
// }
|
504
|
// }
|
505
|
// return builders;
|
506
|
// }
|
507
|
|
508
|
private void map(final Map<String, XmlRecordFactory> builders, final String rowKey, final Map<String, Map<String, byte[]>> row) throws Exception {
|
509
|
|
510
|
final Type type = OafRowKeyDecoder.decode(rowKey).getType();
|
511
|
|
512
|
final Map<String, byte[]> familyMap = row.get(type.toString());
|
513
|
|
514
|
if (familyMap == null) return;
|
515
|
|
516
|
final byte[] bodyB = familyMap.get("body");
|
517
|
|
518
|
if (bodyB != null) {
|
519
|
ensureBuilder(builders, rowKey);
|
520
|
|
521
|
final Oaf oaf = UpdateMerger.mergeBodyUpdates(familyMap);
|
522
|
|
523
|
final OafDecoder mainEntity = OafDecoder.decode(oaf);
|
524
|
|
525
|
builders.get(rowKey).setMainEntity(mainEntity);
|
526
|
|
527
|
for (final LinkDescriptor ld : entityConfigTable.getDescriptors(type)) {
|
528
|
|
529
|
final String it = ld.getRelDescriptor().getIt();
|
530
|
final Map<String, byte[]> cols = row.get(it);
|
531
|
|
532
|
if ((cols != null) && !cols.isEmpty()) {
|
533
|
|
534
|
for (final byte[] oafB : cols.values()) {
|
535
|
|
536
|
final Oaf.Builder relBuilder = Oaf.newBuilder(Oaf.parseFrom(oafB));
|
537
|
|
538
|
if (ld.isSymmetric()) {
|
539
|
final RelDescriptor rd = ld.getRelDescriptor();
|
540
|
|
541
|
relBuilder.getRelBuilder().setCachedTarget(mainEntity.getEntity()).setRelType(rd.getRelType()).setSubRelType(rd.getSubRelType());
|
542
|
}
|
543
|
|
544
|
relBuilder.getRelBuilder().setChild(ld.isChild());
|
545
|
|
546
|
final Oaf.Builder oafBuilder = Oaf.newBuilder().setKind(Kind.relation).setLastupdatetimestamp(System.currentTimeMillis());
|
547
|
oafBuilder.mergeFrom(relBuilder.build());
|
548
|
|
549
|
final String targetId = ld.isSymmetric() ? oafBuilder.getRel().getTarget() : oafBuilder.getRel().getSource();
|
550
|
ensureBuilder(builders, targetId);
|
551
|
final OafDecoder decoder = OafDecoder.decode(oafBuilder.build());
|
552
|
|
553
|
if (ld.isChild()) {
|
554
|
builders.get(targetId).addChild(type, decoder);
|
555
|
} else {
|
556
|
builders.get(targetId).addRelation(type, decoder);
|
557
|
}
|
558
|
}
|
559
|
|
560
|
}
|
561
|
}
|
562
|
}
|
563
|
|
564
|
}
|
565
|
|
566
|
private void printAll(final Map<String, XmlRecordFactory> builders) throws DocumentException {
|
567
|
print(Sets.newHashSet(Type.values()), builders, null);
|
568
|
}
|
569
|
|
570
|
private void printPerson(final Map<String, XmlRecordFactory> builders) throws DocumentException {
|
571
|
print(Sets.newHashSet(Type.person), builders, null);
|
572
|
}
|
573
|
|
574
|
private void printPersonFullnames(final Map<String, XmlRecordFactory> builders) throws DocumentException {
|
575
|
|
576
|
final Map<Type, Set<String>> xpaths = Maps.newHashMap();
|
577
|
|
578
|
final Set<String> personPaths = Sets.newHashSet();
|
579
|
|
580
|
personPaths.add("//fullname");
|
581
|
|
582
|
xpaths.put(Type.person, personPaths);
|
583
|
|
584
|
print(Sets.newHashSet(Type.person), builders, xpaths);
|
585
|
}
|
586
|
|
587
|
private void print(final Set<Type> types, final Map<String, XmlRecordFactory> builders, final Map<Type, Set<String>> xpaths) throws DocumentException {
|
588
|
final SAXReader r = new SAXReader();
|
589
|
|
590
|
for (final Entry<String, XmlRecordFactory> e : builders.entrySet()) {
|
591
|
final OafRowKeyDecoder kd = OafRowKeyDecoder.decode(e.getKey());
|
592
|
|
593
|
if (!e.getValue().isValid()) throw new IllegalArgumentException("invalid builder: " + e.getKey());
|
594
|
if (types.contains(kd.getType())) {
|
595
|
final String val = IndentXmlString.apply(e.getValue().build());
|
596
|
|
597
|
if ((xpaths != null) && !xpaths.isEmpty() && (xpaths.get(kd.getType()) != null)) {
|
598
|
final Document doc = r.read(new StringReader(val));
|
599
|
|
600
|
log.debug("\n" + e.getKey());
|
601
|
for (final String xpath : xpaths.get(kd.getType())) {
|
602
|
log.debug(doc.valueOf(xpath));
|
603
|
}
|
604
|
} else {
|
605
|
|
606
|
log.info(val);
|
607
|
}
|
608
|
}
|
609
|
}
|
610
|
}
|
611
|
|
612
|
private void printNoIndent(final Map<String, XmlRecordFactory> builders) {
|
613
|
for (final Entry<String, XmlRecordFactory> e : builders.entrySet()) {
|
614
|
if (e.getValue().isValid()) {
|
615
|
log.debug(e.getValue().build());
|
616
|
} else {
|
617
|
log.debug("invalid builder: " + e.getKey());
|
618
|
}
|
619
|
}
|
620
|
}
|
621
|
|
622
|
private void ensureBuilder(final Map<String, XmlRecordFactory> builders, final String rowKey) throws Exception {
|
623
|
if (!builders.containsKey(rowKey)) {
|
624
|
builders.put(rowKey, newBuilder());
|
625
|
}
|
626
|
}
|
627
|
|
628
|
private XmlRecordFactory newBuilder() throws TransformerConfigurationException, TransformerFactoryConfigurationError, DocumentException {
|
629
|
return new XmlRecordFactory(entityConfigTable, ContextMapper.fromXml(eu.dnetlib.data.mapreduce.hbase.index.config.Context.xml),
|
630
|
RelClasses.fromJSon(RelClassesTest.relClassesJson), XmlRecordFactoryTest.SCHEMA_LOCATION, true, true, false);
|
631
|
}
|
632
|
|
633
|
private InputStream load(final String fileName) {
|
634
|
return getClass().getResourceAsStream(fileName);
|
635
|
}
|
636
|
|
637
|
private InputStream loadFromTransformationProfile(final String profilePath) {
|
638
|
log.info("Loading xslt from: " + basePathProfiles + profilePath);
|
639
|
InputStream profile = getClass().getResourceAsStream(basePathProfiles + profilePath);
|
640
|
SAXReader saxReader = new SAXReader();
|
641
|
Document doc = null;
|
642
|
try {
|
643
|
doc = saxReader.read(profile);
|
644
|
} catch (DocumentException e) {
|
645
|
e.printStackTrace();
|
646
|
throw new RuntimeException(e);
|
647
|
}
|
648
|
String xslt = doc.selectSingleNode("//SCRIPT/CODE/*[local-name()='stylesheet']").asXML();
|
649
|
return IOUtils.toInputStream(xslt);
|
650
|
}
|
651
|
|
652
|
}
|