<commit_before>package gov.nih.nci.cabig.ctms.web.tabs;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.validation.Errors;
import org.springframework.web.servlet.mvc.AbstractWizardFormController;

/**
 * More object-oriented version of {@link AbstractWizardFormController}.
 * Controller is configured with a {@link Flow} of {@link Tab}s -- most controller
 * page-specific controller methods are delegated to implementations in the tabs.
 * <p>
 * The type parameter <kbd>C</kbd> is the class of the command that will be used with the
 * controller.
 *
 * @author <NAME>, Priyatam
 */
public abstract class AbstractTabbedFlowFormController<C> extends AbstractWizardFormController
    implements InitializingBean
{
    private final Log log = LogFactory.getLog(getClass());

    private Flow<C> flow;

    private TabConfigurer tabConfigurer;

    public Flow<C> getFlow() {
        return flow;
    }

    public void setFlow(Flow<C> flow) {
        this.flow = flow;
    }
    
    public String getFlowAttributeName() {
    	return getClass().getName() + ".FLOW." + getFlow().getName();
	}

    /**
     * Check if Alternate flow (Sub Flows
     * @param request
     * @return
     */
	public boolean isUseAlternateFlow(HttpServletRequest request) {
		return request.getSession().getAttribute(getClass().getName() + ".FLOW." +
			getFlow().getName()+".ALT_FLOW")!=null?true:false;
	}

	/**
	 * Set Alternate Flow (Sub flows)
	 * @param request
	 */
	public void useAlternateFlow(HttpServletRequest request) {
		request.getSession().setAttribute(getClass().getName() + ".FLOW." + 
			getFlow().getName()+".ALT_FLOW","true");
	}
    
    @Override
    @SuppressWarnings("unchecked")
    protected final Map<?, ?> referenceData(HttpServletRequest request, Object command, Errors errors, int page) 
    	throws Exception {
        Map<String, Object> refdata = new HashMap<String, Object>();
        Map refDataCall=referenceData(request, page);
        
        if(refDataCall!=null){
        	refdata.putAll(refDataCall);
        }
        Tab<C> current = getFlow().getTab(page);
        refdata.put("tab", current);
        
        // insert current flow or alternate flow
        if(isUseAlternateFlow(request)){
        	Flow altFlow=(Flow)request.getSession().getAttribute(getFlowAttributeName());
        	if(altFlow!=null)
        		refdata.put("flow", altFlow);
        	else
        		refdata.put("flow", getFlow());
        }else{
        	refdata.put("flow", getFlow());
        }
        
        // get refData from Subclasses
        refdata.putAll(referenceDataController(request, command, errors, page));
        
        // get refData from the Tabs 
        refdata.putAll(current.referenceData((C) command));
        return refdata;
    }

    /**
     * Template method for individual controllers to add refdata
     */
    protected Map<String, Object> referenceDataController(HttpServletRequest request, Object command, Errors errors, int page) {
    	//default implementation
    	return new HashMap<String, Object>();
    }

    @Override
    protected int getPageCount(HttpServletRequest request, Object command) {
        return getFlow().getTabCount();
    }

    @Override
    protected String getViewName(HttpServletRequest request, Object command, int page) {
        return getFlow().getTab(page).getViewName();
    }

    @Override
    @SuppressWarnings("unchecked")
    protected void validatePage(Object oCommand, Errors errors, int page, boolean finish) {
        C command = (C) oCommand;
        Tab<C> tab = getFlow().getTab(page);

        // XXX TODO: this isn't threadsafe at all
        setAllowDirtyForward(tab.isAllowDirtyForward());
        setAllowDirtyBack(tab.isAllowDirtyBack());

        tab.validate(command, errors);
    }

    @Override
    @SuppressWarnings("unchecked")
    protected void postProcessPage(
        HttpServletRequest request, Object oCommand, Errors errors, int page
    ) throws Exception {
        C command = (C) oCommand;
        getFlow().getTab(page).postProcess(request, command, errors);
        
        afterPostProcessPage(request, command, errors, page);
    }
    
    
    /**
     * Template method to do custom processing post 'postProcessPage'
     * This is useful when processing is needed by Controller rather than a Tab
     * (like saves/updates)
     * @param request
     * @param oCommand
     * @param errors
     * @param page
     */
    protected void afterPostProcessPage(HttpServletRequest request, Object oCommand, 
    	Errors errors, int page) {
    	//default null implementation
    }
    <commit_msg>Remove unnecessary template methods; reorganize alt flow code for readability/reuse<commit_after>package gov.nih.nci.cabig.ctms.web.tabs;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.validation.Errors;
import org.springframework.web.servlet.mvc.AbstractWizardFormController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * More object-oriented version of {@link AbstractWizardFormController}.
 * Controller is configured with a {@link Flow} of {@link Tab}s -- most controller
 * page-specific controller methods are delegated to implementations in the tabs.
 * <p>
 * The type parameter <kbd>C</kbd> is the class of the command that will be used with the
 * controller.
 *
 * @author <NAME>
 * @author Priyatam
 */
public abstract class AbstractTabbedFlowFormController<C> extends AbstractWizardFormController
    implements InitializingBean
{
    private final Log log = LogFactory.getLog(getClass());

    private Flow<C> flow;

    private TabConfigurer tabConfigurer;

    public Flow<C> getFlow() {
        return flow;
    }

    public void setFlow(Flow<C> flow) {
        this.flow = flow;
    }

    public String getFlowAttributeName() {
        return getClass().getName() + ".FLOW." + getFlow().getName();
    }

    public String getAlternateFlowAttributeName() {
        return getClass().getName() + ".FLOW." + getFlow().getName() + ".ALT_FLOW";
    }

    /**
     * Check if Alternate flow (Sub Flows
     *
     * @param request
     * @return
     */
    public boolean isUseAlternateFlow(HttpServletRequest request) {
        return request.getSession().getAttribute(getAlternateFlowAttributeName()) != null;
    }

    /**
     * Set Alternate Flow (Sub flows)
     *
     * @param request
     */
    public void useAlternateFlow(HttpServletRequest request) {
        request.getSession().setAttribute(getAlternateFlowAttributeName(), "true");
    }

    @Override
    @SuppressWarnings("unchecked")
    protected Map<?, ?> referenceData(HttpServletRequest request, Object command, Errors errors, int page)
        throws Exception {
        // The super invocation includes all refdata from #referenceData(request, page)
        Map<String, Object> refdata = super.referenceData(request, command, errors, page);
        if (refdata == null) {
            refdata = new HashMap<String, Object>();
        }

        Tab<C> current = getFlow().getTab(page);
        refdata.put("tab", current);
        refdata.put("flow", getEffectiveFlow(request));
        refdata.putAll(current.referenceData((C) command));
        log.debug("Returning reference data for page " + page);
        log.debug("Command is " + command);
        return refdata;
    }

    /**
     * Select current flow or alternate
     */
    @SuppressWarnings("unchecked")
    private Flow<C> getEffectiveFlow(HttpServletRequest request) {
        Flow<C> effective;
        if (isUseAlternateFlow(request)) {
            Flow<C> altFlow = (Flow<C>) request.getSession().getAttribute(getFlowAttributeName());
            effective = altFlow == null ? getFlow() : getFlow();
        } else {
            effective = getFlow();
        }
        return effective;
    }

    @Override
    protected int getPageCount(HttpServletRequest request, Object command) {
        return getFlow().getTabCount();
    }

    @Override
    protected String getViewName(HttpServletRequest request, Object command, int page) {
        return getFlow().getTab(page).getViewName();
    }

    @Override
    @SuppressWarnings("unchecked")
    protected void validatePage(Object oCommand, Errors errors, int page, boolean finish) {
        C command = (C) oCommand;
        Tab<C> tab = getFlow().getTab(page);

        // XXX TODO: this isn't threadsafe at all
        setAllowDirtyForward(tab.isAllowDirtyForward());
        setAllowDirtyBack(tab.isAllowDirtyBack());

        tab.validate(command, errors);
    }

    @Override
    @SuppressWarnings("unchecked")
    protected void postProcessPage(
        HttpServletRequest request, Object oCommand, Errors errors, int page
    ) throws Exception {
        C command = (C) oCommand;
        getFlow().getTab(page).postProcess(request, command, errors);
    }

{'fraction_non_alphanumeric': 0.10864600326264275, 'fraction_numerical': 0.0, 'mean_word_length': 3.1090259159964253, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, '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': '6893589', 'n_tokens_mistral': 2538, 'n_tokens_neox': 2518, 'n_words': 654}