Search in sources :

Example 1 with DiffMatchPatch

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);
    }
}
Also used : AbstractNode(org.structr.core.entity.AbstractNode) DiffMatchPatch(org.bitbucket.cowwoc.diffmatchpatch.DiffMatchPatch) DiffMatchPatch(org.bitbucket.cowwoc.diffmatchpatch.DiffMatchPatch) Patch(org.bitbucket.cowwoc.diffmatchpatch.DiffMatchPatch.Patch) LinkedList(java.util.LinkedList)

Aggregations

LinkedList (java.util.LinkedList)1 DiffMatchPatch (org.bitbucket.cowwoc.diffmatchpatch.DiffMatchPatch)1 Patch (org.bitbucket.cowwoc.diffmatchpatch.DiffMatchPatch.Patch)1 AbstractNode (org.structr.core.entity.AbstractNode)1