<?xml version="1.0" encoding="UTF-8"?>
<!--
  ~ Copyright (c) 2021 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:org="http://midpoint.evolveum.com/xml/ns/public/common/org-3"
        xmlns:q="http://prism.evolveum.com/xml/ns/public/query-3"
        xmlns:t="http://prism.evolveum.com/xml/ns/public/types-3">
    <name>Workflow report</name>
    <description>Report made from audit records - grouping items in workflow with a single request in each wf etc.</description>
    <assignment>
        <targetRef oid="00000000-0000-0000-0000-000000000171" relation="org:default" type="c:ArchetypeType"/>
    </assignment>
    <objectCollection>
        <collection>
            <filter>
                <q:and>
                    <q:equal>
                        <q:path>eventType</q:path>
                        <q:value>workItem</q:value>
                    </q:equal>
                    <q:equal xmlns="">
                        <q:path>eventStage</q:path>
                        <expression xmlns="">
                            <queryInterpretationOfNoValue>filterAll</queryInterpretationOfNoValue>
                            <script xmlns="">
                                <code>
                                    if (!eventStage) {
                                    return null;
                                    }
                                    return eventStage;
                                </code>
                            </script>
                        </expression>
                    </q:equal>
                    <q:ref xmlns="">
                        <q:path>targetRef</q:path>
                        <expression xmlns="">
                            <queryInterpretationOfNoValue>filterAll</queryInterpretationOfNoValue>
                            <script xmlns="">
                                <objectVariableMode>prismReference</objectVariableMode>
                                <code>
                                    import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectReferenceType;

                                    if (!target) {
                                    return null;
                                    }

                                    ObjectReferenceType ort = new ObjectReferenceType();
                                    ort.setOid(target.getOid());
                                    ort.setRelation(target.getRelation());
                                    ort.setType(target.getTargetType());
                                    return ort;
                                </code>
                            </script>
                        </expression>
                    </q:ref>
                    <q:ref xmlns="">
                        <q:path>initiatorRef</q:path>
                        <expression xmlns="">
                            <queryInterpretationOfNoValue>filterAll</queryInterpretationOfNoValue>
                            <script xmlns="">
                                <objectVariableMode>prismReference</objectVariableMode>
                                <code>
                                    import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectReferenceType;

                                    if (!initiator) {
                                    return null;
                                    }

                                    ObjectReferenceType ort = new ObjectReferenceType();
                                    ort.setOid(initiator.getOid());
                                    ort.setRelation(initiator.getRelation());
                                    ort.setType(initiator.getTargetType());
                                    return ort;
                                </code>
                            </script>
                        </expression>
                    </q:ref>
                    <q:or>
                        <q:equal>
                            <q:path>outcome</q:path>
                            <q:value>success</q:value>
                        </q:equal>
                        <q:equal>
                            <q:path>outcome</q:path>
                            <q:value>warning</q:value>
                        </q:equal>
                        <q:equal>
                            <q:path>outcome</q:path>
                            <q:value>partial_error</q:value>
                        </q:equal>
                        <q:equal>
                            <q:path>outcome</q:path>
                            <q:value>handled_error</q:value>
                        </q:equal>
                    </q:or>
                    <q:greater xmlns="">
                        <q:path>timestamp</q:path>
                        <expression xmlns="">
                            <queryInterpretationOfNoValue>filterAll</queryInterpretationOfNoValue>
                            <script xmlns="">
                                <code>
                                    if (!from) {
                                    return null;
                                    }
                                    return from;
                                </code>
                            </script>
                        </expression>
                    </q:greater>
                    <q:less xmlns="">
                        <q:path>timestamp</q:path>
                        <expression xmlns="">
                            <queryInterpretationOfNoValue>filterAll</queryInterpretationOfNoValue>
                            <script xmlns="">
                                <code>
                                    if (!to) {
                                    return null;
                                    }
                                    return to;
                                </code>
                            </script>
                        </expression>
                    </q:less>
                </q:and>
            </filter>
        </collection>
        <view>
            <column>
                <name>workItemIDColumn</name>
                <display>
                    <label>Work item ID</label>
                </display>
                <export>
                    <expression>
                        <script>
                            <code>
                                import com.evolveum.midpoint.wf.api.WorkflowConstants;

                                if (object != null) {
                                for (property in object.getProperty()){
                                if (WorkflowConstants.AUDIT_WORK_ITEM_ID.equals(property.getName())) {
                                return property.getValue()
                                }
                                }
                                }
                            </code>
                        </script>
                    </expression>
                </export>
            </column>
            <column>
                <name>timestampColumn</name>
                <path>timestamp</path>
                <previousColumn>workItemIDColumn</previousColumn>
            </column>
            <column>
                <name>eventStageColumn</name>
                <path>eventStage</path>
                <previousColumn>timestampColumn</previousColumn>
            </column>
            <column>
                <name>deltaColumn</name>
                <path>delta</path>
                <display>
                    <label>Action</label>
                </display>
                <previousColumn>eventStageColumn</previousColumn>
            </column>
            <column>
                <name>initiatorRefColumn</name>
                <path>initiatorRef</path>
                <previousColumn>deltaColumn</previousColumn>
            </column>
            <column>
                <name>targetRefColumn</name>
                <path>targetRef</path>
                <display>
                    <label>Requested For</label>
                </display>
                <previousColumn>initiatorRefColumn</previousColumn>
            </column>
            <column>
                <name>referenceColumn</name>
                <path>reference</path>
                <previousColumn>targetRefColumn</previousColumn>
                <export>
                    <expression>
                        <script>
                            <code>
                                import com.evolveum.midpoint.report.impl.ReportUtils;

                                if (input != null) {
                                list = new ArrayList();
                                for (ref in input) {
                                for (value in ref.getValue()) {
                                list.add(value.getType().getLocalPart() + ":" + value.getTargetName() + "(" + value.getOid() + ")");
                                }
                                }
                                return list;
                                }
                            </code>
                        </script>
                    </expression>
                </export>
            </column>
            <column>
                <name>messageColumn</name>
                <path>message</path>
                <previousColumn>referenceColumn</previousColumn>
            </column>
            <type xmlns:aud="http://midpoint.evolveum.com/xml/ns/public/common/audit-3">aud:AuditEventRecordType</type>
        </view>
        <parameter>
            <name>eventStage</name>
            <type>AuditEventStageType</type>
            <display>
                <label>
                    <t:orig>eventStage</t:orig>
                    <t:norm>eventstage</t:norm>
                    <t:translation>
                        <t:key>AuditEventRecordType.eventStage</t:key>
                    </t:translation>
                </label>
            </display>
        </parameter>
        <parameter>
            <name>from</name>
            <type>dateTime</type>
            <display>
                <label>
                    <t:orig>from</t:orig>
                    <t:norm>from</t:norm>
                    <t:translation>
                        <t:key>AuditPopupPanel.dateFrom</t:key>
                    </t:translation>
                </label>
            </display>
        </parameter>
        <parameter>
            <name>to</name>
            <type>dateTime</type>
            <display>
                <label>
                    <t:orig>to</t:orig>
                    <t:norm>to</t:norm>
                    <t:translation>
                        <t:key>AuditPopupPanel.dateTo</t:key>
                    </t:translation>
                </label>
            </display>
        </parameter>
        <parameter>
            <name>target</name>
            <type>c:ObjectReferenceType</type>
            <display>
                <label>
                    <t:orig>targetRef</t:orig>
                    <t:norm>targetref</t:norm>
                    <t:translation>
                        <t:key>AuditEventRecordType.targetRef</t:key>
                    </t:translation>
                </label>
            </display>
        </parameter>
        <parameter>
            <name>initiator</name>
            <type>c:ObjectReferenceType</type>
            <display>
                <label>
                    <t:orig>initiatorRef</t:orig>
                    <t:norm>initiatorref</t:norm>
                    <t:translation>
                        <t:key>AuditEventRecordType.initiatorRef</t:key>
                    </t:translation>
                </label>
            </display>
        </parameter>
    </objectCollection>
    <fileFormat>
        <type>csv</type>
        <csv>
            <multivalueDelimiter>,</multivalueDelimiter>
            <fieldDelimiter>;</fieldDelimiter>
            <quote>'</quote>
            <quoteMode>all</quoteMode>
            <header>true</header>
        </csv>
    </fileFormat>
</report>
