private static ValuePathExpression attributeExpression()

in scim-tools/src/main/java/org/apache/directory/scim/tools/diff/PatchGenerator.java [257:297]


  private static ValuePathExpression attributeExpression(Schema prefixSchema, List<Schema.Attribute> parentAttributes, Schema.Attribute attribute, Object value) {

    Schema.Attribute typeAttribute = attribute.getAttribute(TYPE);
    Schema.Attribute valueAttribute = attribute.getAttribute(VALUE);
    Optional<Schema.Attribute> refAttribute = attribute.getSubAttributes().stream()
      .filter(attr -> attr.getType() == Schema.Attribute.Type.REFERENCE)
      .findFirst();

    // Special handling if there is this object has reference attribute and a `value`  attribute
    // if right is a ref, the expression will be: `<attribute>[value EQ <right.value>]`
    if (refAttribute.isPresent() && valueAttribute != null) {
      Object right = valueAttribute.getAccessor().get(value);

      if (right != null) {
        if (!(right instanceof String)) {
          throw new IllegalArgumentException("PatchGenerator does not support 'value' attributes that are not a String.");
        }

        AttributeReference attributeReference = new AttributeReference(attributePath(prefixSchema, parentAttributes, attribute));
        AttributeReference expressionAttributeRef = new AttributeReference(attributePath(prefixSchema, parentAttributes, valueAttribute));
        return new ValuePathExpression(attributeReference, new AttributeComparisonExpression(expressionAttributeRef, CompareOperator.EQ, right));
      }
    }

    // Next check for objects that have a `type` attribute: `<attribute>[type EQ <right.type>]`
    if (typeAttribute != null) {
      // force a string value
      Object type = typeAttribute.getAccessor().get(value);
      if (type != null) {
        type = type.toString();
      }

      AttributeReference attributeReference = new AttributeReference(attributePath(prefixSchema, parentAttributes, attribute));
      AttributeReference expressionAttributeRef = new AttributeReference(attributePath(prefixSchema, parentAttributes, typeAttribute));
      return new ValuePathExpression(attributeReference, new AttributeComparisonExpression(expressionAttributeRef, CompareOperator.EQ, type));
    }

    // fall back to an attribute with no value
    AttributeReference attributeReference = new AttributeReference(attributePath(prefixSchema, parentAttributes, attribute));
    return new ValuePathExpression(attributeReference);
  }