use of org.structr.core.GraphObject 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.GraphObject in project structr by structr.
the class AutocompleteCommand method processMessage.
@Override
public void processMessage(final WebSocketMessage webSocketData) {
final Map<String, Object> data = webSocketData.getNodeData();
final String id = webSocketData.getId();
final List<GraphObject> result = new LinkedList<>();
final String contentType = getOrDefault(data.get("contentType"), "text/plain");
if (contentType != null) {
final AbstractHintProvider hintProvider = hintProviders.get(contentType);
if (hintProvider != null) {
final String currentToken = getAndTrim(data.get("currentToken"));
final String previousToken = getAndTrim(data.get("previousToken"));
final String thirdToken = getAndTrim(data.get("thirdToken"));
final String type = getAndTrim(data.get("type"));
final int cursorPosition = getInt(data.get("cursorPosition"));
final int line = getInt(data.get("line"));
try {
final List<GraphObject> hints = hintProvider.getHints(StructrApp.getInstance().get(AbstractNode.class, id), type, currentToken, previousToken, thirdToken, line, cursorPosition);
result.addAll(hints);
} catch (FrameworkException fex) {
logger.warn("", fex);
}
} else {
logger.warn("No HintProvider for content type {}, ignoring.", contentType);
}
} else {
logger.warn("No content type for AutocompleteCommand, ignoring.");
}
// set full result list
webSocketData.setResult(result);
webSocketData.setRawResultCount(result.size());
getWebSocket().send(webSocketData, true);
}
use of org.structr.core.GraphObject 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);
}
}
use of org.structr.core.GraphObject 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);
}
}
use of org.structr.core.GraphObject in project structr by structr.
the class GetCommand method processMessage.
@Override
public void processMessage(final WebSocketMessage webSocketData) {
final SecurityContext securityContext = getWebSocket().getSecurityContext();
final String nodeId = (String) webSocketData.getNodeData().get("nodeId");
final String properties = (String) webSocketData.getNodeData().get("properties");
if (properties != null) {
securityContext.setCustomView(StringUtils.split(properties, ","));
}
final GraphObject graphObject = getGraphObject(webSocketData.getId(), nodeId);
if (graphObject != null) {
webSocketData.setResult(Arrays.asList(graphObject));
// send only over local connection (no broadcast)
getWebSocket().send(webSocketData, true);
} else {
// logger.warn("Node not found for id {}!", webSocketData.getId());
// Not necessary to send a 404 here
// getWebSocket().send(MessageBuilder.status().code(404).build(), true);
}
}
Aggregations