package org.slc.sli.api.security.context;

import org.slc.sli.api.config.EntityDefinitionStore;
import org.slc.sli.api.config.EntityNames;
import org.slc.sli.domain.Entity;
import org.slc.sli.domain.EntityRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

/**
 * TeacherAttendanceContextResolver
 * Determine security authorization.
 * Finds the Attendance records a user has context to access.
 */
@Component
public class SectionSessionContextResolver implements EntityContextResolver {

    @Autowired
    private EntityRepository repository;
    @Autowired
    private EntityDefinitionStore definitionStore;

    @Override
    public String getSourceType() {
        return EntityNames.SECTION;
    }

    @Override
    public String getTargetType() {
        return EntityNames.SESSION;
    }

    @Override
    public List<String> findAccessible(Entity principal) {
        return Arrays.asList((String) principal.getBody().get("sessionId"));
    }

    public void setRepository(EntityRepository repository) {
        this.repository = repository;
    }

    public void setDefinitionStore(EntityDefinitionStore definitionStore) {
        this.definitionStore = definitionStore;
    }
}

@@ -32,24 +32,77 @@
 y.context;%0A%0A
+import org.slc.sli.api.config.AssociationDefinition;%0A
 import org.s
@@ -185,16 +185,61 @@
 yNames;%0A
+import org.slc.sli.api.config.ResourceNames;%0A
 import o
@@ -371,16 +371,135 @@
 owired;%0A
+import org.springframework.data.mongodb.core.query.Criteria;%0Aimport org.springframework.data.mongodb.core.query.Query;%0A
 import o
@@ -536,24 +536,52 @@
 Component;%0A%0A
+import java.util.ArrayList;%0A
 import java.
@@ -1244,76 +1244,2319 @@
     
-return Arrays.asList((String) principal.getBody().get(%22sessionId%22));
+List%3CString%3E ids = new ArrayList%3CString%3E(Arrays.asList(principal.getEntityId()));%0A%0A        AssociationDefinition teacherSectionDef = (AssociationDefinition) definitionStore%0A                .lookupByResourceName(ResourceNames.TEACHER_SECTION_ASSOCIATIONS);%0A        AssociationDefinition sectionStudentDef = (AssociationDefinition) definitionStore%0A                .lookupByResourceName(ResourceNames.STUDENT_SECTION_ASSOCIATIONS);%0A%0A        ids = findIdsFromAssociation(ids, EntityNames.TEACHER, EntityNames.SECTION, teacherSectionDef);%0A        ids = findIdsFromAssociation(ids, EntityNames.SECTION, EntityNames.STUDENT, sectionStudentDef);%0A        ids = findIdsFromAssociation(ids, EntityNames.STUDENT, EntityNames.SECTION, sectionStudentDef);%0A%0A        List%3CString%3E sessionIds = new ArrayList%3CString%3E();%0A        Iterable%3CEntity%3E entities = this.repository.findByQuery(EntityNames.SECTION,%0A                new Query(Criteria.where(%22_id%22).in(ids)), 0, 9999);%0A%0A        for (Entity e : entities) %7B%0A            sessionIds.add((String) e.getBody().get(%22sessionId%22));%0A        %7D%0A%0A        return sessionIds;%0A    %7D%0A%0A    private List%3CString%3E findIdsFromAssociation(List%3CString%3E ids, String sourceType, String targetType, AssociationDefinition definition) %7B%0A        List%3CString%3E associationIds = new ArrayList%3CString%3E();%0A%0A        List%3CString%3E keys = getAssocKeys(sourceType, definition);%0A        String sourceKey = keys.get(0);%0A        String targetKey = keys.get(1);%0A%0A        Iterable%3CEntity%3E entities = this.repository.findByQuery(definition.getStoredCollectionName(),%0A                new Query(Criteria.where(%22body.%22 + sourceKey).in(ids)), 0, 9999);%0A%0A        for (Entity e : entities) %7B%0A            associationIds.add((String) e.getBody().get(targetKey));%0A        %7D%0A%0A        return associationIds;%0A    %7D%0A%0A    private List%3CString%3E getAssocKeys(String entityType, AssociationDefinition ad) %7B%0A%0A        if (ad.getSourceEntity().getType().equals(entityType)) %7B%0A            return Arrays.asList(ad.getSourceKey(), ad.getTargetKey());%0A        %7D else if (ad.getTargetEntity().getType().equals(entityType)) %7B%0A            return Arrays.asList(ad.getTargetKey(), ad.getSourceKey());%0A        %7D else %7B%0A            throw new IllegalArgumentException(%22Entity is not a member of association %22 + entityType + %22 %22 + ad.getType());%0A        %7D
 %0A   

Update security context resolver for section-session direct reference.