<?xml version="1.0" encoding="UTF-8"?>
<!--
  ~ Copyright (C) 2010-2024 Evolveum and contributors
  ~
  ~ This work is dual-licensed under the Apache License 2.0
  ~ and European Union Public License. See LICENSE file for details.
  -->
<objectTemplate oid="00000000-0000-0000-0000-000000000380"
        xmlns="http://midpoint.evolveum.com/xml/ns/public/common/common-3"
        xmlns:q="http://prism.evolveum.com/xml/ns/public/query-3">
    <name>Person Object Template</name>
    <description>Object template for Person users, according to ExAmPLE configuration from midPoint book.</description>

    <iterationSpecification>
        <maxIterations>99</maxIterations>
        <tokenExpression>
            <description>The iteration starts with empty string, then a number is appended, starting with 2 (skipping 1), 3, ... up to 99.</description>
            <script>
                <code>
                    if (iteration == 0) {
                        return ""
                    } else {
                        return iteration + 1
                    }
                </code>
            </script>
        </tokenExpression>
    </iterationSpecification>

    <item>
        <ref>name</ref>
        <mapping>
            <name>generate-name-jsmith</name>
            <description>Generate unique login name using jsmith and iterator. Does not change the login name when user is renamed.</description>
            <strength>weak</strength> <!-- do not change name automatically if user's givenName/familyName changes -->
            <source>
                <path>givenName</path>
            </source>
            <source>
                <path>familyName</path>
            </source>
            <expression>
                <script>
                    <code>
                        tmpGivenName = basic.trim(basic.norm(basic.stringify(givenName)))
                        tmpFamilyName  = basic.trim(basic.norm(basic.stringify(familyName)))
                        tmpGivenNameInitial = tmpGivenName?.take(1)
                        return (tmpGivenNameInitial + tmpFamilyName?.replaceAll(" ", ""))?.take(8) + iterationToken // no spaces
                    </code>
                </script>
            </expression>
        </mapping>
    </item>

    <item>
        <ref>fullName</ref>
        <mapping>
            <name>generate-fullname</name>
            <description>Generate fullName (enforcing on renames because of strong mapping)</description>
            <strength>strong</strength>
            <source>
                <path>givenName</path>
            </source>
            <source>
                <path>familyName</path>
            </source>
            <expression>
                <script>
                    <code>
                        basic.concatName(givenName, familyName)
                    </code>
                </script>
            </expression>
            <target>
                <path>fullName</path>
            </target>
        </mapping>
    </item>

    <item>
        <ref>givenName</ref>
        <displayName>First Name</displayName>
    </item>

    <item>
        <ref>additionalName</ref>
        <displayName>Middle Name</displayName>
    </item>

    <item>
        <ref>familyName</ref>
        <displayName>Last Name</displayName>
    </item>

    <item>
        <ref>emailAddress</ref>
        <mapping>
            <name>generate-email</name>
            <strength>weak</strength>
            <source>
                <path>givenName</path>
            </source>
            <source>
                <path>familyName</path>
            </source>
            <expression>
                <script>
                    <code>
                        if ( givenName == null &amp;&amp; familyName == null ) { return null }
                        if ( familyName == null ) { return givenName?.norm + iterationToken + '@example.com' }
                        if ( givenName == null ) { return familyName?.norm + iterationToken + '@example.com' }
                        givenName?.norm + '.' + familyName?.norm + iterationToken + '@example.com'
                    </code>
                </script>
            </expression>
        </mapping>
    </item>

    <item>
        <ref>assignment</ref>
        <mapping>
            <lifecycleState>draft</lifecycleState>
            <name>autoassign-jobcode</name>
            <strength>strong</strength>
            <source>
                <path>extension/jobCode</path>
            </source>
            <expression>
                <assignmentTargetSearch>
                    <targetType>RoleType</targetType>
                    <filter>
                        <q:text>extension/autoassignJobCode = $jobCode</q:text>
                    </filter>
                </assignmentTargetSearch>
            </expression>
        </mapping>
    </item>

</objectTemplate>
