/**
   * Adds a new trait to an existing entity represented by a guid.
   *
   * @param guid globally unique identifier for the entity
   * @param traitInstance trait instance that needs to be added to entity
   * @throws RepositoryException
   */
  @Override
  @GraphTransaction
  public void addTrait(String guid, ITypedStruct traitInstance) throws RepositoryException {
    Preconditions.checkNotNull(traitInstance, "Trait instance cannot be null");
    final String traitName = traitInstance.getTypeName();
    LOG.debug("Adding a new trait={} for entity={}", traitName, guid);

    try {
      Vertex instanceVertex = graphHelper.getVertexForGUID(guid);

      // add the trait instance as a new vertex
      final String typeName = GraphHelper.getTypeName(instanceVertex);

      TypedInstanceToGraphMapper instanceToGraphMapper =
          new TypedInstanceToGraphMapper(graphToInstanceMapper, deleteHandler);
      instanceToGraphMapper.mapTraitInstanceToVertex(
          traitInstance, typeSystem.getDataType(ClassType.class, typeName), instanceVertex);

      // update the traits in entity once adding trait instance is successful
      GraphHelper.addProperty(instanceVertex, Constants.TRAIT_NAMES_PROPERTY_KEY, traitName);
      GraphHelper.setProperty(
          instanceVertex,
          Constants.MODIFICATION_TIMESTAMP_PROPERTY_KEY,
          RequestContext.get().getRequestTime());

    } catch (RepositoryException e) {
      throw e;
    } catch (Exception e) {
      throw new RepositoryException(e);
    }
  }
  private void updateTraits(Vertex instanceVertex, List<String> traitNames) {
    // remove the key
    instanceVertex.removeProperty(Constants.TRAIT_NAMES_PROPERTY_KEY);

    // add it back again
    for (String traitName : traitNames) {
      GraphHelper.addProperty(instanceVertex, Constants.TRAIT_NAMES_PROPERTY_KEY, traitName);
    }
    GraphHelper.setProperty(
        instanceVertex,
        Constants.MODIFICATION_TIMESTAMP_PROPERTY_KEY,
        RequestContext.get().getRequestTime());
  }
Exemplo n.º 3
0
  public Vertex createVertexWithoutIdentity(
      String typeName, Id typedInstanceId, Set<String> superTypeNames) {
    LOG.debug(
        "Creating vertex for type {} id {}",
        typeName,
        typedInstanceId != null ? typedInstanceId._getId() : null);
    final Vertex vertexWithoutIdentity = titanGraph.addVertex(null);

    // add type information
    setProperty(vertexWithoutIdentity, Constants.ENTITY_TYPE_PROPERTY_KEY, typeName);

    // add super types
    for (String superTypeName : superTypeNames) {
      addProperty(vertexWithoutIdentity, Constants.SUPER_TYPES_PROPERTY_KEY, superTypeName);
    }

    // add timestamp information
    setProperty(
        vertexWithoutIdentity, Constants.TIMESTAMP_PROPERTY_KEY, System.currentTimeMillis());

    return vertexWithoutIdentity;
  }