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 GmbH & Co. KG, 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.file.wrapper;
029
030import org.opencms.file.CmsFile;
031import org.opencms.file.CmsResource;
032
033/**
034 * Helper class to create "virtual" resources not existing in the vfs which are
035 * based on existing resources.<p>
036 *
037 * It is not possible to change a {@link CmsResource} instance. This helper class
038 * clones a <code>CmsResource</code> and can change some attributes of the
039 * <code>CmsResource</code> like the path, the typeId or the length.<p>
040 *
041 * @since 6.2.4
042 */
043public class CmsWrappedResource {
044
045    /** The resource this virtual resources is based on. */
046    private CmsResource m_base;
047
048    /** Indicates if the virtual resource is a folder or not. */
049    private boolean m_isFolder;
050
051    /** The size of the content of the virtual resource. */
052    private int m_length;
053
054    /** The root path of the virtual resource. */
055    private String m_rootPath;
056
057    /** The type id of the virtual resource. */
058    private int m_typeId;
059
060    /**
061     * Creates a new virtual resource.<p>
062     *
063     * @param res the resource this virtual resource is based on
064     */
065    public CmsWrappedResource(CmsResource res) {
066
067        m_base = res;
068
069        m_rootPath = res.getRootPath();
070        m_typeId = res.getTypeId();
071        m_isFolder = res.isFolder();
072        m_length = res.getLength();
073    }
074
075    /**
076     * Returns the virtual resource as a file.<p>
077     *
078     * @return the virtual resource as a file
079     */
080    public CmsFile getFile() {
081
082        if (m_base instanceof CmsFile) {
083            CmsFile file = (CmsFile)m_base;
084
085            return new CmsFile(
086                file.getStructureId(),
087                file.getResourceId(),
088                m_rootPath,
089                m_typeId,
090                file.getFlags(),
091                file.getProjectLastModified(),
092                file.getState(),
093                file.getDateCreated(),
094                file.getUserCreated(),
095                file.getDateLastModified(),
096                file.getUserLastModified(),
097                file.getDateReleased(),
098                file.getDateExpired(),
099                file.getSiblingCount(),
100                file.getLength(),
101                file.getDateContent(),
102                file.getVersion(),
103                file.getContents());
104        }
105
106        return new CmsFile(getResource());
107    }
108
109    /**
110     * Returns the length.<p>
111     *
112     * @return the length
113     */
114    public int getLength() {
115
116        return m_length;
117    }
118
119    /**
120     * Returns the virtual resource.<p>
121     *
122     * @return the virtual resource
123     */
124    public CmsResource getResource() {
125
126        return new CmsResource(
127            m_base.getStructureId(),
128            m_base.getResourceId(),
129            m_rootPath,
130            m_typeId,
131            m_isFolder,
132            m_base.getFlags(),
133            m_base.getProjectLastModified(),
134            m_base.getState(),
135            m_base.getDateCreated(),
136            m_base.getUserCreated(),
137            m_base.getDateLastModified(),
138            m_base.getUserLastModified(),
139            m_base.getDateReleased(),
140            m_base.getDateExpired(),
141            m_base.getSiblingCount(),
142            m_length,
143            m_base.getDateContent(),
144            m_base.getVersion());
145    }
146
147    /**
148     * Returns the rootPath.<p>
149     *
150     * @return the rootPath
151     */
152    public String getRootPath() {
153
154        return m_rootPath;
155    }
156
157    /**
158     * Returns the typeId.<p>
159     *
160     * @return the typeId
161     */
162    public int getTypeId() {
163
164        return m_typeId;
165    }
166
167    /**
168     * Returns the isFolder.<p>
169     *
170     * @return the isFolder
171     */
172    public boolean isFolder() {
173
174        return m_isFolder;
175    }
176
177    /**
178     * Sets the isFolder.<p>
179     *
180     * @param isFolder the isFolder to set
181     */
182    public void setFolder(boolean isFolder) {
183
184        m_isFolder = isFolder;
185
186        if ((m_isFolder) && (!m_rootPath.endsWith("/"))) {
187            m_rootPath += "/";
188        }
189    }
190
191    /**
192     * Sets the length.<p>
193     *
194     * @param length the length to set
195     */
196    public void setLength(int length) {
197
198        m_length = length;
199    }
200
201    /**
202     * Sets the rootPath.<p>
203     *
204     * @param rootPath the rootPath to set
205     */
206    public void setRootPath(String rootPath) {
207
208        m_rootPath = rootPath;
209    }
210
211    /**
212     * Sets the typeId.<p>
213     *
214     * @param typeId the typeId to set
215     */
216    public void setTypeId(int typeId) {
217
218        m_typeId = typeId;
219    }
220}