<commit_before>/*
 * 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.api.security.context.validator;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.slc.sli.common.constants.EntityNames;
import org.slc.sli.common.util.datetime.DateHelper;
import org.slc.sli.domain.Entity;
import org.slc.sli.domain.NeutralCriteria;
import org.slc.sli.domain.NeutralQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * User: dkornishev
 */
@Component
public class TransitiveStudentToStudentValidator extends BasicValidator {
    
    @Autowired
    private DateHelper dateHelper;
    
    public TransitiveStudentToStudentValidator() {
        super(true, EntityNames.STUDENT, EntityNames.STUDENT);
    }
    
    @Override
    protected boolean doValidate(Set<String> ids, Entity authenticatedStudent, String entityType) {
        if (!areParametersValid(EntityNames.STUDENT, entityType, ids)) {
            return false;
        }
        
        ids.remove(authenticatedStudent.getEntityId());
        if (ids.size() == 0) {
            return true;
        }
        
        // check for current students in my sections, programs, and cohorts. Section first.
        Set<String> currentSections = new HashSet<String>();
        List<Map<String, Object>> putativeSections = authenticatedStudent.getDenormalizedData().get("section");
        if (putativeSections != null) {
            for (Map<String, Object> section : putativeSections) {
                if (!dateHelper.isFieldExpired(section)) {
                    currentSections.add((String) section.get("_id"));
                }
            }
            NeutralQuery sectionQuery = new NeutralQuery(new NeutralCriteria("_id", NeutralCriteria.CRITERIA_IN,
                    currentSections, false));
            sectionQuery.setEmbeddedFields(Arrays.asList("studentSectionAssociation"));
            for (Entity section : getRepo().findAll("section", sectionQuery)) {
                List<Entity> ssas = section.getEmbeddedData().get("studentSectionAssociation");
                if (ssas != null) {
                    for (Entity ssa : ssas) {
                        if (!dateHelper.isFieldExpired(ssa.getBody())) {
                            ids.remove((String) ssa.getBody().get("studentId"));
                        }
                    }
                }
            }
        }
        if (ids.size() == 0) {
            return true;
        }
        
        // program and cohorts
        NeutralQuery studentQuery = new NeutralQuery(new NeutralCriteria("_id", NeutralCriteria.CRITERIA_IN, ids, false));
        studentQuery.setEmbeddedFields(Arrays.asList("studentProgramAssociation", "studentCohortAssociation"));
        studentQuery.setIncludeFields(new ArrayList<String>()); // we don't need anything in the body of the superdoc.
        for (Entity student : getRepo().findAll("student", studentQuery)) {
            removeValidIds(ids, authenticatedStudent, student, "studentProgramAssociation", "programId");
            removeValidIds(ids, authenticatedStudent, student, "studentCohortAssociation", "cohortId");
        }
        
        if (ids.size() == 0) {
            return true;
        }
        return false;
    }
    
    private void removeValidIds(Set<String> ids, Entity authenticatedStudent, Entity student, String subdocType, String refField) {
        Set<String> authenticatedStudentCohorts = new HashSet<String>();
        List<Entity> cohortAssocs = authenticatedStudent.getEmbeddedData().get(subdocType);
        if (cohortAssocs != null) {
            for (Entity putativeCohort : cohortAssocs) {
                if (!dateHelper.isFieldExpired(putativeCohort.getBody())) {
                    authenticatedStudentCohorts.add((String) putativeCohort.getBody().get(refField));
                }
            }
        }
        List<Entity> associations = student.getEmbeddedData().get(subdocType);
        if (associations != null) {
            for (Entity association : associations) {
                String reference = (String) association.getBody().get(refField);
                if (authenticatedStudentCohorts.contains(reference)
                        && !dateHelper.isFieldExpired(association.getBody())) {
                    ids.remove(student.getEntityId());
                }
            }
        }
    }
    
}
<commit_msg>US5708: address code review comments.  Are you happy now, Yang?<commit_after>/*
 * 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.api.security.context.validator;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.slc.sli.common.constants.EntityNames;
import org.slc.sli.common.util.datetime.DateHelper;
import org.slc.sli.domain.Entity;
import org.slc.sli.domain.NeutralCriteria;
import org.slc.sli.domain.NeutralQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * User: dkornishev
 */
