use of org.structr.core.graph.NodeInterface in project structr by structr.
the class DeleteNodeCommand method processMessage.
@Override
public void processMessage(final WebSocketMessage webSocketData) {
final Boolean recursive = (Boolean) webSocketData.getNodeData().get("recursive");
final NodeInterface obj = getNode(webSocketData.getId());
if (obj != null) {
deleteNode(getWebSocket(), obj, recursive);
}
}
use of org.structr.core.graph.NodeInterface in project structr by structr.
the class DeleteNodeCommand method deleteNode.
protected static void deleteNode(final StructrWebSocket ws, final NodeInterface obj, final Boolean recursive) {
final SecurityContext securityContext = ws.getSecurityContext();
final App app = StructrApp.getInstance(securityContext);
try (final Tx tx = app.tx()) {
if (!(obj.isGranted(Permission.delete, securityContext))) {
logger.warn("No delete permission for {} on {}", new Object[] { ws.getCurrentUser().toString(), obj.toString() });
ws.send(MessageBuilder.status().message("No delete permission").code(400).build(), true);
tx.success();
return;
}
} catch (FrameworkException ex) {
logger.warn("", ex);
}
if (Boolean.TRUE.equals(recursive)) {
// Remove all child nodes first
try {
final List<NodeInterface> filteredResults = new LinkedList<>();
if (obj instanceof DOMNode) {
DOMNode node = (DOMNode) obj;
filteredResults.addAll(DOMNode.getAllChildNodes(node));
} else if (obj instanceof LinkedTreeNode) {
LinkedTreeNode node = (LinkedTreeNode) obj;
filteredResults.addAll(node.getAllChildNodes());
}
for (NodeInterface node : filteredResults) {
app.delete(node);
}
} catch (FrameworkException fex) {
logger.warn("Exception occured", fex);
ws.send(MessageBuilder.status().code(fex.getStatus()).message(fex.getMessage()).build(), true);
} catch (DOMException dex) {
logger.warn("DOMException occured.", dex);
ws.send(MessageBuilder.status().code(422).message(dex.getMessage()).build(), true);
}
}
try {
app.delete(obj);
} catch (FrameworkException fex) {
logger.warn("Unable to delete node(s)", fex);
}
}
use of org.structr.core.graph.NodeInterface 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.graph.NodeInterface in project structr by structr.
the class ValidationHelper method isValidUniqueProperty.
public static synchronized boolean isValidUniqueProperty(final GraphObject object, final PropertyKey key, final ErrorBuffer errorBuffer) {
if (key != null) {
final Object value = object.getProperty(key);
if (value != null) {
// validation will only be executed for non-null values
List<GraphObject> result = null;
// use declaring class for inheritance-aware uniqueness
Class type = key.getDeclaringClass();
if (type == null || (AbstractNode.name.equals(key) && NodeInterface.class.equals(type))) {
// fallback: object type
type = object.getClass();
}
try {
if (object instanceof NodeInterface) {
result = StructrApp.getInstance().nodeQuery(type).and(key, value).getAsList();
} else {
result = StructrApp.getInstance().relationshipQuery(type).and(key, value).getAsList();
}
} catch (FrameworkException fex) {
logger.warn("", fex);
}
if (result != null) {
for (final GraphObject foundNode : result) {
if (foundNode.getId() != object.getId()) {
// validation is aborted when the first validation failure occurs, so
// we can assume that the object currently exmained is the first
// existing object, hence all others get the error message with the
// UUID of the first one.
errorBuffer.add(new UniqueToken(object.getType(), key, object.getUuid()));
// error!
return false;
}
}
}
}
}
// no error
return true;
}
use of org.structr.core.graph.NodeInterface in project structr by structr.
the class ValidationHelper method areValidCompoundUniqueProperties.
public static synchronized boolean areValidCompoundUniqueProperties(final GraphObject object, final ErrorBuffer errorBuffer, final PropertyKey... keys) {
if (keys != null && keys.length > 0) {
final PropertyMap properties = new PropertyMap();
List<GraphObject> result = null;
Class type = null;
for (final PropertyKey key : keys) {
properties.put(key, object.getProperty(key));
if (type != null) {
// set type on first iteration
type = key.getDeclaringClass();
}
}
if (type == null) {
// fallback: object type
type = object.getClass();
}
try {
if (object instanceof NodeInterface) {
result = StructrApp.getInstance().nodeQuery(type).and(properties).disableSorting().getAsList();
} else {
result = StructrApp.getInstance().relationshipQuery(type).and(properties).disableSorting().getAsList();
}
} catch (FrameworkException fex) {
logger.warn("", fex);
}
if (result != null) {
for (final GraphObject foundNode : result) {
if (foundNode.getId() != object.getId()) {
// validation is aborted when the first validation failure occurs, so
// we can assume that the object currently exmained is the first
// existing object, hence all others get the error message with the
// UUID of the first one.
errorBuffer.add(new CompoundToken(object.getType(), keys, object.getUuid()));
// error!
return false;
}
}
}
}
// no error
return true;
}
Aggregations