<?xml version="1.0" encoding="UTF-8"?>
<!--
  ~ Copyright (c) 2024 Evolveum
  ~
  ~ Licensed under the Apache License, Version 2.0 (the "License");
  ~ you may not use this file except in compliance with the License.
  ~ You may obtain a copy of the License at
  ~
  ~     http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing, software
  ~ distributed under the License is distributed on an "AS IS" BASIS,
  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  ~ See the License for the specific language governing permissions and
  ~ limitations under the License.
  -->
<report xmlns="http://midpoint.evolveum.com/xml/ns/public/common/common-3"
        xmlns:c="http://midpoint.evolveum.com/xml/ns/public/common/common-3"
        xmlns:icfs="http://midpoint.evolveum.com/xml/ns/public/connector/icf-1/resource-schema-3"
        xmlns:org="http://midpoint.evolveum.com/xml/ns/public/common/org-3"
        xmlns:q="http://prism.evolveum.com/xml/ns/public/query-3"
        xmlns:ri="http://midpoint.evolveum.com/xml/ns/public/resource/instance-3"
        xmlns:t="http://prism.evolveum.com/xml/ns/public/types-3"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        oid="0080b237-b8e3-4e3c-8209-d1fcc5fad186">
    <name>Audit report with changed attributes</name>
    <assignment>
        <targetRef oid="00000000-0000-0000-0000-000000000171" relation="default" type="ArchetypeType">
            <!-- Collection report -->
        </targetRef>
    </assignment>
    <archetypeRef oid="00000000-0000-0000-0000-000000000171" relation="default" type="ArchetypeType">
        <!-- Collection report -->
    </archetypeRef>
    <roleMembershipRef oid="00000000-0000-0000-0000-000000000171" relation="default" type="ArchetypeType">
        <!-- Collection report -->
    </roleMembershipRef>
    <objectCollection>
        <collection>
            <collectionRef oid="00000000-0000-0000-0001-000000000002" relation="default" type="ObjectCollectionType">
                <!-- Modification audit records -->
            </collectionRef>
        </collection>
        <view>
            <column>
                <name>timestampColumn</name>
                <path>timestamp</path>
            </column>
            <column>
                <name>targetColumn</name>
                <path>targetRef</path>
                <previousColumn>timestampColumn</previousColumn>
            </column>
            <column>
                <name>changedAttributesColumn</name>
                <path>delta</path>
                <display>
                    <label>Changed attributes</label>
                </display>
                <previousColumn>targetColumn</previousColumn>
                <export>
                    <expression>
                        <script>
                            <code>
                           import com.evolveum.midpoint.schema.ObjectDeltaOperation;
                           import com.evolveum.midpoint.prism.delta.ItemDelta;
                           import com.evolveum.midpoint.prism.path.ItemPath;
                           import com.evolveum.midpoint.xml.ns._public.common.common_3.*;
                           import com.evolveum.midpoint.schema.DeltaConvertor;

                           ret = new ArrayList();
                           for (ObjectDeltaOperationType deltaType : input) {
                              delta = DeltaConvertor.createObjectDeltaOperation(deltaType, midpoint.getPrismContext());
                              for (ItemDelta itemDelta : delta.getObjectDelta().getModifications()){
                                 if(!itemDelta.getPath().toString().startsWith("metadata")) {
                                    stringItem = itemDelta.getElementName().toString();
                                    if (itemDelta.isAdd()) {
                                       stringItem = stringItem + " (Add values: ";
                                       for (addValue in itemDelta.getValuesToAdd()) {
                                          stringItem = stringItem + addValue.getRealValue()
                                          stringItem = stringItem + ","
                                       }
                                       stringItem = stringItem.substring(0, (stringItem.length()-1)) + ")"
                                    }

                                    if (itemDelta.isDelete()) {
                                       stringItem = stringItem + " (Delete values: ";
                                       for (deleteValue in itemDelta.getValuesToDelete()) {
                                          stringItem = stringItem + deleteValue.getRealValue()
                                          stringItem = stringItem + "'"
                                       }
                                       stringItem = stringItem.substring(0, (stringItem.length()-1)) + ")"
                                    }

                                    if (itemDelta.isReplace()) {
                                       stringItem = stringItem + " (Replace values: ";
                                       for (replaceValue in itemDelta.getValuesToReplace()) {
                                          stringItem = stringItem + replaceValue.getRealValue()
                                          stringItem = stringItem + ","
                                       }
                                       stringItem = stringItem.substring(0, (stringItem.length()-1)) + ")"
                                    }
                                    ret.add(stringItem);
                                 }
                              }
                           }
                           return ret
                            </code>
                        </script>
                    </expression>
                </export>
            </column>
        </view>
    </objectCollection>
</report>
