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