001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.ade.configuration.formatters;
029
030import org.opencms.main.CmsLog;
031import org.opencms.util.CmsUUID;
032import org.opencms.xml.containerpage.I_CmsFormatterBean;
033
034import java.util.Collection;
035import java.util.Collections;
036import java.util.HashMap;
037import java.util.Map;
038
039import org.apache.commons.logging.Log;
040
041import com.google.common.base.Predicate;
042import com.google.common.collect.ArrayListMultimap;
043import com.google.common.collect.Collections2;
044import com.google.common.collect.Maps;
045import com.google.common.collect.Multimap;
046import com.google.common.collect.Multimaps;
047
048/**
049 * Represents the currently cached collection of all formatter beans extracted from formatter configuration files.<p>
050 *
051 * Objects of this class are immutable, but have a method to create an updated copy.<p>
052 */
053public class CmsFormatterConfigurationCacheState {
054
055    /** The log instance for this class. */
056    private static final Log LOG = CmsLog.getLog(CmsFormatterConfigurationCacheState.class);
057
058    /** The automatically enabled formatters. */
059    private Map<CmsUUID, I_CmsFormatterBean> m_autoEnabledFormatters;
060
061    /** Map of formatter beans by structure id. */
062    private Map<CmsUUID, I_CmsFormatterBean> m_formatters = new HashMap<CmsUUID, I_CmsFormatterBean>();
063
064    /** The map of formatters by resource type. */
065    private Multimap<String, I_CmsFormatterBean> m_formattersByType;
066
067    /**
068     * Creates a new instance.<p>
069     *
070     * @param formatters the initial map of formatters
071     */
072    public CmsFormatterConfigurationCacheState(Map<CmsUUID, I_CmsFormatterBean> formatters) {
073
074        m_formatters = new HashMap<CmsUUID, I_CmsFormatterBean>(formatters);
075        if (LOG.isDebugEnabled()) {
076            LOG.debug("Created new formatter configuration: ");
077            LOG.debug(m_formatters.toString());
078        }
079    }
080
081    /**
082     * Creates a new copy of this state in which some entries are removed or replaced.<p>
083     *
084     * This does not change the state object on which the method is called.
085     *
086     * @param updateFormatters a map of formatters to change, where the key is the structure id and the value is either the replacement or null if the map entry should be removed
087     *
088     * @return the updated copy
089     */
090    public CmsFormatterConfigurationCacheState createUpdatedCopy(Map<CmsUUID, I_CmsFormatterBean> updateFormatters) {
091
092        Map<CmsUUID, I_CmsFormatterBean> newFormatters = Maps.newHashMap(getFormatters());
093        for (Map.Entry<CmsUUID, I_CmsFormatterBean> entry : updateFormatters.entrySet()) {
094            CmsUUID key = entry.getKey();
095            I_CmsFormatterBean value = entry.getValue();
096            if (value != null) {
097                newFormatters.put(key, value);
098            } else {
099                newFormatters.remove(key);
100            }
101        }
102        return new CmsFormatterConfigurationCacheState(newFormatters);
103    }
104
105    /**
106     * Gets the map of formatters which are automatically enabled.<p>
107     *
108     * @return the map of automatically enabled formatters with structure ids as keys
109     */
110    public Map<CmsUUID, I_CmsFormatterBean> getAutoEnabledFormatters() {
111
112        if (m_autoEnabledFormatters == null) {
113            Map<CmsUUID, I_CmsFormatterBean> result = Maps.newHashMap();
114            for (Map.Entry<CmsUUID, I_CmsFormatterBean> entry : m_formatters.entrySet()) {
115                if (entry.getValue().isAutoEnabled()) {
116                    result.put(entry.getKey(), entry.getValue());
117                }
118            }
119            m_autoEnabledFormatters = result;
120        }
121        return Collections.unmodifiableMap(m_autoEnabledFormatters);
122    }
123
124    /**
125     * Gets the map of all formatters.<p>
126     *
127     * @return the map of all formatters
128     */
129    public Map<CmsUUID, I_CmsFormatterBean> getFormatters() {
130
131        return Collections.unmodifiableMap(m_formatters);
132    }
133
134    /**
135     * Gets the formatters for a specific resource types, and optionally only returns those which are automatically enabled.<p>
136     *
137     * @param resourceType the resource type name
138     * @param filterAutoEnabled true if only the automatically enabled formatters should be returned
139     *
140     * @return the formatters for the type
141     */
142    public Collection<I_CmsFormatterBean> getFormattersForType(String resourceType, boolean filterAutoEnabled) {
143
144        Collection<I_CmsFormatterBean> result = getFormattersByType().get(resourceType);
145        if (filterAutoEnabled) {
146            result = Collections2.filter(result, new Predicate<I_CmsFormatterBean>() {
147
148                public boolean apply(I_CmsFormatterBean formatter) {
149
150                    return formatter.isAutoEnabled();
151                }
152            });
153        }
154        return result;
155    }
156
157    /**
158     * Gets the formatters as a multimap with the resource types as keys and caches this multimap if necessary.<p>
159     *
160     * @return the multimap of formatters by resource type
161     */
162    private Multimap<String, I_CmsFormatterBean> getFormattersByType() {
163
164        if (m_formattersByType == null) {
165            ArrayListMultimap<String, I_CmsFormatterBean> formattersByType = ArrayListMultimap.create();
166            for (I_CmsFormatterBean formatter : m_formatters.values()) {
167                for (String typeName : formatter.getResourceTypeNames()) {
168                    formattersByType.put(typeName, formatter);
169                }
170            }
171            m_formattersByType = formattersByType;
172        }
173        Multimap<String, I_CmsFormatterBean> result = Multimaps.unmodifiableMultimap(m_formattersByType);
174        return result;
175    }
176
177}