/*
 * ICESOFT COMMERCIAL SOURCE CODE LICENSE V 1.1
 *
 * The contents of this file are subject to the ICEsoft Commercial Source
 * Code License Agreement V1.1 (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.icesoft.com/license/commercial-source-v1.1.html
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations under
 * the License.
 *
 * Copyright 2009-2015 ICEsoft Technologies Canada, Corp. All Rights Reserved.
 */

package com.icesoft.faces.impl.event;

import org.icefaces.util.EnvUtils;

import java.util.logging.Level;
import java.util.logging.Logger;

import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;
import javax.servlet.http.HttpSession;

public class RenderResponsePhaseListener
implements PhaseListener {
    private static final Logger LOGGER = Logger.getLogger(RenderResponsePhaseListener.class.getName());

    public void afterPhase(final PhaseEvent phaseEvent) {
        HttpSession _httpSession = EnvUtils.getSafeSession(phaseEvent.getFacesContext(), false);
        if (_httpSession != null) {
            Object _activeViewMaps =
                _httpSession.getAttribute("com.sun.faces.application.view.activeViewMaps");
            if (_activeViewMaps != null) {
                _httpSession.setAttribute(
                    "com.sun.faces.application.view.activeViewMaps", _activeViewMaps);
            }
            Object _logicalViewMap =
                _httpSession.getAttribute("com.sun.faces.renderkit.ServerSideStateHelper.LogicalViewMap");
            if (_logicalViewMap != null) {
                _httpSession.setAttribute(
                    "com.sun.faces.renderkit.ServerSideStateHelper.LogicalViewMap", _logicalViewMap);
            }
        }
    }

    public void beforePhase(final PhaseEvent phaseEvent) {
        // Do nothing.
    }

    public PhaseId getPhaseId() {
        return PhaseId.RENDER_RESPONSE;
    }
}
