<commit_before>package gov.nih.nci.cabig.caaers.domain.repository.ajax;

import gov.nih.nci.cabig.caaers.dao.query.ajax.AbstractAjaxableDomainObjectQuery;
import gov.nih.nci.cabig.caaers.domain.ResearchStaff;
import gov.nih.nci.cabig.caaers.domain.ajax.StudySearchableAjaxableDomainObject;
import gov.nih.nci.cabig.caaers.domain.ajax.StudySiteAjaxableDomainObject;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author <NAME>
 */

@Transactional(readOnly = true)
public class StudySearchableAjaxableDomainObjectRepository<T extends StudySearchableAjaxableDomainObject> extends AbstractAjaxableDomainObjectRepository {

    public List<T> findStudies(final AbstractAjaxableDomainObjectQuery query) {

        List<Object[]> objects = super.find(query);
        List<T> studySearchableAjaxableDomainObjects = new ArrayList<T>();

        for (Object[] o : objects) {
            T studySearchableAjaxableDomainObject = (T) getObjectById(studySearchableAjaxableDomainObjects, (Integer) o[0]);

            if (studySearchableAjaxableDomainObject == null) {
                studySearchableAjaxableDomainObject = (T) BeanUtils.instantiateClass(getObjectClass());
                studySearchableAjaxableDomainObject.setId((Integer) o[0]);
                studySearchableAjaxableDomainObject.setShortTitle((String) o[1]);
                if (o[3] != null && (Boolean) o[3]) {
                    studySearchableAjaxableDomainObject.setPrimaryIdentifierValue((String) o[2]);
                }

                addAdditionalProperties(studySearchableAjaxableDomainObject, o);

                studySearchableAjaxableDomainObjects.add(studySearchableAjaxableDomainObject);

            } else {
                updateAdditionalProperties(studySearchableAjaxableDomainObject, o);

            }


        }
        return studySearchableAjaxableDomainObjects;

    }

    protected void updateAdditionalProperties(T studySearchableAjaxableDomainObject, Object[] o) {
        if (o[3] != null && (Boolean) o[3]) {
            studySearchableAjaxableDomainObject.setPrimaryIdentifierValue((String) o[2]);
        }
        updateFundingSponsor(studySearchableAjaxableDomainObject, o);
        updateCoordinatingCenter(studySearchableAjaxableDomainObject, o);
        updateStudySite(studySearchableAjaxableDomainObject, o);
        updateStudyPersonnel(studySearchableAjaxableDomainObject, o);


    }

    protected void addAdditionalProperties(StudySearchableAjaxableDomainObject studySearchableAjaxableDomainObject, Object[] o) {
        studySearchableAjaxableDomainObject.setPhaseCode((String) o[4]);
        studySearchableAjaxableDomainObject.setStatus((String) o[5]);
        updateFundingSponsor(studySearchableAjaxableDomainObject, o);
        updateCoordinatingCenter(studySearchableAjaxableDomainObject, o);

        updateStudySite(studySearchableAjaxableDomainObject, o);
        updateStudyPersonnel(studySearchableAjaxableDomainObject, o);
    }

    private void updateStudySite(StudySearchableAjaxableDomainObject studySearchableAjaxableDomainObject, Object[] o) {
        //if (!StringUtils.isBlank((String) o[7]) && (StringUtils.equals((String) o[9], "SST") || StringUtils.equals((String) o[9], "SCC"))) {
        if (!StringUtils.isBlank((String) o[7]) && (StringUtils.equals((String) o[9], "SST"))) {
            StudySiteAjaxableDomainObject studySiteAjaxableDomainObject = new StudySiteAjaxableDomainObject();
            studySiteAjaxableDomainObject.setId((Integer) o[8]);
            studySiteAjaxableDomainObject.setName((String) o[7]);
            studySiteAjaxableDomainObject.setNciInstituteCode((String) o[10]);
            studySearchableAjaxableDomainObject.addStudySite(studySiteAjaxableDomainObject);
        }
    }

    private void updateStudyPersonnel(StudySearchableAjaxableDomainObject studySearchableAjaxableDomainObject, Object[] o) {
    	if (o[11] != null) {
            studySearchableAjaxableDomainObject.addStudyPersonnelId((Integer) o[11]);
        }
    }
    
    private void updateFundingSponsor(StudySearchableAjaxableDomainObject studySearchableAjaxableDomainObject, Object[] o) {
        if (!StringUtils.isBlank((String) o[6])) {
            studySearchableAjaxableDomainObject.setPrimarySponsorCode((String) o[6]);
        }
    }

    private void updateCoordinatingCenter(StudySearchableAjaxableDomainObject studySearchableAjaxableDomainObject, Object[] o) {
        if (!StringUtils.isBlank((String) o[12])) {
            studySearchableAjaxableDomainObject.setCoordinatingCenterCode((String) o[12]);
        }
    }    

    protected Class getObjectClass() {
        return StudySearchableAjaxableDomainObject.class;


    }


}<commit_msg>optimized the loop filtering logic<commit_after>package gov.nih.nci.cabig.caaers.domain.repository.ajax;

