<?xml version="1.0" encoding="UTF-8"?>
<!--
  ~ Copyright (c) 2010-2019 Evolveum and contributors
  ~
  ~ Licensed under the EUPL-1.2 or later.
  -->

<report xmlns="http://midpoint.evolveum.com/xml/ns/public/common/common-3"
        xmlns:q="http://prism.evolveum.com/xml/ns/public/query-3"
        xmlns:c="http://midpoint.evolveum.com/xml/ns/public/common/common-3"
        xmlns:t="http://prism.evolveum.com/xml/ns/public/types-3"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        oid="00000000-0000-0000-0000-000000000160">
    <name>Certification work items report</name>
    <description>Work items created for certification campaign(s).</description>
    <assignment id="1">
        <identifier>archetype</identifier>
        <targetRef oid="00000000-0000-0000-0000-000000000171" type="ArchetypeType"/>
    </assignment>
    <objectCollection>
        <collection>
            <filter>
                <q:text>
                    . ownedByOid yaml```
                        queryInterpretationOfNoValue: filterAll
                        script:
                            objectVariableMode: prismReference
                            code: campaignRef?.oid
                    ```
                </q:text>
            </filter>
        </collection>
        <view>
            <!-- We do sorting on object name. Hence, it is the first column. The campaign name would be more logical as the
                 first one, but we currently cannot sort on both campaign name and object name. And perhaps the primary use
                 case will be reporting on a specific campaign. -->
            <column id="2">
                <name>object</name>
                <path>objectRef</path>
                <display>
                    <label>
                        <orig>Object</orig>
                        <translation>
                            <key>reportCertificationWorkItems.objectColumn.label</key>
                        </translation>
                    </label>
                </display>
                <export>
                    <expression>
                        <script>
                            <objectVariableMode>prismReference</objectVariableMode>
                            <code>
                                import com.evolveum.midpoint.report.impl.ReportUtils

                                // Provides the type plus object name
                                ReportUtils.prettyPrintForReport(input)
                            </code>
                        </script>
                    </expression>
                </export>
            </column>
            <column id="3">
                <name>target</name>
                <path>targetRef</path>
                <display>
                    <label>
                        <orig>Target</orig>
                        <translation>
                            <key>reportCertificationWorkItems.targetColumn.label</key>
                        </translation>
                    </label>
                </display>
                <previousColumn>object</previousColumn>
                <export>
                    <expression>
                        <script>
                            <objectVariableMode>prismReference</objectVariableMode>
                            <code>
                                import com.evolveum.midpoint.report.impl.ReportUtils

                                // Provides the type plus object name
                                ReportUtils.prettyPrintForReport(input)
                            </code>
                        </script>
                    </expression>
                </export>
            </column>
            <column id="4">
                <name>campaignName</name>
                <display>
                    <label>
                        <orig>Campaign</orig>
                        <translation>
                            <key>reportCertificationWorkItems.campaignNameColumn.label</key>
                        </translation>
                    </label>
                </display>
                <previousColumn>target</previousColumn>
                <export>
                    <expression>
                        <script>
                            <code>
                                import com.evolveum.midpoint.schema.util.CertCampaignTypeUtil
                                import com.evolveum.midpoint.xml.ns._public.common.common_3.AccessCertificationCaseType

                                CertCampaignTypeUtil.getCampaign(object as AccessCertificationCaseType)?.name?.orig
                            </code>
                        </script>
                    </expression>
                </export>
            </column>
            <column id="5">
                <name>iteration</name>
                <display>
                    <label>
                        <orig>Iteration</orig>
                        <translation>
                            <key>reportCertificationWorkItems.iterationColumn.label</key>
                        </translation>
                    </label>
                </display>
                <previousColumn>campaignName</previousColumn>
                <export>
                    <expression>
                        <script>
                            <code><![CDATA[
                                import com.evolveum.midpoint.xml.ns._public.common.common_3.AccessCertificationWorkItemType

                                (workItems as Collection<AccessCertificationWorkItemType>)[0].iteration
                            ]]></code>
                        </script>
                    </expression>
                </export>
            </column>
            <column id="6">
                <name>stageNumber</name>
                <display>
                    <label>
                        <orig>Stage number</orig>
                        <translation>
                            <key>reportCertificationWorkItems.stageNumberColumn.label</key>
                        </translation>
                    </label>
                </display>
                <previousColumn>iteration</previousColumn>
                <export>
                    <expression>
                        <script>
                            <code><![CDATA[
                                import com.evolveum.midpoint.xml.ns._public.common.common_3.AccessCertificationWorkItemType

                                (workItems as Collection<AccessCertificationWorkItemType>)[0].stageNumber
                            ]]></code>
                        </script>
                    </expression>
                </export>
            </column>
            <column id="7">
                <name>originalAssigneeRef</name>
                <display>
                    <label>
                        <orig>Original assignee</orig>
                        <translation>
                            <key>reportCertificationWorkItems.originalAssigneeRefColumn.label</key>
                        </translation>
                    </label>
                </display>
                <previousColumn>stageNumber</previousColumn>
                <export>
                    <expression>
                        <script>
                            <code><![CDATA[
                                import com.evolveum.midpoint.xml.ns._public.common.common_3.AccessCertificationWorkItemType

                                (workItems as Collection<AccessCertificationWorkItemType>)[0].originalAssigneeRef
                            ]]></code>
                        </script>
                    </expression>
                </export>
            </column>
            <column id="8">
                <name>deadline</name>
                <display>
                    <label>
                        <orig>Deadline</orig>
                        <translation>
                            <key>reportCertificationWorkItems.deadlineColumn.label</key>
                        </translation>
                    </label>
                </display>
                <previousColumn>originalAssigneeRef</previousColumn>
                <export>
                    <expression>
                        <script>
                            <code><![CDATA[
                                import com.evolveum.midpoint.xml.ns._public.common.common_3.AccessCertificationWorkItemType

                                (workItems as Collection<AccessCertificationWorkItemType>)[0].deadline
                            ]]></code>
                        </script>
                    </expression>
                </export>
            </column>
            <column id="9">
                <name>assigneeRef</name>
                <display>
                    <label>
                        <orig>Current assignee(s)</orig>
                        <translation>
                            <key>reportCertificationWorkItems.assigneeRefColumn.label</key>
                        </translation>
                    </label>
                </display>
                <previousColumn>deadline</previousColumn>
                <export>
                    <expression>
                        <script>
                            <code><![CDATA[
                                import com.evolveum.midpoint.xml.ns._public.common.common_3.AccessCertificationWorkItemType

                                (workItems as Collection<AccessCertificationWorkItemType>)[0].assigneeRef
                            ]]></code>
                        </script>
                    </expression>
                </export>
            </column>
            <column id="10">
                <name>escalationLevel</name>
                <display>
                    <label>
                        <orig>Escalation level</orig>
                        <translation>
                            <key>reportCertificationWorkItems.escalationLevelColumn.label</key>
                        </translation>
                    </label>
                </display>
                <previousColumn>assigneeRef</previousColumn>
                <export>
                    <expression>
                        <script>
                            <code><![CDATA[
                                import com.evolveum.midpoint.xml.ns._public.common.common_3.AccessCertificationWorkItemType

                                (workItems as Collection<AccessCertificationWorkItemType>)[0].escalationLevel?.number
                            ]]></code>
                        </script>
                    </expression>
                </export>
            </column>
            <column id="11">
                <name>performerRef</name>
                <display>
                    <label>
                        <orig>Performer</orig>
                        <translation>
                            <key>reportCertificationWorkItems.performerRefColumn.label</key>
                        </translation>
                    </label>
                </display>
                <previousColumn>escalationLevel</previousColumn>
                <export>
                    <expression>
                        <script>
                            <code><![CDATA[
                                import com.evolveum.midpoint.xml.ns._public.common.common_3.AccessCertificationWorkItemType

                                (workItems as Collection<AccessCertificationWorkItemType>)[0].performerRef
                            ]]></code>
                        </script>
                    </expression>
                </export>
            </column>
            <column id="12">
                <name>outcome</name>
                <display>
                    <label>
                        <orig>Outcome</orig>
                        <translation>
                            <key>reportCertificationWorkItems.outcomeColumn.label</key>
                        </translation>
                    </label>
                </display>
                <previousColumn>performerRef</previousColumn>
                <export>
                    <expression>
                        <script>
                            <code><![CDATA[
                                import com.evolveum.midpoint.report.impl.ReportUtils
                                import com.evolveum.midpoint.xml.ns._public.common.common_3.AccessCertificationWorkItemType

                                def output = (workItems as Collection<AccessCertificationWorkItemType>)[0].output
                                ReportUtils.prettyPrintCertOutcomeForReport(output, false)
                            ]]></code>
                        </script>
                    </expression>
                </export>
            </column>
            <column id="13">
                <name>comment</name>
                <display>
                    <label>
                        <orig>Comment</orig>
                        <translation>
                            <key>reportCertificationWorkItems.commentColumn.label</key>
                        </translation>
                    </label>
                </display>
                <previousColumn>outcome</previousColumn>
                <export>
                    <expression>
                        <script>
                            <code><![CDATA[
                                import com.evolveum.midpoint.xml.ns._public.common.common_3.AccessCertificationWorkItemType

                                (workItems as Collection<AccessCertificationWorkItemType>)[0].output?.comment
                            ]]></code>
                        </script>
                    </expression>
                </export>
            </column>
            <column id="14">
                <name>outputChangeTimestamp</name>
                <display>
                    <label>
                        <orig>Last changed on</orig>
                        <translation>
                            <key>reportCertificationWorkItems.outputChangeTimestampColumn.label</key>
                        </translation>
                    </label>
                </display>
                <previousColumn>comment</previousColumn>
                <export>
                    <expression>
                        <script>
                            <code><![CDATA[
                                import com.evolveum.midpoint.xml.ns._public.common.common_3.AccessCertificationWorkItemType

                                (workItems as Collection<AccessCertificationWorkItemType>)[0].outputChangeTimestamp
                            ]]></code>
                        </script>
                    </expression>
                </export>
            </column>
            <column id="15">
                <name>closeTimestamp</name>
                <display>
                    <label>
                        <orig>Closed on</orig>
                        <translation>
                            <key>reportCertificationWorkItems.closeTimestampColumn.label</key>
                        </translation>
                    </label>
                </display>
                <previousColumn>outputChangeTimestamp</previousColumn>
                <export>
                    <expression>
                        <script>
                            <code><![CDATA[
                                import com.evolveum.midpoint.xml.ns._public.common.common_3.AccessCertificationWorkItemType

                                (workItems as Collection<AccessCertificationWorkItemType>)[0].closeTimestamp
                            ]]></code>
                        </script>
                    </expression>
                </export>
            </column>
            <paging>
                <q:orderBy>objectRef/@/name</q:orderBy>
            </paging>
            <type>c:AccessCertificationCaseType</type>
        </view>
        <parameter id="16">
            <name>campaignRef</name>
            <type>c:ObjectReferenceType</type>
            <targetType>c:AccessCertificationCampaignType</targetType>
            <display>
                <label>
                    <t:orig>campaignName</t:orig>
                    <t:translation>
                        <t:key>runReportPopupContent.param.name.campaignName</t:key>
                    </t:translation>
                </label>
            </display>
        </parameter>
        <parameter id="17">
            <name>stageNumber</name>
            <type>xsd:int</type>
            <display>
                <label>
                    <t:orig>stageNumber</t:orig>
                    <t:translation>
                        <t:key>runReportPopupContent.param.name.stageNumber</t:key>
                    </t:translation>
                </label>
            </display>
        </parameter>
        <parameter id="18">
            <name>iteration</name>
            <type>xsd:int</type>
            <display>
                <label>
                    <t:orig>iteration</t:orig>
                    <t:translation>
                        <t:key>runReportPopupContent.param.name.iteration</t:key>
                    </t:translation>
                </label>
            </display>
        </parameter>
        <subreport id="19">
            <name>workItems</name>
            <type>AccessCertificationWorkItemType</type>
            <expression>
                <script>
                    <code>

                        import com.evolveum.midpoint.schema.util.CertCampaignTypeUtil
                        import com.evolveum.midpoint.xml.ns._public.common.common_3.AccessCertificationCaseType

                        def aCase = object as AccessCertificationCaseType
                        aCase.workItem
                            .findAll { stageNumber == null || it.stageNumber == stageNumber }
                            .findAll { iteration == null || CertCampaignTypeUtil.norm(iteration) == iteration }
                            .collect()
                    </code>
                </script>
            </expression>
            <order>10</order>
            <resultHandling>
                <multipleValues>splitParentRow</multipleValues>
                <noValues>removeParentRow</noValues>
            </resultHandling>
        </subreport>
    </objectCollection>
</report>
