/*
 * Copyright (c) 2017, University of Oslo
 *
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * Neither the name of the HISP project nor the names of its contributors may
 * be used to endorse or promote products derived from this software without
 * specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.hisp.dhis.android.core.user;

import org.hisp.dhis.android.core.common.ModelBuilder;
import org.hisp.dhis.android.core.organisationunit.OrganisationUnit;
import org.hisp.dhis.android.core.organisationunit.OrganisationUnitModel;

import java.util.Set;

import static org.hisp.dhis.android.core.organisationunit.OrganisationUnitTree.findRoots;

public class UserOrganisationUnitLinkModelBuilder
        extends ModelBuilder<OrganisationUnit, UserOrganisationUnitLinkModel> {

    private final UserOrganisationUnitLinkModel.Builder builder;
    private final User user;

    public UserOrganisationUnitLinkModelBuilder(OrganisationUnitModel.Scope scope, User user) {
        this.user = user;
        this.builder = UserOrganisationUnitLinkModel.builder()
                .organisationUnitScope(scope.name())
                .user(user.uid());
    }

    @Override
    public UserOrganisationUnitLinkModel buildModel(OrganisationUnit organisationUnit) {
        return builder
                .organisationUnit(organisationUnit.uid())
                .root(isRoot(organisationUnit))
                .build();
    }

    private boolean isRoot(OrganisationUnit organisationUnit) {
        if (user.organisationUnits() == null) {
            return false;
        } else {
            Set<String> rootOrgUnitUids = findRoots(user.organisationUnits());
            return rootOrgUnitUids.contains(organisationUnit.uid());
        }
    }
}
@@ -1787,16 +1787,69 @@
 Model;%0A%0A
+import java.util.Collections;%0Aimport java.util.List;%0A
 import j
@@ -2178,16 +2178,71 @@
 er user;
+%0A    OrganisationUnitModel.Scope organisationUnitScope;
 %0A%0A    pu
@@ -2353,24 +2353,68 @@
 ser = user;%0A
+        this.organisationUnitScope = scope;%0A
         this
@@ -2820,24 +2820,53 @@
 d();%0A    %7D%0A%0A
+    @SuppressWarnings(%22PMD%22)%0A
     private 
@@ -2917,24 +2917,25 @@
 Unit) %7B%0A
+%0A
         
 if (user
@@ -2930,36 +2930,559 @@
     
-if (user.organisationUnits()
+List%3COrganisationUnit%3E selectedScopeOrganisationUnits = null;%0A%0A        switch (this.organisationUnitScope) %7B%0A%0A            case SCOPE_TEI_SEARCH:%0A                selectedScopeOrganisationUnits = Collections.unmodifiableList(%0A                        user.teiSearchOrganisationUnits());%0A                break;%0A%0A            case SCOPE_DATA_CAPTURE:%0A                selectedScopeOrganisationUnits = Collections.unmodifiableList(%0A                        user.organisationUnits());%0A                break;%0A        %7D%0A%0A        if (selectedScopeOrganisationUnits
  == 
@@ -3584,22 +3584,30 @@
 ndRoots(
-u
 se
-r.o
+lectedScopeO
 rganisat
@@ -3606,34 +3606,32 @@
 rganisationUnits
-()
 );%0A            r

Modify UserOrganisationUnitLinkModelBuilder to support root checking for search scope Organization Units