Ejemplo n.º 1
0
  public static void relationshipModified(
      final Principal user,
      final RelationshipInterface relationship,
      final PropertyKey key,
      final Object previousValue,
      final Object newValue) {

    TransactionCommand command = currentCommand.get();
    if (command != null) {

      ModificationQueue modificationQueue = command.getModificationQueue();
      if (modificationQueue != null) {

        modificationQueue.modify(user, relationship, key, previousValue, newValue);

      } else {

        logger.log(Level.SEVERE, "Got empty changeSet from command!");
      }

    } else {

      logger.log(Level.SEVERE, "Relationship deleted while outside of transaction!");
    }
  }
Ejemplo n.º 2
0
  protected List<AbstractRelationship> createTestRelationships(
      final RelationshipType relType, final int number) throws FrameworkException {

    List<AbstractNode> nodes = createTestNodes("UnknownTestType", 2);
    final AbstractNode startNode = nodes.get(0);
    final AbstractNode endNode = nodes.get(1);

    return (List<AbstractRelationship>)
        transactionCommand.execute(
            new StructrTransaction() {

              @Override
              public Object execute() throws FrameworkException {

                List<AbstractRelationship> rels = new LinkedList<AbstractRelationship>();

                for (int i = 0; i < number; i++) {

                  rels.add(
                      (AbstractRelationship)
                          createRelationshipCommand.execute(startNode, endNode, relType));
                }

                return rels;
              }
            });
  }
Ejemplo n.º 3
0
  protected <T extends AbstractNode> List<T> createTestNodes(final Class<T> type, final int number)
      throws FrameworkException {

    final PropertyMap props = new PropertyMap();
    props.put(AbstractNode.type, type.getSimpleName());

    return (List<T>)
        transactionCommand.execute(
            new StructrTransaction<List<T>>() {

              @Override
              public List<T> execute() throws FrameworkException {

                List<T> nodes = new LinkedList<T>();

                for (int i = 0; i < number; i++) {

                  props.put(AbstractNode.name, type.getSimpleName() + i);

                  nodes.add((T) createNodeCommand.execute(props));
                }

                return nodes;
              }
            });
  }
Ejemplo n.º 4
0
  public static void nodeDeleted(final Principal user, final NodeInterface node) {

    TransactionCommand command = currentCommand.get();
    if (command != null) {

      ModificationQueue modificationQueue = command.getModificationQueue();
      if (modificationQueue != null) {

        modificationQueue.delete(user, node);

      } else {

        logger.log(Level.SEVERE, "Got empty changeSet from command!");
      }

    } else {

      logger.log(Level.SEVERE, "Node deleted while outside of transaction!");
    }
  }
Ejemplo n.º 5
0
  @Override
  public void onNodeDeletion() {

    final String signature = getResourceSignature();
    if (StringUtils.isNotBlank(signature)) {

      SchemaHelper.removeDynamicGrants(getResourceSignature());
    }

    // register transaction post processing that recreates the schema information
    TransactionCommand.postProcess("reloadSchema", new ReloadSchema());
  }
Ejemplo n.º 6
0
  public static void relationshipDeleted(
      final Principal user, final RelationshipInterface relationship, final boolean passive) {

    TransactionCommand command = currentCommand.get();
    if (command != null) {

      ModificationQueue modificationQueue = command.getModificationQueue();
      if (modificationQueue != null) {

        modificationQueue.delete(user, relationship, passive);

      } else {

        logger.log(Level.SEVERE, "Got empty changeSet from command!");
      }

    } else {

      logger.log(Level.SEVERE, "Relationship deleted while outside of transaction!");
    }
  }
Ejemplo n.º 7
0
  public static void postProcess(final String key, final TransactionPostProcess process) {

    TransactionCommand command = currentCommand.get();
    if (command != null) {

      ModificationQueue modificationQueue = command.getModificationQueue();
      if (modificationQueue != null) {

        modificationQueue.postProcess(key, process);

      } else {

        logger.log(Level.SEVERE, "Got empty changeSet from command!");
      }

    } else {

      logger.log(
          Level.SEVERE,
          "Trying to register transaction post processing while outside of transaction!");
    }
  }
