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.loader.CmsLoaderException;
034import org.opencms.main.OpenCms;
035import org.opencms.xml.containerpage.CmsFormatterBean;
036import org.opencms.xml.containerpage.CmsFunctionFormatterBean;
037import org.opencms.xml.containerpage.I_CmsFormatterBean;
038
039import java.util.ArrayList;
040import java.util.List;
041import java.util.Set;
042import java.util.TreeSet;
043
044/**
045 * Wrapper bean for querying information related to a container type in JSPs.
046 */
047public class CmsContainerTypeInfoWrapper {
048
049    /** The  CMS context. */
050    private CmsObject m_cms;
051
052    /** The current sitemap configuration. */
053    private CmsADEConfigData m_config;
054
055    /** The container type. */
056    private String m_containerType;
057
058    /** The matching resource types. */
059    private Set<String> m_matchingResourceTypes = new TreeSet<>();
060
061    /** The list of matching functions. */
062    private List<CmsFunctionFormatterBean> m_matchingFunctions = new ArrayList<>();
063
064    private CmsJspStandardContextBean m_context;
065
066    /**
067     * Creates a new instance.
068     * @param context the standard context bean
069     * @param cms the CMS context
070     * @param config the sitemap configuration
071     * @param type the container type to wrap
072     */
073    public CmsContainerTypeInfoWrapper(
074        CmsJspStandardContextBean context,
075        CmsObject cms,
076        CmsADEConfigData config,
077        String type) {
078
079        m_cms = cms;
080        m_config = config;
081        m_context = context;
082        m_containerType = type;
083        for (I_CmsFormatterBean formatter : m_config.getActiveFormatters().values()) {
084            if ((formatter.getContainerTypes() != null) && formatter.getContainerTypes().contains(m_containerType)) {
085                if (formatter instanceof CmsFunctionFormatterBean) {
086                    m_matchingFunctions.add((CmsFunctionFormatterBean)formatter);
087                } else if (formatter instanceof CmsFormatterBean) {
088                    m_matchingResourceTypes.addAll(formatter.getResourceTypeNames());
089                }
090            }
091        }
092    }
093
094    /**
095     * Gets the combined list of both resource types which have matching formatter for this container type, and functions which match this container type.
096     *
097     * @return the combined list of resource types and functions for this container type
098     */
099    public List<I_CmsFormatterInfo> getAllowedElements() {
100
101        List<I_CmsFormatterInfo> result = new ArrayList<>();
102        result.addAll(getAllowedResourceTypes());
103        result.addAll(getAllowedFunctions());
104        return result;
105    }
106
107    /**
108     * Gets the list of active functions which match this container type.-
109     *
110     * @return the list of matching functions for the container type
111     */
112    public List<CmsFormatterInfoWrapper> getAllowedFunctions() {
113
114        return m_context.wrapFormatters(m_matchingFunctions);
115
116    }
117
118    /**
119     * Gets the list of resource types which have active formatters matching this container type.
120     *
121     * @return the list of resource types matching this container type
122     */
123    public List<I_CmsFormatterInfo> getAllowedResourceTypes() {
124
125        List<I_CmsFormatterInfo> result = new ArrayList<>();
126        for (String name : m_matchingResourceTypes) {
127            try {
128                I_CmsResourceType type = OpenCms.getResourceManager().getResourceType(name);
129                CmsResourceTypeInfoWrapper wrapper = new CmsResourceTypeInfoWrapper(m_context, m_cms, m_config, type);
130                result.add(wrapper);
131            } catch (CmsLoaderException e) {
132                // type not found, ignore and continue
133            }
134        }
135        return result;
136    }
137
138}