<commit_before>package org.adaptlab.chpir.android.participanttracker;

import java.util.HashMap;
import java.util.List;

import org.adaptlab.chpir.android.participanttracker.models.Participant;
import org.adaptlab.chpir.android.participanttracker.models.ParticipantProperty;
import org.adaptlab.chpir.android.participanttracker.models.ParticipantType;
import org.adaptlab.chpir.android.participanttracker.models.Property;
import org.adaptlab.chpir.android.participanttracker.models.Relationship;
import org.adaptlab.chpir.android.participanttracker.models.RelationshipType;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.graphics.PorterDuff;

public class NewParticipantFragment extends Fragment {
    private static final String TAG = "NewParticipantFragment";
    public final static String EXTRA_PARTICIPANT_TYPE_ID = 
            "org.adaptlab.chpir.participanttracker.newparticipantfragment.participant_type_id";
    public final static String EXTRA_PARTICIPANT_ID = 
            "org.adaptlab.chpir.participanttracker.newparticipantfragment.participant_id";
    
    private ParticipantType mParticipantType;
    private Participant mParticipant;
    private HashMap<Property, View> mPropertyFields;
    private HashMap<RelationshipType, Participant> mRelationshipFields;
    
    private LinearLayout mParticipantPropertiesContainer;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        if (savedInstanceState != null) {
            mParticipantType = ParticipantType.findById(savedInstanceState.getLong(EXTRA_PARTICIPANT_TYPE_ID));
        } else {
            Long participantTypeId = getActivity().getIntent().getLongExtra(EXTRA_PARTICIPANT_TYPE_ID, -1);
            if (participantTypeId == -1) return;

            mParticipantType = ParticipantType.findById(participantTypeId);
            mPropertyFields = new HashMap<Property, View>();
            mRelationshipFields = new HashMap<RelationshipType, Participant>();
        }
        
