/*
 * Copyright 2012-2013 inBloom, Inc. and its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.slc.sli.bulk.extract.context.resolver;

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

import org.slc.sli.bulk.extract.context.resolver.impl.EducationOrganizationContextResolver;
import org.slc.sli.bulk.extract.context.resolver.impl.ParentContextResolver;
import org.slc.sli.bulk.extract.context.resolver.impl.StudentContextResolver;
import org.slc.sli.bulk.extract.context.resolver.impl.StudentDirectRelatedContextResolver;

/**
 * Factory class for context resolvers, which are used to
 * enforce business visibility rule as in determines which
 * LEAs own the entity
 * 
 * @author ycao
 * 
 */
@Component
public class EdOrgContextResolverFactory {
    
    @Autowired
    private EducationOrganizationContextResolver edOrgContextResolver;
    
    /**
     * Two things must in common for a entity to be a student direct related entity:
     * 1. the entity must have a "body.studentId" field
     * 2. the business rule for visibility for those entities must be: all for all current students,
     * i.e. we only check if the student belongs to a certain LEA
     */
    @Autowired
    private StudentDirectRelatedContextResolver studentDirectRelatedContextResolver;
    
    @Autowired
    private StudentContextResolver studentResolver;

    @Autowired
    private ParentContextResolver parentResolver;
    
    /**
     * find responsible resolver for this entity type
     * 
     * @param entityType
     * @return context resolver for this entity type
     */
    public ContextResolver getResolver(String entityType) {
        
        if ("educationOrganization".equals(entityType)) {
            return edOrgContextResolver;
        }
        
        if ("student".equals(entityType)) {
            return studentResolver;
        }

        if ("studentSchoolAssociation".equals(entityType)
                || "studentAssessment".equals(entityType)
                || "studentParentAssociation".equals(entityType)) {
            return studentDirectRelatedContextResolver;
        }
        
        if ("studentGradebookEntry".equals(entityType)) {
            // for now use the simple resolver, but be advised this
            // entity may have additional business rules that is depended
            // on section. Need to revisit this entity when we play
            // "section" story
            return studentDirectRelatedContextResolver;
        }
        
        if ("parent".equals(entityType)) {
            return parentResolver;
        }
        

        return null;
    }
}

@@ -1114,16 +1114,65 @@
 esolver;
+%0Aimport org.slc.sli.common.constants.EntityNames;
 %0A%0A/**%0A *
@@ -2295,31 +2295,42 @@
 if (
-%22educationOrganization%22
+EntityNames.EDUCATION_ORGANIZATION
 .equ
@@ -2424,17 +2424,27 @@
 if (
-%22student%22
+EntityNames.STUDENT
 .equ
@@ -2525,34 +2525,46 @@
 if (
-%22studentSchoolAssociation%22
+EntityNames.STUDENT_SCHOOL_ASSOCIATION
 .equ
@@ -2602,27 +2602,38 @@
  %7C%7C 
-%22studentAssessment%22
+EntityNames.STUDENT_ASSESSMENT
 .equ
@@ -2671,34 +2671,46 @@
  %7C%7C 
-%22studentParentAssociation%22
+EntityNames.STUDENT_PARENT_ASSOCIATION
 .equ
@@ -2819,31 +2819,43 @@
 if (
-%22studentGradebookEntry%22
+EntityNames.STUDENT_GRADEBOOK_ENTRY
 .equ
@@ -3205,16 +3205,26 @@
 if (
-%22parent%22
+EntityNames.PARENT
 .equ

fix up hardcoded strings