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.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 list of configuration file paths in inheritance order, not including the module configuration.
090     *
091     * @return the list of configuration file paths
092     */
093    public List<String> getConfigPaths() {
094
095        List<String> result = new ArrayList<>();
096        for (int i = 0; i <= m_configIndex; i++) {
097            CmsResource res = m_configDatas.get(i).getConfig().getResource();
098            if (res != null) {
099                result.add(res.getRootPath());
100            }
101        }
102        return Collections.unmodifiableList(result);
103    }
104
105    /**
106     * Gets the metadata associated with the path of master configuration references to the current configuration.
107     *
108     * @return the metadata associated with the path of master configuration references to the current configuration.
109     */
110    public ConfigReferenceMeta getMeta() {
111
112        return m_configDatas.get(m_configIndex).getMeta();
113    }
114
115    /**
116     * Returns a sequence which only differs from this instance in that its index is one less.<p>
117     *
118     * However, if the index of this instance is already 0, Optional.absent will be returned.
119     *
120     * @return the parent sequence, or Optional.absent if we are already at the start of the sequence
121     */
122    public Optional<CmsADEConfigurationSequence> getParent() {
123
124        if (m_configIndex <= 0) {
125            return Optional.absent();
126        }
127        return Optional.fromNullable(new CmsADEConfigurationSequence(m_configDatas, m_configIndex - 1));
128    }
129
130}