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;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsResource;
032import org.opencms.file.CmsVfsResourceNotFoundException;
033import org.opencms.file.types.CmsResourceTypeFolder;
034import org.opencms.main.CmsException;
035import org.opencms.main.OpenCms;
036
037/**
038 * Helper class for creating a folder if it doesn'T already exist.<p>
039 *
040 * @since 8.0.0
041 */
042public class CmsLazyFolder {
043
044    /** The folder, if it already exists. */
045    private CmsResource m_folder;
046
047    /** The path at which the folder should be created if it doesn'T exist. */
048    private String m_path;
049
050    /**
051     * Initializes this object with an existing folder.<p>
052     *
053     * @param folder the existing folder
054     */
055    public CmsLazyFolder(CmsResource folder) {
056
057        assert folder != null;
058        m_folder = folder;
059        m_path = null;
060    }
061
062    /**
063     * Initializes this object with a path at which the folder should be created.<p>
064     *
065     * @param path the path at which the folder should be created
066     */
067    public CmsLazyFolder(String path) {
068
069        assert path != null;
070        m_path = path;
071        m_folder = null;
072    }
073
074    /**
075     * Creates the folder with the given name if it doesn't already exist, and returns it.<p>
076     *
077     * @param cms the current CMS context
078     *
079     * @return the created folder or the already existing folder
080     *
081     * @throws CmsException if something goes wrong
082     */
083    public CmsResource createFolder(CmsObject cms) throws CmsException {
084
085        if (m_folder != null) {
086            return m_folder;
087        }
088        return cms.createResource(
089            m_path,
090            OpenCms.getResourceManager().getResourceType(CmsResourceTypeFolder.RESOURCE_TYPE_NAME).getTypeId());
091    }
092
093    /**
094     * Returns the folder if it already exists, or null if it doesn't.<p>
095     *
096     * @param cms the current CMS context
097     *
098     * @return the folder if it exists, else null
099     *
100     * @throws CmsException if something goes wrong
101     */
102    public CmsResource getFolder(CmsObject cms) throws CmsException {
103
104        if (m_folder != null) {
105            return m_folder;
106        }
107        try {
108            CmsResource folder = cms.readResource(m_path);
109            return folder;
110        } catch (CmsVfsResourceNotFoundException e) {
111            return null;
112        }
113    }
114
115    /**
116     * Returns the folder if it already exists, or creates and returns it if it doesn't.<p>
117     *
118     * @param cms the current CMS context
119     *
120     * @return the folder
121     *
122     * @throws CmsException if something goes wrong
123     */
124    public CmsResource getOrCreateFolder(CmsObject cms) throws CmsException {
125
126        CmsResource result = getFolder(cms);
127        if (result != null) {
128            return result;
129        }
130        return createFolder(cms);
131    }
132
133    /**
134     * Returns the folder to check for permissions, which is either the folder itself if it already exists,
135     * or the parent folder if it doesn't.<p>
136     *
137     * @param cms the current CMS context
138     *
139     * @return the folder to check for permissions
140     *
141     * @throws CmsException if something goes wrong
142     */
143    public CmsResource getPermissionCheckFolder(CmsObject cms) throws CmsException {
144
145        CmsResource folder = getFolder(cms);
146        if (folder != null) {
147            return folder;
148        }
149        String parentPath = CmsResource.getParentFolder(m_path);
150        CmsResource parent = cms.readResource(parentPath);
151        return parent;
152    }
153
154    /**
155     * Computes the site path of the folder, which is either the original path constructor argument, or the site
156     * path of the original resource constructor argument.<p>
157     *
158     * @param cms the current CMS context
159     * @return the site path of the lazy folder
160     */
161    public String getSitePath(CmsObject cms) {
162
163        if (m_path != null) {
164            return m_path;
165        } else if (m_folder != null) {
166            return cms.getRequestContext().removeSiteRoot(m_folder.getRootPath());
167        }
168        return null;
169    }
170
171}