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 GmbH & Co. KG, 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.file;
029
030import org.opencms.db.CmsResourceState;
031import org.opencms.db.CmsSecurityManager;
032import org.opencms.loader.CmsLoaderException;
033import org.opencms.main.CmsIllegalArgumentException;
034import org.opencms.main.CmsLog;
035import org.opencms.main.OpenCms;
036import org.opencms.util.CmsUUID;
037
038import org.apache.commons.logging.Log;
039
040/**
041 * A folder resource in the OpenCms VFS.<p>
042 *
043 * A folder resource is a CmsResource object that can contain sub-resources.<p>
044 *
045 * @since 6.0.0
046 */
047public class CmsFolder extends CmsResource {
048
049    /** The log object for this class. */
050    private static final Log LOG = CmsLog.getLog(CmsSecurityManager.class);
051    /** Serial version UID required for safe serialization. */
052    private static final long serialVersionUID = 5527163725725725452L;
053
054    /**
055     * Constructor, creates a new CmsFolder Object from the given CmsResource.<p>
056     *
057     * @param resource the base resource object to create a folder from
058     */
059    public CmsFolder(CmsResource resource) {
060
061        this(
062            resource.getStructureId(),
063            resource.getResourceId(),
064            resource.getRootPath(),
065            resource.getTypeId(),
066            resource.getFlags(),
067            resource.getProjectLastModified(),
068            resource.getState(),
069            resource.getDateCreated(),
070            resource.getUserCreated(),
071            resource.getDateLastModified(),
072            resource.getUserLastModified(),
073            resource.getDateReleased(),
074            resource.getDateExpired(),
075            resource.getVersion());
076    }
077
078    /**
079     * Constructor, creates a new CmsFolder object.<p>
080     *
081     * @param structureId the id of this resources structure record
082     * @param resourceId the id of this resources resource record
083     * @param path the filename of this resouce
084     * @param type the type of this resource
085     * @param flags the flags of this resource
086     * @param projectId the project id this resource was last modified in
087     * @param state the state of this resource
088     * @param dateCreated the creation date of this resource
089     * @param userCreated the id of the user who created this resource
090     * @param dateLastModified the date of the last modification of this resource
091     * @param userLastModified the id of the user who did the last modification of this resource    * @param size the size of the file content of this resource
092     * @param dateReleased the release date of this resource
093     * @param dateExpired the expiration date of this resource
094     * @param version the version number of this resource
095     */
096    public CmsFolder(
097        CmsUUID structureId,
098        CmsUUID resourceId,
099        String path,
100        int type,
101        int flags,
102        CmsUUID projectId,
103        CmsResourceState state,
104        long dateCreated,
105        CmsUUID userCreated,
106        long dateLastModified,
107        CmsUUID userLastModified,
108        long dateReleased,
109        long dateExpired,
110        int version) {
111
112        super(
113            structureId,
114            resourceId,
115            path,
116            type,
117            true,
118            flags,
119            projectId,
120            state,
121            dateCreated,
122            userCreated,
123            dateLastModified,
124            userLastModified,
125            dateReleased,
126            dateExpired,
127            1,
128            -1,
129            -1,
130            version);
131    }
132
133    /**
134     * Returns <code>true</code> if the given resource size describes a folder type.<p>
135     *
136     * This is <code>true</code> in case <code>size &lt; 0</code>.<p>
137     *
138     * @param size the resource size to check
139     *
140     * @return true if the given resource size describes a folder type or false if it is no folder
141     */
142    public static final boolean isFolderSize(long size) {
143
144        return (size < 0);
145    }
146
147    /**
148     * Returns <code>true</code> if the given resource type id describes a folder type.<p>
149     *
150     * @param typeId the resource type id to check
151     *
152     * @return true if the given resource type id describes a folder type or false if it is no folder or an unknown type.
153     */
154    public static final boolean isFolderType(int typeId) {
155
156        try {
157            return OpenCms.getResourceManager().getResourceType(typeId).isFolder();
158        } catch (CmsLoaderException e) {
159            if (LOG.isWarnEnabled()) {
160                LOG.warn(
161                    Messages.get().getBundle().key(Messages.ERR_UNKNOWN_RESOURCE_TYPE_1, Integer.valueOf(typeId)),
162                    e);
163            }
164        }
165        return false;
166    }
167
168    /**
169     * Returns <code>true</code> if the given resource type name describes a folder type.<p>
170     *
171     * @param typeName the resource type name to check
172     *
173     * @return true if the given resource type name describes a folder type
174     */
175    public static final boolean isFolderType(String typeName) {
176
177        try {
178            return OpenCms.getResourceManager().getResourceType(typeName).isFolder();
179        } catch (CmsLoaderException e) {
180            throw new CmsIllegalArgumentException(
181                Messages.get().container(Messages.ERR_UNKNOWN_RESOURCE_TYPE_1, typeName),
182                e);
183        }
184    }
185
186    /**
187     * Returns a clone of this Objects instance.<p>
188     *
189     * @return a clone of this instance
190     */
191    @Override
192    public Object clone() {
193
194        CmsResource clone = new CmsFolder(
195            getStructureId(),
196            getResourceId(),
197            getRootPath(),
198            getTypeId(),
199            getFlags(),
200            getProjectLastModified(),
201            getState(),
202            getDateCreated(),
203            getUserCreated(),
204            getDateLastModified(),
205            getUserLastModified(),
206            getDateReleased(),
207            getDateExpired(),
208            getVersion());
209
210        if (isTouched()) {
211            clone.setDateLastModified(getDateLastModified());
212        }
213
214        return clone;
215    }
216
217    /**
218     * A folder does always have the content date <code>-1</code>.<p>
219     *
220     * @see org.opencms.file.CmsResource#getDateContent()
221     */
222    @Override
223    public long getDateContent() {
224
225        return -1;
226    }
227
228    /**
229     * A folder does always have length <code>-1</code>.<p>
230     *
231     * @see org.opencms.file.CmsResource#getLength()
232     */
233    @Override
234    public int getLength() {
235
236        return -1;
237    }
238
239    /**
240     * Since this is a folder, not a file, <code>false</code> is always returned.<p>
241     *
242     * @see org.opencms.file.CmsResource#isFile()
243     */
244    @Override
245    public boolean isFile() {
246
247        return false;
248    }
249
250    /**
251     * Since this is a folder, <code>true</code> is always returned.<p>
252     *
253     * @see org.opencms.file.CmsResource#isFolder()
254     */
255    @Override
256    public boolean isFolder() {
257
258        return true;
259    }
260
261    /**
262     * @see org.opencms.file.CmsResource#isTemporaryFile()
263     */
264    @Override
265    public boolean isTemporaryFile() {
266
267        return false;
268    }
269}