Search in sources :

Example 16 with DOMNode

use of org.structr.web.entity.dom.DOMNode in project structr by structr.

the class WebsocketController method getMessageForEvent.

// ----- private methods -----
private WebSocketMessage getMessageForEvent(final SecurityContext securityContext, final ModificationEvent modificationEvent) throws FrameworkException {
    final String callbackId = modificationEvent.getCallbackId();
    if (modificationEvent.isNode()) {
        final NodeInterface node = (NodeInterface) modificationEvent.getGraphObject();
        if (modificationEvent.isDeleted()) {
            final WebSocketMessage message = createMessage("DELETE", callbackId);
            message.setId(modificationEvent.getRemovedProperties().get(GraphObject.id));
            message.setCode(200);
            return message;
        }
        if (modificationEvent.isCreated()) {
            final WebSocketMessage message = createMessage("CREATE", callbackId);
            message.setGraphObject(node);
            message.setResult(Arrays.asList(new GraphObject[] { node }));
            message.setCode(201);
            return message;
        }
        if (modificationEvent.isModified()) {
            final WebSocketMessage message = createMessage("UPDATE", callbackId);
            // at login the securityContext is still null
            if (securityContext != null) {
                // only include changed properties (+ id and type)
                LinkedHashSet<String> propertySet = new LinkedHashSet();
                propertySet.add("id");
                propertySet.add("type");
                for (Iterator<PropertyKey> it = modificationEvent.getModifiedProperties().keySet().iterator(); it.hasNext(); ) {
                    final String jsonName = ((PropertyKey) it.next()).jsonName();
                    if (!propertySet.contains(jsonName)) {
                        propertySet.add(jsonName);
                    }
                }
                for (Iterator<PropertyKey> it = modificationEvent.getRemovedProperties().keySet().iterator(); it.hasNext(); ) {
                    final String jsonName = ((PropertyKey) it.next()).jsonName();
                    if (!propertySet.contains(jsonName)) {
                        propertySet.add(jsonName);
                    }
                }
                if (propertySet.size() > 2) {
                    securityContext.setCustomView(propertySet);
                }
            }
            message.setGraphObject(node);
            message.setResult(Arrays.asList(new GraphObject[] { node }));
            message.setId(node.getUuid());
            message.getModifiedProperties().addAll(modificationEvent.getModifiedProperties().keySet());
            message.getRemovedProperties().addAll(modificationEvent.getRemovedProperties().keySet());
            message.setNodeData(modificationEvent.getData(securityContext));
            message.setCode(200);
            if (securityContext != null) {
                // Clear custom view here. This is necessary because the security context is reused for all websocket frames.
                securityContext.clearCustomView();
            }
            return message;
        }
    } else {
        // handle relationship
        final RelationshipInterface relationship = (RelationshipInterface) modificationEvent.getGraphObject();
        final RelationshipType relType = modificationEvent.getRelationshipType();
        // special treatment of CONTAINS relationships
        if ("CONTAINS".equals(relType.name())) {
            if (modificationEvent.isDeleted()) {
                final WebSocketMessage message = createMessage("REMOVE_CHILD", callbackId);
                message.setNodeData("parentId", relationship.getSourceNodeId());
                message.setId(relationship.getTargetNodeId());
                message.setCode(200);
                return message;
            }
            if (modificationEvent.isCreated()) {
                final WebSocketMessage message = new WebSocketMessage();
                final NodeInterface startNode = relationship.getSourceNode();
                final NodeInterface endNode = relationship.getTargetNode();
                // don't send a notification
                if (startNode == null || endNode == null) {
                    return null;
                }
                message.setResult(Arrays.asList(new GraphObject[] { endNode }));
                message.setId(endNode.getUuid());
                message.setNodeData("parentId", startNode.getUuid());
                message.setCode(200);
                message.setCommand("APPEND_CHILD");
                if (endNode instanceof DOMNode) {
                    org.w3c.dom.Node refNode = ((DOMNode) endNode).getNextSibling();
                    if (refNode != null) {
                        message.setCommand("INSERT_BEFORE");
                        message.setNodeData("refId", ((AbstractNode) refNode).getUuid());
                    }
                } else if (endNode instanceof User) {
                    message.setCommand("APPEND_USER");
                    message.setNodeData("refId", startNode.getUuid());
                } else if (endNode instanceof AbstractFile) {
                    message.setCommand("APPEND_FILE");
                    message.setNodeData("refId", startNode.getUuid());
                }
                return message;
            }
        }
        if (modificationEvent.isDeleted()) {
            final WebSocketMessage message = createMessage("DELETE", callbackId);
            message.setId(modificationEvent.getRemovedProperties().get(GraphObject.id));
            message.setCode(200);
            return message;
        }
        if (modificationEvent.isModified()) {
            final WebSocketMessage message = createMessage("UPDATE", callbackId);
            message.getModifiedProperties().addAll(modificationEvent.getModifiedProperties().keySet());
            message.getRemovedProperties().addAll(modificationEvent.getRemovedProperties().keySet());
            message.setNodeData(modificationEvent.getData(securityContext));
            message.setGraphObject(relationship);
            message.setId(relationship.getUuid());
            message.setCode(200);
            final PropertyMap relProperties = relationship.getProperties();
            // final NodeInterface startNode = relationship.getSourceNode();
            // final NodeInterface endNode = relationship.getTargetNode();
            // relProperties.put(new StringProperty("startNodeId"), startNode.getUuid());
            // relProperties.put(new StringProperty("endNodeId"), endNode.getUuid());
            final Map<String, Object> properties = PropertyMap.javaTypeToInputType(securityContext, relationship.getClass(), relProperties);
            message.setRelData(properties);
            return message;
        }
    }
    return null;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) User(org.structr.web.entity.User) AbstractFile(org.structr.web.entity.AbstractFile) RelationshipType(org.structr.api.graph.RelationshipType) GraphObject(org.structr.core.GraphObject) PropertyMap(org.structr.core.property.PropertyMap) RelationshipInterface(org.structr.core.graph.RelationshipInterface) GraphObject(org.structr.core.GraphObject) WebSocketMessage(org.structr.websocket.message.WebSocketMessage) DOMNode(org.structr.web.entity.dom.DOMNode) NodeInterface(org.structr.core.graph.NodeInterface) PropertyKey(org.structr.core.property.PropertyKey)

