<commit_before>package org.openregistry.core.web;

import org.openregistry.core.domain.*;

import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Errors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.context.MessageSource;

import javax.servlet.http.HttpServletRequest;
import javax.annotation.Resource;

import org.javalid.core.AnnotationValidator;
import org.javalid.core.AnnotationValidatorImpl;
import org.javalid.core.ValidationMessage;
import org.javalid.external.spring.SpringMessageConverter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import org.openregistry.core.web.propertyeditors.*;
import org.openregistry.core.service.PersonService;
import org.openregistry.core.service.ServiceExecutionResult;
import org.openregistry.core.repository.PersonRepository;
import org.openregistry.core.repository.ReferenceRepository;
import org.openregistry.service.DefaultServiceExecutionResultImpl;
import com.sun.xml.internal.bind.v2.TODO;

/**
 * @author <NAME>
 * @version $Revision$ $Date$
 * @since 1.0.0
 */
@Controller
@RequestMapping("/addRole.htm")
@SessionAttributes({"role","personKey"})
public class PersonRegistryController {

	protected final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private PersonService personService;

    private MessageSource messageSource;
    
    @Autowired(required=true)
    public PersonRegistryController(MessageSource messageSource) {
        this.messageSource = messageSource;
    }
    
    @Autowired(required=true)
    private ReferenceRepository referenceRepository;

    @ModelAttribute("countryLookup")
    public List<Country> populateCountryLookup() {
        return this.referenceRepository.getCountries();
    }

    @ModelAttribute("campusLookup")
    public List<Campus> populateCampusLookup() {
        return this.referenceRepository.getCampuses();
    }

    @ModelAttribute("departmentLookup")
    public List<Department> populateDepartmentLookup() {
        return this.referenceRepository.getDepartments();
    }

    @ModelAttribute("sponsorLookup")
    public List<Person> populateSponsorLookup() {
        return this.referenceRepository.getPeople();
    }

	@RequestMapping(method = RequestMethod.GET)
    public String addRoleSetUpForm(ModelMap model, @RequestParam("personKey")String personKey, @RequestParam("roleInfoKey")String roleInfoKey) {
    	logger.info("Populating: setUpForm: ");

        Person person = personService.findPersonById(new Long(personKey));
        RoleInfo roleInfo = referenceRepository.getRoleInfo(new Long(roleInfoKey));
        Role role = addRole(person, roleInfo);

        model.addAttribute("personDescription", getPersonDisplayName(person));
        model.addAttribute("role",role);
        model.addAttribute("personKey", personKey);
    	return "addRole";
    }

	@RequestMapping(method = RequestMethod.POST)
	public String addRoleProcessSubmit(ModelMap model, @ModelAttribute("personKey") String personKey, @ModelAttribute("role") Role role, BindingResult result, SessionStatus status ) {
        logger.info("processSubmit in add role");

        Person person = personService.findPersonById(new Long(personKey));
        if (!result.hasErrors()){
            ServiceExecutionResult res = personService.validateAndSaveRoleForPerson(person, role);

            Errors errors = ((DefaultServiceExecutionResultImpl)res).getErrors();
            if (!errors.hasErrors()){
                model.addAttribute("infoModel", "Role has been added.");
                status.setComplete();
            } else {
                result.addAllErrors(errors);
            }
        }

        model.addAttribute("personDescription", getPersonDisplayName(person));
        model.addAttribute("role",role);
        model.addAttribute("personKey", personKey);
		return "addRole";
	}

    @InitBinder
    protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
        DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
        binder.registerCustomEditor(Date.class, null, new CustomDateEditor(df, true));
        binder.registerCustomEditor(String.class,"phone.number", new PhoneEditor());
        binder.registerCustomEditor(Country.class,"addresses.country", new CountryEditor(referenceRepository));
        binder.registerCustomEditor(Region.class,"addresses.region", new RegionEditor(referenceRepository));
        binder.registerCustomEditor(Person.class,"sponsor", new SponsorEditor(referenceRepository));
        binder.registerCustomEditor(Department.class,"department", new DepartmentEditor(referenceRepository));
    }

    /**
     * Construct person information to display in heading.
     * @param person
     * @return
     */
    protected String getPersonDisplayName(Person person){
        String name = person.getOfficialName().toString()  + " (ID: " + person.getId() + ")";
        return name;
    }

    /**
     * Add and initialize new role.
     * @param person
     * @param roleInfo
     * @return role
     */
    protected Role addRole(Person person, RoleInfo roleInfo){
        Role role = person.addRole(roleInfo);
        role.addEmailAddress();
        role.addPhone();
        role.addAddress();

        //provide default values for start and end date of role
        Calendar cal = Calendar.getInstance();
        role.setStart(cal.getTime());
        cal.add(Calendar.MONTH, 6);
        role.setEnd(cal.getTime());
        return role;
    }
    

}<commit_msg>NOJIRA<commit_after>package org.openregistry.core.web;

import org.openregistry.core.domain.*;

import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Errors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.context.MessageSource;

import javax.servlet.http.HttpServletRequest;
import javax.annotation.Resource;