@Component
public class TransitiveStudentToStudentValidator extends BasicValidator {
    
    @Autowired
    private DateHelper dateHelper;
    
    public TransitiveStudentToStudentValidator() {
        super(true, EntityNames.STUDENT, EntityNames.STUDENT);
    }
    
    @Override
    protected boolean doValidate(Set<String> ids, Entity authenticatedStudent, String entityType) {
        if (!areParametersValid(EntityNames.STUDENT, entityType, ids)) {
            return false;
        }
        
        ids.remove(authenticatedStudent.getEntityId());
        if (ids.size() == 0) {
            return true;
        }
        
        // check for current students in my sections, programs, and cohorts. Section first.
        Set<String> currentSections = new HashSet<String>();
        List<Map<String, Object>> putativeSections = authenticatedStudent.getDenormalizedData().get("section");
        if (putativeSections != null) {
            for (Map<String, Object> section : putativeSections) {
                if (!dateHelper.isFieldExpired(section)) {
                    currentSections.add((String) section.get("_id"));
                }
            }
            NeutralQuery sectionQuery = new NeutralQuery(new NeutralCriteria("_id", NeutralCriteria.CRITERIA_IN,
                    currentSections, false));
            sectionQuery.setEmbeddedFields(Arrays.asList("studentSectionAssociation"));
            for (Entity section : getRepo().findAll("section", sectionQuery)) {
                List<Entity> ssas = section.getEmbeddedData().get("studentSectionAssociation");
                if (ssas != null) {
                    for (Entity ssa : ssas) {
                        if (!dateHelper.isFieldExpired(ssa.getBody())) {
                            ids.remove((String) ssa.getBody().get("studentId"));
                        }
                    }
                }
            }
        }
        if (ids.size() == 0) {
            return true;
        }
        
        // program and cohorts
        NeutralQuery studentQuery = new NeutralQuery(new NeutralCriteria("_id", NeutralCriteria.CRITERIA_IN, ids, false));
        studentQuery.setEmbeddedFields(Arrays.asList("studentProgramAssociation", "studentCohortAssociation"));
        studentQuery.setIncludeFields(new ArrayList<String>()); // we don't need anything in the body of the superdoc.
        for (Entity student : getRepo().findAll("student", studentQuery)) {
            removeValidIds(ids, authenticatedStudent, student, "studentProgramAssociation", "programId");
            removeValidIds(ids, authenticatedStudent, student, "studentCohortAssociation", "cohortId");
        }
        
        if (ids.size() == 0) {
            return true;
        }
        return false;
    }
    
    private void removeValidIds(Set<String> ids, Entity authenticatedStudent, Entity student, String subdocType, String refField) {
        Set<String> allowedEntitiesForTheStudentWhoIsCurrentlyLoggedIntoTheAPI = new HashSet<String>();
        List<Entity> associationsToTheEntitiesInQuestionForTheStudentWhoIsCurrentlyLoggedIntoTheAPI = authenticatedStudent.getEmbeddedData().get(subdocType);
        if (associationsToTheEntitiesInQuestionForTheStudentWhoIsCurrentlyLoggedIntoTheAPI != null) {
            for (Entity putativeCohort : associationsToTheEntitiesInQuestionForTheStudentWhoIsCurrentlyLoggedIntoTheAPI) {
                if (!dateHelper.isFieldExpired(putativeCohort.getBody())) {
                    allowedEntitiesForTheStudentWhoIsCurrentlyLoggedIntoTheAPI.add((String) putativeCohort.getBody().get(refField));
                }
            }
        }
        List<Entity> associations = student.getEmbeddedData().get(subdocType);
        if (associations != null) {
            for (Entity association : associations) {
                String reference = (String) association.getBody().get(refField);
                if (allowedEntitiesForTheStudentWhoIsCurrentlyLoggedIntoTheAPI.contains(reference)
                        && !dateHelper.isFieldExpired(association.getBody())) {
                    ids.remove(student.getEntityId());
                }
            }
        }
    }
    
}

{'fraction_non_alphanumeric': 0.09995285242809995, 'fraction_numerical': 0.003206034889203206, 'mean_word_length': 2.652203856749311, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '382446', 'n_tokens_mistral': 2891, 'n_tokens_neox': 2763, 'n_words': 637}