        loadOrCreateParticipant();
    }
        
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent,
            Bundle savedInstanceState) {
        setHasOptionsMenu(true);
        
        View v = inflater.inflate(R.layout.fragment_new_participant, parent,
                false);
        
        mParticipantPropertiesContainer = (LinearLayout) v.findViewById(R.id.new_participant_properties_container);

        for (Property property : mParticipantType.getProperties()) {
            attachLabelForProperty(property);
            attachFieldForProperty(property);
            attachRequiredLabel(property);
        }
                
        for (RelationshipType relationshipType : mParticipantType.getRelationshipTypes()) {
            attachSelectRelationshipButton(relationshipType);
        }
        
        return v;
    }
    
    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.new_participant, menu);
        super.onCreateOptionsMenu(menu,inflater);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_save:
                saveParticipant();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    
    private boolean isMissingRequiredValue() {
        boolean missingField = false;
        for (Property property : mParticipantType.getProperties()) {
            if (property.getRequired() && getValueForProperty(property).trim().equals("")) {
                if (mPropertyFields.get(property) instanceof EditText) {
                    ((EditText) mPropertyFields.get(property)).setError(getString(R.string.required_field));
                }
                missingField = true;
            }
        }
        
        return missingField;
    }
    
    private boolean hasInvalidValidator() {
        for (Property property : mParticipantType.getProperties()) {
            if (property.hasValidator() && !property.getValidationCallable().validate(getValueForProperty(property))) {
                return true;
            }
        }
        
        return false;
    }
    
    private void attachRequiredLabel(Property property) {
        if (property.getRequired()) {
            TextView requiredTextView = new TextView(getActivity());
            requiredTextView.setText(getString(R.string.required_field));
            requiredTextView.setTextColor(Color.RED);
            mParticipantPropertiesContainer.addView(requiredTextView);
        }
    }
    
    private void attachLabelForProperty(Property property) {
        TextView textView = new TextView(getActivity());
        textView.setText(property.getLabel());
        textView.setTextAppearance(getActivity(), R.style.sectionHeader);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(0, 50, 0, 0);
        textView.setLayoutParams(layoutParams);
        mParticipantPropertiesContainer.addView(textView);
    }
    
    private void attachFieldForProperty(final Property property) {       
        String propertyValue = "";        
        if (mParticipant.hasParticipantProperty(property)) {
            propertyValue = mParticipant.getParticipantProperty(property).getValue();
        }
        
        View propertyView = null;
        
        if (property.getTypeOf() == Property.PropertyType.INTEGER) {
            propertyView = new EditText(getActivity());            
            ((EditText) propertyView).setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED);
            ((EditText) propertyView).setText(propertyValue);       
            attachValidator(property, (EditText) propertyView);
        } else if (property.getTypeOf() == Property.PropertyType.DATE) {
            propertyView = new SerializableDatePicker(getActivity());           
            ((SerializableDatePicker) propertyView).setCalendarViewShown(false);
            ((SerializableDatePicker) propertyView).deserialize(propertyValue);           
        } else {
            propertyView = new EditText(getActivity());            
            ((EditText) propertyView).setText(propertyValue);
            attachValidator(property, (EditText) propertyView);           
        }
        
        mPropertyFields.put(property, propertyView);
        mParticipantPropertiesContainer.addView(propertyView);   
    }
    
    private void attachValidator(final Property property, final EditText propertyView) {
        propertyView.addTextChangedListener(new TextWatcher(){
            public void afterTextChanged(Editable s) {
                if (property.getValidationCallable() == null) return;
<commit_msg>Modify escape from validation to be more clear<commit_after>package org.adaptlab.chpir.android.participanttracker;

import java.util.HashMap;
import java.util.List;

import org.adaptlab.chpir.android.participanttracker.models.Participant;
import org.adaptlab.chpir.android.participanttracker.models.ParticipantProperty;
import org.adaptlab.chpir.android.participanttracker.models.ParticipantType;
import org.adaptlab.chpir.android.participanttracker.models.Property;
import org.adaptlab.chpir.android.participanttracker.models.Relationship;
import org.adaptlab.chpir.android.participanttracker.models.RelationshipType;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.graphics.PorterDuff;

public class NewParticipantFragment extends Fragment {
    private static final String TAG = "NewParticipantFragment";
    public final static String EXTRA_PARTICIPANT_TYPE_ID = 
            "org.adaptlab.chpir.participanttracker.newparticipantfragment.participant_type_id";
    public final static String EXTRA_PARTICIPANT_ID = 
            "org.adaptlab.chpir.participanttracker.newparticipantfragment.participant_id";
    
    private ParticipantType mParticipantType;
    private Participant mParticipant;
    private HashMap<Property, View> mPropertyFields;
    private HashMap<RelationshipType, Participant> mRelationshipFields;
    
    private LinearLayout mParticipantPropertiesContainer;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        if (savedInstanceState != null) {
            mParticipantType = ParticipantType.findById(savedInstanceState.getLong(EXTRA_PARTICIPANT_TYPE_ID));
        } else {
            Long participantTypeId = getActivity().getIntent().getLongExtra(EXTRA_PARTICIPANT_TYPE_ID, -1);
            if (participantTypeId == -1) return;

            mParticipantType = ParticipantType.findById(participantTypeId);
            mPropertyFields = new HashMap<Property, View>();
            mRelationshipFields = new HashMap<RelationshipType, Participant>();
        }
        
        loadOrCreateParticipant();
    }
        
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent,
            Bundle savedInstanceState) {
        setHasOptionsMenu(true);
        
        View v = inflater.inflate(R.layout.fragment_new_participant, parent,
                false);
        
        mParticipantPropertiesContainer = (LinearLayout) v.findViewById(R.id.new_participant_properties_container);

        for (Property property : mParticipantType.getProperties()) {
            attachLabelForProperty(property);
            attachFieldForProperty(property);
            attachRequiredLabel(property);
        }
                
        for (RelationshipType relationshipType : mParticipantType.getRelationshipTypes()) {
            attachSelectRelationshipButton(relationshipType);
        }
        
        return v;
    }
    
    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.new_participant, menu);
        super.onCreateOptionsMenu(menu,inflater);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_save:
                saveParticipant();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    
    private boolean isMissingRequiredValue() {
        boolean missingField = false;
        for (Property property : mParticipantType.getProperties()) {
            if (property.getRequired() && getValueForProperty(property).trim().equals("")) {
                if (mPropertyFields.get(property) instanceof EditText) {
                    ((EditText) mPropertyFields.get(property)).setError(getString(R.string.required_field));
                }
                missingField = true;
            }
        }
        
        return missingField;
    }
    
    private boolean hasInvalidValidator() {
        for (Property property : mParticipantType.getProperties()) {
            if (property.hasValidator() && !property.getValidationCallable().validate(getValueForProperty(property))) {
                return true;
            }
        }
        
        return false;
    }
    
    private void attachRequiredLabel(Property property) {
        if (property.getRequired()) {
            TextView requiredTextView = new TextView(getActivity());
            requiredTextView.setText(getString(R.string.required_field));
            requiredTextView.setTextColor(Color.RED);
            mParticipantPropertiesContainer.addView(requiredTextView);
        }
    }
    
    private void attachLabelForProperty(Property property) {
        TextView textView = new TextView(getActivity());
        textView.setText(property.getLabel());
        textView.setTextAppearance(getActivity(), R.style.sectionHeader);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(0, 50, 0, 0);
        textView.setLayoutParams(layoutParams);
        mParticipantPropertiesContainer.addView(textView);
    }
    
    private void attachFieldForProperty(final Property property) {       
        String propertyValue = "";        
        if (mParticipant.hasParticipantProperty(property)) {
            propertyValue = mParticipant.getParticipantProperty(property).getValue();
        }
        
        View propertyView = null;
        
        if (property.getTypeOf() == Property.PropertyType.INTEGER) {
            propertyView = new EditText(getActivity());            
            ((EditText) propertyView).setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED);
            ((EditText) propertyView).setText(propertyValue);       
            attachValidator(property, (EditText) propertyView);
        } else if (property.getTypeOf() == Property.PropertyType.DATE) {
            propertyView = new SerializableDatePicker(getActivity());           
            ((SerializableDatePicker) propertyView).setCalendarViewShown(false);
            ((SerializableDatePicker) propertyView).deserialize(propertyValue);           
        } else {
            propertyView = new EditText(getActivity());            
            ((EditText) propertyView).setText(propertyValue);
            attachValidator(property, (EditText) propertyView);           
        }
        
        mPropertyFields.put(property, propertyView);
        mParticipantPropertiesContainer.addView(propertyView);   
    }
    
    private void attachValidator(final Property property, final EditText propertyView) {
        propertyView.addTextChangedListener(new TextWatcher(){
            public void afterTextChanged(Editable s) {
                if (!property.hasValidator()) return;

{'fraction_non_alphanumeric': 0.08518640712636094, 'fraction_numerical': 0.0010557571758495546, 'mean_word_length': 2.924391506991196, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '6543942', 'n_tokens_mistral': 3978, 'n_tokens_neox': 3761, 'n_words': 666}