<commit_before>/*
 * 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());
        }
    }
}<commit_msg>[ANDROSDK-382] Modify UserOrganisationUnitLinkModelBuilder to support root checking for search scope Organization Units<commit_after>/*
 * 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.Collections;
import java.util.List;
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;
    OrganisationUnitModel.Scope organisationUnitScope;

    public UserOrganisationUnitLinkModelBuilder(OrganisationUnitModel.Scope scope, User user) {
        this.user = user;
        this.organisationUnitScope = scope;
        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();
    }

    @SuppressWarnings("PMD")
    private boolean isRoot(OrganisationUnit organisationUnit) {

        List<OrganisationUnit> selectedScopeOrganisationUnits = null;

        switch (this.organisationUnitScope) {

            case SCOPE_TEI_SEARCH:
                selectedScopeOrganisationUnits = Collections.unmodifiableList(
                        user.teiSearchOrganisationUnits());
                break;

            case SCOPE_DATA_CAPTURE:
                selectedScopeOrganisationUnits = Collections.unmodifiableList(
                        user.organisationUnits());
                break;
        }

        if (selectedScopeOrganisationUnits == null) {
            return false;
        } else {
            Set<String> rootOrgUnitUids = findRoots(selectedScopeOrganisationUnits);
            return rootOrgUnitUids.contains(organisationUnit.uid());
        }
    }
}
{'fraction_non_alphanumeric': 0.0631824137429029, 'fraction_numerical': 0.001601397583345465, 'mean_word_length': 3.6076458752515093, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6992227', 'n_tokens_mistral': 1779, 'n_tokens_neox': 1641, 'n_words': 609}