use of org.structr.core.entity.AbstractNode 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);
}
}
use of org.structr.core.entity.AbstractNode in project structr by structr.
the class CreateRelationshipCommand method processMessage.
@Override
public void processMessage(final WebSocketMessage webSocketData) {
final Map<String, Object> properties = webSocketData.getRelData();
final String sourceId = (String) properties.get("sourceId");
final String targetId = (String) properties.get("targetId");
final String relType = (String) properties.get("relType");
final AbstractNode sourceNode = getNode(sourceId);
final AbstractNode targetNode = getNode(targetId);
if ((sourceNode != null) && (targetNode != null)) {
final SecurityContext securityContext = getWebSocket().getSecurityContext();
final App app = StructrApp.getInstance(securityContext);
try {
final Class relationClass = StructrApp.getConfiguration().getRelationClassForCombinedType(sourceNode.getType(), relType, targetNode.getType());
final RelationshipInterface rel = app.create(sourceNode, targetNode, relationClass);
TransactionCommand.registerRelCallback(rel, callback);
if (rel != null) {
webSocketData.setResult(Arrays.asList(rel));
getWebSocket().send(webSocketData, true);
}
} catch (FrameworkException t) {
getWebSocket().send(MessageBuilder.status().code(400).message(t.getMessage()).build(), true);
}
} else {
getWebSocket().send(MessageBuilder.status().code(400).message("The CREATE_RELATIONSHIP command needs source and target!").build(), true);
}
}
use of org.structr.core.entity.AbstractNode 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.entity.AbstractNode in project structr by structr.
the class AppendFileCommand method processMessage.
@Override
public void processMessage(final WebSocketMessage webSocketData) {
String id = webSocketData.getId();
Map<String, Object> nodeData = webSocketData.getNodeData();
String parentId = (String) nodeData.get("parentId");
// check node to append
if (id == null) {
getWebSocket().send(MessageBuilder.status().code(422).message("Cannot append node, no id is given").build(), true);
return;
}
// check for parent ID
if (parentId == null) {
getWebSocket().send(MessageBuilder.status().code(422).message("Cannot append node without parentId").build(), true);
return;
}
// never append to self
if (parentId.equals(id)) {
getWebSocket().send(MessageBuilder.status().code(422).message("Cannot append node as its own child.").build(), true);
return;
}
// check if parent node with given ID exists
AbstractNode parentNode = getNode(parentId);
if (parentNode == null) {
getWebSocket().send(MessageBuilder.status().code(404).message("Parent node not found").build(), true);
return;
}
if (parentNode instanceof Folder) {
Folder folder = (Folder) parentNode;
AbstractFile file = (AbstractFile) getNode(id);
if (file != null) {
try {
// Remove from existing parent
Folder currentParent = (Folder) file.treeGetParent();
if (currentParent != null) {
currentParent.treeRemoveChild(file);
}
folder.treeAppendChild(file);
TransactionCommand.registerNodeCallback(file, callback);
} catch (FrameworkException ex) {
logger.error("", ex);
getWebSocket().send(MessageBuilder.status().code(422).message("Cannot append file").build(), true);
}
}
} else {
// send exception
getWebSocket().send(MessageBuilder.status().code(422).message("Parent node is not instance of Folder").build(), true);
}
}
use of org.structr.core.entity.AbstractNode in project structr by structr.
the class AppendWidgetCommand method processMessage.
@Override
public void processMessage(final WebSocketMessage webSocketData) {
final String pageId = webSocketData.getPageId();
final Map<String, Object> nodeData = webSocketData.getNodeData();
final String parentId = (String) nodeData.get("parentId");
final String baseUrl = (String) nodeData.get("widgetHostBaseUrl");
final boolean processDeploymentInfo = (Boolean) nodeData.get("processDeploymentInfo");
// check for parent ID
if (parentId == null) {
getWebSocket().send(MessageBuilder.status().code(422).message("Cannot add node without parentId").build(), true);
return;
}
// check if parent node with given ID exists
AbstractNode parentNode = getNode(parentId);
if (parentNode == null) {
getWebSocket().send(MessageBuilder.status().code(404).message("Parent node not found").build(), true);
return;
}
if (parentNode instanceof DOMNode) {
DOMNode parentDOMNode = getDOMNode(parentId);
if (parentDOMNode == null) {
getWebSocket().send(MessageBuilder.status().code(422).message("Parent node is no DOM node").build(), true);
return;
}
Page page = getPage(pageId);
if (page != null) {
try {
Widget.expandWidget(getWebSocket().getSecurityContext(), page, parentDOMNode, baseUrl, nodeData, processDeploymentInfo);
} catch (FrameworkException fex) {
logger.warn(fex.toString());
// send exception
getWebSocket().send(MessageBuilder.status().code(422).message(fex.toString()).build(), true);
}
}
} else {
// send exception
getWebSocket().send(MessageBuilder.status().code(422).message("Cannot use given node, not instance of DOMNode").build(), true);
}
}
Aggregations