Ejemplo n.º 8
0
  @Override
  public boolean onModification(SecurityContext securityContext, ErrorBuffer errorBuffer)
      throws FrameworkException {

    if (super.onModification(securityContext, errorBuffer)) {

      // register transaction post processing that recreates the schema information
      TransactionCommand.postProcess("reloadSchema", new ReloadSchema());

      return true;
    }

    return false;
  }
Ejemplo n.º 9
0
  protected List<AbstractNode> createTestNodes(final String type, final int number)
      throws FrameworkException {

    final PropertyMap props = new PropertyMap();
    props.put(AbstractNode.type, type);

    return (List<AbstractNode>)
        transactionCommand.execute(
            new StructrTransaction() {

              @Override
              public Object execute() throws FrameworkException {

                List<AbstractNode> nodes = new LinkedList<AbstractNode>();

                for (int i = 0; i < number; i++) {

                  nodes.add((AbstractNode) createNodeCommand.execute(props));
                }

                return nodes;
              }
            });
  }
  private synchronized <
          A extends NodeInterface, B extends NodeInterface, R extends Relation<A, B, ?, ?>>
      R createRelationship(
          final A fromNode, final B toNode, final Class<R> relType, final PropertyMap properties)
          throws FrameworkException {

    final RelationshipFactory<R> factory = new RelationshipFactory(securityContext);
    final R template = instantiate(relType);
    final Node startNode = fromNode.getNode();
    final Node endNode = toNode.getNode();
    final Relationship rel = startNode.createRelationshipTo(endNode, template);
    final R newRel = factory.instantiate(rel);
    final Date now = new Date();

    // logger.log(Level.INFO, "CREATING relationship {0}-[{1}]->{2}", new Object[] {
    // fromNode.getType(), newRel.getRelType(), toNode.getType() } );

    if (newRel != null) {

      newRel.unlockReadOnlyPropertiesOnce();
      newRel.setProperty(GraphObject.type, relType.getSimpleName());

      // set UUID
      newRel.unlockReadOnlyPropertiesOnce();
      newRel.setProperty(GraphObject.id, getNextUuid());

      // set created date
      newRel.unlockReadOnlyPropertiesOnce();
      newRel.setProperty(AbstractRelationship.createdDate, now);

      // set last modified date
      newRel.unlockReadOnlyPropertiesOnce();
      newRel.setProperty(AbstractRelationship.lastModifiedDate, now);

      // Try to get the cascading delete flag from the domain specific relationship type
      newRel.unlockReadOnlyPropertiesOnce();
      newRel.setProperty(
          AbstractRelationship.cascadeDelete, factory.instantiate(rel).getCascadingDeleteFlag());

      // notify transaction handler
      TransactionCommand.relationshipCreated(newRel);

      if (properties != null) {

        for (Entry<PropertyKey, Object> entry : properties.entrySet()) {

          PropertyKey key = entry.getKey();

          // on creation, writing of read-only properties should be possible
          if (key.isReadOnly() || key.isWriteOnce()) {
            newRel.unlockReadOnlyPropertiesOnce();
          }

          newRel.setProperty(entry.getKey(), entry.getValue());
        }
      }

      // notify relationship of its creation
      newRel.onRelationshipCreation();

      // iterate post creation transformations
      for (Transformation<GraphObject> transformation :
          StructrApp.getConfiguration().getEntityCreationTransformations(newRel.getClass())) {

        transformation.apply(securityContext, newRel);
      }
    }

    return newRel;
  }
