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

import javax.faces.application.StateManager;
import javax.faces.application.StateManager.SerializedView;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;

import org.ajax4jsf.application.AjaxStateManager;

/**
 * @author asmirnov
 *
 */
public class PortalStateManager extends AjaxStateManager {
    
    private static final String VIEW_STATES_MAP = PortalStateManager.class.getName();
    
    private ComponentsLoader componentLoader;
    /**
     * 
     */
    public PortalStateManager(StateManager parentStateManager) {
	super(parentStateManager);
	componentLoader = new ComponentsLoaderImpl();
    }


    protected SerializedView saveStateInSession(FacesContext context,
    		Object treeStructure, Object state) {
    	PortletViewState windowState = PortletStateHolder.getInstance(context).getWindowState(context);
    	windowState.setTreeStructure(treeStructure);
    	windowState.setComponentsState(state);
    	windowState.setViewRoot(context.getViewRoot());
    	String viewId = context.getViewRoot().getViewId();
		windowState.setViewId(viewId);
    	return new SerializedView(viewId,null);
    }
    
    //@Override
    protected Object[] restoreStateFromSession(FacesContext context,
    		String viewId, String renderKitId) {
    	Object[] state = new Object[2];
    	PortletViewState windowState = PortletStateHolder.getInstance(context).getWindowState(context);
    	state[0]=windowState.getTreeStructure();
    	state[1]=windowState.getComponentsState();
    	return state;
    }
    
    public boolean isSavingStateInClient(FacesContext context) {
    	// Client side view saving is not applicable for portal.
    	return false;
    }
}