import org.javalid.core.AnnotationValidator;
import org.javalid.core.AnnotationValidatorImpl;
import org.javalid.core.ValidationMessage;
import org.javalid.external.spring.SpringMessageConverter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import org.openregistry.core.web.propertyeditors.*;
import org.openregistry.core.service.PersonService;
import org.openregistry.core.service.ServiceExecutionResult;
import org.openregistry.core.repository.PersonRepository;
import org.openregistry.core.repository.ReferenceRepository;
import org.openregistry.service.DefaultServiceExecutionResultImpl;
import com.sun.xml.internal.bind.v2.TODO;

/**
 * @author <NAME>
 * @version $Revision$ $Date$
 * @since 1.0.0
 */
@Controller
@RequestMapping("/addRole.htm")
@SessionAttributes({"role","person"})
public class PersonRegistryController {

	protected final Logger logger = LoggerFactory.getLogger(getClass());
    protected final String ACTIVE_STATUS = "Active";

    @Autowired
    private PersonService personService;

    private MessageSource messageSource;
    
    @Autowired(required=true)
    public PersonRegistryController(MessageSource messageSource) {
        this.messageSource = messageSource;
    }
    
    @Autowired(required=true)
    private ReferenceRepository referenceRepository;

    @ModelAttribute("countryLookup")
    public List<Country> populateCountryLookup() {
        return this.referenceRepository.getCountries();
    }

    @ModelAttribute("campusLookup")
    public List<Campus> populateCampusLookup() {
        return this.referenceRepository.getCampuses();
    }

    @ModelAttribute("departmentLookup")
    public List<Department> populateDepartmentLookup() {
        return this.referenceRepository.getDepartments();
    }

    @ModelAttribute("sponsorLookup")
    public List<Person> populateSponsorLookup() {
        return this.referenceRepository.getPeople();
    }

    /*
    @ModelAttribute("personStatusLookup")
    public List<Type> populatePersonStatusLookup() {
        return this.referenceRepository.getPersonStatus();
    }*/

	@RequestMapping(method = RequestMethod.GET)
    public String addRoleSetUpForm(ModelMap model, @RequestParam("personKey")String personKey, @RequestParam("roleInfoKey")String roleInfoKey) {
    	logger.info("Populating: setUpForm: ");

        Person person = personService.findPersonById(new Long(personKey));
        RoleInfo roleInfo = referenceRepository.getRoleInfo(new Long(roleInfoKey));
        Role role = addRole(person, roleInfo);

        model.addAttribute("personDescription", getPersonDisplayName(person));
        model.addAttribute("role",role);
        model.addAttribute("person",person);
    	return "addRole";
    }

	@RequestMapping(method = RequestMethod.POST)
	public String addRoleProcessSubmit(ModelMap model, @ModelAttribute("person") Person person, @ModelAttribute("role") Role role, BindingResult result, SessionStatus status ) {
        logger.info("processSubmit in add role");

        if (!result.hasErrors()){
            ServiceExecutionResult res = personService.validateAndSaveRoleForPerson(person, role);

            Errors errors = ((DefaultServiceExecutionResultImpl)res).getErrors();
            if (!errors.hasErrors()){
                model.addAttribute("infoModel", "Role has been added.");
                status.setComplete();
            } else {
                result.addAllErrors(errors);
            }
        }

        model.addAttribute("personDescription", getPersonDisplayName(person));
        model.addAttribute("role",role);
        model.addAttribute("person",person);
		return "addRole";
	}

    @InitBinder
    protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
        DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
        binder.registerCustomEditor(Date.class, null, new CustomDateEditor(df, true));
        binder.registerCustomEditor(String.class,"phone.number", new PhoneEditor());
        binder.registerCustomEditor(Country.class,"addresses.country", new CountryEditor(referenceRepository));
        binder.registerCustomEditor(Region.class,"addresses.region", new RegionEditor(referenceRepository));
        binder.registerCustomEditor(Person.class,"sponsor", new SponsorEditor(referenceRepository));
        binder.registerCustomEditor(Department.class,"department", new DepartmentEditor(referenceRepository));
    }

    /**
     * Construct person information to display in heading.
     * @param person
     * @return
     */
    protected String getPersonDisplayName(Person person){
        String name = person.getOfficialName().toString()  + " (ID: " + person.getId() + ")";
        return name;
    }

    /**
     * Add and initialize new role.
     * @param person
     * @param roleInfo
     * @return role
     */
    protected Role addRole(Person person, RoleInfo roleInfo){
        Role role = person.addRole(roleInfo);
        role.addEmailAddress();
        role.addPhone();
        role.addAddress();
        setPersonStatus(role,ACTIVE_STATUS);

        //provide default values for start and end date of role
        Calendar cal = Calendar.getInstance();
        role.setStart(cal.getTime());
        cal.add(Calendar.MONTH, 6);
        role.setEnd(cal.getTime());
        return role;
    }

    /**
     * This needs to be redone to use a helper.
     * @param role
     * @param status
     */
    protected void setPersonStatus(Role role, String status) {
        List statusList = referenceRepository.getPersonStatus();
        Iterator<Type> iterator = statusList.iterator();
	    while ( iterator.hasNext() ){
	        Type type = iterator.next();
            if (type.getDescription().equals(status)){
                role.setPersonStatus(type);
                break;
            }
	   }
    }
    

}
{'fraction_non_alphanumeric': 0.10707039175901358, 'fraction_numerical': 0.0010925550179491181, 'mean_word_length': 3.9497875627655463, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '7492124', 'n_tokens_mistral': 3383, 'n_tokens_neox': 3301, 'n_words': 636}