<commit_before>package org.openmrs.web.controller.provider;

import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openmrs.Provider;
import org.openmrs.ProviderAttribute;
import org.openmrs.ProviderAttributeType;
import org.openmrs.api.ProviderService;
import org.openmrs.api.context.Context;
import org.openmrs.attribute.handler.AttributeHandler;
import org.openmrs.propertyeditor.PersonEditor;
import org.openmrs.validator.ProviderValidator;
import org.openmrs.web.WebConstants;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.WebRequest;

@Controller
@RequestMapping("/admin/provider/provider.form")
public class ProviderFormController {
	
	protected final Log log = LogFactory.getLog(getClass());
	
	@InitBinder
	protected void initBinder(WebDataBinder binder) throws Exception {
		binder.registerCustomEditor(org.openmrs.Person.class, new PersonEditor());
	}
	
	@RequestMapping(method = RequestMethod.POST)
	public String onSubmit(WebRequest request, @RequestParam(required = false) String saveProviderButton,
	        @RequestParam(required = false) String retireProviderButton,
	        @RequestParam(required = false) String unretireProviderButton,
	        @RequestParam(required = false) boolean linkToPerson, @ModelAttribute("provider") Provider provider,
	        BindingResult errors, ModelMap model) throws Exception {
		
		//For existing providers, switch between linking to person or use name
		if (provider.getProviderId() != null) {
			if (linkToPerson)
				provider.setName(null);
			else
				provider.setPerson(null);
		}
		
		handleAttributeParameteres(request, provider, model);
		new ProviderValidator().validate(provider, errors);
		
		if (!errors.hasErrors()) {
			if (Context.isAuthenticated()) {
				ProviderService service = Context.getProviderService();
				
				String message = "Provider.saved";
				if (saveProviderButton != null) {
					service.saveProvider(provider);
				} else if (retireProviderButton != null) {
					service.retireProvider(provider, provider.getRetireReason());
					message = "Provider.retired";
				} else if (unretireProviderButton != null) {
					service.unretireProvider(provider);
					message = "Provider.unretired";
				}
				
				request.setAttribute(WebConstants.OPENMRS_MSG_ATTR, message, WebRequest.SCOPE_SESSION);<commit_msg>Follow up to fix broken retire/unretire functionalities - TRUNK-2686<commit_after>package org.openmrs.web.controller.provider;

import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openmrs.Provider;
import org.openmrs.ProviderAttribute;
import org.openmrs.ProviderAttributeType;
import org.openmrs.api.ProviderService;
import org.openmrs.api.context.Context;
import org.openmrs.attribute.handler.AttributeHandler;
import org.openmrs.propertyeditor.PersonEditor;
import org.openmrs.validator.ProviderValidator;
import org.openmrs.web.WebConstants;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.WebRequest;

@Controller
@RequestMapping("/admin/provider/provider.form")
public class ProviderFormController {
	
	protected final Log log = LogFactory.getLog(getClass());
	
	@InitBinder
	protected void initBinder(WebDataBinder binder) throws Exception {
		binder.registerCustomEditor(org.openmrs.Person.class, new PersonEditor());
	}
	
	@RequestMapping(method = RequestMethod.POST)
	public String onSubmit(WebRequest request, @RequestParam(required = false) String saveProviderButton,
	        @RequestParam(required = false) String retireProviderButton,
	        @RequestParam(required = false) String unretireProviderButton,
	        @RequestParam(required = false) boolean linkToPerson, @ModelAttribute("provider") Provider provider,
	        BindingResult errors, ModelMap model) throws Exception {
		
		if (saveProviderButton != null) {
			//For existing providers, switch between linking to person or use name
			if (provider.getProviderId() != null) {
				if (linkToPerson)
					provider.setName(null);
				else
					provider.setPerson(null);
			}
		}
		
		handleAttributeParameteres(request, provider, model);
		new ProviderValidator().validate(provider, errors);
		
		if (!errors.hasErrors()) {
			if (Context.isAuthenticated()) {
				ProviderService service = Context.getProviderService();
				
				String message = "Provider.saved";
				if (saveProviderButton != null) {
					service.saveProvider(provider);
				} else if (retireProviderButton != null) {
					service.retireProvider(provider, provider.getRetireReason());
					message = "Provider.retired";
				} else if (unretireProviderButton != null) {
					service.unretireProvider(provider);
					message = "Provider.unretired";
				}
				
				request.setAttribute(WebConstants.OPENMRS_MSG_ATTR, message, WebRequest.SCOPE_SESSION);
{'fraction_non_alphanumeric': 0.09976836532097948, 'fraction_numerical': 0.0006618133686300463, 'mean_word_length': 5.356466876971608, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5047310', 'n_tokens_mistral': 1819, 'n_tokens_neox': 1628, 'n_words': 329}