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.history;
029
030import org.opencms.db.CmsResourceState;
031import org.opencms.file.CmsFile;
032import org.opencms.file.CmsObject;
033import org.opencms.file.CmsResource;
034import org.opencms.main.CmsException;
035import org.opencms.security.CmsPrincipal;
036import org.opencms.util.CmsUUID;
037
038/**
039 * A historical version of a file in the OpenCms VFS resource history.<p>
040 *
041 * @since 6.9.1
042 */
043public class CmsHistoryFile extends CmsFile implements I_CmsHistoryResource {
044
045    /** Serial version UID required for safe serialization. */
046    private static final long serialVersionUID = 4073076414399668662L;
047
048    /** The structure id of the parent of this historical resource. */
049    private CmsUUID m_parentId;
050
051    /** The publish tag of this historical resource. */
052    private int m_publishTag;
053
054    /** The version number of the structure part for this historical resource. */
055    private int m_structureVersion;
056
057    /** The version number of the resource part for this historical resource. */
058    private int m_resourceVersion;
059
060    /**
061     * Creates a dummy historical file from the given resource.<p>
062     *
063     * @param resource the resource to wrap
064     */
065    public CmsHistoryFile(CmsResource resource) {
066
067        super(resource);
068        m_publishTag = 0;
069        m_parentId = null;
070    }
071
072    /**
073     * Constructor from a history resource.<p>
074     *
075     * @param resource the base history resource
076     */
077    public CmsHistoryFile(I_CmsHistoryResource resource) {
078
079        this(
080            resource.getPublishTag(),
081            resource.getStructureId(),
082            resource.getResourceId(),
083            resource.getRootPath(),
084            resource.getTypeId(),
085            resource.getFlags(),
086            resource.getProjectLastModified(),
087            resource.getState(),
088            resource.getDateCreated(),
089            resource.getUserCreated(),
090            resource.getDateLastModified(),
091            resource.getUserLastModified(),
092            resource.getDateReleased(),
093            resource.getDateExpired(),
094            resource.getLength(),
095            resource.getDateContent(),
096            resource.getVersion(),
097            resource.getParentId(),
098            resource.isFile() ? ((CmsFile)resource).getContents() : null,
099            resource.getResourceVersion(),
100            resource.getStructureVersion());
101    }
102
103    /**
104     * Default Constructor.<p>
105     *
106     * @param publishTag the publish tag of this historical resource
107     * @param structureId the id of this resources structure record
108     * @param resourceId the id of this resources resource record
109     * @param path the filename of this resouce
110     * @param type the type of this resource
111     * @param flags the flags of this resource
112     * @param projectId the project id this resource was last modified in
113     * @param state the state of this resource
114     * @param dateCreated the creation date of this resource
115     * @param userCreated the id of the user who created this resource
116     * @param dateLastModified the date of the last modification of this resource
117     * @param userLastModified the id of the user who did the last modification of this resource
118     * @param dateReleased the release date of this resource
119     * @param dateExpired the expiration date of this resource
120     * @param size the size of the file content of this resource
121     * @param dateContent the date of the last modification of the content of this resource
122     * @param version the version number of this resource
123     * @param parentId structure id of the parent of this historical resource
124     * @param content the content of this version
125     * @param resourceVersion the version number of the resource part for this historical resource
126     * @param structureVersion the version number of the structure part for this historical resource
127     */
128    public CmsHistoryFile(
129        int publishTag,
130        CmsUUID structureId,
131        CmsUUID resourceId,
132        String path,
133        int type,
134        int flags,
135        CmsUUID projectId,
136        CmsResourceState state,
137        long dateCreated,
138        CmsUUID userCreated,
139        long dateLastModified,
140        CmsUUID userLastModified,
141        long dateReleased,
142        long dateExpired,
143        int size,
144        long dateContent,
145        int version,
146        CmsUUID parentId,
147        byte[] content,
148        int resourceVersion,
149        int structureVersion) {
150
151        super(
152            structureId,
153            resourceId,
154            path,
155            type,
156            flags,
157            projectId,
158            state,
159            dateCreated,
160            userCreated,
161            dateLastModified,
162            userLastModified,
163            dateReleased,
164            dateExpired,
165            0,
166            size,
167            dateContent,
168            version,
169            content);
170
171        m_publishTag = publishTag;
172        m_parentId = parentId;
173        m_resourceVersion = resourceVersion;
174        m_structureVersion = structureVersion;
175    }
176
177    /**
178     * Returns a clone of this Objects instance.<p>
179     *
180     * @return a clone of this instance
181     */
182    @Override
183    public Object clone() {
184
185        return new CmsHistoryFile(
186            getPublishTag(),
187            getStructureId(),
188            getResourceId(),
189            getRootPath(),
190            getTypeId(),
191            getFlags(),
192            getProjectLastModified(),
193            getState(),
194            getDateCreated(),
195            getUserCreated(),
196            getDateLastModified(),
197            getUserLastModified(),
198            getDateReleased(),
199            getDateExpired(),
200            getLength(),
201            getDateContent(),
202            getVersion(),
203            getParentId(),
204            getContents(),
205            getResourceVersion(),
206            getStructureVersion());
207    }
208
209    /**
210     * @see org.opencms.file.history.I_CmsHistoryResource#getParentId()
211     */
212    public CmsUUID getParentId() {
213
214        return m_parentId;
215    }
216
217    /**
218     * @see org.opencms.file.history.I_CmsHistoryResource#getPublishTag()
219     */
220    public int getPublishTag() {
221
222        return m_publishTag;
223    }
224
225    /**
226     * Returns the name of the user that created this resource.<p>
227     *
228     * @param cms the current cms context
229     *
230     * @return the name of the user that created this resource
231     */
232    public String getUserCreatedName(CmsObject cms) {
233
234        try {
235            return CmsPrincipal.readPrincipalIncludingHistory(cms, getUserCreated()).getName();
236        } catch (CmsException e) {
237            return getUserCreated().toString();
238        }
239    }
240
241    /**
242     * Returns the name of the user that last modified this resource.<p>
243     *
244     * @param cms the current cms context
245     *
246     * @return the name of the user that last modified this resource
247     */
248    public String getUserLastModifiedName(CmsObject cms) {
249
250        try {
251            return CmsPrincipal.readPrincipalIncludingHistory(cms, getUserLastModified()).getName();
252        } catch (CmsException e) {
253            return getUserLastModified().toString();
254        }
255    }
256
257    /**
258     * @see org.opencms.file.history.I_CmsHistoryResource#getResourceVersion()
259     */
260    public int getResourceVersion() {
261
262        return m_resourceVersion;
263    }
264
265    /**
266     * @see org.opencms.file.history.I_CmsHistoryResource#getStructureVersion()
267     */
268    public int getStructureVersion() {
269
270        return m_structureVersion;
271    }
272
273    /**
274     * @see org.opencms.file.CmsResource#toString()
275     */
276    @Override
277    public String toString() {
278
279        StringBuffer result = new StringBuffer();
280
281        result.append("[");
282        result.append(super.toString());
283        result.append(", resource version: ");
284        result.append(m_resourceVersion);
285        result.append(", structure version ");
286        result.append(m_structureVersion);
287        result.append(", parent id: ");
288        result.append(m_parentId);
289        result.append(", publish tag: ");
290        result.append(m_publishTag);
291        result.append("]");
292
293        return result.toString();
294    }
295}