<commit_before>package gov.nih.nci.cabig.caaers.web.study;

import gov.nih.nci.cabig.caaers.dao.workflow.WorkflowConfigDao;
import gov.nih.nci.cabig.caaers.domain.Study;
import gov.nih.nci.cabig.caaers.domain.StudySite;
import gov.nih.nci.cabig.caaers.domain.StudyOrganization;
import gov.nih.nci.cabig.caaers.domain.Organization;
import gov.nih.nci.cabig.caaers.domain.workflow.StudySiteWorkflowConfig;
import gov.nih.nci.cabig.caaers.domain.workflow.WorkflowConfig;
import gov.nih.nci.cabig.caaers.web.fields.InputField;
import gov.nih.nci.cabig.caaers.web.fields.InputFieldFactory;
import gov.nih.nci.cabig.caaers.web.fields.InputFieldGroup;
import gov.nih.nci.cabig.caaers.web.fields.InputFieldGroupMap;
import gov.nih.nci.cabig.caaers.web.fields.RepeatingFieldGroupFactory;

import java.util.*;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.validation.Errors;

/**
 * @author <NAME>
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 */
class SitesTab extends StudyTab {

    private RepeatingFieldGroupFactory rfgFactory;
    private WorkflowConfigDao workflowConfigDao;
     
    public SitesTab() {
        super("Sites", "Sites", "study/study_sites");
    }

