// ----- private methods -----
  private void collectActiveElements(
      final List<GraphObject> resultList,
      final DOMNode root,
      final Set<String> parentDataKeys,
      final String parent,
      final int depth) {

    final String childDataKey = root.getProperty(DOMElement.dataKey);
    final Set<String> dataKeys = new LinkedHashSet<>(parentDataKeys);
    String parentId = parent;
    int dataCentricDepth = depth;

    if (!StringUtils.isEmpty(childDataKey)) {

      dataKeys.add(childDataKey);
      dataCentricDepth++;
    }

    final ActiveElementState state = isActive(root, dataKeys);
    if (!state.equals(ActiveElementState.None)) {

      resultList.add(extractActiveElement(root, dataKeys, parentId, state, depth));
      if (state.equals(ActiveElementState.Query)) {

        parentId = root.getUuid();
      }
    }

    for (final DOMChildren children : root.getChildRelationships()) {

      final DOMNode child = children.getTargetNode();
      collectActiveElements(resultList, child, dataKeys, parentId, dataCentricDepth);
    }
  }
  private GraphObject extractActiveElement(
      final DOMNode node,
      final Set<String> dataKeys,
      final String parentId,
      final ActiveElementState state,
      final int depth) {

    final GraphObjectMap activeElement = new GraphObjectMap();

    activeElement.put(GraphObject.id, node.getUuid());
    activeElement.put(GraphObject.type, node.getType());
    activeElement.put(DOMElement.dataKey, StringUtils.join(dataKeys, ","));
    activeElement.put(Content.content, node.getProperty(Content.content));

    switch (state) {
      case Button:
        activeElement.put(actionProperty, node.getProperty(DOMElement._action));
        break;

      case Link:
        activeElement.put(actionProperty, node.getProperty(Link._href));
        break;

      case Query:
        extractQueries(activeElement, node);
        break;
    }

    activeElement.put(stateProperty, state.name());
    activeElement.put(recursionDepthProperty, depth);
    activeElement.put(parentIdProperty, parentId);

    return activeElement;
  }
示例#3
0
  @Test
  public void testScripting() {

    NodeInterface detailsDataObject = null;
    Page page = null;
    DOMNode html = null;
    DOMNode head = null;
    DOMNode body = null;
    DOMNode title = null;
    DOMNode div = null;
    DOMNode p = null;
    DOMNode text = null;

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

      detailsDataObject = app.create(TestOne.class, "TestOne");
      page = Page.createNewPage(securityContext, "testpage");

      page.setProperties(
          page.getSecurityContext(), new PropertyMap(Page.visibleToPublicUsers, true));

      assertTrue(page != null);
      assertTrue(page instanceof Page);

      html = (DOMNode) page.createElement("html");
      head = (DOMNode) page.createElement("head");
      body = (DOMNode) page.createElement("body");
      title = (DOMNode) page.createElement("title");
      div = (DOMNode) page.createElement("div");
      p = (DOMNode) page.createElement("p");
      text = (DOMNode) page.createTextNode("x");

      // add HTML element to page
      page.appendChild(html);

      // add HEAD and BODY elements to HTML
      html.appendChild(head);
      html.appendChild(body);

      // add TITLE element to HEAD
      head.appendChild(title);

      body.appendChild(div);
      div.appendChild(p);

      final PropertyMap changedProperties = new PropertyMap();
      changedProperties.put(DOMElement.restQuery, "/divs");
      changedProperties.put(DOMElement.dataKey, "div");
      p.setProperties(p.getSecurityContext(), changedProperties);

      p.appendChild(text);

      tx.success();

    } catch (FrameworkException fex) {

      fail("Unexpected exception");
    }

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

      final RenderContext ctx =
          new RenderContext(
              securityContext,
              new RequestMockUp(),
              new ResponseMockUp(),
              RenderContext.EditMode.NONE);
      ctx.setDetailsDataObject(detailsDataObject);
      ctx.setPage(page);

      test(p, text, "${{ return Structr.get('div').id}}", "<p>" + div.getUuid() + "</p>", ctx);
      test(p, text, "${{ return Structr.get('page').id}}", "<p>" + page.getUuid() + "</p>", ctx);
      test(p, text, "${{ return Structr.get('parent').id}}", "<p>" + p.getUuid() + "</p>", ctx);

      tx.success();

    } catch (FrameworkException fex) {

      logger.warn("", fex);
      fail("Unexpected exception.");
    }
  }
  @Override
  public void processMessage(WebSocketMessage webSocketData) {

    final Map<String, Object> nodeData = webSocketData.getNodeData();
    final String parentId = (String) nodeData.get("parentId");
    final String childContent = (String) nodeData.get("childContent");
    final String pageId = webSocketData.getPageId();

    nodeData.remove("parentId");

    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;
      }

      final Document document = getPage(pageId);
      if (document != null) {

        final String tagName = (String) nodeData.get("tagName");
        final App app = StructrApp.getInstance();

        nodeData.remove("tagName");

        try {
          app.beginTx();

          DOMNode newNode;

          if (tagName != null && !tagName.isEmpty()) {

            newNode = (DOMNode) document.createElement(tagName);

          } else {

            newNode = (DOMNode) document.createTextNode("#text");
          }

          // append new node to parent
          if (newNode != null) {

            parentNode.appendChild(newNode);

            for (Entry entry : nodeData.entrySet()) {

              String key = (String) entry.getKey();
              Object val = entry.getValue();

              PropertyKey propertyKey =
                  StructrApp.getConfiguration()
                      .getPropertyKeyForDatabaseName(newNode.getClass(), key);
              if (propertyKey != null) {

                try {
                  Object convertedValue = val;

                  PropertyConverter inputConverter =
                      propertyKey.inputConverter(SecurityContext.getSuperUserInstance());
                  if (inputConverter != null) {

                    convertedValue = inputConverter.convert(val);
                  }

                  // newNode.unlockReadOnlyPropertiesOnce();
                  newNode.setProperty(propertyKey, convertedValue);

                } catch (FrameworkException fex) {

                  logger.log(
                      Level.WARNING,
                      "Unable to set node property {0} of node {1} to {2}: {3}",
                      new Object[] {propertyKey, newNode.getUuid(), val, fex.getMessage()});
                }
              }
            }

            // create a child text node if content is given
            if (StringUtils.isNotBlank(childContent)) {

              DOMNode childNode = (DOMNode) document.createTextNode(childContent);

              if (newNode != null) {

                newNode.appendChild(childNode);
              }
            }
          }
          app.commitTx();

        } catch (DOMException dex) {

          // send DOM exception
          getWebSocket()
              .send(MessageBuilder.status().code(422).message(dex.getMessage()).build(), true);

        } catch (FrameworkException ex) {

          Logger.getLogger(CreateAndAppendDOMNodeCommand.class.getName())
              .log(Level.SEVERE, null, ex);

        } finally {

          app.finishTx();
        }

      } 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);
    }
  }