Search in sources :

Example 86 with NodeInterface

use of org.structr.core.graph.NodeInterface in project structr by structr.

the class StructrXMPPModuleTest method cleanDatabase.

@After
@Before
public void cleanDatabase() {
    try (final Tx tx = app.tx()) {
        final List<? extends NodeInterface> nodes = app.nodeQuery().getAsList();
        logger.info("Cleaning database: {} nodes", nodes.size());
        for (final NodeInterface node : nodes) {
            app.delete(node);
        }
        // delete remaining nodes without UUIDs etc.
        app.cypher("MATCH (n)-[r]-(m) DELETE n, r, m", Collections.emptyMap());
        tx.success();
    } catch (FrameworkException fex) {
        logger.error("Exception while trying to clean database: {}", fex);
    }
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) NodeInterface(org.structr.core.graph.NodeInterface) Before(org.junit.Before) After(org.junit.After)

Example 87 with NodeInterface

use of org.structr.core.graph.NodeInterface 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 88 with NodeInterface

use of org.structr.core.graph.NodeInterface in project structr by structr.

the class ListUnattachedNodesCommand method processMessage.

@Override
public void processMessage(final WebSocketMessage webSocketData) {
    final SecurityContext securityContext = getWebSocket().getSecurityContext();
    final int pageSize = webSocketData.getPageSize();
    final int page = webSocketData.getPage();
    final App app = StructrApp.getInstance(securityContext);
    try (final Tx tx = app.tx()) {
        // do search
        List<NodeInterface> filteredResults = getUnattachedNodes(app, securityContext, webSocketData);
        // 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);
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("Exception occured", fex);
        getWebSocket().send(MessageBuilder.status().code(fex.getStatus()).message(fex.getMessage()).build(), true);
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) SecurityContext(org.structr.common.SecurityContext) NodeInterface(org.structr.core.graph.NodeInterface)

Example 89 with NodeInterface

use of org.structr.core.graph.NodeInterface 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 90 with NodeInterface

use of org.structr.core.graph.NodeInterface in project structr by structr.

the class CreateCommand method processMessage.

// ~--- methods --------------------------------------------------------
@Override
public void processMessage(final WebSocketMessage webSocketData) {
    final SecurityContext securityContext = getWebSocket().getSecurityContext();
    final App app = StructrApp.getInstance(securityContext);
    Map<String, Object> nodeData = webSocketData.getNodeData();
    try {
        final PropertyMap properties = PropertyMap.inputTypeToJavaType(securityContext, nodeData);
        Class type = SchemaHelper.getEntityClassForRawType(properties.get(AbstractNode.type));
        final NodeInterface newNode = app.create(type, properties);
        TransactionCommand.registerNodeCallback(newNode, callback);
        // check for File node and store in WebSocket to receive chunks
        if (newNode instanceof File) {
            getWebSocket().createFileUploadHandler((File) newNode);
        }
    } catch (FrameworkException fex) {
        logger.warn("Could not create node.", fex);
        getWebSocket().send(MessageBuilder.status().code(fex.getStatus()).message(fex.getMessage()).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) File(org.structr.web.entity.File) NodeInterface(org.structr.core.graph.NodeInterface)

Aggregations

NodeInterface (org.structr.core.graph.NodeInterface)186 FrameworkException (org.structr.common.error.FrameworkException)120 Tx (org.structr.core.graph.Tx)116 Test (org.junit.Test)81 PropertyKey (org.structr.core.property.PropertyKey)61 LinkedList (java.util.LinkedList)36 StructrTest (org.structr.common.StructrTest)29 PropertyMap (org.structr.core.property.PropertyMap)26 TestOne (org.structr.core.entity.TestOne)24 List (java.util.List)23 GraphObject (org.structr.core.GraphObject)22 App (org.structr.core.app.App)21 StructrApp (org.structr.core.app.StructrApp)21 GenericNode (org.structr.core.entity.GenericNode)21 Before (org.junit.Before)18 Result (org.structr.core.Result)18 AbstractRelationship (org.structr.core.entity.AbstractRelationship)16 Random (java.util.Random)15 RelationshipInterface (org.structr.core.graph.RelationshipInterface)14 ErrorToken (org.structr.common.error.ErrorToken)12