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);
}
}
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;
}
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);
}
}
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;
}
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);
}
}
Aggregations