Search in sources :

Example 26 with DOMNode

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

the class CreateComponentCommand method processMessage.

@Override
public void processMessage(final WebSocketMessage webSocketData) {
    String id = webSocketData.getId();
    if (id != null) {
        final DOMNode node = (DOMNode) getDOMNode(id);
        try {
            final DOMNode clonedNode = create(node);
            TransactionCommand.registerNodeCallback(clonedNode, callback);
        } catch (DOMException | FrameworkException ex) {
            // send DOM exception
            getWebSocket().send(MessageBuilder.status().code(422).message(ex.getMessage()).build(), true);
        }
    } else {
        getWebSocket().send(MessageBuilder.status().code(422).message("Cannot append node without id").build(), true);
    }
}
Also used : DOMException(org.w3c.dom.DOMException) FrameworkException(org.structr.common.error.FrameworkException) DOMNode(org.structr.web.entity.dom.DOMNode)

Example 27 with DOMNode

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

the class CreateLocalWidgetCommand method processMessage.

@Override
public void processMessage(final WebSocketMessage webSocketData) {
    final App app = StructrApp.getInstance(getWebSocket().getSecurityContext());
    final String id = webSocketData.getId();
    final Map<String, Object> nodeData = webSocketData.getNodeData();
    final String source = (String) nodeData.get("source");
    final String name = (String) nodeData.get("name");
    // check for ID
    if (id == null) {
        getWebSocket().send(MessageBuilder.status().code(422).message("Cannot create widget without id").build(), true);
        return;
    }
    // check if parent node with given ID exists
    DOMNode node = getDOMNode(id);
    if (node == null) {
        getWebSocket().send(MessageBuilder.status().code(404).message("Node not found").build(), true);
        return;
    }
    try {
        // convertFromInput
        PropertyMap properties = new PropertyMap();
        properties.put(AbstractNode.type, Widget.class.getSimpleName());
        properties.put(AbstractNode.name, name);
        properties.put(StructrApp.key(Widget.class, "source"), source);
        final Widget widget = app.create(Widget.class, properties);
        TransactionCommand.registerNodeCallback(widget, callback);
    } catch (Throwable t) {
        logger.warn(t.toString());
        // send exception
        getWebSocket().send(MessageBuilder.status().code(422).message(t.toString()).build(), true);
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) PropertyMap(org.structr.core.property.PropertyMap) Widget(org.structr.web.entity.Widget) DOMNode(org.structr.web.entity.dom.DOMNode)

Example 28 with DOMNode

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

the class DeleteNodeCommand method deleteNode.

protected static void deleteNode(final StructrWebSocket ws, final NodeInterface obj, final Boolean recursive) {
    final SecurityContext securityContext = ws.getSecurityContext();
    final App app = StructrApp.getInstance(securityContext);
    try (final Tx tx = app.tx()) {
        if (!(obj.isGranted(Permission.delete, securityContext))) {
            logger.warn("No delete permission for {} on {}", new Object[] { ws.getCurrentUser().toString(), obj.toString() });
            ws.send(MessageBuilder.status().message("No delete permission").code(400).build(), true);
            tx.success();
            return;
        }
    } catch (FrameworkException ex) {
        logger.warn("", ex);
    }
    if (Boolean.TRUE.equals(recursive)) {
        // Remove all child nodes first
        try {
            final List<NodeInterface> filteredResults = new LinkedList<>();
            if (obj instanceof DOMNode) {
                DOMNode node = (DOMNode) obj;
                filteredResults.addAll(DOMNode.getAllChildNodes(node));
            } else if (obj instanceof LinkedTreeNode) {
                LinkedTreeNode node = (LinkedTreeNode) obj;
                filteredResults.addAll(node.getAllChildNodes());
            }
            for (NodeInterface node : filteredResults) {
                app.delete(node);
            }
        } catch (FrameworkException fex) {
            logger.warn("Exception occured", fex);
            ws.send(MessageBuilder.status().code(fex.getStatus()).message(fex.getMessage()).build(), true);
        } catch (DOMException dex) {
            logger.warn("DOMException occured.", dex);
            ws.send(MessageBuilder.status().code(422).message(dex.getMessage()).build(), true);
        }
    }
    try {
        app.delete(obj);
    } catch (FrameworkException fex) {
        logger.warn("Unable to delete node(s)", fex);
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) DOMException(org.w3c.dom.DOMException) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) LinkedTreeNode(org.structr.core.entity.LinkedTreeNode) SecurityContext(org.structr.common.SecurityContext) DOMNode(org.structr.web.entity.dom.DOMNode) NodeInterface(org.structr.core.graph.NodeInterface) LinkedList(java.util.LinkedList)

Example 29 with DOMNode

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

the class InsertCommand method processMessage.

@Override
public void processMessage(final WebSocketMessage webSocketData) {
    final SecurityContext securityContext = getWebSocket().getSecurityContext();
    final App app = StructrApp.getInstance(securityContext);
    // new node properties
    final Map<String, Object> properties = webSocketData.getNodeData();
    String parentId = (String) properties.get("id");
    final Map<String, Object> relData = webSocketData.getRelData();
    if (parentId != null) {
        DOMNode parentNode = (DOMNode) getNode(parentId);
        DOMNode nodeToInsert = null;
        try {
            PropertyMap nodeProperties = PropertyMap.inputTypeToJavaType(securityContext, properties);
            nodeToInsert = app.create(DOMNode.class, nodeProperties);
        } catch (FrameworkException fex) {
            logger.warn("Could not create node.", fex);
            getWebSocket().send(MessageBuilder.status().code(fex.getStatus()).message(fex.getMessage()).build(), true);
        }
        if ((nodeToInsert != null) && (parentNode != null)) {
            try {
                PropertyMap relProperties = PropertyMap.inputTypeToJavaType(securityContext, relData);
                app.create(parentNode, nodeToInsert, parentNode.getChildLinkType(), relProperties);
            } catch (FrameworkException t) {
                getWebSocket().send(MessageBuilder.status().code(400).message(t.getMessage()).build(), true);
            }
        } else {
            getWebSocket().send(MessageBuilder.status().code(404).build(), true);
        }
    } else {
        getWebSocket().send(MessageBuilder.status().code(400).message("Insertion of new node failed.").build(), true);
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) PropertyMap(org.structr.core.property.PropertyMap) FrameworkException(org.structr.common.error.FrameworkException) SecurityContext(org.structr.common.SecurityContext) DOMNode(org.structr.web.entity.dom.DOMNode)

Example 30 with DOMNode

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

the class AppendWidgetCommand method processMessage.

@Override
public void processMessage(final WebSocketMessage webSocketData) {
    final String pageId = webSocketData.getPageId();
    final Map<String, Object> nodeData = webSocketData.getNodeData();
    final String parentId = (String) nodeData.get("parentId");
    final String baseUrl = (String) nodeData.get("widgetHostBaseUrl");
    final boolean processDeploymentInfo = (Boolean) nodeData.get("processDeploymentInfo");
    // check for parent ID
    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
    AbstractNode parentNode = getNode(parentId);
    if (parentNode == null) {
        getWebSocket().send(MessageBuilder.status().code(404).message("Parent node not found").build(), true);
        return;
    }
    if (parentNode instanceof DOMNode) {
        DOMNode parentDOMNode = getDOMNode(parentId);
        if (parentDOMNode == null) {
            getWebSocket().send(MessageBuilder.status().code(422).message("Parent node is no DOM node").build(), true);
            return;
        }
        Page page = getPage(pageId);
        if (page != null) {
            try {
                Widget.expandWidget(getWebSocket().getSecurityContext(), page, parentDOMNode, baseUrl, nodeData, processDeploymentInfo);
            } catch (FrameworkException fex) {
                logger.warn(fex.toString());
                // send exception
                getWebSocket().send(MessageBuilder.status().code(422).message(fex.toString()).build(), true);
            }
        }
    } else {
        // send exception
        getWebSocket().send(MessageBuilder.status().code(422).message("Cannot use given node, not instance of DOMNode").build(), true);
    }
}
Also used : FrameworkException(org.structr.common.error.FrameworkException) AbstractNode(org.structr.core.entity.AbstractNode) Page(org.structr.web.entity.dom.Page) DOMNode(org.structr.web.entity.dom.DOMNode)

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