Search in sources :

Example 21 with DOMNode

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

the class CreateAndInsertBeforeDOMNodeCommand 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 refId = (String) nodeData.get("refId");
    final String pageId = webSocketData.getPageId();
    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;
        }
        // check for ref ID before creating any nodes
        if (refId == null) {
            getWebSocket().send(MessageBuilder.status().code(422).message("Cannot add node without refId").build(), true);
            return;
        }
        // check if ref node with given ID exists
        final DOMNode refNode = getDOMNode(refId);
        if (refNode == null) {
            getWebSocket().send(MessageBuilder.status().code(404).message("Reference node not found").build(), true);
            return;
        }
        final Document document = getPage(pageId);
        if (document != null) {
            String tagName = (String) nodeData.get("tagName");
            DOMNode newNode = null;
            try {
                if (tagName != null && !tagName.isEmpty()) {
                    newNode = (DOMNode) document.createElement(tagName);
                } else {
                    newNode = (DOMNode) document.createTextNode("");
                }
                // append new node to parent
                if (newNode != null) {
                    parentNode.insertBefore(newNode, refNode);
                }
            } 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 22 with DOMNode

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

the class ListUnattachedNodesCommand method getUnattachedNodes.

/**
 * Return list of nodes which are not attached to a page and have no
 * parent element (no incoming CONTAINS rel)
 *
 * @param app
 * @param securityContext
 * @param webSocketData
 * @return
 * @throws FrameworkException
 */
protected static List<NodeInterface> getUnattachedNodes(final App app, final SecurityContext securityContext, final WebSocketMessage webSocketData) throws FrameworkException {
    final String sortOrder = webSocketData.getSortOrder();
    final String sortKey = webSocketData.getSortKey();
    Query query;
    if (sortKey != null) {
        final PropertyKey sortProperty = StructrApp.key(DOMNode.class, sortKey);
        query = StructrApp.getInstance(securityContext).nodeQuery().includeDeletedAndHidden().sort(sortProperty).order("desc".equals(sortOrder));
    } else {
        query = StructrApp.getInstance(securityContext).nodeQuery().includeDeletedAndHidden();
    }
    query.orTypes(DOMElement.class);
    query.orType(Content.class);
    query.orType(Template.class);
    // do search
    final List<NodeInterface> filteredResults = new LinkedList();
    List<? extends GraphObject> resultList = null;
    try (final Tx tx = app.tx()) {
        resultList = query.getAsList();
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("Exception occured", fex);
    }
    if (resultList != null) {
        // determine which of the nodes have no incoming CONTAINS relationships and no page id
        for (GraphObject obj : resultList) {
            if (obj instanceof DOMNode) {
                DOMNode node = (DOMNode) obj;
                if (!node.hasIncomingRelationships(node.getChildLinkType()) && node.getOwnerDocument() == null && !(node instanceof ShadowDocument)) {
                    filteredResults.add(node);
                }
            }
        }
    }
    return filteredResults;
}
Also used : Query(org.structr.core.app.Query) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) GraphObject(org.structr.core.GraphObject) DOMNode(org.structr.web.entity.dom.DOMNode) ShadowDocument(org.structr.web.entity.dom.ShadowDocument) PropertyKey(org.structr.core.property.PropertyKey) NodeInterface(org.structr.core.graph.NodeInterface) LinkedList(java.util.LinkedList)

Example 23 with DOMNode

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

the class CloneComponentCommand method processMessage.

