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

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

import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

import gov.nih.nci.cabig.caaers.domain.expeditedfields.ExpeditedReportSection;
import gov.nih.nci.cabig.caaers.domain.expeditedfields.ExpeditedReportTree;
import gov.nih.nci.cabig.caaers.domain.expeditedfields.TreeNode;
import gov.nih.nci.cabig.caaers.web.fields.CompositeField;
import gov.nih.nci.cabig.caaers.web.fields.InputField;
import gov.nih.nci.cabig.caaers.web.fields.InputFieldGroup;
import gov.nih.nci.cabig.caaers.web.fields.TabWithFields;

/**
 * @author <NAME>
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 */
public abstract class AeTab extends TabWithFields<ExpeditedAdverseEventInputCommand> {

	private ExpeditedReportTree expeditedReportTree;

    public AeTab(String longTitle, String shortTitle, String viewName) {
        super(longTitle, shortTitle, viewName);
    }

  /**
   * Will also update the InputField mandatory flag.
   */
   @Override
   public Map<String, Object> referenceData(ExpeditedAdverseEventInputCommand command) {
	   Map <String, Object> refData = super.referenceData(command);
	   Object fieldGroups = refData.get("fieldGroups");
	   populateMandatoryFlag(fieldGroups, command, refData);
	   return refData;
   }



   /**
    * Will populate the mandatory flag.
    */
    @SuppressWarnings("unchecked")
    public void populateMandatoryFlag(Object fieldGroups, ExpeditedAdverseEventInputCommand command, Map<String, Object> refData){
      //TODO: need to see how to manage (this or that) kind mandatory fields
      //TODO: Why not this we handle in createFields() of every tab, so that the looping through the fields
      // here can be avoided.
	   if(!isMandatory(command)) return;

	   Map<String, InputFieldGroup> groupMap = (Map<String, InputFieldGroup>)fieldGroups;
	   if(groupMap == null ) return;
	   Map<String, Boolean> mandatoryFields = command.getMandatoryFieldMap();

	   boolean mandatory;
	   for(InputFieldGroup group : groupMap.values()){
		  for(InputField field : group.getFields()){
			 mandatory = fetchMandatoryValue(mandatoryFields, field);
			 field.setMandatory(mandatory);
		  }
	   }
    }

    public boolean fetchMandatoryValue(Map<String, Boolean> mandatoryFieldMap , InputField field){
    	boolean mandatory = false;
    	Boolean objMandatoryFlag = null;
    	String propertyName = field.getPropertyName().replaceAll("(\\[\\d+\\])", "[]");
    	if(propertyName.indexOf('.') > 0){
		  objMandatoryFlag = mandatoryFieldMap.get(propertyName.split("\\.", 2)[1]);
    	}else{
		  objMandatoryFlag = mandatoryFieldMap.get(propertyName);
    	}
    	mandatory = (objMandatoryFlag == null) ? false : objMandatoryFlag.booleanValue();
    	if(field.getCategory() == InputField.Category.COMPOSITE){
    		for(InputField subfield : CompositeField.getSubfields(field))
    			mandatory |= fetchMandatoryValue(mandatoryFieldMap, subfield);
    	}
    	return mandatory;
    }

   /**
    * Check's whether this tab is mandatory
    */
    public boolean isMandatory(ExpeditedAdverseEventInputCommand command){<commit_msg>fix for mandatory field (overriding issue)<commit_after>package gov.nih.nci.cabig.caaers.web.ae;

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

import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

import gov.nih.nci.cabig.caaers.domain.expeditedfields.ExpeditedReportSection;
import gov.nih.nci.cabig.caaers.domain.expeditedfields.ExpeditedReportTree;
import gov.nih.nci.cabig.caaers.domain.expeditedfields.TreeNode;
import gov.nih.nci.cabig.caaers.web.fields.CompositeField;
import gov.nih.nci.cabig.caaers.web.fields.InputField;
import gov.nih.nci.cabig.caaers.web.fields.InputFieldGroup;
import gov.nih.nci.cabig.caaers.web.fields.TabWithFields;

/**
 * @author <NAME>
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 */
public abstract class AeTab extends TabWithFields<ExpeditedAdverseEventInputCommand> {

	private ExpeditedReportTree expeditedReportTree;

    public AeTab(String longTitle, String shortTitle, String viewName) {
        super(longTitle, shortTitle, viewName);
    }

  /**
   * Will also update the InputField mandatory flag.
   */
   @Override
   public Map<String, Object> referenceData(ExpeditedAdverseEventInputCommand command) {
	   Map <String, Object> refData = super.referenceData(command);
	   Object fieldGroups = refData.get("fieldGroups");
	   populateMandatoryFlag(fieldGroups, command, refData);
	   return refData;
   }



   /**
    * Will populate the mandatory flag.
    */
    @SuppressWarnings("unchecked")
    public void populateMandatoryFlag(Object fieldGroups, ExpeditedAdverseEventInputCommand command, Map<String, Object> refData){
      //TODO: need to see how to manage (this or that) kind mandatory fields
      //TODO: Why not this we handle in createFields() of every tab, so that the looping through the fields
      // here can be avoided.
	   if(!isMandatory(command)) return;

	   Map<String, InputFieldGroup> groupMap = (Map<String, InputFieldGroup>)fieldGroups;
	   if(groupMap == null ) return;
	   Map<String, Boolean> mandatoryFields = command.getMandatoryFieldMap();

	   boolean mandatory;
	   for(InputFieldGroup group : groupMap.values()){
		  for(InputField field : group.getFields()){
			 mandatory = fetchMandatoryValue(mandatoryFields, field);
			 if(mandatory) field.setMandatory(mandatory);
		  }
	   }
    }

    public boolean fetchMandatoryValue(Map<String, Boolean> mandatoryFieldMap , InputField field){
    	boolean mandatory = false;
    	Boolean objMandatoryFlag = null;
    	String propertyName = field.getPropertyName().replaceAll("(\\[\\d+\\])", "[]");
    	if(propertyName.indexOf('.') > 0){
		  objMandatoryFlag = mandatoryFieldMap.get(propertyName.split("\\.", 2)[1]);
    	}else{
		  objMandatoryFlag = mandatoryFieldMap.get(propertyName);
    	}
    	mandatory = (objMandatoryFlag == null) ? false : objMandatoryFlag.booleanValue();
    	if(field.getCategory() == InputField.Category.COMPOSITE){
    		for(InputField subfield : CompositeField.getSubfields(field))
    			mandatory |= fetchMandatoryValue(mandatoryFieldMap, subfield);
    	}
    	return mandatory;
    }

   /**
    * Check's whether this tab is mandatory
    */
    public boolean isMandatory(ExpeditedAdverseEventInputCommand command){
{'fraction_non_alphanumeric': 0.11316715094051079, 'fraction_numerical': 0.0009175714941122495, 'mean_word_length': 4.32139951179821, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 31, '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': '6079701', 'n_tokens_mistral': 2115, 'n_tokens_neox': 2017, 'n_words': 411}