private Set createPatchOperations()

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


  private <T extends ScimResource> Set<PatchOperation> createPatchOperations(T left, T right) {

    Set<PatchOperation> patchOperations = new HashSet<>();

    // make sure types are the same, we could support subtypes in the future, but for now, keep it simple
    if (left.getClass() != right.getClass() || !left.getBaseUrn().equals(right.getBaseUrn())) {
      throw new IllegalArgumentException("Objects must be of the same type");
    }

    // base schema attributes
    String baseUrn = left.getBaseUrn();
    Schema baseSchema = schemaRegistry.getSchema(baseUrn);

    // for each attribute compare them
    baseSchema.getAttributes().forEach(attribute -> {
      patchOperations.addAll(processAttribute(null, emptyList(), attribute, left, right));
    });

    // Extensions attributes
    Set<String> schemas = new HashSet<>();
    schemas.addAll(left.getExtensions().keySet());
    schemas.addAll(right.getExtensions().keySet());

    schemas.forEach(urn -> {
      Schema extSchema = schemaRegistry.getSchema(urn);
      ScimExtension leftExt = left.getExtensions().get(urn);
      ScimExtension rightExt = right.getExtensions().get(urn);

      if (leftExt == null) {
        patchOperations.add(new PatchOperation()
          .setPath(new PatchOperationPath(new ValuePathExpression(new AttributeReference(attributePath(extSchema, emptyList(), null)))))
          .setOperation(PatchOperation.Type.ADD)
          .setValue(rightExt));
      } else if (rightExt == null) {
        patchOperations.add(new PatchOperation()
          .setPath(new PatchOperationPath(new ValuePathExpression(new AttributeReference(attributePath(extSchema, emptyList(), null)))))
          .setOperation(PatchOperation.Type.REMOVE));
      } else {
        // for each attribute compare them
        extSchema.getAttributes().forEach(attribute -> {
          patchOperations.addAll(processAttribute(extSchema, emptyList(), attribute, leftExt, rightExt));
        });
      }
    });
    return patchOperations;
  }