001/*
002 * (C) Copyright 2011 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Anahide Tchertchian
016 */
017package org.nuxeo.ecm.platform.ui.web.component;
018
019import java.util.Map;
020
021import javax.faces.context.FacesContext;
022
023/**
024 * Helper class with static methods to remove/add variables to the request during a component rendering.
025 *
026 * @since 5.5
027 */
028public class VariableManager {
029
030    /**
031     * Returns the value exposed in request map for the var name.
032     * <p>
033     * This is useful for restoring this value in the request map.
034     */
035    public static final Object saveRequestMapVarValue(String var) {
036        if (var != null) {
037            FacesContext context = FacesContext.getCurrentInstance();
038            Map<String, Object> requestMap = context.getExternalContext().getRequestMap();
039            if (requestMap.containsKey(var)) {
040                return requestMap.get(var);
041            }
042        }
043        return null;
044    }
045
046    /**
047     * Restores the given value in the request map for the var name.
048     */
049    public static final void restoreRequestMapVarValue(String var, Object value) {
050        if (var != null) {
051            FacesContext context = FacesContext.getCurrentInstance();
052            Map<String, Object> requestMap = context.getExternalContext().getRequestMap();
053            if (value == null) {
054                requestMap.remove(var);
055            } else {
056                requestMap.put(var, value);
057            }
058        }
059    }
060
061    public static final void putVariableToRequestParam(String var, Object object) {
062        if (var != null) {
063            FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put(var, object);
064        }
065    }
066
067    public static final void removeVariableFromRequestParam(String var) {
068        if (var != null) {
069            FacesContext.getCurrentInstance().getExternalContext().getRequestMap().remove(var);
070        }
071    }
072
073}