|
56 | 56 |
|
57 | 57 | public class JSONImporterFormat implements FormatImporter { |
58 | 58 | static class CascadingProperties { |
59 | | - CascadingProperties parent; |
60 | | - Map<String, Object> map; |
| 59 | + final CascadingProperties parent; |
| 60 | + final Map<String, Object> map; |
61 | 61 |
|
62 | | - public CascadingProperties(CascadingProperties parent, Map<String, Object> map) { |
| 62 | + public CascadingProperties(final CascadingProperties parent, final Map<String, Object> map) { |
63 | 63 | this.parent = parent; |
64 | 64 | this.map = map; |
65 | 65 | } |
66 | 66 | } |
67 | 67 |
|
68 | 68 | @Override |
69 | | - public void load(final SourceSchema sourceSchema, final AnalyzedEntity.ENTITY_TYPE entityType, final Parser parser, final DatabaseInternal database, |
| 69 | + public void load(final SourceSchema sourceSchema, final AnalyzedEntity.ENTITY_TYPE entityType, final Parser parser, |
| 70 | + final DatabaseInternal database, |
70 | 71 | final ImporterContext context, final ImporterSettings settings) throws IOException { |
71 | 72 |
|
72 | 73 | final JSONObject mapping = settings.mapping != null ? new JSONObject(settings.mapping) : null; |
@@ -119,7 +120,8 @@ public String getFormat() { |
119 | 120 | return "JSON"; |
120 | 121 | } |
121 | 122 |
|
122 | | - private void parseRecords(final JsonReader reader, final Database database, final ImporterSettings settings, final ImporterContext context, |
| 123 | + private void parseRecords(final JsonReader reader, final Database database, final ImporterSettings settings, |
| 124 | + final ImporterContext context, |
123 | 125 | final JSONArray mapping, boolean ignore) throws IOException { |
124 | 126 | reader.beginArray(); |
125 | 127 |
|
@@ -148,13 +150,15 @@ private void parseRecords(final JsonReader reader, final Database database, fina |
148 | 150 | reader.endArray(); |
149 | 151 | } |
150 | 152 |
|
151 | | - private static MutableDocument saveAnonymousRecord(final Database database, final ImporterSettings settings, final Map<String, Object> map) { |
| 153 | + private static MutableDocument saveAnonymousRecord(final Database database, final ImporterSettings settings, |
| 154 | + final Map<String, Object> map) { |
152 | 155 | // NO MAPPING, SAVE THE RECORD AS A DOCUMENT |
153 | 156 | database.getSchema().getOrCreateDocumentType(settings.documentTypeName); |
154 | 157 | return database.newDocument(settings.documentTypeName).set(map).save(); |
155 | 158 | } |
156 | 159 |
|
157 | | - private Object parseRecord(final JsonReader reader, final ImporterSettings settings, final ImporterContext context, final Database database, |
| 160 | + private Object parseRecord(final JsonReader reader, final ImporterSettings settings, final ImporterContext context, |
| 161 | + final Database database, |
158 | 162 | final JSONObject mapping, final boolean ignore) throws IOException { |
159 | 163 | final CascadingProperties attributes = ignore ? null : new CascadingProperties(null, new LinkedHashMap<>()); |
160 | 164 |
|
@@ -250,20 +254,23 @@ private Object getAttribute(final CascadingProperties properties, final String n |
250 | 254 | return properties.map.get(name); |
251 | 255 | } |
252 | 256 |
|
253 | | - private Document createRecord(final Database database, final ImporterContext context, final CascadingProperties attributes, final JSONObject mapping, |
| 257 | + private Document createRecord(final Database database, final ImporterContext context, final CascadingProperties attributes, |
| 258 | + final JSONObject mapping, |
254 | 259 | final ImporterSettings settings) { |
255 | 260 | if (mapping == null) |
256 | 261 | return null; |
257 | 262 | //return saveAnonymousRecord(database, settings, (Map<String, Object>) attributes.map); |
258 | 263 |
|
259 | 264 | if (!mapping.has("@cat")) { |
260 | | - LogManager.instance().log(this, Level.WARNING, "No @cat tag defined in mapping object. The following object will be skipped %s", attributes); |
| 265 | + LogManager.instance() |
| 266 | + .log(this, Level.WARNING, "No @cat tag defined in mapping object. The following object will be skipped %s", attributes); |
261 | 267 | context.errors.incrementAndGet(); |
262 | 268 | return null; |
263 | 269 | } |
264 | 270 |
|
265 | 271 | if (!mapping.has("@type")) { |
266 | | - LogManager.instance().log(this, Level.WARNING, "No @type tag defined in mapping object. The following object will be skipped %s", attributes); |
| 272 | + LogManager.instance() |
| 273 | + .log(this, Level.WARNING, "No @type tag defined in mapping object. The following object will be skipped %s", attributes); |
267 | 274 | context.errors.incrementAndGet(); |
268 | 275 | return null; |
269 | 276 | } |
@@ -314,7 +321,8 @@ private Document createRecord(final Database database, final ImporterContext con |
314 | 321 | if (prop == null) { |
315 | 322 | if (idValue == null) { |
316 | 323 | // NO ID FOUND, SKIP THE RECORD |
317 | | - LogManager.instance().log(this, Level.WARNING, "@id property not found on current record, skipping record: %s", attributes); |
| 324 | + LogManager.instance() |
| 325 | + .log(this, Level.WARNING, "@id property not found on current record, skipping record: %s", attributes); |
318 | 326 | context.errors.incrementAndGet(); |
319 | 327 | return null; |
320 | 328 | } |
@@ -362,7 +370,8 @@ record = database.newDocument(typeName); |
362 | 370 | return record; |
363 | 371 | } |
364 | 372 |
|
365 | | - private void applyMappingRules(final Database database, final ImporterContext context, final MutableDocument record, final CascadingProperties attributes, |
| 373 | + private void applyMappingRules(final Database database, final ImporterContext context, final MutableDocument record, |
| 374 | + final CascadingProperties attributes, |
366 | 375 | final JSONObject mapping, final ImporterSettings settings) { |
367 | 376 | resolveProperties(mapping, attributes); |
368 | 377 |
|
@@ -390,7 +399,8 @@ private void applyMappingRules(final Database database, final ImporterContext co |
390 | 399 | } else if (mappingValue instanceof JSONArray) { |
391 | 400 | if (!(attributeValue instanceof Collection)) { |
392 | 401 | LogManager.instance() |
393 | | - .log(this, Level.WARNING, "Defined an array on mapping for property '%s' but found the object of class %s as attribute", mappingName, |
| 402 | + .log(this, Level.WARNING, |
| 403 | + "Defined an array on mapping for property '%s' but found the object of class %s as attribute", mappingName, |
394 | 404 | attributeValue.getClass()); |
395 | 405 | context.errors.incrementAndGet(); |
396 | 406 | continue; |
@@ -411,7 +421,8 @@ private void applyMappingRules(final Database database, final ImporterContext co |
411 | 421 | } |
412 | 422 | } |
413 | 423 |
|
414 | | - private List<Object> parseArray(final JsonReader reader, final ImporterSettings settings, final ImporterContext context, final Database database, |
| 424 | + private List<Object> parseArray(final JsonReader reader, final ImporterSettings settings, final ImporterContext context, |
| 425 | + final Database database, |
415 | 426 | final JSONArray mapping, boolean ignore) throws IOException { |
416 | 427 | final List<Object> list = ignore ? null : new ArrayList<>(); |
417 | 428 | reader.beginArray(); |
@@ -455,7 +466,8 @@ private List<Object> parseArray(final JsonReader reader, final ImporterSettings |
455 | 466 | return list; |
456 | 467 | } |
457 | 468 |
|
458 | | - private Object convertMap(final Database database, final ImporterContext context, final MutableDocument record, final Object value, final Object mapping, |
| 469 | + private Object convertMap(final Database database, final ImporterContext context, final MutableDocument record, |
| 470 | + final Object value, final Object mapping, |
459 | 471 | final CascadingProperties attributes, final ImporterSettings settings) { |
460 | 472 | if (mapping instanceof JSONObject) { |
461 | 473 | final JSONObject mappingObject = (JSONObject) mapping; |
@@ -464,24 +476,25 @@ private Object convertMap(final Database database, final ImporterContext context |
464 | 476 | if (value instanceof Map) |
465 | 477 | // CONVERT EMBEDDED MAP INTO A RECORD |
466 | 478 | attributeMap = new LinkedHashMap<>((Map<String, Object>) value); |
467 | | - else { |
| 479 | + else |
468 | 480 | // TREAT THE VALUE AS ID |
469 | 481 | attributeMap = new LinkedHashMap<>(); |
470 | | - } |
471 | 482 |
|
472 | 483 | final String subCategory = mappingObject.has("@cat") ? mappingObject.getString("@cat") : null; |
473 | 484 | final String subTypeName = mappingObject.has("@type") ? mappingObject.getString("@type") : null; |
474 | 485 |
|
475 | 486 | if ("e".equals(subCategory)) { |
476 | 487 | // TRANSFORM INTO AN EDGE |
477 | 488 | if (subTypeName == null) { |
478 | | - LogManager.instance().log(this, Level.WARNING, "Cannot convert object into an edge because the edge @type is not defined"); |
| 489 | + LogManager.instance() |
| 490 | + .log(this, Level.WARNING, "Cannot convert object into an edge because the edge @type is not defined"); |
479 | 491 | context.errors.incrementAndGet(); |
480 | 492 | return null; |
481 | 493 | } |
482 | 494 |
|
483 | 495 | if (!(record instanceof Vertex)) { |
484 | | - LogManager.instance().log(this, Level.WARNING, "Cannot convert object into an edge because the root record is not a vertex"); |
| 496 | + LogManager.instance() |
| 497 | + .log(this, Level.WARNING, "Cannot convert object into an edge because the root record is not a vertex"); |
485 | 498 | context.errors.incrementAndGet(); |
486 | 499 | return null; |
487 | 500 | } |
@@ -497,15 +510,18 @@ private Object convertMap(final Database database, final ImporterContext context |
497 | 510 | destVertexItem = attributeMap.get(inVertex); |
498 | 511 | } else if (inValue instanceof JSONObject) { |
499 | 512 | destVertexMappingObject = (JSONObject) inValue; |
| 513 | + attributeMap.put((String) destVertexMappingObject.get("@id"), value); |
500 | 514 | destVertexItem = attributeMap; |
501 | 515 | } else { |
502 | 516 | LogManager.instance() |
503 | | - .log(this, Level.WARNING, "Cannot convert object into an edge because the destination vertx @in type is not supported: " + inValue); |
| 517 | + .log(this, Level.WARNING, |
| 518 | + "Cannot convert object into an edge because the destination vertx @in type is not supported: " + inValue); |
504 | 519 | context.errors.incrementAndGet(); |
505 | 520 | return null; |
506 | 521 | } |
507 | 522 | } else { |
508 | | - LogManager.instance().log(this, Level.WARNING, "Cannot convert object into an edge because the destination vertx @in is not defined"); |
| 523 | + LogManager.instance() |
| 524 | + .log(this, Level.WARNING, "Cannot convert object into an edge because the destination vertx @in is not defined"); |
509 | 525 | context.errors.incrementAndGet(); |
510 | 526 | return null; |
511 | 527 | } |
@@ -514,7 +530,8 @@ private Object convertMap(final Database database, final ImporterContext context |
514 | 530 | if (destVertexItem instanceof Document) |
515 | 531 | destVertex = (MutableVertex) destVertexItem; |
516 | 532 | else if (destVertexItem instanceof Map) { |
517 | | - destVertex = (MutableVertex) createRecord(record.getDatabase(), context, new CascadingProperties(attributes, (Map<String, Object>) destVertexItem), |
| 533 | + destVertex = (MutableVertex) createRecord(record.getDatabase(), context, |
| 534 | + new CascadingProperties(attributes, (Map<String, Object>) destVertexItem), |
518 | 535 | destVertexMappingObject, settings); |
519 | 536 | if (destVertex == null) { |
520 | 537 | LogManager.instance().log(this, Level.WARNING, "Cannot convert inner map into destination vertex: %s", destVertexItem); |
|