Example 17 with DOMNode

use of org.structr.web.entity.dom.DOMNode in project structr by structr.

the class AbstractCommand method moveChildNodes.

/**
 * Make child nodes of the source nodes child nodes of the target node.
 *
 * @param sourceNode
 * @param targetNode
 */
protected void moveChildNodes(final DOMNode sourceNode, final DOMNode targetNode) {
    DOMNode child = (DOMNode) sourceNode.getFirstChild();
    while (child != null) {
        DOMNode next = (DOMNode) child.getNextSibling();
        targetNode.appendChild(child);
        child = next;
    }
}
Also used : DOMNode(org.structr.web.entity.dom.DOMNode)

Example 18 with DOMNode

use of org.structr.web.entity.dom.DOMNode in project structr by structr.

the class SyncModeCommand method processMessage.

@Override
public void processMessage(final WebSocketMessage webSocketData) {
    final Class<Relation> relType = StructrApp.getConfiguration().getRelationshipEntityClass("DOMNodeSYNCDOMNode");
    final SecurityContext securityContext = getWebSocket().getSecurityContext();
    final String sourceId = webSocketData.getId();
    final Map<String, Object> properties = webSocketData.getNodeData();
    final String targetId = (String) properties.get("targetId");
    final String syncMode = (String) properties.get("syncMode");
    final DOMNode sourceNode = (DOMNode) getNode(sourceId);
    final DOMNode targetNode = (DOMNode) getNode(targetId);
    final App app = StructrApp.getInstance(securityContext);
    if ((sourceNode != null) && (targetNode != null)) {
        try {
            app.create(sourceNode, targetNode, relType);
            if (syncMode.equals("bidir")) {
                app.create(targetNode, sourceNode, relType);
            }
        } catch (Throwable t) {
            getWebSocket().send(MessageBuilder.status().code(400).message(t.getMessage()).build(), true);
        }
    } else {
        getWebSocket().send(MessageBuilder.status().code(400).message("The SYNC_MODE command needs id and targetId!").build(), true);
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) Relation(org.structr.core.entity.Relation) SecurityContext(org.structr.common.SecurityContext) DOMNode(org.structr.web.entity.dom.DOMNode)

Example 19 with DOMNode

use of org.structr.web.entity.dom.DOMNode in project structr by structr.

the class WrapContentCommand method processMessage.

@Override
public void processMessage(final WebSocketMessage webSocketData) {
    final Map<String, Object> nodeData = webSocketData.getNodeData();
    final String parentId = (String) nodeData.get("parentId");
    final String pageId = webSocketData.getPageId();
    nodeData.remove("parentId");
    if (pageId != null) {
        // check for parent ID before creating any nodes
        if (parentId == null) {
            getWebSocket().send(MessageBuilder.status().code(422).message("Cannot add node without parentId").build(), true);
            return;
        }
        // check if content node with given ID exists
        final DOMNode contentNode = getDOMNode(parentId);
        if (contentNode == null) {
            getWebSocket().send(MessageBuilder.status().code(404).message("Parent node not found").build(), true);
            return;
        }
        final Document document = getPage(pageId);
        if (document != null) {
            final String tagName = (String) nodeData.get("tagName");
            nodeData.remove("tagName");
            final DOMNode parentNode = (DOMNode) contentNode.getParentNode();
            try {
                DOMNode elementNode = null;
                if (tagName != null && !tagName.isEmpty()) {
                    elementNode = (DOMNode) document.createElement(tagName);
                }
                // append new node to parent parent node
                if (elementNode != null) {
                    parentNode.appendChild(elementNode);
                }
                // append new node to parent parent node
                if (elementNode != null) {
                    // append content node to new node
                    elementNode.appendChild(contentNode);
                }
            } catch (DOMException dex) {
                // send DOM exception
                getWebSocket().send(MessageBuilder.status().code(422).message(dex.getMessage()).build(), true);
            }
        } else {
            getWebSocket().send(MessageBuilder.status().code(404).message("Page not found").build(), true);
        }
    } else {
        getWebSocket().send(MessageBuilder.status().code(422).message("Cannot create node without pageId").build(), true);
    }
}
Also used : DOMException(org.w3c.dom.DOMException) DOMNode(org.structr.web.entity.dom.DOMNode) Document(org.w3c.dom.Document)

Example 20 with DOMNode

use of org.structr.web.entity.dom.DOMNode in project structr by structr.

the class CreateAndAppendDOMNodeCommand method processMessage.

@Override
public void processMessage(final WebSocketMessage webSocketData) {
    final Map<String, Object> nodeData = webSocketData.getNodeData();
    final String parentId = (String) nodeData.get("parentId");
    final String childContent = (String) nodeData.get("childContent");
    final String pageId = webSocketData.getPageId();
    Boolean inheritVisibilityFlags = (Boolean) nodeData.get("inheritVisibilityFlags");
    if (inheritVisibilityFlags == null) {
        inheritVisibilityFlags = false;
    }
    // remove configuration elements from the nodeData so we don't set it on the node
    nodeData.remove("parentId");
    nodeData.remove("inheritVisibilityFlags");
    if (pageId != null) {
        // check for parent ID before creating any nodes
        if (parentId == null) {
            getWebSocket().send(MessageBuilder.status().code(422).message("Cannot add node without parentId").build(), true);
            return;
        }
        // check if parent node with given ID exists
        final DOMNode parentNode = getDOMNode(parentId);
        if (parentNode == null) {
            getWebSocket().send(MessageBuilder.status().code(404).message("Parent node not found").build(), true);
            return;
        }
        final Document document = getPage(pageId);
        if (document != null) {
            final String tagName = (String) nodeData.get("tagName");
            nodeData.remove("tagName");
            try {
                DOMNode newNode;
                if (tagName != null && "comment".equals(tagName)) {
                    newNode = (DOMNode) document.createComment("#comment");
                } else if (tagName != null && "template".equals(tagName)) {
                    newNode = (DOMNode) document.createTextNode("#template");
                    try {
                        newNode.unlockSystemPropertiesOnce();
                        newNode.setProperties(newNode.getSecurityContext(), new PropertyMap(NodeInterface.type, Template.class.getSimpleName()));
                    } catch (FrameworkException fex) {
                        logger.warn("Unable to set type of node {} to Template: {}", new Object[] { newNode.getUuid(), fex.getMessage() });
                    }
                } else if (tagName != null && !tagName.isEmpty()) {
                    if ("custom".equals(tagName)) {
                        try {
                            // experimental: create DOM element with literal tag
                            newNode = (DOMElement) StructrApp.getInstance(webSocket.getSecurityContext()).create(DOMElement.class, new NodeAttribute(StructrApp.key(DOMElement.class, "tag"), "custom"), new NodeAttribute(StructrApp.key(DOMElement.class, "hideOnDetail"), false), new NodeAttribute(StructrApp.key(DOMElement.class, "hideOnIndex"), false));
                            if (newNode != null && document != null) {
                                newNode.doAdopt((Page) document);
                            }
                        } catch (FrameworkException fex) {
                            // abort
                            getWebSocket().send(MessageBuilder.status().code(422).message(fex.getMessage()).build(), true);
                            return;
                        }
                    } else {
                        newNode = (DOMNode) document.createElement(tagName);
                    }
                } else {
                    newNode = (DOMNode) document.createTextNode("#text");
                }
                // Instantiate node again to get correct class
                newNode = getDOMNode(newNode.getUuid());
                // append new node to parent
                if (newNode != null) {
                    parentNode.appendChild(newNode);
                    for (Entry entry : nodeData.entrySet()) {
                        final String key = (String) entry.getKey();
                        final Object val = entry.getValue();
                        PropertyKey propertyKey = StructrApp.getConfiguration().getPropertyKeyForDatabaseName(newNode.getClass(), key);
                        if (propertyKey != null) {
                            try {
                                Object convertedValue = val;
                                PropertyConverter inputConverter = propertyKey.inputConverter(SecurityContext.getSuperUserInstance());
                                if (inputConverter != null) {
                                    convertedValue = inputConverter.convert(val);
                                }
                                newNode.setProperties(newNode.getSecurityContext(), new PropertyMap(propertyKey, convertedValue));
                            } catch (FrameworkException fex) {
                                logger.warn("Unable to set node property {} of node {} to {}: {}", new Object[] { propertyKey, newNode.getUuid(), val, fex.getMessage() });
                            }
                        }
                    }
                    PropertyMap visibilityFlags = null;
                    if (inheritVisibilityFlags) {
                        visibilityFlags = new PropertyMap();
                        visibilityFlags.put(DOMNode.visibleToAuthenticatedUsers, parentNode.getProperty(DOMNode.visibleToAuthenticatedUsers));
                        visibilityFlags.put(DOMNode.visibleToPublicUsers, parentNode.getProperty(DOMNode.visibleToPublicUsers));
                        try {
                            newNode.setProperties(newNode.getSecurityContext(), visibilityFlags);
                        } catch (FrameworkException fex) {
                            logger.warn("Unable to inherit visibility flags for node {} from parent node {}", newNode, parentNode);
                        }
                    }
                    // create a child text node if content is given
                    if (StringUtils.isNotBlank(childContent)) {
                        final DOMNode childNode = (DOMNode) document.createTextNode(childContent);
                        newNode.appendChild(childNode);
                        if (inheritVisibilityFlags) {
                            try {
                                childNode.setProperties(childNode.getSecurityContext(), visibilityFlags);
                            } catch (FrameworkException fex) {
                                logger.warn("Unable to inherit visibility flags for node {} from parent node {}", childNode, newNode);
                            }
                        }
                    }
                }
            } catch (DOMException dex) {
                // send DOM exception
                getWebSocket().send(MessageBuilder.status().code(422).message(dex.getMessage()).build(), true);
            }
        } else {
            getWebSocket().send(MessageBuilder.status().code(404).message("Page not found").build(), true);
        }
    } else {
        getWebSocket().send(MessageBuilder.status().code(422).message("Cannot create node without pageId").build(), true);
    }
}
Also used : NodeAttribute(org.structr.core.graph.NodeAttribute) FrameworkException(org.structr.common.error.FrameworkException) Document(org.w3c.dom.Document) Template(org.structr.web.entity.dom.Template) DOMException(org.w3c.dom.DOMException) Entry(java.util.Map.Entry) PropertyMap(org.structr.core.property.PropertyMap) PropertyConverter(org.structr.core.converter.PropertyConverter) DOMNode(org.structr.web.entity.dom.DOMNode) PropertyKey(org.structr.core.property.PropertyKey)

Aggregations

DOMNode (org.structr.web.entity.dom.DOMNode)61 FrameworkException (org.structr.common.error.FrameworkException)26 Tx (org.structr.core.graph.Tx)19 PropertyMap (org.structr.core.property.PropertyMap)19 Page (org.structr.web.entity.dom.Page)19 DOMException (org.w3c.dom.DOMException)16 App (org.structr.core.app.App)14 StructrApp (org.structr.core.app.StructrApp)14 SecurityContext (org.structr.common.SecurityContext)12 GraphObject (org.structr.core.GraphObject)9 NodeInterface (org.structr.core.graph.NodeInterface)9 LinkedList (java.util.LinkedList)8 Test (org.junit.Test)8 StructrUiTest (org.structr.web.StructrUiTest)8 ShadowDocument (org.structr.web.entity.dom.ShadowDocument)8 AbstractNode (org.structr.core.entity.AbstractNode)7 Template (org.structr.web.entity.dom.Template)7 Document (org.w3c.dom.Document)6 IOException (java.io.IOException)5 PropertyKey (org.structr.core.property.PropertyKey)5