use of org.structr.web.entity.dom.DOMNode in project structr by structr.
the class CreateAndInsertBeforeDOMNodeCommand method processMessage.
@Override
public void processMessage(final WebSocketMessage webSocketData) {
final Map<String, Object> nodeData = webSocketData.getNodeData();
final String parentId = (String) nodeData.get("parentId");
final String refId = (String) nodeData.get("refId");
final String pageId = webSocketData.getPageId();
if (pageId != null) {
// check for parent ID before creating any nodes
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
final DOMNode parentNode = getDOMNode(parentId);
if (parentNode == null) {
getWebSocket().send(MessageBuilder.status().code(404).message("Parent node not found").build(), true);
return;
}
// check for ref ID before creating any nodes
if (refId == null) {
getWebSocket().send(MessageBuilder.status().code(422).message("Cannot add node without refId").build(), true);
return;
}
// check if ref node with given ID exists
final DOMNode refNode = getDOMNode(refId);
if (refNode == null) {
getWebSocket().send(MessageBuilder.status().code(404).message("Reference node not found").build(), true);
return;
}
final Document document = getPage(pageId);
if (document != null) {
String tagName = (String) nodeData.get("tagName");
DOMNode newNode = null;
try {
if (tagName != null && !tagName.isEmpty()) {
newNode = (DOMNode) document.createElement(tagName);
} else {
newNode = (DOMNode) document.createTextNode("");
}
// append new node to parent
if (newNode != null) {
parentNode.insertBefore(newNode, refNode);
}
} catch (DOMException dex) {
// send DOM exception
getWebSocket().send(MessageBuilder.status().code(422).message(dex.getMessage()).build(), true);
}
} else {
getWebSocket().send(MessageBuilder.status().code(404).message("Page not found").build(), true);
}
} else {
getWebSocket().send(MessageBuilder.status().code(422).message("Cannot create node without pageId").build(), true);
}
}
use of org.structr.web.entity.dom.DOMNode 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.web.entity.dom.DOMNode in project structr by structr.
the class CloneComponentCommand 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 clone component, no id is given").build(), true);
return;
}
// check for parent ID
if (parentId == null) {
getWebSocket().send(MessageBuilder.status().code(422).message("Cannot clone component node without parentId").build(), true);
return;
}
// check if parent node with given ID exists
final DOMNode parentNode = (DOMNode) getNode(parentId);
if (parentNode == null) {
getWebSocket().send(MessageBuilder.status().code(404).message("Parent node not found").build(), true);
return;
}
final DOMNode node = getDOMNode(id);
try {
cloneComponent(node, parentNode);
} catch (DOMException | FrameworkException ex) {
// send DOM exception
getWebSocket().send(MessageBuilder.status().code(422).message(ex.getMessage()).build(), true);
}
}
use of org.structr.web.entity.dom.DOMNode 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.web.entity.dom.DOMNode in project structr by structr.
the class CreateComponentCommand method create.
public DOMNode create(final DOMNode node) throws FrameworkException {
final DOMNode clonedNode = (DOMNode) node.cloneNode(false);
// Child nodes of a template must stay in page tree
if (!(clonedNode instanceof Template)) {
moveChildNodes(node, clonedNode);
}
final ShadowDocument hiddenDoc = CreateComponentCommand.getOrCreateHiddenDocument();
clonedNode.setOwnerDocument(hiddenDoc);
// Change page (owner document) of all children recursively
for (DOMNode child : DOMNode.getAllChildNodes(clonedNode)) {
child.setOwnerDocument(hiddenDoc);
}
node.setSharedComponent(clonedNode);
return clonedNode;
}
Aggregations