Search in sources :

Example 96 with PropertyMap

use of org.structr.core.property.PropertyMap 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 97 with PropertyMap

use of org.structr.core.property.PropertyMap 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)

Example 98 with PropertyMap

use of org.structr.core.property.PropertyMap 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 99 with PropertyMap

use of org.structr.core.property.PropertyMap in project structr by structr.

the class QueryCommand method processMessage.

@Override
public void processMessage(final WebSocketMessage webSocketData) {
    final SecurityContext securityContext = getWebSocket().getSecurityContext();
    final Map<String, Object> nodeData = webSocketData.getNodeData();
    final String rawType = (String) nodeData.get("type");
    final String properties = (String) nodeData.get("properties");
    final Boolean exact = (Boolean) nodeData.get("exact");
    final Class type = SchemaHelper.getEntityClassForRawType(rawType);
    if (type == null) {
        getWebSocket().send(MessageBuilder.status().code(404).message("Type " + rawType + " not found").build(), true);
        return;
    }
    final String sortKey = webSocketData.getSortKey();
    final int pageSize = webSocketData.getPageSize();
    final int page = webSocketData.getPage();
    final Query query = StructrApp.getInstance(securityContext).nodeQuery(type).page(page).pageSize(pageSize);
    if (sortKey != null) {
        final PropertyKey sortProperty = StructrApp.key(type, sortKey);
        final String sortOrder = webSocketData.getSortOrder();
        query.sort(sortProperty).order("desc".equals(sortOrder));
    }
    if (properties != null) {
        try {
            final Gson gson = new GsonBuilder().create();
            final Map<String, Object> querySource = gson.fromJson(properties, new TypeToken<Map<String, Object>>() {
            }.getType());
            final PropertyMap queryMap = PropertyMap.inputTypeToJavaType(securityContext, type, querySource);
            final boolean inexactQuery = exact != null && exact == false;
            // add properties to query
            for (final Entry<PropertyKey, Object> entry : queryMap.entrySet()) {
                query.and(entry.getKey(), entry.getValue(), !inexactQuery);
            }
        } catch (FrameworkException fex) {
            logger.warn("Exception occured", fex);
            getWebSocket().send(MessageBuilder.status().code(fex.getStatus()).message(fex.getMessage()).build(), true);
            return;
        }
    }
    try {
        // do search
        final Result result = query.getResult();
        // save raw result count
        // filteredResults.size();
        int resultCountBeforePaging = result.getRawResultCount();
        // set full result list
        webSocketData.setResult(result.getResults());
        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 : Query(org.structr.core.app.Query) FrameworkException(org.structr.common.error.FrameworkException) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) Result(org.structr.core.Result) PropertyMap(org.structr.core.property.PropertyMap) TypeToken(com.google.gson.reflect.TypeToken) SecurityContext(org.structr.common.SecurityContext) PropertyKey(org.structr.core.property.PropertyKey)

Example 100 with PropertyMap

use of org.structr.core.property.PropertyMap in project structr by structr.

the class RemoveFromCollectionCommand method processMessage.

// ~--- methods --------------------------------------------------------
@Override
public void processMessage(final WebSocketMessage webSocketData) {
    final String keyString = (String) webSocketData.getNodeData().get("key");
    if (keyString == null) {
        logger.error("Unable to remove given object from collection: key is null");
        getWebSocket().send(MessageBuilder.status().code(400).build(), true);
    }
    final String idToRemove = (String) webSocketData.getNodeData().get("idToRemove");
    if (idToRemove == null) {
        logger.error("Unable to remove given object from collection: idToRemove is null");
        getWebSocket().send(MessageBuilder.status().code(400).build(), true);
    }
    GraphObject obj = getNode(webSocketData.getId());
    if (obj != null) {
        if (!((AbstractNode) obj).isGranted(Permission.write, getWebSocket().getSecurityContext())) {
            getWebSocket().send(MessageBuilder.status().message("No write permission").code(400).build(), true);
            logger.warn("No write permission for {} on {}", new Object[] { getWebSocket().getCurrentUser().toString(), obj.toString() });
            return;
        }
    }
    if (obj == null) {
        // No node? Try to find relationship
        obj = getRelationship(webSocketData.getId());
    }
    GraphObject objToRemove = getNode(idToRemove);
    if (obj != null && objToRemove != null) {
        try {
            PropertyKey key = StructrApp.key(obj.getClass(), keyString);
            if (key != null) {
                List collection = (List) obj.getProperty(key);
                collection.remove(objToRemove);
                obj.setProperties(obj.getSecurityContext(), new PropertyMap(key, collection));
                if (obj instanceof NodeInterface) {
                    TransactionCommand.registerNodeCallback((NodeInterface) obj, callback);
                } else if (obj instanceof RelationshipInterface) {
                    TransactionCommand.registerRelCallback((RelationshipInterface) obj, callback);
                }
            }
        } catch (FrameworkException ex) {
            logger.error("Unable to set properties: {}", ((FrameworkException) ex).toString());
            getWebSocket().send(MessageBuilder.status().code(400).build(), true);
        }
    } else {
        logger.warn("Graph object with uuid {} not found.", webSocketData.getId());
        getWebSocket().send(MessageBuilder.status().code(404).build(), true);
    }
}
Also used : PropertyMap(org.structr.core.property.PropertyMap) FrameworkException(org.structr.common.error.FrameworkException) RelationshipInterface(org.structr.core.graph.RelationshipInterface) List(java.util.List) GraphObject(org.structr.core.GraphObject) PropertyKey(org.structr.core.property.PropertyKey) NodeInterface(org.structr.core.graph.NodeInterface)

Aggregations

PropertyMap (org.structr.core.property.PropertyMap)233 FrameworkException (org.structr.common.error.FrameworkException)100 Tx (org.structr.core.graph.Tx)93 Test (org.junit.Test)60 App (org.structr.core.app.App)34 StructrApp (org.structr.core.app.StructrApp)34 PropertyKey (org.structr.core.property.PropertyKey)34 LinkedList (java.util.LinkedList)28 NodeInterface (org.structr.core.graph.NodeInterface)25 SchemaProperty (org.structr.core.entity.SchemaProperty)23 SecurityContext (org.structr.common.SecurityContext)22 StructrUiTest (org.structr.web.StructrUiTest)21 GraphObject (org.structr.core.GraphObject)20 Result (org.structr.core.Result)19 File (org.structr.web.entity.File)19 DOMNode (org.structr.web.entity.dom.DOMNode)19 TestOne (org.structr.core.entity.TestOne)17 AbstractNode (org.structr.core.entity.AbstractNode)16 Folder (org.structr.web.entity.Folder)15 Page (org.structr.web.entity.dom.Page)15