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