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.db;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsResource;
032import org.opencms.file.CmsUser;
033import org.opencms.util.CmsStringUtil;
034
035import java.util.Date;
036
037/**
038 * Provides filters for getting resources visited by a user in a specified time range.<p>
039 *
040 * @since 8.0
041 */
042public class CmsVisitedByFilter {
043
044    /** The date specifying the start point in time from which a resource was visited. */
045    private long m_fromDate;
046
047    /** The flag to determine if subfolders should be included to search for visited resources. */
048    private boolean m_includeSubFolders;
049
050    /** The parent path to read visited resources from. */
051    private String m_parentPath;
052
053    /** The date specifying the end point in time to which a resource was visited. */
054    private long m_toDate;
055
056    /** The user to check the visited resources for. */
057    private CmsUser m_user;
058
059    /**
060     * Constructor, without parameters.<p>
061     *
062     * It is required to set the user manually if using this constructor.<p>
063     */
064    public CmsVisitedByFilter() {
065
066        m_fromDate = 0L;
067        m_toDate = Long.MAX_VALUE;
068    }
069
070    /**
071     * Constructor, setting the user to the current user from the context.<p>
072     *
073     * @param cms the current users context
074     */
075    public CmsVisitedByFilter(CmsObject cms) {
076
077        this();
078        m_user = cms.getRequestContext().getCurrentUser();
079    }
080
081    /**
082     * @see java.lang.Object#clone()
083     */
084    @Override
085    public Object clone() {
086
087        CmsVisitedByFilter filter = new CmsVisitedByFilter();
088        filter.m_fromDate = m_fromDate;
089        filter.m_includeSubFolders = m_includeSubFolders;
090        filter.m_parentPath = m_parentPath;
091        filter.m_toDate = m_toDate;
092        filter.m_user = m_user;
093        return filter;
094    }
095
096    /**
097     * Returns the date specifying the start point in time from which a resource was visited.<p>
098     *
099     * @return the date specifying the start point in time from which a resource was visited
100     */
101    public long getFromDate() {
102
103        return m_fromDate;
104    }
105
106    /**
107     * Returns the parent root path to read visited resources from.<p>
108     *
109     * @return the parent root path to read visited resources from
110     */
111    public String getParentPath() {
112
113        return m_parentPath;
114    }
115
116    /**
117     * Returns the date specifying the end point in time to which a resource was visited.<p>
118     *
119     * @return the date specifying the end point in time to which a resource was visited
120     */
121    public long getToDate() {
122
123        return m_toDate;
124    }
125
126    /**
127     * Returns the user to check the visited resources for.<p>
128     *
129     * @return the user to check the visited resources for
130     */
131    public CmsUser getUser() {
132
133        return m_user;
134    }
135
136    /**
137     * Returns if subfolders should be included to search for visited resources.<p>
138     *
139     * @return <code>true</code> if subfolders should be included to search for visited resources, otherwise <code>false</code>
140     */
141    public boolean isIncludeSubFolders() {
142
143        return m_includeSubFolders;
144    }
145
146    /**
147     * Sets the date specifying the start point in time from which a resource was visited.<p>
148     *
149     * @param fromDate the date specifying the start point in time from which a resource was visited
150     */
151    public void setFromDate(long fromDate) {
152
153        m_fromDate = fromDate;
154    }
155
156    /**
157     * Returns if subfolders should be included to search for visited resources.<p>
158     *
159     * @param includeSubFolders the flag to determine if subfolders should be included
160     */
161    public void setIncludeSubfolders(boolean includeSubFolders) {
162
163        m_includeSubFolders = includeSubFolders;
164    }
165
166    /**
167     * Sets the parent path to read visited resources from.<p>
168     *
169     * This has to be the root path of the parent resource, not the site path.<p>
170     *
171     * @param parentPath the parent path to read visited resources from
172     */
173    public void setParentPath(String parentPath) {
174
175        m_parentPath = parentPath;
176    }
177
178    /**
179     * Sets the parent path to read visited resources from using the given resource as parent.<p>
180     *
181     * @param parentResource the resource to use as parent resource
182     */
183    public void setParentResource(CmsResource parentResource) {
184
185        if (parentResource.isFile()) {
186            m_parentPath = CmsResource.getFolderPath(parentResource.getRootPath());
187        } else {
188            m_parentPath = parentResource.getRootPath();
189        }
190    }
191
192    /**
193     * Sets the date specifying the end point in time to which a resource was visited.<p>
194     *
195     * @param toDate the date specifying the end point in time to which a resource was visited
196     */
197    public void setToDate(long toDate) {
198
199        m_toDate = toDate;
200    }
201
202    /**
203     * Sets the user to check the visited resources for.<p>
204     *
205     * @param user the user to check the visited resources for
206     */
207    public void setUser(CmsUser user) {
208
209        m_user = user;
210    }
211
212    /**
213     * Sets the start and end point in time in which a resource was visited.<p>
214     *
215     * @param fromDate the date specifying the start point in time from which a resource was visited
216     * @param toDate the date specifying the end point in time to which a resource was visited
217     */
218    public void setVisitedDates(long fromDate, long toDate) {
219
220        setFromDate(fromDate);
221        setToDate(toDate);
222    }
223
224    /**
225     * Returns a user readable representation of the filter.<p>
226     *
227     * @see java.lang.Object#toString()
228     */
229    @Override
230    public String toString() {
231
232        StringBuffer result = new StringBuffer(256);
233        result.append("From: ").append(new Date(getFromDate()).toString());
234        result.append(", To: ").append(new Date(getToDate()).toString());
235        if (getUser() != null) {
236            result.append(", User: ").append(getUser().getName());
237        }
238        if (CmsStringUtil.isNotEmpty(getParentPath())) {
239            result.append(", Parent path: ").append(getParentPath());
240            result.append(", Subfolders: ").append(isIncludeSubFolders());
241        }
242        return result.toString();
243    }
244}