package org.slc.sli.api.service;

import java.util.HashSet;
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.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;

    /**
     * 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)) {
            edOrgIds.add((String) e.getBody().get("stateOrganizationId"));
        }
        return edOrgIds;
    }
}

@@ -92,171 +92,41 @@
 rg.s
-pringframework.beans.factory.annotation.Autowired;%0Aimport org.springframework.beans.factory.annotation.Qualifier;%0Aimport org.springframework.stereotype.Component;%0A
+lc.sli.api.constants.EntityNames;
 %0Aimp
@@ -145,37 +145,36 @@
 sli.api.
-constants.EntityNames
+init.RoleInitializer
 ;%0Aimport
@@ -381,16 +381,191 @@
 ository;
+%0Aimport org.springframework.beans.factory.annotation.Autowired;%0Aimport org.springframework.beans.factory.annotation.Qualifier;%0Aimport org.springframework.stereotype.Component;
 %0A%0A/**%0A *
@@ -2015,16 +2015,189 @@
       %7D%0A
+        %0A        if (secUtil.hasRole(RoleInitializer.SEA_ADMINISTRATOR) && !edOrgIds.contains(secUtil.getEdOrg())) %7B%0A            edOrgIds.add(secUtil.getEdOrg());%0A        %7D%0A
         

allow sea to create ingestion user in same level edorg