Example #1
0
  @Override
  public void execute(final StructrShellCommand parent) throws IOException {

    final App app = StructrApp.getInstance();

    try (final Tx tx = app.tx()) {

      final Folder currentFolder = parent.getCurrentFolder();
      if (currentFolder != null) {

        listFolder(parent, currentFolder.getProperty(AbstractFile.children));

      } else {

        listFolder(
            parent, app.nodeQuery(AbstractFile.class).and(AbstractFile.parent, null).getAsList());
      }

      tx.success();

    } catch (FrameworkException fex) {

      logger.warn("", fex);
    }
  }
  @Override
  public void processMessage(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 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 Folder) {

      Folder folder = (Folder) parentNode;

      AbstractFile file = (AbstractFile) getNode(id);

      if (file != null) {
        try {
          // Remove from existing parent
          LinkedTreeNode currentParent = file.treeGetParent(RelType.CONTAINS);
          if (currentParent != null) {

            currentParent.treeRemoveChild(RelType.CONTAINS, file);
          }

          folder.treeAppendChild(RelType.CONTAINS, file);
        } catch (FrameworkException ex) {
          logger.log(Level.SEVERE, null, 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);
    }
  }