<commit_before>package org.slc.sli.api.service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import org.slc.sli.api.constants.EntityNames;
import org.slc.sli.api.init.RoleInitializer;
import org.slc.sli.api.util.SecurityUtil.SecurityUtilProxy;
import org.slc.sli.domain.Entity;
import org.slc.sli.domain.NeutralCriteria;
import org.slc.sli.domain.NeutralQuery;
import org.slc.sli.domain.Repository;

/**
 * Implements logic needed by the SAMT Users resource.
 */
@Component
public class SuperAdminService {
    @Autowired
    @Qualifier("validationRepo")
    private Repository<Entity> repo;

    @Autowired
    private SecurityUtilProxy secUtil;

    private static final String STATE_EDUCATION_AGENCY = "State Education Agency";
    private static final String LOCAL_EDUCATION_AGENCY = "Local Education Agency";

    /**
     * Returns a list of possible ed-orgs.
     * @param tenant Tenant in which the ed-orgs reside.
     * @param edOrg if provided, list will be restricted to that ed-org or lower.
     */
    public Set<String> getAllowedEdOrgs(String tenant, String edOrg) {
        NeutralQuery query = new NeutralQuery();

        if (secUtil.getTenantId() == null && tenant != null) {
            query.addCriteria(new NeutralCriteria("metaData.tenantId", NeutralCriteria.OPERATOR_EQUAL, tenant, false));
        }

        if (edOrg != null) {
            NeutralQuery currentEdOrgQuery = new NeutralQuery(new NeutralCriteria("stateOrganizationId",
                    NeutralCriteria.OPERATOR_EQUAL, edOrg));
            Entity usersEdOrg = this.repo.findOne(EntityNames.EDUCATION_ORGANIZATION, currentEdOrgQuery);
            query.addCriteria(new NeutralCriteria("metaData.edOrgs", NeutralCriteria.OPERATOR_EQUAL, usersEdOrg
                    .getEntityId(), false));
        }

        Set<String> edOrgIds = new HashSet<String>();
        for (Entity e : this.repo.findAll(EntityNames.EDUCATION_ORGANIZATION, query)) {
            @SuppressWarnings("unchecked")
            List<String> organizationCategories = (List<String>) e.getBody().get("organizationCategories");
            if (organizationCategories != null && isStateOrDistrictEdOrg(organizationCategories)) {
                edOrgIds.add((String) e.getBody().get("stateOrganizationId"));
            }
        }

        if (secUtil.hasRole(RoleInitializer.SEA_ADMINISTRATOR) && !edOrgIds.contains(secUtil.getEdOrg())) {
            edOrgIds.add(secUtil.getEdOrg());
        }
        return edOrgIds;
    }

    private boolean isStateOrDistrictEdOrg(List<String> orgCategories) {
        return orgCategories.contains(STATE_EDUCATION_AGENCY) || orgCategories.contains(LOCAL_EDUCATION_AGENCY);
    }
}<commit_msg>refactore getallowededorgs to avoid useing edorg stamper<commit_after>package org.slc.sli.api.service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.slc.sli.api.constants.EntityNames;
import org.slc.sli.api.init.RoleInitializer;
import org.slc.sli.api.util.SecurityUtil.SecurityUtilProxy;
import org.slc.sli.domain.Entity;
import org.slc.sli.domain.NeutralCriteria;
import org.slc.sli.domain.NeutralQuery;
import org.slc.sli.domain.Repository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

/**
 * Implements logic needed by the SAMT Users resource.
 */
@Component
public class SuperAdminService {
    @Autowired
    @Qualifier("validationRepo")
    private Repository<Entity> repo;

    @Autowired
    private SecurityUtilProxy secUtil;

    private static final String STATE_EDUCATION_AGENCY = "State Education Agency";
    private static final String LOCAL_EDUCATION_AGENCY = "Local Education Agency";

    /**
     * Returns a list of possible ed-orgs.
     * 
     * @param tenant
     *            Tenant in which the ed-orgs reside.
     * @param edOrg
     *            if provided, list will be restricted to that ed-org or lower.
     */
    public Set<String> getAllowedEdOrgs(String tenant, String edOrg) {
        Set<String> edOrgIds = new HashSet<String>();
        if (secUtil.hasRole(RoleInitializer.LEA_ADMINISTRATOR)) {
            edOrgIds.add(secUtil.getEdOrg());
            return edOrgIds;
        }
        
        NeutralQuery query = new NeutralQuery();

        if (secUtil.getTenantId() == null && tenant != null) {
            query.addCriteria(new NeutralCriteria("metaData.tenantId", NeutralCriteria.OPERATOR_EQUAL, tenant, false));
        }

        for (Entity e : this.repo.findAll(EntityNames.EDUCATION_ORGANIZATION, query)) {
            @SuppressWarnings("unchecked")
            List<String> organizationCategories = (List<String>) e.getBody().get("organizationCategories");
            if (organizationCategories != null && isStateOrDistrictEdOrg(organizationCategories)) {
                edOrgIds.add((String) e.getBody().get("stateOrganizationId"));
            }
        }

        if (secUtil.hasRole(RoleInitializer.SEA_ADMINISTRATOR) && !edOrgIds.contains(secUtil.getEdOrg())) {
            edOrgIds.add(secUtil.getEdOrg());
        }
        return edOrgIds;
    }

    private boolean isStateOrDistrictEdOrg(List<String> orgCategories) {
        return orgCategories.contains(STATE_EDUCATION_AGENCY) || orgCategories.contains(LOCAL_EDUCATION_AGENCY);
    }
}
{'fraction_non_alphanumeric': 0.10367713004484305, 'fraction_numerical': 0.0, 'mean_word_length': 3.4218873909595557, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '679441', 'n_tokens_mistral': 1720, 'n_tokens_neox': 1638, 'n_words': 321}