return nodeKey != null ? nodeKey.getIdentifier() : null;
}
private EditableDocument replaceConnectorIdsWithNodeKeys( Document externalDocument,
String sourceName ) {
DocumentReader reader = new FederatedDocumentReader(translator(), externalDocument);
DocumentWriter writer = new FederatedDocumentWriter(translator(), externalDocument);
// replace document id with node key
String externalDocumentId = reader.getDocumentId();
String externalDocumentKey = null;
if (!StringUtil.isBlank(externalDocumentId)) {
externalDocumentKey = documentIdToNodeKeyString(sourceName, externalDocumentId);
writer.setId(externalDocumentKey);
}
// replace the id of each parent and add the optional federated parent
List<String> parentKeys = new ArrayList<String>();
for (String parentId : reader.getParentIds()) {
String parentKey = documentIdToNodeKeyString(sourceName, parentId);
parentKeys.add(parentKey);
}
// replace the id of each block (if they exist)
EditableDocument childrenInfo = writer.document().getDocument(DocumentTranslator.CHILDREN_INFO);
if (childrenInfo != null) {
String nextBlockKey = childrenInfo.getString(DocumentTranslator.NEXT_BLOCK);
if (!StringUtil.isBlank(nextBlockKey)) {
childrenInfo.setString(DocumentTranslator.NEXT_BLOCK, documentIdToNodeKeyString(sourceName, nextBlockKey));
}
String lastBlockKey = childrenInfo.getString(DocumentTranslator.LAST_BLOCK);
if (!StringUtil.isBlank(lastBlockKey)) {
childrenInfo.setString(DocumentTranslator.LAST_BLOCK, documentIdToNodeKeyString(sourceName, lastBlockKey));
}
}
// create the federated node key - external project back reference
if (externalDocumentKey != null) {
String projectedNodeKey = connectors.getProjectedNodeKey(externalDocumentKey);
if (!StringUtil.isBlank(projectedNodeKey)) {
parentKeys.add(projectedNodeKey);
}
}
writer.setParents(parentKeys);
// process each child in the same way
List<Document> updatedChildren = new ArrayList<Document>();
for (Document child : reader.getChildren()) {
EditableDocument childWithReplacedIds = replaceConnectorIdsWithNodeKeys(child, sourceName);
updatedChildren.add(childWithReplacedIds);
}
writer.setChildren(updatedChildren);
// process the properties to look for **INTERNAL** references ...
for (Property property : reader.getProperties().values()) {
if (property.isEmpty()) continue;
if (property.isReference()) {
if (property.isSingle()) {
Object value = convertReferenceValue(property.getFirstValue(), sourceName);
writer.addProperty(property.getName(), value);