    @Override
    public void postProcess(HttpServletRequest request, StudyCommand command, Errors errors) {
        String action = request.getParameter("_action");
        Object isAjax = request.getAttribute("_isAjax");

        if (isAjax != null) return;

        if ("removeSite".equals(action)) {

            int index = Integer.parseInt(request.getParameter("_selected"));
            StudySite site = command.getStudy().getStudySites().get(index);

            if (site.getId() != null) {
                if (CollectionUtils.isNotEmpty(site.getActiveStudyInvestigators())) {
                  errors.reject("STU_013", "The site is associated to investigators, so unable to delete");
                }
                if (CollectionUtils.isNotEmpty(site.getActiveStudyPersonnel())) {
                  errors.reject("STU_014", "The site is associated to research staffs, so unable to delete");
                }
            }
            
            //remove site, if no investigator or research person is associated to site.
            if(!errors.hasErrors()){
            	command.deleteStudySiteAtIndex(index);
                command.setStudySiteIndex(-1);
            }
            
            
        }else{
        	if(isAjax == null){
        		for(StudySite site : command.getStudy().getStudySites()){
        			if(site.getStudySiteWorkflowConfigs().isEmpty()){
        				WorkflowConfig rpWorkflowConfig = workflowConfigDao.getByWorkflowDefinitionName("reportingperiod_coordinating_center");
        				site.addStudySiteWorkflowConfig(new StudySiteWorkflowConfig("reportingPeriod", site, rpWorkflowConfig));
        				WorkflowConfig rWorkflowConfig =  workflowConfigDao.getByWorkflowDefinitionName("expedited_domestic");
        				site.addStudySiteWorkflowConfig(new StudySiteWorkflowConfig("report", site, rWorkflowConfig));
        			}
            	}
        	}
        }

        // checking new Study Sites
        HashMap<Integer, StudyOrganization> hm = new HashMap<Integer, StudyOrganization>();
        List<StudyOrganization> copySS = new ArrayList(command.getStudy().getStudyOrganizations());

        int i = 0;
        for (StudyOrganization ss : copySS) {
            if (ss instanceof StudySite && ss.getOrganization() != null && ss.getOrganization().getId() != null) {
                if (ss.getId() == null) {
                    StudyOrganization existingStudySite = hm.get(ss.getOrganization().getId());
                    if (existingStudySite != null) {
                        if (existingStudySite.getRetiredIndicator()) {
                            command.getStudy().getStudyOrganizations().remove(i);
                            i--;
                            existingStudySite.setRetiredIndicator(false);
                        }
                    }
                } else {
                    hm.put(ss.getOrganization().getId(), ss);
                }
            }
            i++;
        }

        command.getStudyRepository().synchronizeStudyPersonnel(command.getStudy());<commit_msg>CAAERS-3326<commit_after>package gov.nih.nci.cabig.caaers.web.study;

import gov.nih.nci.cabig.caaers.dao.workflow.WorkflowConfigDao;
import gov.nih.nci.cabig.caaers.domain.Study;
import gov.nih.nci.cabig.caaers.domain.StudySite;
import gov.nih.nci.cabig.caaers.domain.StudyOrganization;
import gov.nih.nci.cabig.caaers.domain.Organization;
import gov.nih.nci.cabig.caaers.domain.workflow.StudySiteWorkflowConfig;
import gov.nih.nci.cabig.caaers.domain.workflow.WorkflowConfig;
import gov.nih.nci.cabig.caaers.web.fields.InputField;
import gov.nih.nci.cabig.caaers.web.fields.InputFieldFactory;
import gov.nih.nci.cabig.caaers.web.fields.InputFieldGroup;
import gov.nih.nci.cabig.caaers.web.fields.InputFieldGroupMap;
import gov.nih.nci.cabig.caaers.web.fields.RepeatingFieldGroupFactory;

import java.util.*;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.validation.Errors;

/**
 * @author <NAME>
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 */
class SitesTab extends StudyTab {

    private RepeatingFieldGroupFactory rfgFactory;
    private WorkflowConfigDao workflowConfigDao;
     
    public SitesTab() {
        super("Sites", "Sites", "study/study_sites");
    }

    @Override
    public void postProcess(HttpServletRequest request, StudyCommand command, Errors errors) {
        String action = request.getParameter("_action");
        Object isAjax = request.getAttribute("_isAjax");

        if (isAjax != null) return;

        if ("removeSite".equals(action)) {

            int index = Integer.parseInt(request.getParameter("_selected"));
            StudySite site = command.getStudy().getStudySites().get(index);

            if (site.getId() != null) {
                if (CollectionUtils.isNotEmpty(site.getActiveStudyInvestigators())) {
                  errors.reject("STU_013", "The site is associated to investigators, so unable to delete");
                }
                if (CollectionUtils.isNotEmpty(site.getActiveStudyPersonnel())) {
                  errors.reject("STU_014", "The site is associated to research staffs, so unable to delete");
                }
            }
            
            //remove site, if no investigator or research person is associated to site.
            if(!errors.hasErrors()){
            	command.deleteStudySiteAtIndex(index);
                command.setStudySiteIndex(-1);
            }
            
            
        }

        // checking new Study Sites
        HashMap<Integer, StudyOrganization> hm = new HashMap<Integer, StudyOrganization>();
        List<StudyOrganization> copySS = new ArrayList(command.getStudy().getStudyOrganizations());

        int i = 0;
        for (StudyOrganization ss : copySS) {
            if (ss instanceof StudySite && ss.getOrganization() != null && ss.getOrganization().getId() != null) {
                if (ss.getId() == null) {
                    StudyOrganization existingStudySite = hm.get(ss.getOrganization().getId());
                    if (existingStudySite != null) {
                        if (existingStudySite.getRetiredIndicator()) {
                            command.getStudy().getStudyOrganizations().remove(i);
                            i--;
                            existingStudySite.setRetiredIndicator(false);
                        }
                    }
                } else {
                    hm.put(ss.getOrganization().getId(), ss);
                }
            }
            i++;
        }

        command.getStudyRepository().synchronizeStudyPersonnel(command.getStudy());
{'fraction_non_alphanumeric': 0.11251091430709742, 'fraction_numerical': 0.0024946987651241113, 'mean_word_length': 2.4830582102519547, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '6003431', 'n_tokens_mistral': 2420, 'n_tokens_neox': 2299, 'n_words': 372}