<?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: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"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        oid="c4590282-9ef2-4124-9980-a6a22fae009b">
    <name>Comparing attributes</name>
    <assignment>
        <targetRef oid="00000000-0000-0000-0000-000000000171" relation="default" type="ArchetypeType">
            <!-- Collection report -->
        </targetRef>
    </assignment>
    <archetypeRef oid="00000000-0000-0000-0000-000000000171" relation="default" type="ArchetypeType">
        <!-- Collection report -->
    </archetypeRef>
    <roleMembershipRef oid="00000000-0000-0000-0000-000000000171" relation="default" type="ArchetypeType">
        <!-- Collection report -->
    </roleMembershipRef>
    <objectCollection>
        <collection>
            <collectionRef oid="72b1f98e-f587-4b9f-b92b-72e251dbb244" relation="default" type="ObjectCollectionType">
                <!-- Account shadows 'Test CSV: username' -->
            </collectionRef>
        </collection>
        <view>
            <identifier>default-shadow</identifier>
            <column>
                <name>nameColumn</name>
                <path>attributes/username</path>
                <display>
                    <label>Login</label>
                </display>
            </column>
            <column>
                <name>firstNameColumn</name>
                <path>attributes/firstname</path>
                <display>
                    <label>First name</label>
                </display>
                <previousColumn>nameColumn</previousColumn>
            </column>
            <column>
                <name>lastNameColumn</name>
                <path>attributes/lastname</path>
                <display>
                    <label>Last name</label>
                </display>
                <previousColumn>firstNameColumn</previousColumn>
            </column>
            <column>
                <name>emailInMpColumn</name>
                <display>
                    <label>Email in midPoint</label>
                </display>
                <previousColumn>lastNameColumn</previousColumn>
                <export>
                    <expression>
                        <script>
                            <code>
                                import com.evolveum.midpoint.xml.ns._public.common.common_3.UserType;
                                import com.evolveum.midpoint.prism.path.ItemPath;
                                import org.apache.commons.lang3.StringUtils;

                                user = midpoint.searchShadowOwner(object.getOid());
                                return user.asObjectable().getEmailAddress();
                            </code>
                        </script>
                    </expression>
                </export>
            </column>
            <type>ShadowType</type>
        </view>
        <condition>
            <script>
                <code>
                   import com.evolveum.midpoint.xml.ns._public.common.common_3.UserType;
                   import com.evolveum.midpoint.prism.path.ItemPath;
                   import org.apache.commons.lang3.StringUtils;

                  shadowAttributes = object.getAttributes().asPrismContainerValue()
                  login = shadowAttributes.findProperty(ItemPath.create("username")).getRealValue();
                  user = midpoint.searchShadowOwner(object.getOid());

                  if (user != null &amp;&amp; user.asObjectable() != null) {
                     email = user.asObjectable().getEmailAddress();
                     if (StringUtils.isEmpty(email)) {
                        return true;
                     }
                     firstName = shadowAttributes.findProperty(ItemPath.create("firstname"));
                     lastName = shadowAttributes.findProperty(ItemPath.create("lastname"));
                     if (firstName == null &amp;&amp; lastName == null) {
                        return false;
                     }

                     prefix = "";
                     if (firstName != null &amp;&amp; firstName.getRealValue() != null) {
                        prefix = firstName.getRealValue().toLowerCase().replace(" ", ".");
                     }

                     if (lastName != null &amp;&amp; lastName.getRealValue() != null) {
                        if (StringUtils.isNotEmpty(prefix)) {
                           prefix = prefix + "."
                        }
                        prefix = prefix + lastName.getRealValue().toLowerCase().replace(" ", ".");
                     }

                     if (StringUtils.isEmpty(prefix)) {
                        return true;
                     }

                     if (!prefix.equals(email.substring(0,email.indexOf("@")))) {
                        return true;
                     }

                  }
                  return false;
                </code>
            </script>
        </condition>
        <useOnlyReportView>true</useOnlyReportView>
    </objectCollection>
</report>