@Override
public void processMessage(final WebSocketMessage webSocketData) {
    String id = webSocketData.getId();
    Map<String, Object> nodeData = webSocketData.getNodeData();
    String parentId = (String) nodeData.get("parentId");
    // check node to append
    if (id == null) {
        getWebSocket().send(MessageBuilder.status().code(422).message("Cannot clone component, no id is given").build(), true);
        return;
    }
    // check for parent ID
    if (parentId == null) {
        getWebSocket().send(MessageBuilder.status().code(422).message("Cannot clone component node without parentId").build(), true);
        return;
    }
    // check if parent node with given ID exists
    final DOMNode parentNode = (DOMNode) getNode(parentId);
    if (parentNode == null) {
        getWebSocket().send(MessageBuilder.status().code(404).message("Parent node not found").build(), true);
        return;
    }
    final DOMNode node = getDOMNode(id);
    try {
        cloneComponent(node, parentNode);
    } catch (DOMException | FrameworkException ex) {
        // send DOM exception
        getWebSocket().send(MessageBuilder.status().code(422).message(ex.getMessage()).build(), true);
    }
}
Also used : DOMException(org.w3c.dom.DOMException) FrameworkException(org.structr.common.error.FrameworkException) DOMNode(org.structr.web.entity.dom.DOMNode)

Example 24 with DOMNode

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

the class ClonePageCommand method processMessage.

@Override
public void processMessage(final WebSocketMessage webSocketData) {
    final SecurityContext securityContext = getWebSocket().getSecurityContext();
    final String nodeId = webSocketData.getId();
    final AbstractNode nodeToClone = getNode(nodeId);
    if (nodeToClone != null) {
        try {
            final Page pageToClone = nodeToClone instanceof Page ? (Page) nodeToClone : null;
            if (pageToClone != null) {
                final Page newPage = (Page) pageToClone.cloneNode(false);
                newPage.setProperties(securityContext, new PropertyMap(Page.name, pageToClone.getProperty(Page.name) + "-" + newPage.getIdString()));
                DOMNode firstChild = (DOMNode) pageToClone.getFirstChild().getNextSibling();
                if (firstChild == null) {
                    firstChild = (DOMNode) pageToClone.getFirstChild();
                }
                if (firstChild != null) {
                    final DOMNode newHtmlNode = DOMNode.cloneAndAppendChildren(securityContext, firstChild);
                    newPage.appendChild(newHtmlNode);
                }
            }
        } catch (FrameworkException fex) {
            logger.warn("Could not create node.", fex);
            getWebSocket().send(MessageBuilder.status().code(fex.getStatus()).message(fex.getMessage()).build(), true);
        } catch (DOMException dex) {
            logger.warn("Could not create node.", dex);
            getWebSocket().send(MessageBuilder.status().code(422).message(dex.getMessage()).build(), true);
        }
    } else {
        logger.warn("Node with uuid {} not found.", webSocketData.getId());
        getWebSocket().send(MessageBuilder.status().code(404).build(), true);
    }
}
Also used : DOMException(org.w3c.dom.DOMException) PropertyMap(org.structr.core.property.PropertyMap) FrameworkException(org.structr.common.error.FrameworkException) AbstractNode(org.structr.core.entity.AbstractNode) SecurityContext(org.structr.common.SecurityContext) Page(org.structr.web.entity.dom.Page) DOMNode(org.structr.web.entity.dom.DOMNode)

Example 25 with DOMNode

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

the class CreateComponentCommand method create.

public DOMNode create(final DOMNode node) throws FrameworkException {
    final DOMNode clonedNode = (DOMNode) node.cloneNode(false);
    // Child nodes of a template must stay in page tree
    if (!(clonedNode instanceof Template)) {
        moveChildNodes(node, clonedNode);
    }
    final ShadowDocument hiddenDoc = CreateComponentCommand.getOrCreateHiddenDocument();
    clonedNode.setOwnerDocument(hiddenDoc);
    // Change page (owner document) of all children recursively
    for (DOMNode child : DOMNode.getAllChildNodes(clonedNode)) {
        child.setOwnerDocument(hiddenDoc);
    }
    node.setSharedComponent(clonedNode);
    return clonedNode;
}
Also used : DOMNode(org.structr.web.entity.dom.DOMNode) ShadowDocument(org.structr.web.entity.dom.ShadowDocument) Template(org.structr.web.entity.dom.Template)

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