EditableDocument original = config.get().edit(); EditableDocument added = updatedConfigWithIndexes.edit(); original.merge(added); RepositoryConfiguration updatedConfig = new RepositoryConfiguration(original, config.get().getName()); log.debug("Original ModeShape configuration: {}", config.toString());
@Test public void shouldMergeTwoDocuments() throws Exception { Document doc1 = Json.read(stream("json/merge-1.json")); Document doc2 = Json.read(stream("json/merge-2.json")); Document doc3 = Json.read(stream("json/merge-3.json")); EditableDocument editor = new DocumentEditor((MutableDocument)doc1); assertThat(Json.writePretty(editor).equals(Json.writePretty(doc3)), is(false)); editor.merge(doc2); assertThat(Json.writePretty(editor).equals(Json.writePretty(doc3)), is(true)); } }
@Override public void merge( Document other ) { if (other == this) return; for (Field field : other.fields()) { Document otherDoc = field.getValueAsDocument(); if (!Null.matches(otherDoc)) { // Get the corresponding value in this document ... EditableDocument thisField = getDocument(field.getName()); if (!Null.matches(thisField)) { // There are docs in both sides, so merge them ... thisField.merge(otherDoc); } else { // There is not a document on this side (perhaps another value), so replace with that other doc ... doSetValue(field.getName(), otherDoc); } } else { // The field is something other than a document, so just set it on this document ... doSetValue(field.getName(), field.getValue()); } } }
@Override public void merge( Document other ) { if (other == this) return; for (Field field : other.fields()) { Document otherDoc = field.getValueAsDocument(); if (!Null.matches(otherDoc)) { // Get the corresponding value in this document ... EditableDocument thisField = getDocument(field.getName()); if (!Null.matches(thisField)) { // There are docs in both sides, so merge them ... thisField.merge(otherDoc); } else { // There is not a document on this side (perhaps another value), so replace with that other doc ... doSetValue(field.getName(), otherDoc); } } else { // The field is something other than a document, so just set it on this document ... doSetValue(field.getName(), field.getValue()); } } }