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.xml.containerpage.mutable;
029
030import org.opencms.xml.containerpage.CmsContainerBean;
031import org.opencms.xml.containerpage.CmsContainerPageBean;
032
033import java.util.ArrayList;
034import java.util.List;
035import java.util.stream.Collectors;
036
037/**
038 * Mutable bean representing a container page, for use in programmatically editing container pages.
039 */
040public class CmsMutableContainerPage {
041
042    /** The list of containers (we just store them as a list instead of a map because the container names may be changed later). */
043    private List<CmsMutableContainer> m_containers = new ArrayList<>();
044
045    /**
046     * Creates a new instance.
047     *
048     * @param containers the containers comprising this container page.
049     */
050    public CmsMutableContainerPage(List<CmsMutableContainer> containers) {
051
052        m_containers = new ArrayList<>(containers);
053    }
054
055    /**
056     * Converts a CmsContainerPageBean to an instance of this class.
057     *
058     * @param page the container page bean to convert
059     * @return an instance of this class with the data from the container page bean
060     */
061    public static CmsMutableContainerPage fromImmutable(CmsContainerPageBean page) {
062
063        List<CmsMutableContainer> containers = page.getContainers().values().stream().map(
064            container -> CmsMutableContainer.fromImmutable(container)).collect(Collectors.toList());
065        return new CmsMutableContainerPage(containers);
066    }
067
068    /**
069     * Returns the mutable list of containers for this container page.
070     *
071     * @return the mutable list of containers
072     */
073    public List<CmsMutableContainer> containers() {
074
075        return m_containers;
076    }
077
078    /**
079     * Gets all containrs from the container page with a container name matching the given name
080     *
081     * @param name the name for which to look
082     * @return the list of containers matching the given name
083     */
084    public List<CmsMutableContainer> containers(String name) {
085
086        return m_containers.stream().filter(container -> container.matches(name)).collect(Collectors.toList());
087    }
088
089    /**
090     * Gets the container with exactly the given container name.
091     *
092     * @param name the container name
093     * @return the container with the given name, or null if none was found
094     */
095    public CmsMutableContainer exactContainer(String name) {
096
097        return m_containers.stream().filter(container -> name.equals(container.getName())).findFirst().orElse(null);
098    }
099
100    /**
101     * Gets the first container which has a name matching the given name.
102     *
103     * @param name the name to match
104     * @return the matching container, or null if none was found
105     */
106    public CmsMutableContainer firstContainer(String name) {
107
108        return m_containers.stream().filter(container -> container.matches(name)).findFirst().orElse(null);
109    }
110
111    /**
112     * Converts this object to a CmsContainerPageBean.
113     *
114     * @return the container page bean
115     */
116    public CmsContainerPageBean toImmutable() {
117
118        List<CmsContainerBean> containers = m_containers.stream().map(cnt -> cnt.toImmutable()).collect(
119            Collectors.toList());
120        return new CmsContainerPageBean(containers);
121    }
122}