/**
 * 
 */
package org.ajax4jsf.portlet.component;

import java.io.Serializable;

import javax.faces.component.UIViewRoot;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.portlet.faces.component.PortletNamingContainer;

import org.ajax4jsf.component.AjaxViewRoot;

/**
 * @author asmirnov
 *
 */
public class UIPortletViewRoot extends AjaxViewRoot implements
		PortletNamingContainer, Serializable {

	
	public static final String PORTLET_VIEW_ROOT_RENDERER = "org.ajax4jsf.portlet.ViewRoot";

	public UIPortletViewRoot() {
		super();
	}
	
	/**
	 * 
	 */
	public UIPortletViewRoot(UIViewRoot viewRootToReplace) {
        super();
        setViewId(viewRootToReplace.getViewId());
        setLocale(viewRootToReplace.getLocale());
        setRenderKitId(viewRootToReplace.getRenderKitId());
    }
	
	@Override
	public String getContainerClientId(FacesContext context) {		
		String containerClientId = super.getContainerClientId(context);
		ExternalContext externalContext = context.getExternalContext();
		if(null == containerClientId){
			containerClientId = externalContext.encodeNamespace("");
		} else {
			containerClientId = externalContext.encodeNamespace(SEPARATOR_CHAR+containerClientId);
		}
		return containerClientId;
	}
	
	@Override
	public String getRendererType() {
		// Override defauilt ViewRoot renderer type.
		return PORTLET_VIEW_ROOT_RENDERER;
	}
}
