use of org.bitbucket.cowwoc.diffmatchpatch.DiffMatchPatch in project structr by structr.
the class PatchCommand method processMessage.
@Override
public void processMessage(final WebSocketMessage webSocketData) {
final PropertyKey<String> contentKey = StructrApp.key(Content.class, "content");
final AbstractNode node = getNode(webSocketData.getId());
Map<String, Object> properties = webSocketData.getNodeData();
String patch = (String) properties.get("patch");
if (node != null) {
final DiffMatchPatch dmp = new DiffMatchPatch();
final String oldText = node.getProperty(contentKey);
final LinkedList<Patch> patches = new LinkedList<>(dmp.patchFromText(patch));
final Object[] results = dmp.patchApply(patches, oldText);
try {
node.setProperty(contentKey, results[0].toString());
TransactionCommand.registerNodeCallback(node, callback);
} catch (Throwable t) {
logger.warn("Could not apply patch {}", patch);
getWebSocket().send(MessageBuilder.status().code(400).message("Could not apply patch. " + t.getMessage()).build(), true);
}
} else {
logger.warn("Node with uuid {} not found.", webSocketData.getId());
getWebSocket().send(MessageBuilder.status().code(404).message("Node with uuid " + webSocketData.getId() + " not found.").build(), true);
}
}
Aggregations