<?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>Users in MidPoint Advanced</name>
    <description>List users in MidPoint with advanced input parameters.</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>activation/administrativeStatus</q:path>
                        <expression>
                            <queryInterpretationOfNoValue>filterAll</queryInterpretationOfNoValue>
                            <path>$activation</path>
                        </expression>
                    </q:equal>
                    <q:equal>
                        <q:path>name</q:path>
                        <expression>
                            <queryInterpretationOfNoValue>filterAll</queryInterpretationOfNoValue>
                            <path>$name</path>
                        </expression>
                    </q:equal>
                    <q:ref>
                        <q:path>assignment/targetRef</q:path>
                        <expression>
                            <queryInterpretationOfNoValue>filterAll</queryInterpretationOfNoValue>
                            <path>$organizationRef</path>
                        </expression>
                    </q:ref>
                    <q:ref>
                        <q:path>assignment/targetRef</q:path>
                        <expression>
                            <queryInterpretationOfNoValue>filterAll</queryInterpretationOfNoValue>
                            <path>$roleRef</path>
                        </expression>
                    </q:ref>
                    <q:ref>
                        <q:path>assignment/construction/resourceRef</q:path>
                        <expression>
                            <queryInterpretationOfNoValue>filterAll</queryInterpretationOfNoValue>
                            <path>$resourceRef</path>
                        </expression>
                    </q:ref>
                </q:and>
            </filter>
        </collection>
        <view>
            <column>
                <name>nameColumn</name>
                <path>name</path>
            </column>
            <column>
                <name>fullNameColumn</name>
                <path>fullName</path>
                <previousColumn>nameColumn</previousColumn>
            </column>
            <column>
                <name>activationColumn</name>
                <path>activation/administrativeStatus</path>
                <previousColumn>fullNameColumn</previousColumn>
            </column>
            <column>
                <name>roleColumn</name>
                <path>assignment</path>
                <display>
                    <label>Role</label>
                </display>
                <previousColumn>activationColumn</previousColumn>
                <export>
                    <expression>
                        <script>
                            <code>
                                if (input != null){
                                roles = report.resolveRoles(input);
                                if (roles.isEmpty()) {
                                return null;
                                }
                                list = new ArrayList();
                                for (role in roles) {
                                list.add(role.getName());
                                }
                                return list;
                                }
                            </code>
                        </script>
                    </expression>
                </export>
            </column>
            <column>
                <name>orgColumn</name>
                <path>assignment</path>
                <display>
                    <label>Organization</label>
                </display>
                <previousColumn>roleColumn</previousColumn>
                <export>
                    <expression>
                        <script>
                            <code>
                                if (input != null){
                                orgs = report.resolveOrgs(input);
                                if (orgs.isEmpty()) {
                                return null;
                                }
                                list = new ArrayList();
                                for (org in orgs) {
                                list.add(org.getName());
                                }
                                return list;
                                }
                            </code>
                        </script>
                    </expression>
                </export>
            </column>
            <column>
                <name>accountColumn</name>
                <path>linkRef</path>
                <display>
                    <label>Account</label>
                </display>
                <previousColumn>orgColumn</previousColumn>
                <export>
                    <expression>
                        <script>
                            <code>
                                import com.evolveum.midpoint.schema.SelectorOptions;
                                import com.evolveum.midpoint.schema.GetOperationOptions;
                                import com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType;

                                if (input != null){
                                list = new ArrayList();
                                for (linkRef in input){
                                shadow = midpoint.getObject(ShadowType.class, linkRef.getOid(),
                                SelectorOptions.createCollection(GetOperationOptions.createNoFetch().resolveNames(true)));
                                list.add(shadow.getName().getOrig() + "(Resource: " + shadow.getResourceRef().getTargetName()?.getOrig() + ")");
                                }
                                return list;
                                }
                            </code>
                        </script>
                    </expression>
                </export>
            </column>
            <type>UserType</type>
        </view>
        <parameter>
            <name>activation</name>
            <type>ActivationStatusType</type>
            <display>
                <label>
                    <t:orig>activation</t:orig>
                    <t:norm>activation</t:norm>
                    <t:translation>
                        <t:key>ActivationType.administrativeStatus</t:key>
                    </t:translation>
                </label>
            </display>
        </parameter>
        <parameter>
            <name>organizationRef</name>
            <type>c:ObjectReferenceType</type>
            <targetType>c:OrgType</targetType>
            <display>
                <label>
                    <t:orig>organization</t:orig>
                    <t:norm>organization</t:norm>
                    <t:translation>
                        <t:key>ObjectTypeGuiDescriptor.org</t:key>
                    </t:translation>
                </label>
            </display>
        </parameter>
        <parameter>
            <name>roleRef</name>
            <type>c:ObjectReferenceType</type>
            <targetType>c:RoleType</targetType>
            <display>
                <label>
                    <t:orig>role</t:orig>
                    <t:norm>role</t:norm>
                    <t:translation>
                        <t:key>ObjectTypeGuiDescriptor.role</t:key>
                    </t:translation>
                </label>
            </display>
        </parameter>
        <parameter>
            <name>resourceRef</name>
            <type>c:ObjectReferenceType</type>
            <targetType>c:ResourceType</targetType>
            <display>
                <label>
                    <t:orig>resource</t:orig>
                    <t:norm>resource</t:norm>
                    <t:translation>
                        <t:key>ObjectTypeGuiDescriptor.resource</t:key>
                    </t:translation>
                </label>
            </display>
        </parameter>
        <parameter>
            <name>name</name>
            <type>string</type>
            <display>
                <label>
                    <t:orig>name</t:orig>
                    <t:norm>name</t:norm>
                    <t:translation>
                        <t:key>ObjectType.name</t:key>
                    </t:translation>
                </label>
            </display>
        </parameter>
    </objectCollection>
    <fileFormat>
        <type>html</type>
    </fileFormat>
</report>
