Search in sources :

Example 56 with DOMNode

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

the class AppendChildCommand 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 append node, no id is given").build(), true);
        return;
    }
    // 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;
        }
        DOMNode node = (DOMNode) getDOMNode(id);
        // append node to parent
        if (node != null) {
            parentDOMNode.appendChild(node);
        }
    } else {
        // send exception
        getWebSocket().send(MessageBuilder.status().code(422).message("Cannot use given node, not instance of DOMNode").build(), true);
    }
}
Also used : AbstractNode(org.structr.core.entity.AbstractNode) DOMNode(org.structr.web.entity.dom.DOMNode)

Example 57 with DOMNode

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

the class ListActiveElementsCommand method collectActiveElements.

// ----- private methods -----
private void collectActiveElements(final List<GraphObject> resultList, final DOMNode root, final Set<String> parentDataKeys, final String parent, final int depth) {
    final String childDataKey = root.getDataKey();
    final Set<String> dataKeys = new LinkedHashSet<>(parentDataKeys);
    String parentId = parent;
    int dataCentricDepth = depth;
    if (!StringUtils.isEmpty(childDataKey)) {
        dataKeys.add(childDataKey);
        dataCentricDepth++;
    }
    final ActiveElementState state = isActive(root, dataKeys);
    if (!state.equals(ActiveElementState.None)) {
        resultList.add(extractActiveElement(root, dataKeys, parentId, state, depth));
        if (state.equals(ActiveElementState.Query)) {
            parentId = root.getUuid();
        }
    }
    for (final DOMNode child : root.getChildren()) {
        collectActiveElements(resultList, child, dataKeys, parentId, dataCentricDepth);
    }
}
Also used : DOMNode(org.structr.web.entity.dom.DOMNode)

Example 58 with DOMNode

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

the class ListComponentsCommand method processMessage.

@Override
public void processMessage(final WebSocketMessage webSocketData) {
    final int pageSize = webSocketData.getPageSize();
    final int page = webSocketData.getPage();
    try {
        final ShadowDocument hiddenDoc = CreateComponentCommand.getOrCreateHiddenDocument();
        List<DOMNode> filteredResults = new LinkedList();
        List<DOMNode> resultList = hiddenDoc.getElements();
        // Filter list and return only top level nodes
        for (DOMNode node : resultList) {
            if (Boolean.FALSE.equals(node.hasIncomingRelationships(node.getChildLinkType()))) {
                filteredResults.add(node);
            }
        }
        // Sort the components by name
        Collections.sort(filteredResults, new Comparator<DOMNode>() {

            @Override
            public int compare(DOMNode node1, DOMNode node2) {
                final String nameNode1 = node1.getProperty(DOMNode.name);
                final String nameNode2 = node2.getProperty(DOMNode.name);
                if (nameNode1 != null && nameNode2 != null) {
                    return nameNode1.compareTo(nameNode2);
                } else if (nameNode1 == null && nameNode2 == null) {
                    return 0;
                } else if (nameNode1 == null) {
                    return -1;
                } else {
                    return 1;
                }
            }
        });
        // save raw result count
        int resultCountBeforePaging = filteredResults.size();
        // set full result list
        webSocketData.setResult(PagingHelper.subList(filteredResults, pageSize, page));
        webSocketData.setRawResultCount(resultCountBeforePaging);
        // send only over local connection
        getWebSocket().send(webSocketData, true);
    } catch (FrameworkException fex) {
        logger.warn("Exception occured", fex);
        getWebSocket().send(MessageBuilder.status().code(fex.getStatus()).message(fex.getMessage()).build(), true);
    }
}
Also used : FrameworkException(org.structr.common.error.FrameworkException) ShadowDocument(org.structr.web.entity.dom.ShadowDocument) DOMNode(org.structr.web.entity.dom.DOMNode)

Example 59 with DOMNode

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

the class TemplateImportVisitor method createTemplate.

private void createTemplate(final Path file, final String fileName) throws IOException, FrameworkException {
    final String templateName = StringUtils.substringBeforeLast(fileName, ".html");
    // either the template was exported with name + uuid or just the uuid
    final boolean byNameAndId = DeployCommand.endsWithUuid(templateName);
    final boolean byId = DeployCommand.isUuid(templateName);
    try (final Tx tx = app.tx(true, false, false)) {
        final PropertyMap properties = getPropertiesForTemplate(templateName);
        if (properties == null) {
            logger.info("Ignoring {} (not in templates.json)", fileName);
        } else {
            final String src = new String(Files.readAllBytes(file), Charset.forName("UTF-8"));
            final Template template;
            if (byId) {
                logger.info("Importing template {} from {}..", new Object[] { templateName, fileName });
                final DOMNode existingTemplate = app.get(DOMNode.class, templateName);
                if (existingTemplate != null) {
                    deleteTemplate(app, existingTemplate);
                }
                template = app.create(Template.class, new NodeAttribute(AbstractNode.id, templateName));
            } else if (byNameAndId) {
                // the last characters in the name string are the uuid
                final String uuid = templateName.substring(templateName.length() - 32);
                final String name = templateName.substring(0, templateName.length() - 33);
                logger.info("Importing template {} from {}..", new Object[] { name, fileName });
                final DOMNode existingTemplate = app.get(DOMNode.class, uuid);
                if (existingTemplate != null) {
                    deleteTemplate(app, existingTemplate);
                }
                template = app.create(Template.class, new NodeAttribute(AbstractNode.id, uuid));
                properties.put(Template.name, name);
            } else {
                final String name = byId ? null : templateName;
                logger.info("Importing template {} from {}..", new Object[] { name, fileName });
                final DOMNode existingTemplate = getExistingTemplate(name);
                if (existingTemplate != null) {
                    deleteTemplate(app, existingTemplate);
                }
                template = app.create(Template.class);
                properties.put(Template.name, name);
            }
            properties.put(StructrApp.key(Template.class, "content"), src);
            // insert "shared" templates into ShadowDocument
            final Object value = properties.get(internalSharedTemplateKey);
            if (value != null) {
                if ("true".equals(value)) {
                    template.setOwnerDocument(CreateComponentCommand.getOrCreateHiddenDocument());
                }
                properties.remove(internalSharedTemplateKey);
            }
            // store properties from templates.json if present
            template.setProperties(securityContext, properties);
        }
        tx.success();
    } catch (Throwable t) {
        logger.debug("Error trying to create template {}", fileName);
    }
}
Also used : NodeAttribute(org.structr.core.graph.NodeAttribute) PropertyMap(org.structr.core.property.PropertyMap) Tx(org.structr.core.graph.Tx) DOMNode(org.structr.web.entity.dom.DOMNode) Template(org.structr.web.entity.dom.Template)

Example 60 with DOMNode

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

the class CloneComponentCommand method cloneComponent.

public DOMNode cloneComponent(final DOMNode node, final DOMNode parentNode) throws FrameworkException {
    final DOMNode clonedNode = (DOMNode) node.cloneNode(false);
    parentNode.appendChild(clonedNode);
    final PropertyMap changedProperties = new PropertyMap();
    changedProperties.put(StructrApp.key(DOMNode.class, "sharedComponent"), node);
    changedProperties.put(StructrApp.key(DOMNode.class, "ownerDocument"), (parentNode instanceof Page ? (Page) parentNode : parentNode.getOwnerDocument()));
    clonedNode.setProperties(clonedNode.getSecurityContext(), changedProperties);
    return clonedNode;
}
Also used : PropertyMap(org.structr.core.property.PropertyMap) 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