<?xml version="1.0" encoding="UTF-8"?>
<!--
  ~ Copyright (c) 2010-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.
  -->
<objectTemplate xmlns="http://midpoint.evolveum.com/xml/ns/public/common/common-3"
                xmlns:q="http://prism.evolveum.com/xml/ns/public/query-3"
                oid="a5a2c13d-a549-43eb-b260-fbfacbc834fc">
    <name>Default user template</name>

    <mapping>
        <name>compute-accountsCount</name>
        <documentation>Computes number of accounts assigned to user - on all resources. Just linkRef count.
            Mapping processed during recomputation only.
        </documentation>
        <strength>strong</strength>
<!-- channels can be defined to optimize recomputation when mapping is triggered -->
<!--        <channel>http://midpoint.evolveum.com/xml/ns/public/common/channels-3#recompute</channel>-->
<!--        <channel>http://midpoint.evolveum.com/xml/ns/public/common/channels-3#user</channel>-->
        <source>
            <path>linkRef</path>
        </source>
        <expression>
            <script>
                <relativityMode>absolute</relativityMode>
                <code>
                    if (basic.isEmpty(linkRef)) {
                        return 0
                    }
                    return linkRef.size()       // info - to run this, the relativityMode must be absolute
                </code>
            </script>
        </expression>
        <target>
            <path>extension/accountsCount</path>
        </target>
    </mapping>

    <mapping>
        <name>compute-rolesCount</name>
        <documentation>Computes number of roles actively assigned to user. Count of RoleType in roleMembershipRef.
            Mapping processed during recomputation only.
        </documentation>
        <strength>strong</strength>
<!-- channels can be defined to optimize recomputation when mapping is triggered -->
<!--        <channel>http://midpoint.evolveum.com/xml/ns/public/common/channels-3#recompute</channel>-->
<!--        <channel>http://midpoint.evolveum.com/xml/ns/public/common/channels-3#user</channel>-->
        <source>
            <path>roleMembershipRef</path>
        </source>
        <expression>
            <script>
                <relativityMode>absolute</relativityMode>
                <code>
                    import com.evolveum.midpoint.xml.ns._public.common.common_3.RoleType

                    if (basic.isEmpty(roleMembershipRef)) {
                        return 0
                    }

                    // Iterate over roleMembershipRef
                    int indirectRoleAssignmentCount = 0
                    focus.roleMembershipRef.each { roleRef ->
                        // Check if the reference type is Role
                        if (roleRef.type?.localPart == "RoleType") {
                            indirectRoleAssignmentCount++
                        }
                    }
                    return indirectRoleAssignmentCount
                </code>
            </script>
        </expression>
        <target>
            <path>extension/rolesCount</path>
        </target>
    </mapping>

    <mapping>
        <name>compute-appsCount</name>
        <documentation>Computes number of roles actively assigned to user. Count of ServiceType in roleMembershipRef.
            Mapping processed during recomputation only.
        </documentation>
        <strength>strong</strength>
<!-- channels can be defined to optimize recomputation when mapping is triggered -->
<!--        <channel>http://midpoint.evolveum.com/xml/ns/public/common/channels-3#recompute</channel>-->
<!--        <channel>http://midpoint.evolveum.com/xml/ns/public/common/channels-3#user</channel>-->
        <source>
            <path>roleMembershipRef</path>
        </source>
        <expression>
            <script>
                <relativityMode>absolute</relativityMode>
                <code>
                    import com.evolveum.midpoint.xml.ns._public.common.common_3.ServiceType

                    if (basic.isEmpty(roleMembershipRef)) {
                        return 0
                    }

                    // Iterate over roleMembershipRef
                    int indirectServiceAssignmentCount = 0
                    focus.roleMembershipRef.each { roleRef ->
                        // Check if the reference type is Service
                        if (roleRef.type?.localPart == "ServiceType") {
                            indirectServiceAssignmentCount++
                        }
                    }
                    return indirectServiceAssignmentCount
                </code>
            </script>
        </expression>
        <target>
            <path>extension/appsCount</path>
        </target>
    </mapping>

    <!-- set computed attributes read only -->
    <item>
        <ref>extension/accountsCount</ref>
        <limitations>
            <layer>presentation</layer>
            <access>
                <read>true</read>
                <add>false</add>
                <modify>false</modify>
            </access>
        </limitations>
    </item>
    <item>
        <ref>extension/rolesCount</ref>
        <limitations>
            <layer>presentation</layer>
            <access>
                <read>true</read>
                <add>false</add>
                <modify>false</modify>
            </access>
        </limitations>
    </item>
    <item>
        <ref>extension/appsCount</ref>
        <limitations>
            <layer>presentation</layer>
            <access>
                <read>true</read>
                <add>false</add>
                <modify>false</modify>
            </access>
        </limitations>
    </item>

</objectTemplate>