001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (C) Alkacon Software (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.file.CmsObject;
031import org.opencms.file.CmsResource;
032import org.opencms.main.CmsException;
033import org.opencms.main.CmsLog;
034import org.opencms.main.OpenCms;
035import org.opencms.util.CmsStringUtil;
036
037import org.apache.commons.logging.Log;
038
039/**
040 * A bean which represents the location configured for content elements of a specific type in a sitemap configuration.<p>
041 */
042public class CmsContentFolderDescriptor {
043
044    /** Name of the folder for elements stored with container pages. */
045    public static final String ELEMENTS_FOLDER_NAME = ".elements";
046
047    /** The logger instance for this class. */
048    private static final Log LOG = CmsLog.getLog(CmsContentFolderDescriptor.class);
049
050    /** The base path which the folder name is relative to. */
051    private String m_basePath;
052
053    /** The folder resource's structure id. */
054    private CmsResource m_folder;
055
056    /** The folder name. */
057    private String m_folderName;
058
059    /** The 'isPageRelative' flag. If true, elements of this type will be stored with the pages on which they were created. */
060    private boolean m_isPageRelative;
061
062    /**
063     * Creates an instance based on an existing folder.<p>
064     *
065     * @param folder the folder
066     */
067    public CmsContentFolderDescriptor(CmsResource folder) {
068
069        m_folder = folder;
070    }
071
072    /**
073     * Creates an instance based on a relative folder name.<p>
074     *
075     * @param basePath the base path which the folder name is relative to
076     * @param name the relative folder name
077     */
078    public CmsContentFolderDescriptor(String basePath, String name) {
079
080        m_basePath = basePath;
081
082        m_folderName = name;
083    }
084
085    /**
086     * Private constructor which does nothing.<p>
087     */
088    private CmsContentFolderDescriptor() {
089
090    }
091
092    /**
093     * Creates folder descriptor which represents the 'page relative' setting.<p>
094     *
095     * @return the folder descriptor for the 'page relative' setting
096     */
097    public static CmsContentFolderDescriptor createPageRelativeFolderDescriptor() {
098
099        CmsContentFolderDescriptor result = new CmsContentFolderDescriptor();
100        result.m_isPageRelative = true;
101        return result;
102    }
103
104    /**
105     * Gets the base path.<p>
106     *
107     * @return the base path
108     */
109    public String getBasePath() {
110
111        return m_basePath;
112    }
113
114    /**
115     * Gets the folder.<p>
116     *
117     * @return the folder
118     */
119    public CmsResource getFolder() {
120
121        return m_folder;
122    }
123
124    /**
125     * Gets the relative folder name if available, else null.<p>
126     *
127     * @return the relative folder name null
128     */
129    public String getFolderName() {
130
131        return m_folderName;
132    }
133
134    /**
135     * Computes the folder root path.<p>
136     *
137     * @param cms the CMS context to use
138     * @param pageFolderPath the root path of the folder containing the current container page
139     * @return the folder root path
140     */
141    public String getFolderPath(CmsObject cms, String pageFolderPath) {
142
143        if (m_folder != null) {
144            try {
145                return OpenCms.getADEManager().getRootPath(
146                    m_folder.getStructureId(),
147                    cms.getRequestContext().getCurrentProject().isOnlineProject());
148            } catch (CmsException e) {
149                LOG.error(e.getLocalizedMessage(), e);
150                return m_folder.getRootPath();
151            }
152        } else if (m_basePath != null) {
153            return CmsStringUtil.joinPaths(m_basePath, m_folderName);
154        } else if (m_isPageRelative) {
155            if (pageFolderPath == null) {
156                throw new IllegalArgumentException(
157                    "getFolderPath called without page folder, but pageRelative is enabled!");
158            }
159            return CmsStringUtil.joinPaths(pageFolderPath, ELEMENTS_FOLDER_NAME);
160        } else {
161            return CmsStringUtil.joinPaths(
162                cms.getRequestContext().getSiteRoot(),
163                CmsADEManager.CONTENT_FOLDER_NAME,
164                m_folderName);
165        }
166
167    }
168
169    /**
170     * Returns true if the current instance was created with a folder structure id parameter.<p>
171     *
172     * @return true if this instance was created with a folder structure id parameter
173     */
174    public boolean isFolder() {
175
176        return m_folder != null;
177    }
178
179    /**
180     * Returns true if this instance was created with a folder name parameter.<p>
181     *
182     * @return true if this instance was created with a folder name parameter
183     */
184    public boolean isName() {
185
186        return m_folderName != null;
187    }
188
189    /**
190     * Returns true if this page descriptor represents the 'page relative' setting.<p>
191     *
192     * @return true if this is page relative
193     */
194    public boolean isPageRelative() {
195
196        return m_isPageRelative;
197    }
198
199}