001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://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: https://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: https://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;
029
030import org.opencms.ade.configuration.CmsADEConfigDataInternal.ConfigReferenceInstance;
031import org.opencms.ade.configuration.CmsADEConfigDataInternal.ConfigReferenceMeta;
032import org.opencms.file.CmsResource;
033
034import java.util.ArrayList;
035import java.util.Collections;
036import java.util.List;
037
038import com.google.common.base.Optional;
039
040/**
041 * Represents a sequence of inherited module/sitemap configurations, together with an index into that list.<p>
042 *
043 * Used for computing the configuration inheritance.
044 *
045 */
046public class CmsADEConfigurationSequence {
047
048    /** The list of configuration data. */
049    private List<ConfigReferenceInstance> m_configDatas;
050
051    /** The index for the current configuration. */
052    private int m_configIndex;
053
054    /**
055     * Creates a new instance for the given list of configuration data, with the index pointing to the last element.<p>
056     *
057     * @param configDatas the config data list
058     */
059    public CmsADEConfigurationSequence(List<ConfigReferenceInstance> configDatas) {
060
061        this(configDatas, configDatas.size() - 1);
062    }
063
064    /**
065     * Creates a new instance.<p>
066     *
067     * @param configDatas the configuration data list
068     * @param index the index into the list
069     */
070    protected CmsADEConfigurationSequence(List<ConfigReferenceInstance> configDatas, int index) {
071
072        assert (0 <= index) && (index < configDatas.size());
073        m_configDatas = configDatas;
074        m_configIndex = index;
075
076    }
077
078    /**
079     * Gets the current configuration data.<p>
080     *
081     * @return the current configuration data
082     */
083    public CmsADEConfigDataInternal getConfig() {
084
085        return m_configDatas.get(m_configIndex).getConfig();
086    }
087
088    /**
089     * Gets the internal configuration data for all nodes in this sequence, in inheritance order, for
090     * read-only configuration origin discovery.<p>
091     *
092     * @return the internal configuration data of each node in the sequence
093     */
094    public List<CmsADEConfigDataInternal> getConfigNodes() {
095
096        List<CmsADEConfigDataInternal> result = new ArrayList<>();
097        for (int i = 0; i <= m_configIndex; i++) {
098            result.add(m_configDatas.get(i).getConfig());
099        }
100        return Collections.unmodifiableList(result);
101    }
102
103    /**
104     * Gets the list of configuration file paths in inheritance order, not including the module configuration.
105     *
106     * @return the list of configuration file paths
107     */
108    public List<String> getConfigPaths() {
109
110        List<String> result = new ArrayList<>();
111        for (int i = 0; i <= m_configIndex; i++) {
112            CmsResource res = m_configDatas.get(i).getConfig().getResource();
113            if (res != null) {
114                result.add(res.getRootPath());
115            }
116        }
117        return Collections.unmodifiableList(result);
118    }
119
120    /**
121     * Gets the metadata associated with the path of master configuration references to the current configuration.
122     *
123     * @return the metadata associated with the path of master configuration references to the current configuration.
124     */
125    public ConfigReferenceMeta getMeta() {
126
127        return m_configDatas.get(m_configIndex).getMeta();
128    }
129
130    /**
131     * Returns a sequence which only differs from this instance in that its index is one less.<p>
132     *
133     * However, if the index of this instance is already 0, Optional.absent will be returned.
134     *
135     * @return the parent sequence, or Optional.absent if we are already at the start of the sequence
136     */
137    public Optional<CmsADEConfigurationSequence> getParent() {
138
139        if (m_configIndex <= 0) {
140            return Optional.absent();
141        }
142        return Optional.fromNullable(new CmsADEConfigurationSequence(m_configDatas, m_configIndex - 1));
143    }
144
145}