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 GmbH & Co. KG, 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.xml.containerpage;
029
030import org.opencms.util.CmsCollectionsGenericWrapper;
031import org.opencms.util.CmsUUID;
032
033import java.util.ArrayList;
034import java.util.Collections;
035import java.util.HashSet;
036import java.util.LinkedHashMap;
037import java.util.List;
038import java.util.Map;
039import java.util.Set;
040
041import org.apache.commons.collections.Transformer;
042
043/**
044 * Describes one locale of a container page.<p>
045 *
046 * @since 8.0
047 */
048public class CmsContainerPageBean {
049
050    /** The containers. */
051    private final Map<String, CmsContainerBean> m_containers;
052
053    /** A lazy initialized map that describes if a certain element if part of this container. */
054    private transient Map<CmsUUID, Boolean> m_containsElement;
055
056    /** The id's of of all elements in this page. */
057    private transient List<CmsUUID> m_elementIds;
058
059    /** The container elements. */
060    private transient List<CmsContainerElementBean> m_elements;
061
062    /** The container names in the right order. */
063    private final List<String> m_names;
064
065    /** The supported types. */
066    private final Set<String> m_types;
067
068    /**
069     * Creates a new container page bean.<p>
070     *
071     * @param containers the containers
072     **/
073    public CmsContainerPageBean(List<CmsContainerBean> containers) {
074
075        // we want to preserve container order
076        Map<String, CmsContainerBean> cnts = new LinkedHashMap<String, CmsContainerBean>();
077        Set<String> types = new HashSet<String>();
078        List<String> names = new ArrayList<String>();
079        for (CmsContainerBean container : containers) {
080            cnts.put(container.getName(), container);
081            types.add(container.getType());
082            names.add(container.getName());
083        }
084        m_containers = Collections.unmodifiableMap(cnts);
085        m_types = Collections.unmodifiableSet(types);
086        m_names = Collections.unmodifiableList(names);
087    }
088
089    /**
090     * Returns <code>true</code> if the element with the provided id is contained in this container.<p>
091     *
092     * @param elementId the element id to check
093     *
094     * @return <code>true</code> if the element with the provided id is contained in this container
095     */
096    public boolean containsElement(CmsUUID elementId) {
097
098        return getElementIds().contains(elementId);
099    }
100
101    /**
102     * Returns all container of this page.<p>
103     *
104     * @return all container of this page
105     */
106    public Map<String, CmsContainerBean> getContainers() {
107
108        return m_containers;
109    }
110
111    /**
112     * Returns a lazy initialized map that describes if a certain element if part of this container.<p>
113     *
114     * @return a lazy initialized map that describes if a certain element if part of this container
115     */
116    public Map<CmsUUID, Boolean> getContainsElement() {
117
118        if (m_containsElement == null) {
119            m_containsElement = CmsCollectionsGenericWrapper.createLazyMap(new Transformer() {
120
121                public Object transform(Object input) {
122
123                    return Boolean.valueOf(containsElement((CmsUUID)input));
124                }
125            });
126        }
127        return m_containsElement;
128    }
129
130    /**
131     * Returns the id's of all elements in this container.<p>
132     *
133     * @return the id's of all elements in this container
134     */
135    public List<CmsUUID> getElementIds() {
136
137        if (m_elementIds == null) {
138            m_elementIds = new ArrayList<CmsUUID>(getElements().size());
139            for (CmsContainerElementBean element : getElements()) {
140                m_elementIds.add(element.getId());
141            }
142        }
143        return m_elementIds;
144    }
145
146    /**
147     * Returns the elements of all containers in this page.<p>
148     *
149     * @return the elements of all containers in this page
150     */
151    public List<CmsContainerElementBean> getElements() {
152
153        if (m_elements == null) {
154            m_elements = new ArrayList<CmsContainerElementBean>();
155            for (CmsContainerBean container : m_containers.values()) {
156                m_elements.addAll(container.getElements());
157            }
158        }
159        return m_elements;
160    }
161
162    /**
163     * Returns the list of container names.<p>
164     *
165     * @return the list of container names
166     */
167    public List<String> getNames() {
168
169        return m_names;
170    }
171
172    /**
173     * Returns the types.<p>
174     *
175     * @return the types
176     */
177    public Set<String> getTypes() {
178
179        return m_types;
180    }
181}