All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.primefaces.renderkit.OutcomeTargetRenderer Maven / Gradle / Ivy

There is a newer version: 14.0.0-RC3
Show newest version
/*
 * Copyright 2009-2013 PrimeTek.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.primefaces.renderkit;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.faces.application.ConfigurableNavigationHandler;
import javax.faces.application.NavigationCase;
import javax.faces.context.FacesContext;
import org.primefaces.component.api.UIOutcomeTarget;

public class OutcomeTargetRenderer extends CoreRenderer {
    
    protected NavigationCase findNavigationCase(FacesContext context, UIOutcomeTarget outcomeTarget) {
        ConfigurableNavigationHandler navHandler = (ConfigurableNavigationHandler) context.getApplication().getNavigationHandler();
        String outcome = outcomeTarget.getOutcome();
        
        if(outcome == null) {
            outcome = context.getViewRoot().getViewId();
        }
        
        return navHandler.getNavigationCase(context, null, outcome);
    }

    /**
     * Find all parameters to include by looking at nested uiparams and params of navigation case
     */
    protected Map> getParams(NavigationCase navCase, UIOutcomeTarget outcomeTarget) {
        //UI Params
        Map> params = outcomeTarget.getParams();       

        //NavCase Params
        Map> navCaseParams = navCase.getParameters();
        if(navCaseParams != null && !navCaseParams.isEmpty()) {
            if(params == null) {
                params = new LinkedHashMap>();
            }
            
            for(Map.Entry> entry : navCaseParams.entrySet()) {
                String key = entry.getKey();

                //UIParams take precedence
                if(!params.containsKey(key)) {
                    params.put(key, entry.getValue());
                }
            }
        }

        return params;
    }

    protected boolean isIncludeViewParams(UIOutcomeTarget outcomeTarget, NavigationCase navCase) {
        return outcomeTarget.isIncludeViewParams() || navCase.isIncludeViewParams();
    }
    
    protected String getTargetURL(FacesContext context, UIOutcomeTarget outcomeTarget) {
        String url;
        String href = outcomeTarget.getHref();
        
        if(href != null) {
            url = getResourceURL(context, href);
        }
        else {
            NavigationCase navCase = findNavigationCase(context, outcomeTarget);
            String toViewId = navCase.getToViewId(context);
            boolean isIncludeViewParams = isIncludeViewParams(outcomeTarget, navCase);
            Map> params = getParams(navCase, outcomeTarget);

            if (params == null)
            {
                params = Collections.emptyMap();
            }

            url = context.getApplication().getViewHandler().getBookmarkableURL(context, toViewId, params, isIncludeViewParams);

            if(outcomeTarget.getFragment() != null) {
                url += "#" + outcomeTarget.getFragment();
            }
        }
        
        return url;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy