001/*
002 * (C) Copyright 2014 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-2.1.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.binding.alias;
018
019import java.io.Serializable;
020import java.util.Collections;
021import java.util.HashMap;
022import java.util.Map;
023
024import javax.annotation.PostConstruct;
025import javax.annotation.PreDestroy;
026
027import org.apache.commons.logging.Log;
028import org.apache.commons.logging.LogFactory;
029
030/**
031 * Keep alias variable mappers in request on a managed bean.
032 * <p>
033 * References to these variables mappers will be done from UI.
034 *
035 * @since 6.0
036 */
037public class NuxeoAliasBean implements Serializable {
038
039    private static final long serialVersionUID = 1L;
040
041    public static final String NAME = "nuxeoAliasBean";
042
043    private static final Log log = LogFactory.getLog(NuxeoAliasBean.class);
044
045    protected Map<String, AliasVariableMapper> vms;
046
047    public NuxeoAliasBean() {
048        super();
049    }
050
051    /**
052     * Init marked public for NXP-16182.
053     *
054     * @since 7.1
055     */
056    @PostConstruct
057    @PreDestroy
058    public void init() {
059        vms = new HashMap<>();
060    }
061
062    public Map<String, AliasVariableMapper> getValues() {
063        return Collections.unmodifiableMap(vms);
064    }
065
066    public AliasVariableMapper get(String id) {
067        return vms.get(id);
068    }
069
070    public void add(AliasVariableMapper vm) {
071        if (vm == null) {
072            return;
073        }
074        String id = vm.getId();
075        if (id == null && log.isDebugEnabled()) {
076            log.debug("Adding alias variable mapper with null id");
077        }
078        if (vms.containsKey(id) && log.isTraceEnabled()) {
079            log.trace(String.format("Overriding alias variable mapper with id '%s'", id));
080        }
081        vms.put(id, vm);
082        if (log.isTraceEnabled()) {
083            log.trace(String.format("Expose alias variable mapper with id '%s': %s", id, vm.getVariables()));
084        }
085    }
086
087    public void remove(String id) {
088        if (log.isTraceEnabled()) {
089            log.trace(String.format("Remove alias variable mapper with id '%s' from request", id));
090        }
091        if (id == null) {
092            log.debug("Remove alias variable mapper with null id");
093        }
094        vms.remove(id);
095    }
096
097}