<?xml version="1.0" encoding="UTF-8"?>
<resource xmlns="http://midpoint.evolveum.com/xml/ns/public/common/common-3" xmlns:c="http://midpoint.evolveum.com/xml/ns/public/common/common-3"
    xmlns:icfs="http://midpoint.evolveum.com/xml/ns/public/connector/icf-1/resource-schema-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:ri="http://midpoint.evolveum.com/xml/ns/public/resource/instance-3"
    xmlns:t="http://prism.evolveum.com/xml/ns/public/types-3" oid="c29764b3-6205-4460-811a-9c3af7b0a59f" version="6">
    <name>SWITCH edu-ID</name>
    <connectorRef type="ConnectorType">
        <filter>
            <q:text>c:connectorType = "com.evolveum.polygon.connector.eduid.EduIdConnector"</q:text>
        </filter>
    </connectorRef>

    <connectorConfiguration xmlns:icfc="http://midpoint.evolveum.com/xml/ns/public/connector/icf-1/connector-schema-3"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="c:ConnectorConfigurationType">
        <icfc:configurationProperties
            xmlns:gen872="http://midpoint.evolveum.com/xml/ns/public/connector/icf-1/bundle/com.evolveum.polygon.connector-eduid/com.evolveum.polygon.connector.eduid.EduIdConnector"
            xsi:type="icfc:ConfigurationPropertiesType">
            <gen872:serviceAddress>https://api.test.eduid.ch/scim/</gen872:serviceAddress>
            <gen872:password>
                <clearValue>password</clearValue>
            </gen872:password>
            <gen872:authMethod>BASIC</gen872:authMethod>
            <gen872:username>username</gen872:username>
            <gen872:trustAllCertificates>true</gen872:trustAllCertificates>
        </icfc:configurationProperties>
        <icfc:resultsHandlerConfiguration>
            <icfc:enableNormalizingResultsHandler>false</icfc:enableNormalizingResultsHandler>
            <icfc:enableFilteredResultsHandler>false</icfc:enableFilteredResultsHandler>
            <icfc:enableAttributesToGetSearchResultsHandler>false</icfc:enableAttributesToGetSearchResultsHandler>
        </icfc:resultsHandlerConfiguration>
    </connectorConfiguration>

    <schemaHandling>
        <objectType>
            <displayName>Default Affiliation</displayName>
            <kind>account</kind>
            <intent>default</intent>
            <default>true</default>
            <objectClass>ri:affiliation</objectClass>
            <attribute>
                <ref>icfs:uid</ref>
                <displayName>Entry UUID</displayName>
                <limitations>
                    <access>
                        <read>true</read>
                    </access>
                </limitations>
            </attribute>
            <attribute>
                <ref>icfs:name</ref>
                <displayName>Entry NAME</displayName>
                <outbound>
                    <strength>strong</strength>
                    <source>
                        <path>$user/name</path>
                    </source>
                </outbound>
            </attribute>
            <attribute>
                <ref>id</ref>
                <outbound>
                    <strength>strong</strength>
                    <source>
                        <path>$user/name</path>
                    </source>
                </outbound>
            </attribute>
            <attribute>
                <ref>externalId</ref>
                <outbound>
                    <strength>strong</strength>
                    <source>
                        <path>$user/name</path>
                    </source>
                </outbound>
            </attribute>
            <attribute>
                <ref>swissEduPersonUniqueID</ref>
                <outbound>
                    <strength>strong</strength>
                    <source>
                        <path>$user/name</path>
                    </source>
                </outbound>
            </attribute>
            <attribute>
                <ref>swissEduID</ref>
                <outbound>
                    <strength>strong</strength>
                    <source>
                        <path>$user/employeeNumber</path>
                    </source>
                </outbound>
            </attribute>
            <attribute>
                <ref>email</ref>
                <readReplaceMode>true</readReplaceMode>
                <outbound>
                    <strength>strong</strength>
                    <source>
                        <path>$user/emailAddress</path>
                    </source>
                </outbound>
            </attribute>
            <attribute>
                <ref>givenName</ref>
                <outbound>
                    <strength>strong</strength>
                    <source>
                        <path>$user/givenName</path>
                    </source>
                </outbound>
            </attribute>
            <attribute>
                <ref>surname</ref>
                <outbound>
                    <strength>strong</strength>
                    <source>
                        <path>$user/familyName</path>
                    </source>
                </outbound>
            </attribute>
            <attribute>
                <ref>eduPersonAffiliation</ref>
                <readReplaceMode>true</readReplaceMode>
                <outbound>
                    <strength>strong</strength>
                    <source>
                        <path>$user/subtype</path>
                    </source>
                </outbound>
            </attribute>

            <attribute>
                <ref>swissEduIDAffiliationStatus</ref>
                <outbound>
                    <strength>strong</strength>
                    <source>
                        <path>$user/activation/administrativeStatus</path>
                    </source>
                    <expression>
                        <script>
                            <code>
                                import com.evolveum.midpoint.xml.ns._public.common.common_3.*;

                                if (!basic.isEmpty(administrativeStatus) &amp;&amp; administrativeStatus == ActivationStatusType.ENABLED) {
                                    return "current";
                                }

                                return "suspended"; //TODO: fine tune
                            </code>
                        </script>
                    </expression>
                </outbound>
            </attribute>
            <attribute>
                <ref>swissEduIDAffiliationPeriodBegin</ref>
                <outbound>
                    <strength>strong</strength>
                    <source>
                        <path>$user/activation/validTo</path>
                    </source>
                    <expression>
                        <script>
                            <code>
                                import java.util.*
                                import javax.xml.datatype.XMLGregorianCalendar
                                import com.evolveum.midpoint.xml.ns._public.common.common_3.*
                                import com.evolveum.midpoint.prism.xml.XmlTypeConverter
                                import java.text.SimpleDateFormat;

                                Date date;
                                XMLGregorianCalendar gregCal;

                                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                                if(basic.isEmpty(validTo)){
                                    return "2019-01-01"; //mandatory required something to fill
                                }else{
                                    return simpleDateFormat.format(validTo.toGregorianCalendar().getTime());
                                }
                            </code>
                        </script>
                    </expression>
                </outbound>
            </attribute>
        </objectType>
    </schemaHandling>

    <synchronization>
        <objectSynchronization>
            <kind>account</kind>
            <intent>default</intent>
            <enabled>true</enabled>
            <correlation>
                <q:equal>
                    <q:matching>polyStringNorm</q:matching>
                    <q:path>c:name</q:path>
                    <expression>
                        <description>Matches using Name</description>
                        <path>
                        declare namespace ri="http://midpoint.evolveum.com/xml/ns/public/resource/instance-3";
                        $account/attributes/icfs:name
                    </path>
                    </expression>
                </q:equal>
            </correlation>
            <limitPropagation>true</limitPropagation>
            <reaction>
                <situation>linked</situation>
                <synchronize>true</synchronize>
            </reaction>
            <reaction>
                <situation>deleted</situation>
                <synchronize>true</synchronize>
                <action>
                    <handlerUri>http://midpoint.evolveum.com/xml/ns/public/model/action-3#unlink</handlerUri>
                </action>
            </reaction>
            <reaction>
                <situation>unlinked</situation>
                <synchronize>true</synchronize>
                <action>
                    <handlerUri>http://midpoint.evolveum.com/xml/ns/public/model/action-3#link</handlerUri>
                </action>
            </reaction>
            <reaction>
                <situation>unmatched</situation>
                <synchronize>true</synchronize>
                <action>
                    <handlerUri>http://midpoint.evolveum.com/xml/ns/public/model/action-3#deleteShadow</handlerUri>
                </action>
            </reaction>
        </objectSynchronization>
    </synchronization>

</resource>