Ejemplo n.º 11
0
  @Override
  public Object setProperty(
      final SecurityContext securityContext, final GraphObject obj, final T value)
      throws FrameworkException {

    final PropertyConverter converter = databaseConverter(securityContext, obj);
    final Object convertedValue;

    if (converter != null) {

      convertedValue = converter.convert(value);

    } else {

      convertedValue = value;
    }

    final PropertyContainer propertyContainer = obj.getPropertyContainer();
    if (propertyContainer != null) {

      if (!TransactionCommand.inTransaction()) {

        throw new NotInTransactionException("setProperty outside of transaction");
      }

      boolean internalSystemPropertiesUnlocked = (obj instanceof CreationContainer);

      // notify only non-system properties

      // collect modified properties
      if (obj instanceof AbstractNode) {

        if (!unvalidated) {

          TransactionCommand.nodeModified(
              securityContext.getCachedUser(),
              (AbstractNode) obj,
              AbstractPrimitiveProperty.this,
              propertyContainer.hasProperty(dbName())
                  ? propertyContainer.getProperty(dbName())
                  : null,
              value);
        }

        internalSystemPropertiesUnlocked = ((AbstractNode) obj).internalSystemPropertiesUnlocked;

      } else if (obj instanceof AbstractRelationship) {

        if (!unvalidated) {

          TransactionCommand.relationshipModified(
              securityContext.getCachedUser(),
              (AbstractRelationship) obj,
              AbstractPrimitiveProperty.this,
              propertyContainer.hasProperty(dbName())
                  ? propertyContainer.getProperty(dbName())
                  : null,
              value);
        }

        internalSystemPropertiesUnlocked =
            ((AbstractRelationship) obj).internalSystemPropertiesUnlocked;
      }

      // catch all sorts of errors and wrap them in a FrameworkException
      try {

        // save space
        if (convertedValue == null) {

          propertyContainer.removeProperty(dbName());

        } else {

          if (!isSystemInternal() || internalSystemPropertiesUnlocked) {

            propertyContainer.setProperty(dbName(), convertedValue);

          } else {

            logger.warn(
                "Tried to set internal system property {} to {}. Action was denied.",
                new Object[] {dbName(), convertedValue});
          }
        }

        updateAccessInformation(securityContext, propertyContainer);

      } catch (Throwable t) {

        // throw FrameworkException with the given cause
        final FrameworkException fex =
            new FrameworkException(
                500,
                "Unable to set property "
                    + jsonName()
                    + " on entity with ID "
                    + obj.getUuid()
                    + ": "
                    + t.toString());
        fex.initCause(t);

        throw fex;
      }

      if (isIndexed()) {

        // do indexing, needs to be done after
        // setProperty to make spatial index
        // work
        if (!isPassivelyIndexed()) {

          index(obj, convertedValue);
        }
      }
    }

    return null;
  }
  // ~--- methods --------------------------------------------------------
  @Override
  public void processMessage(final WebSocketMessage webSocketData) {

    final String keyString = (String) webSocketData.getNodeData().get("key");
    if (keyString == null) {

      logger.error("Unable to remove given object from collection: key is null");
      getWebSocket().send(MessageBuilder.status().code(400).build(), true);
    }

    final String idToRemove = (String) webSocketData.getNodeData().get("idToRemove");
    if (idToRemove == null) {

      logger.error("Unable to remove given object from collection: idToRemove is null");
      getWebSocket().send(MessageBuilder.status().code(400).build(), true);
    }

    GraphObject obj = getNode(webSocketData.getId());
    if (obj != null) {

      if (!((AbstractNode) obj).isGranted(Permission.write, getWebSocket().getSecurityContext())) {

        getWebSocket()
            .send(MessageBuilder.status().message("No write permission").code(400).build(), true);
        logger.warn(
            "No write permission for {} on {}",
            new Object[] {getWebSocket().getCurrentUser().toString(), obj.toString()});
        return;
      }
    }

    if (obj == null) {

      // No node? Try to find relationship
      obj = getRelationship(webSocketData.getId());
    }

    GraphObject objToRemove = getNode(idToRemove);

    if (obj != null && objToRemove != null) {

      try {

        PropertyKey key =
            StructrApp.getConfiguration().getPropertyKeyForJSONName(obj.getClass(), keyString);
        if (key != null) {

          List collection = (List) obj.getProperty(key);
          collection.remove(objToRemove);
          obj.setProperties(obj.getSecurityContext(), new PropertyMap(key, collection));

          if (obj instanceof NodeInterface) {

            TransactionCommand.registerNodeCallback((NodeInterface) obj, callback);

          } else if (obj instanceof RelationshipInterface) {

            TransactionCommand.registerRelCallback((RelationshipInterface) obj, callback);
          }
        }

      } catch (FrameworkException ex) {

        logger.error("Unable to set properties: {}", ((FrameworkException) ex).toString());
        getWebSocket().send(MessageBuilder.status().code(400).build(), true);
      }

    } else {

      logger.warn("Graph object with uuid {} not found.", webSocketData.getId());
      getWebSocket().send(MessageBuilder.status().code(404).build(), true);
    }
  }