Search in sources :

Example 26 with AbstractNode

use of org.structr.core.entity.AbstractNode 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 27 with AbstractNode

use of org.structr.core.entity.AbstractNode in project structr by structr.

the class CreateRelationshipCommand method processMessage.

@Override
public void processMessage(final WebSocketMessage webSocketData) {
    final Map<String, Object> properties = webSocketData.getRelData();
    final String sourceId = (String) properties.get("sourceId");
    final String targetId = (String) properties.get("targetId");
    final String relType = (String) properties.get("relType");
    final AbstractNode sourceNode = getNode(sourceId);
    final AbstractNode targetNode = getNode(targetId);
    if ((sourceNode != null) && (targetNode != null)) {
        final SecurityContext securityContext = getWebSocket().getSecurityContext();
        final App app = StructrApp.getInstance(securityContext);
        try {
            final Class relationClass = StructrApp.getConfiguration().getRelationClassForCombinedType(sourceNode.getType(), relType, targetNode.getType());
            final RelationshipInterface rel = app.create(sourceNode, targetNode, relationClass);
            TransactionCommand.registerRelCallback(rel, callback);
            if (rel != null) {
                webSocketData.setResult(Arrays.asList(rel));
                getWebSocket().send(webSocketData, true);
            }
        } catch (FrameworkException t) {
            getWebSocket().send(MessageBuilder.status().code(400).message(t.getMessage()).build(), true);
        }
    } else {
        getWebSocket().send(MessageBuilder.status().code(400).message("The CREATE_RELATIONSHIP command needs source and target!").build(), true);
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) FrameworkException(org.structr.common.error.FrameworkException) AbstractNode(org.structr.core.entity.AbstractNode) SecurityContext(org.structr.common.SecurityContext) RelationshipInterface(org.structr.core.graph.RelationshipInterface)

Example 28 with AbstractNode

use of org.structr.core.entity.AbstractNode in project structr by structr.

the class FindDuplicateFilesCommand method processMessage.

@Override
public void processMessage(final WebSocketMessage webSocketData) {
    final PropertyKey<String> path = StructrApp.key(AbstractFile.class, "path");
    final SecurityContext securityContext = getWebSocket().getSecurityContext();
    final Query query = StructrApp.getInstance(securityContext).nodeQuery(AbstractFile.class).sort(path);
    try {
        AbstractFile lastFile = null;
        String lastFilepath = null;
        boolean lastWasDupe = false;
        final ArrayList<GraphObject> filesWithSamePath = new ArrayList<>();
        final Iterator it = query.getAsList().iterator();
        while (it.hasNext()) {
            final AbstractNode node = (AbstractNode) it.next();
            try {
                final AbstractFile file = (AbstractFile) node;
                final String currentFilepath = file.getProperty(path);
                // skip the first file as we can not compare it to the previous one
                if (lastFile != null) {
                    if (currentFilepath.equals(lastFilepath)) {
                        if (!lastWasDupe) {
                            // if this is the first duplicate found we need to add both files
                            filesWithSamePath.add(lastFile);
                        }
                        filesWithSamePath.add(file);
                        lastWasDupe = true;
                    } else {
                        lastWasDupe = false;
                    }
                }
                lastFilepath = currentFilepath;
                lastFile = file;
            } catch (ClassCastException cce) {
                logger.warn("Tried casting node '{}' of type '{}' to AbstractFile. Most likely a node type inheriting from File was deleted and an instance remains. Please delete this node or change its type.", node.getUuid(), node.getType());
            }
        }
        // set full result list
        webSocketData.setResult(filesWithSamePath);
        webSocketData.setRawResultCount(filesWithSamePath.size());
        // 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 : AbstractFile(org.structr.web.entity.AbstractFile) Query(org.structr.core.app.Query) FrameworkException(org.structr.common.error.FrameworkException) AbstractNode(org.structr.core.entity.AbstractNode) ArrayList(java.util.ArrayList) GraphObject(org.structr.core.GraphObject) SecurityContext(org.structr.common.SecurityContext) Iterator(java.util.Iterator)

Example 29 with AbstractNode

use of org.structr.core.entity.AbstractNode in project structr by structr.

the class AppendFileCommand 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 append node without parentId").build(), true);
        return;
    }
    // never append to self
    if (parentId.equals(id)) {
        getWebSocket().send(MessageBuilder.status().code(422).message("Cannot append node as its own child.").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 Folder) {
        Folder folder = (Folder) parentNode;
        AbstractFile file = (AbstractFile) getNode(id);
        if (file != null) {
            try {
                // Remove from existing parent
                Folder currentParent = (Folder) file.treeGetParent();
                if (currentParent != null) {
                    currentParent.treeRemoveChild(file);
                }
                folder.treeAppendChild(file);
                TransactionCommand.registerNodeCallback(file, callback);
            } catch (FrameworkException ex) {
                logger.error("", ex);
                getWebSocket().send(MessageBuilder.status().code(422).message("Cannot append file").build(), true);
            }
        }
    } else {
        // send exception
        getWebSocket().send(MessageBuilder.status().code(422).message("Parent node is not instance of Folder").build(), true);
    }
}
Also used : AbstractFile(org.structr.web.entity.AbstractFile) FrameworkException(org.structr.common.error.FrameworkException) AbstractNode(org.structr.core.entity.AbstractNode) Folder(org.structr.web.entity.Folder)

Example 30 with AbstractNode

use of org.structr.core.entity.AbstractNode 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

AbstractNode (org.structr.core.entity.AbstractNode)62 FrameworkException (org.structr.common.error.FrameworkException)31 Tx (org.structr.core.graph.Tx)20 App (org.structr.core.app.App)18 StructrApp (org.structr.core.app.StructrApp)18 GraphObject (org.structr.core.GraphObject)17 SecurityContext (org.structr.common.SecurityContext)16 PropertyMap (org.structr.core.property.PropertyMap)12 Result (org.structr.core.Result)10 Test (org.junit.Test)9 AbstractRelationship (org.structr.core.entity.AbstractRelationship)9 LinkedList (java.util.LinkedList)8 TestOne (org.structr.core.entity.TestOne)8 DatabaseService (org.structr.api.DatabaseService)7 NodeInterface (org.structr.core.graph.NodeInterface)7 PropertyKey (org.structr.core.property.PropertyKey)7 Principal (org.structr.core.entity.Principal)6 CmisObjectNotFoundException (org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException)5 DOMNode (org.structr.web.entity.dom.DOMNode)5 LinkedHashSet (java.util.LinkedHashSet)4