/**
   * Deletes a given trait from an existing entity represented by a guid.
   *
   * @param guid globally unique identifier for the entity
   * @param traitNameToBeDeleted name of the trait
   * @throws RepositoryException
   */
  @Override
  @GraphTransaction
  public void deleteTrait(String guid, String traitNameToBeDeleted)
      throws TraitNotFoundException, EntityNotFoundException, RepositoryException {
    LOG.debug("Deleting trait={} from entity={}", traitNameToBeDeleted, guid);

    Vertex instanceVertex = graphHelper.getVertexForGUID(guid);

    List<String> traitNames = GraphHelper.getTraitNames(instanceVertex);
    if (!traitNames.contains(traitNameToBeDeleted)) {
      throw new TraitNotFoundException(
          "Could not find trait="
              + traitNameToBeDeleted
              + " in the repository for entity: "
              + guid);
    }

    try {
      final String entityTypeName = GraphHelper.getTypeName(instanceVertex);
      String relationshipLabel = GraphHelper.getTraitLabel(entityTypeName, traitNameToBeDeleted);
      Edge edge = GraphHelper.getEdgeForLabel(instanceVertex, relationshipLabel);
      deleteHandler.deleteEdgeReference(edge, DataTypes.TypeCategory.TRAIT, false, true);

      // update the traits in entity once trait removal is successful
      traitNames.remove(traitNameToBeDeleted);
      updateTraits(instanceVertex, traitNames);
    } catch (Exception e) {
      throw new RepositoryException(e);
    }
  }
 public void deleteEdgeReference(
     Vertex outVertex, String edgeLabel, DataTypes.TypeCategory typeCategory, boolean isComposite)
     throws AtlasException {
   Edge edge = GraphHelper.getEdgeForLabel(outVertex, edgeLabel);
   if (edge != null) {
     deleteEdgeReference(edge, typeCategory, isComposite, false);
   }
 }
  /**
   * Deletes the edge between outvertex and inVertex. The edge is for attribute attributeName of
   * outVertex
   *
   * @param outVertex
   * @param inVertex
   * @param attributeName
   * @throws AtlasException
   */
  protected void deleteEdgeBetweenVertices(Vertex outVertex, Vertex inVertex, String attributeName)
      throws AtlasException {
    LOG.debug(
        "Removing edge from {} to {} with attribute name {}",
        string(outVertex),
        string(inVertex),
        attributeName);
    String typeName = GraphHelper.getTypeName(outVertex);
    String outId = GraphHelper.getIdFromVertex(outVertex);
    Id.EntityState state = GraphHelper.getState(outVertex);
    if ((outId != null && RequestContext.get().isDeletedEntity(outId))
        || state == Id.EntityState.DELETED) {
      // If the reference vertex is marked for deletion, skip updating the reference
      return;
    }

    IDataType type = typeSystem.getDataType(IDataType.class, typeName);
    AttributeInfo attributeInfo = getFieldMapping(type).fields.get(attributeName);
    String propertyName = GraphHelper.getQualifiedFieldName(type, attributeName);
    String edgeLabel = EDGE_LABEL_PREFIX + propertyName;
    Edge edge = null;

    switch (attributeInfo.dataType().getTypeCategory()) {
      case CLASS:
        // If its class attribute, its the only edge between two vertices
        if (attributeInfo.multiplicity.nullAllowed()) {
          edge = GraphHelper.getEdgeForLabel(outVertex, edgeLabel);
          if (shouldUpdateReverseAttribute) {
            GraphHelper.setProperty(outVertex, propertyName, null);
          }
        } else {
          // Cannot unset a required attribute.
          throw new NullRequiredAttributeException(
              "Cannot unset required attribute "
                  + GraphHelper.getQualifiedFieldName(type, attributeName)
                  + " on "
                  + string(outVertex)
                  + " edge = "
                  + edgeLabel);
        }
        break;

      case ARRAY:
        // If its array attribute, find the right edge between the two vertices and update array
        // property
        List<String> elements = outVertex.getProperty(propertyName);
        if (elements != null) {
          elements =
              new ArrayList<>(
                  elements); // Make a copy, else list.remove reflects on titan.getProperty()
          for (String elementEdgeId : elements) {
            Edge elementEdge = graphHelper.getEdgeByEdgeId(outVertex, edgeLabel, elementEdgeId);
            if (elementEdge == null) {
              continue;
            }

            Vertex elementVertex = elementEdge.getVertex(Direction.IN);
            if (elementVertex.getId().toString().equals(inVertex.getId().toString())) {
              edge = elementEdge;

              // TODO element.size includes deleted items as well. should exclude
              if (!attributeInfo.multiplicity.nullAllowed()
                  && elements.size() <= attributeInfo.multiplicity.lower) {
                // Deleting this edge would violate the attribute's lower bound.
                throw new NullRequiredAttributeException(
                    "Cannot remove array element from required attribute "
                        + GraphHelper.getQualifiedFieldName(type, attributeName)
                        + " on "
                        + string(outVertex)
                        + " "
                        + string(elementEdge));
              }

              if (shouldUpdateReverseAttribute) {
                // if composite attribute, remove the reference as well. else, just remove the edge
                // for example, when table is deleted, process still references the table
                // but when column is deleted, table will not reference the deleted column
                LOG.debug(
                    "Removing edge {} from the array attribute {}",
                    string(elementEdge),
                    attributeName);
                elements.remove(elementEdge.getId().toString());
                GraphHelper.setProperty(outVertex, propertyName, elements);
                break;
              }
            }
          }
        }
        break;

      case MAP:
        // If its map attribute, find the right edge between two vertices and update map property
        List<String> keys = outVertex.getProperty(propertyName);
        if (keys != null) {
          keys =
              new ArrayList<>(
                  keys); // Make a copy, else list.remove reflects on titan.getProperty()
          for (String key : keys) {
            String keyPropertyName = GraphHelper.getQualifiedNameForMapKey(propertyName, key);
            String mapEdgeId = outVertex.getProperty(keyPropertyName);
            Edge mapEdge = graphHelper.getEdgeByEdgeId(outVertex, keyPropertyName, mapEdgeId);
            Vertex mapVertex = mapEdge.getVertex(Direction.IN);
            if (mapVertex.getId().toString().equals(inVertex.getId().toString())) {
              // TODO keys.size includes deleted items as well. should exclude
              if (attributeInfo.multiplicity.nullAllowed()
                  || keys.size() > attributeInfo.multiplicity.lower) {
                edge = mapEdge;
              } else {
                // Deleting this entry would violate the attribute's lower bound.
                throw new NullRequiredAttributeException(
                    "Cannot remove map entry "
                        + keyPropertyName
                        + " from required attribute "
                        + GraphHelper.getQualifiedFieldName(type, attributeName)
                        + " on "
                        + string(outVertex)
                        + " "
                        + string(mapEdge));
              }

              if (shouldUpdateReverseAttribute) {
                // remove this key
                LOG.debug(
                    "Removing edge {}, key {} from the map attribute {}",
                    string(mapEdge),
                    key,
                    attributeName);
                keys.remove(key);
                GraphHelper.setProperty(outVertex, propertyName, keys);
                GraphHelper.setProperty(outVertex, keyPropertyName, null);
              }
              break;
            }
          }
        }
        break;

      case STRUCT:
      case TRAIT:
        break;

      default:
        throw new IllegalStateException(
            "There can't be an edge from "
                + string(outVertex)
                + " to "
                + string(inVertex)
                + " with attribute name "
                + attributeName
                + " which is not class/array/map attribute");
    }

    if (edge != null) {
      deleteEdge(edge, false);
      RequestContext requestContext = RequestContext.get();
      GraphHelper.setProperty(
          outVertex,
          Constants.MODIFICATION_TIMESTAMP_PROPERTY_KEY,
          requestContext.getRequestTime());
      requestContext.recordEntityUpdate(outId);
    }
  }