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.flex;
029
030import org.opencms.file.CmsResource;
031
032/**
033 * Contains information about the OpenCms request context required by the
034 * Flex implementation.<p>
035 *
036 * An instance of this class is attached to every <code>CmsRequestContext</code> as
037 * an attribute as soon as the request context is wrapped in a flex response.
038 * Information about the "last modified" and "expire" times of VFS resources are
039 * stored in this Object.<p>
040 *
041 * @since 6.0.0
042 */
043public class CmsFlexRequestContextInfo {
044
045    /** The currently calculated "expires" date for this request context .*/
046    private long m_dateExpires;
047
048    /** The currently calculated "last modified" date for this request context.  */
049    private long m_dateLastModified;
050
051    /**
052     * Public constructor.<p>
053     */
054    public CmsFlexRequestContextInfo() {
055
056        // by default the expiration date is the max long value
057        m_dateExpires = CmsResource.DATE_EXPIRED_DEFAULT;
058    }
059
060    /**
061     * Returns the "expires" date for this context.<p>
062     *
063     * @return the "expires" date for this context
064     */
065    public long getDateExpires() {
066
067        return m_dateExpires;
068    }
069
070    /**
071     * Returns the "last modified" date for this context.<p>
072     *
073     * @return the "last modified" date for this context
074     */
075    public long getDateLastModified() {
076
077        return m_dateLastModified;
078    }
079
080    /**
081     * Merges this context info with the values from the other context info.<p>
082     *
083     * @param other the context info to merge with
084     */
085    public void merge(CmsFlexRequestContextInfo other) {
086
087        updateDateLastModified(other.getDateLastModified());
088        updateDateExpires(other.getDateExpires());
089    }
090
091    /**
092     * Updates the "expires" date for this context with the given value.<p>
093     *
094     * @param dateExpires the value to update the "expires" date with
095     */
096    public void updateDateExpires(long dateExpires) {
097
098        if (dateExpires > System.currentTimeMillis()) {
099            if (dateExpires < m_dateExpires) {
100                m_dateExpires = dateExpires;
101            }
102        } else {
103            updateDateLastModified(dateExpires);
104        }
105    }
106
107    /**
108     * Updates the "last modified" date for this context with the given value.<p>
109     *
110     * The currently stored value is only updated with the new value if
111     * the new value is either larger (i.e. newer) then the stored value,
112     * or if the new value is less then zero, which indicates that the "last modified"
113     * optimization can not be used because the element is dynamic.<p>
114     *
115     * @param dateLastModified the value to update the "last modified" date with
116     */
117    public void updateDateLastModified(long dateLastModified) {
118
119        if ((m_dateLastModified > -1) && ((dateLastModified > m_dateLastModified) || (dateLastModified < 0))) {
120            m_dateLastModified = dateLastModified;
121        }
122    }
123
124    /**
125     * Updates both the "last modified" and the "expires" date
126     * for this context with the given values.<p>
127     *
128     * @param dateLastModified the value to update the "last modified" date with
129     * @param dateExpires the value to update the "expires" date with
130     */
131    public void updateDates(long dateLastModified, long dateExpires) {
132
133        updateDateLastModified(dateLastModified);
134        updateDateExpires(dateExpires);
135    }
136
137    /**
138     * Updates the "last modified" date for this context as well as the
139     * "expires" date with the values from a given resource.<p>
140     *
141     * The "expires" date is the calculated from the given date values
142     * of resource release and expiration and also the current time.<p>
143     *
144     * @param resource the resource to use for updating the context values
145     */
146    public void updateFromResource(CmsResource resource) {
147
148        // first set the last modification date
149        updateDateLastModified(resource.getDateLastModified());
150        // now use both release and expiration date from the resource to update the expires info
151        updateDateExpires(resource.getDateReleased());
152        updateDateExpires(resource.getDateExpired());
153    }
154}