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

import org.slc.sli.api.security.context.resolver.DenyAllContextResolver;
import org.slc.sli.api.security.context.resolver.EntityContextResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Collection;

/**
 * Stores context based permission resolvers.
 * Can determine if a principal entity has permission to access a request entity.
 */
@Component
public class ContextResolverStore implements ApplicationContextAware {

    private static final Logger LOG = LoggerFactory.getLogger(ContextResolverStore.class);
    private static final DenyAllContextResolver denyAllContextResolver = new DenyAllContextResolver();
    
    private Collection<EntityContextResolver> resolvers;
    
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        resolvers = applicationContext.getBeansOfType(EntityContextResolver.class).values();
    }
    
    /**
     * Locates a resolver that can naviage the security context path from source entity type to target entity type
     * 
     * @param fromEntityType
     * @param toEntityType
     * @return
     * @throws IllegalStateException
     */
    public EntityContextResolver findResolver(String fromEntityType, String toEntityType) throws IllegalStateException {
        
        EntityContextResolver found = null;
        for (EntityContextResolver resolver : this.resolvers) {
            if (resolver.canResolve(fromEntityType, toEntityType)) {
                found = resolver;
                break;
            }
        }
        
        if (found == null) {
            found = denyAllContextResolver;
            LOG.warn("No path resolver defined for {} -> {}. Returning deny-all resolver.", fromEntityType, toEntityType);
        }
        
        return found;
    }
}

@@ -32,24 +32,104 @@
 y.context;%0A%0A
+import org.slc.sli.api.security.context.resolver.AllowAllEntityContextResolver;%0A
 import org.s
@@ -1937,32 +1937,61 @@
 l) %7B%0A           
+ //TODO enable%0A            //
  found = denyAll
@@ -1999,32 +1999,89 @@
 ontextResolver;%0A
+            found = new AllowAllEntityContextResolver();%0A
             LOG.

set allow by default until all associations are secured