import gov.nih.nci.cabig.caaers.dao.query.ajax.AbstractAjaxableDomainObjectQuery;
import gov.nih.nci.cabig.caaers.domain.ResearchStaff;
import gov.nih.nci.cabig.caaers.domain.ajax.StudySearchableAjaxableDomainObject;
import gov.nih.nci.cabig.caaers.domain.ajax.StudySiteAjaxableDomainObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author <NAME>
 */

@Transactional(readOnly = true)
public class StudySearchableAjaxableDomainObjectRepository<T extends StudySearchableAjaxableDomainObject> extends AbstractAjaxableDomainObjectRepository {

    public List<T> findStudies(final AbstractAjaxableDomainObjectQuery query) {

        List<Object[]> objects = super.find(query);
        Map<Integer, T> existingStudyMap = new HashMap<Integer, T>();
        for (Object[] o : objects) {
        	
        	
            T studySearchableAjaxableDomainObject = existingStudyMap.get((Integer) o[0]);

            if (studySearchableAjaxableDomainObject == null) {
                studySearchableAjaxableDomainObject = (T) BeanUtils.instantiateClass(getObjectClass());
                studySearchableAjaxableDomainObject.setId((Integer) o[0]);
                studySearchableAjaxableDomainObject.setShortTitle((String) o[1]);
                if (o[3] != null && (Boolean) o[3]) {
                    studySearchableAjaxableDomainObject.setPrimaryIdentifierValue((String) o[2]);
                }

                addAdditionalProperties(studySearchableAjaxableDomainObject, o);

                existingStudyMap.put(studySearchableAjaxableDomainObject.getId(), studySearchableAjaxableDomainObject);
                

            } else {
                updateAdditionalProperties(studySearchableAjaxableDomainObject, o);

            }


        }
        return new ArrayList<T>(existingStudyMap.values());

    }

    protected void updateAdditionalProperties(T studySearchableAjaxableDomainObject, Object[] o) {
        if (o[3] != null && (Boolean) o[3]) {
            studySearchableAjaxableDomainObject.setPrimaryIdentifierValue((String) o[2]);
        }
        updateFundingSponsor(studySearchableAjaxableDomainObject, o);
        updateCoordinatingCenter(studySearchableAjaxableDomainObject, o);
        updateStudySite(studySearchableAjaxableDomainObject, o);
        updateStudyPersonnel(studySearchableAjaxableDomainObject, o);


    }

    protected void addAdditionalProperties(StudySearchableAjaxableDomainObject studySearchableAjaxableDomainObject, Object[] o) {
        studySearchableAjaxableDomainObject.setPhaseCode((String) o[4]);
        studySearchableAjaxableDomainObject.setStatus((String) o[5]);
        updateFundingSponsor(studySearchableAjaxableDomainObject, o);
        updateCoordinatingCenter(studySearchableAjaxableDomainObject, o);

        updateStudySite(studySearchableAjaxableDomainObject, o);
        updateStudyPersonnel(studySearchableAjaxableDomainObject, o);
    }

    private void updateStudySite(StudySearchableAjaxableDomainObject studySearchableAjaxableDomainObject, Object[] o) {
        //if (!StringUtils.isBlank((String) o[7]) && (StringUtils.equals((String) o[9], "SST") || StringUtils.equals((String) o[9], "SCC"))) {
        if (!StringUtils.isBlank((String) o[7]) && (StringUtils.equals((String) o[9], "SST"))) {
            StudySiteAjaxableDomainObject studySiteAjaxableDomainObject = new StudySiteAjaxableDomainObject();
            studySiteAjaxableDomainObject.setId((Integer) o[8]);
            studySiteAjaxableDomainObject.setName((String) o[7]);
            studySiteAjaxableDomainObject.setNciInstituteCode((String) o[10]);
            studySearchableAjaxableDomainObject.addStudySite(studySiteAjaxableDomainObject);
        }
    }

    private void updateStudyPersonnel(StudySearchableAjaxableDomainObject studySearchableAjaxableDomainObject, Object[] o) {
    	if (o[11] != null) {
            studySearchableAjaxableDomainObject.addStudyPersonnelId((Integer) o[11]);
        }
    }
    
    private void updateFundingSponsor(StudySearchableAjaxableDomainObject studySearchableAjaxableDomainObject, Object[] o) {
        if (!StringUtils.isBlank((String) o[6])) {
            studySearchableAjaxableDomainObject.setPrimarySponsorCode((String) o[6]);
        }
    }

    private void updateCoordinatingCenter(StudySearchableAjaxableDomainObject studySearchableAjaxableDomainObject, Object[] o) {
        if (!StringUtils.isBlank((String) o[12])) {
            studySearchableAjaxableDomainObject.setCoordinatingCenterCode((String) o[12]);
        }
    }    

    protected Class getObjectClass() {
        return StudySearchableAjaxableDomainObject.class;


    }


}
{'fraction_non_alphanumeric': 0.09546612328069282, 'fraction_numerical': 0.006113092205807438, 'mean_word_length': 4.070247933884297, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5331735', 'n_tokens_mistral': 2901, 'n_tokens_neox': 2738, 'n_words': 384}