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