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.jsp.util;
029
030import org.opencms.ade.configuration.CmsADEConfigData;
031import org.opencms.file.CmsObject;
032import org.opencms.file.types.I_CmsResourceType;
033import org.opencms.main.OpenCms;
034import org.opencms.workplace.CmsWorkplaceMessages;
035import org.opencms.workplace.explorer.CmsExplorerTypeSettings;
036import org.opencms.xml.containerpage.I_CmsFormatterBean;
037
038import java.util.ArrayList;
039import java.util.Collections;
040import java.util.List;
041import java.util.Locale;
042import java.util.Set;
043
044import com.google.common.collect.ArrayListMultimap;
045import com.google.common.collect.Multimap;
046
047/**
048 * Wrapper for resource type information for use in JSPs.
049 */
050public class CmsResourceTypeInfoWrapper implements I_CmsFormatterInfo {
051
052    /** Whether the type is active in the sitemap configuration. */
053    private boolean m_active;
054
055    /** The active formatters. */
056    private List<I_CmsFormatterBean> m_activeFormatters = new ArrayList<>();
057
058    /** The active formatters grouped by container type. */
059    private Multimap<String, I_CmsFormatterBean> m_activeFormattersByContainerType = ArrayListMultimap.create();
060
061    /** The current CMS context. */
062    private CmsObject m_cms;
063
064    /** The current sitemap configuration. */
065    private CmsADEConfigData m_config;
066
067    /** The wrapped resource type. */
068    private I_CmsResourceType m_type;
069
070    private CmsJspStandardContextBean m_context;
071
072    /**
073     * Creates a new instance.
074     *
075     * @param context the standard context bean
076     * @param cms the current CMS context
077     * @param config the current sitemap configuration
078     * @param type the type to wrap
079     */
080    public CmsResourceTypeInfoWrapper(
081        CmsJspStandardContextBean context,
082        CmsObject cms,
083        CmsADEConfigData config,
084        I_CmsResourceType type) {
085
086        m_cms = cms;
087        m_config = config;
088        m_type = type;
089        m_context = context;
090        for (I_CmsFormatterBean formatter : config.getActiveFormatters().values()) {
091            if (!formatter.getResourceTypeNames().contains(type.getTypeName())) {
092                continue;
093            }
094            m_activeFormatters.add(formatter);
095            for (String containerType : formatter.getContainerTypes()) {
096                m_activeFormattersByContainerType.put(containerType, formatter);
097            }
098        }
099        m_active = m_config.getResourceTypes().stream().anyMatch(
100            sitemapConfigType -> m_type.getTypeName().equals(sitemapConfigType.getTypeName()));
101    }
102
103    /**
104     * Gets the description for the type in the given locale.
105     *
106     * @param locale the locale to use
107     * @return the type description
108     */
109    public String description(Locale locale) {
110
111        try {
112            return CmsWorkplaceMessages.getResourceTypeDescription(locale, m_type.getTypeName());
113        } catch (Throwable e) {
114            return m_type.getTypeName();
115        }
116    }
117
118    /**
119     * Gets the formatter information beans for a specific container type.
120     *
121     * @param containerType the container type
122     * @return the formatter information
123     */
124    public List<CmsFormatterInfoWrapper> formatterInfoForContainer(String containerType) {
125
126        return m_context.wrapFormatters(m_activeFormattersByContainerType.get(containerType));
127
128    }
129
130    /**
131     * Gets the type description in the current locale.
132     *
133     * @return the type description
134     */
135    public String getDescription() {
136
137        return description(m_cms.getRequestContext().getLocale());
138    }
139
140    /**
141     * @see org.opencms.jsp.util.I_CmsFormatterInfo#getDescriptionKey()
142     */
143    public String getDescriptionKey() {
144
145        CmsExplorerTypeSettings explorerType = OpenCms.getWorkplaceManager().getExplorerTypeSetting(
146            m_type.getTypeName());
147        return explorerType.getInfo();
148    }
149
150    /**
151     * @see org.opencms.jsp.util.I_CmsFormatterInfo#getDescriptionRaw()
152     */
153    public String getDescriptionRaw() {
154
155        return getDescriptionKey();
156    }
157
158    /**
159     * Gets the set of container types configured for any active formatters for this resource type.
160     *
161     * @return the set of container types for formatters
162     */
163    public Set<String> getFormatterContainerTypes() {
164
165        return Collections.unmodifiableSet(m_activeFormattersByContainerType.keySet());
166    }
167
168    /**
169     * Gets the formatter information beans for all active formatters for this type.
170     *
171     * @return the formatter information beans
172     */
173    public List<CmsFormatterInfoWrapper> getFormatterInfo() {
174
175        return m_context.wrapFormatters(m_activeFormatters);
176
177    }
178
179    /**
180     * Returns true if the type is active in the current sitemap configuration.
181     *
182     * @return true if the type is active
183     */
184    public boolean getIsActive() {
185
186        return m_active;
187
188    }
189
190    /**
191     * @see org.opencms.jsp.util.I_CmsFormatterInfo#getIsFormatter()
192     */
193    public boolean getIsFormatter() {
194
195        return false;
196    }
197
198    /**
199     * @see org.opencms.jsp.util.I_CmsFormatterInfo#getIsFunction()
200     */
201    public boolean getIsFunction() {
202
203        return false;
204    }
205
206    /**
207     * @see org.opencms.jsp.util.I_CmsFormatterInfo#getIsResourceType()
208     */
209    public boolean getIsResourceType() {
210
211        return true;
212    }
213
214    /**
215     * Gets the type name.
216     *
217     * @return the type name
218     */
219    public String getName() {
220
221        return m_type.getTypeName();
222    }
223
224    /**
225     * Gets the user-readable nice name of the type in the current locale.
226     *
227     * @return the nice name
228     */
229    public String getNiceName() {
230
231        return niceName(m_cms.getRequestContext().getLocale());
232    }
233
234    /**
235     * @see org.opencms.jsp.util.I_CmsFormatterInfo#getNiceNameKey()
236     */
237    public String getNiceNameKey() {
238
239        CmsExplorerTypeSettings explorerType = OpenCms.getWorkplaceManager().getExplorerTypeSetting(
240            m_type.getTypeName());
241        return explorerType.getKey();
242
243    }
244
245    /**
246     * @see org.opencms.jsp.util.I_CmsFormatterInfo#getNiceNameRaw()
247     */
248    public String getNiceNameRaw() {
249
250        return getNiceNameKey();
251    }
252
253    /**
254     * Gets the nice name of the type in the given locale.
255     *
256     * @param locale the locale to use
257     * @return the nice name for the type
258     */
259    public String niceName(Locale locale) {
260
261        try {
262            return CmsWorkplaceMessages.getResourceTypeName(locale, m_type.getTypeName());
263        } catch (Throwable e) {
264            return m_type.getTypeName();
265        }
266    }
267
268}