<!--
  ~ Copyright (c) 2010-2017 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:c="http://midpoint.evolveum.com/xml/ns/public/common/common-3"
          xmlns:q="http://prism.evolveum.com/xml/ns/public/query-3"
          xmlns:icfc="http://midpoint.evolveum.com/xml/ns/public/connector/icf-1/connector-schema-3"
          xmlns:icfs="http://midpoint.evolveum.com/xml/ns/public/connector/icf-1/resource-schema-3"
          xmlns:t="http://prism.evolveum.com/xml/ns/public/types-3"
          xmlns:cap="http://midpoint.evolveum.com/xml/ns/public/resource/capabilities-3"
          xmlns:apti="http://midpoint.evolveum.com/xml/ns/public/common/api-types-3"
          xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
          xmlns:enc="http://www.w3.org/2001/04/xmlenc#"
          xmlns:ri="http://midpoint.evolveum.com/xml/ns/public/resource/instance-3"
          xmlns:cdoext="http://xml.uniba.sk/cdoext"
          xmlns:xsd="http://www.w3.org/2001/XMLSchema"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          oid="10000000-0000-0000-0210-000000000101">
   <name>User Template</name>

   <c:mapping>
      <c:strength>strong</c:strength>
      <c:source>
         <c:path>$user/givenName</c:path>
      </c:source>
      <c:source>
         <c:path>$user/familyName</c:path>
      </c:source>
      <c:source>
         <c:path>$user/honorificPrefix</c:path>
      </c:source>
      <c:source>
         <c:path>$user/honorificSuffix</c:path>
      </c:source>
      <c:expression>
          <allowEmptyValues>true</allowEmptyValues>        <!--  null zda sa ze nefunguje riadne -->
         <c:script>
            <c:code>name = (basic.stringify(familyName) + ' ' + basic.stringify(givenName) + ' ' + basic.stringify(honorificPrefix) + ' ' + basic.stringify(honorificSuffix)).trim()
            </c:code>
         </c:script>
      </c:expression>
      <c:target>
         <c:path>fullName</c:path>
      </c:target>
   </c:mapping>

    <!-- TODO if needed -->
       <!--<mapping>-->
        <!--<name>End user role for all CDO users</name>-->
        <!--<authoritative>true</authoritative>-->
        <!--<strength>strong</strength>-->
        <!--<source>-->
            <!--<path>subtype</path>-->
        <!--</source>-->
        <!--<expression>-->
            <!--<assignmentTargetSearch>-->
                <!--<targetType>RoleType</targetType>-->
                <!--<oid>10000000-0000-0000-0020-000000000001</oid> &lt;!&ndash; midpoint:endUser &ndash;&gt;-->
            <!--</assignmentTargetSearch>-->
        <!--</expression>-->
        <!--<target>-->
            <!--<path>assignment</path>-->
        <!--</target>-->
        <!--<condition>-->
            <!--<script>-->
                <!--<code>subtype == 'cdo'</code>-->
            <!--</script>-->
        <!--</condition>-->
    <!--</mapping>-->

    <!-- TODO: how to initialize vs. PoC? -->

     <mapping>
      <name>User - SAP Role mapping</name>
      <authoritative>true</authoritative>
      <strength>strong</strength>
      <source>
         <c:name>sapRole</c:name>
         <c:path xmlns:ext="http://xml.uniba.sk/cdoext">extension/sapRoles</c:path>
      </source>
      <expression>
         <script>
             <!-- <relativityMode>absolute</relativityMode> --><!-- FIX for multivalue -->
            <code>
           import com.evolveum.midpoint.xml.ns._public.common.common_3.AssignmentType;
           import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectReferenceType;
           import com.evolveum.midpoint.xml.ns._public.common.common_3.RoleType;
           import com.evolveum.midpoint.xml.ns._public.common.common_3.OrgType;
           import java.util.*;

           log.info("XXX assigning from SAP role {}" , sapRole)
           if (sapRole != null){
                orgName = 'r:'+sapRole;
                <!-- finding org -->
                org = midpoint.searchObjectByName(OrgType.class, orgName);
                log.info("org {}" , org)
                orgOrt = new ObjectReferenceType();
                orgOrt.setOid(org.getOid());
                orgOrt.setType(OrgType.COMPLEX_TYPE);
                <!-- creating assignment -->
                AssignmentType assignment = new AssignmentType();
                assignment.asPrismContainerValue()
                assignment.setTargetRef(orgOrt);

                return assignment
           }
           </code>
         </script>
      </expression>
      <target>
         <c:path>assignment</c:path>
      </target>
      <condition>
         <script>
            <code>
                log.info("XXX runing condition {}, sapRole {}" , (sapRole != null), sapRole)

                return sapRole != null
            </code>
         </script>
      </condition>
   </mapping>

    <mapping>
        <name>User - SAP Profile mapping</name>
        <authoritative>true</authoritative>
        <strength>strong</strength>
        <source>
            <c:name>sapProfile</c:name>
            <c:path xmlns:ext="http://xml.uniba.sk/cdoext">extension/sapProfiles</c:path>
        </source>
        <expression>
            <script>
                <!-- <relativityMode>absolute</relativityMode> --><!-- FIX for multivalue -->
                <code>
                    import com.evolveum.midpoint.xml.ns._public.common.common_3.AssignmentType;
                    import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectReferenceType;
                    import com.evolveum.midpoint.xml.ns._public.common.common_3.RoleType;
                    import com.evolveum.midpoint.xml.ns._public.common.common_3.OrgType;
                    import java.util.*;

                    log.info("XXX assigning from SAP role {}" , sapProfile)
                    if (sapProfile != null){
                        orgName = 'p:'+sapProfile;
                        <!-- finding org -->
                        org = midpoint.searchObjectByName(OrgType.class, orgName);
                        log.info("org {}" , org)
                        orgOrt = new ObjectReferenceType();
                        orgOrt.setOid(org.getOid());
                        orgOrt.setType(OrgType.COMPLEX_TYPE);
                        <!-- creating assignment -->
                        AssignmentType assignment = new AssignmentType();
                        assignment.asPrismContainerValue()
                        assignment.setTargetRef(orgOrt);

                        return assignment
                    }
                </code>
            </script>
        </expression>
        <target>
            <c:path>assignment</c:path>
        </target>
        <condition>
            <script>
                <code>
                    log.info("XXX runing condition {}, sapProfile {}" , (sapProfile != null), sapProfile)

                    return sapProfile != null
                </code>
            </script>
        </condition>
    </mapping>

    <mapping>
        <name>User - SAP Group mapping</name>
        <authoritative>true</authoritative>
        <strength>strong</strength>
        <source>
            <c:name>sapGroup</c:name>
            <c:path xmlns:ext="http://xml.uniba.sk/cdoext">extension/sapGroups</c:path>
        </source>
        <expression>
            <script>
                <!-- <relativityMode>absolute</relativityMode> --><!-- FIX for multivalue -->
                <code>
                    import com.evolveum.midpoint.xml.ns._public.common.common_3.AssignmentType;
                    import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectReferenceType;
                    import com.evolveum.midpoint.xml.ns._public.common.common_3.RoleType;
                    import com.evolveum.midpoint.xml.ns._public.common.common_3.OrgType;
                    import java.util.*;

                    log.info("XXX assigning from SAP role {}" , sapGroup)
                    if (sapGroup != null){
                        orgName = 'g:'+sapGroup;
                        <!-- finding org -->
                        org = midpoint.searchObjectByName(OrgType.class, orgName);
                        log.info("org {}" , org)
                        orgOrt = new ObjectReferenceType();
                        orgOrt.setOid(org.getOid());
                        orgOrt.setType(OrgType.COMPLEX_TYPE);
                        <!-- creating assignment -->
                        AssignmentType assignment = new AssignmentType();
                        assignment.asPrismContainerValue()
                        assignment.setTargetRef(orgOrt);

                        return assignment
                    }
                </code>
            </script>
        </expression>
        <target>
            <c:path>assignment</c:path>
        </target>
        <condition>
            <script>
                <code>
                    log.info("XXX runing condition {}, sapGroup {}" , (sapGroup != null), sapGroup)

                    return sapGroup != null
                </code>
            </script>
        </condition>
    </mapping>

</objectTemplate>
