001/*
002 * (C) Copyright 2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Anahide Tchertchian
018 */
019package org.nuxeo.ecm.platform.ui.web.component;
020
021import java.util.Map;
022
023import javax.faces.context.FacesContext;
024
025/**
026 * Helper class with static methods to remove/add variables to the request during a component rendering.
027 *
028 * @since 5.5
029 */
030public class VariableManager {
031
032    /**
033     * Returns the value exposed in request map for the var name.
034     * <p>
035     * This is useful for restoring this value in the request map.
036     */
037    public static final Object saveRequestMapVarValue(String var) {
038        if (var != null) {
039            FacesContext context = FacesContext.getCurrentInstance();
040            Map<String, Object> requestMap = context.getExternalContext().getRequestMap();
041            if (requestMap.containsKey(var)) {
042                return requestMap.get(var);
043            }
044        }
045        return null;
046    }
047
048    /**
049     * Restores the given value in the request map for the var name.
050     */
051    public static final void restoreRequestMapVarValue(String var, Object value) {
052        if (var != null) {
053            FacesContext context = FacesContext.getCurrentInstance();
054            Map<String, Object> requestMap = context.getExternalContext().getRequestMap();
055            if (value == null) {
056                requestMap.remove(var);
057            } else {
058                requestMap.put(var, value);
059            }
060        }
061    }
062
063    public static final void putVariableToRequestParam(String var, Object object) {
064        if (var != null) {
065            FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put(var, object);
066        }
067    }
068
069    public static final void removeVariableFromRequestParam(String var) {
070        if (var != null) {
071            FacesContext.getCurrentInstance().getExternalContext().getRequestMap().remove(var);
072        }
073    }
074
075}