use of org.structr.web.entity.ContentItem in project structr by structr.
the class AppendContentItemCommand 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 ContentContainer) {
ContentContainer container = (ContentContainer) parentNode;
ContentItem item = (ContentItem) getNode(id);
if (item != null) {
try {
final List<ContentItem> items = container.getItems();
items.add(item);
container.setProperty(StructrApp.key(ContentContainer.class, "items"), items);
TransactionCommand.registerNodeCallback(item, callback);
} catch (FrameworkException ex) {
logger.error("", ex);
getWebSocket().send(MessageBuilder.status().code(422).message("Cannot append content item").build(), true);
}
}
} else {
// send exception
getWebSocket().send(MessageBuilder.status().code(422).message("Parent node is not instance of ContentContainer").build(), true);
}
}
Aggregations