001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH (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.main.CmsException;
033import org.opencms.main.CmsLog;
034import org.opencms.main.I_CmsResourceInit;
035import org.opencms.main.OpenCms;
036import org.opencms.util.CmsStringUtil;
037
038import javax.servlet.http.HttpServletRequest;
039import javax.servlet.http.HttpServletResponse;
040
041import org.apache.commons.logging.Log;
042
043/**
044 * This resource handler checks if a resource has to be marked as visited by the current user.
045 * It checks the value of the <code>usertracking.mark</code> property.<p>
046 * 
047 * Possible values are:
048 * <ul>
049 * <li><code>online</code>: The resource is marked only in the online project
050 * <li><code>true</code>: The resource is marked in all projects
051 * <li><code>false</code>: The resource is not marked at all
052 * </ul>
053 * 
054 * @since 8.0
055 */
056public class CmsUserTrackingResourceHandler implements I_CmsResourceInit {
057
058    /** Property that indicates if resources should be tracked,
059     *  value has to be <code>true</code>, <code>false</code> or <code>online</code>.
060     */
061    public static final String PROPERTY_USERTRACKING_MARK = "usertracking.mark";
062
063    /** Constant for the property value <code>online</code>. */
064    public static final String VALUE_ONLINE = "online";
065
066    /** The log object for this class. */
067    private static final Log LOG = CmsLog.getLog(CmsUserTrackingResourceHandler.class);
068
069    /**
070     * @see org.opencms.main.I_CmsResourceInit#initResource(org.opencms.file.CmsResource, org.opencms.file.CmsObject, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
071     */
072    public CmsResource initResource(CmsResource resource, CmsObject cms, HttpServletRequest req, HttpServletResponse res) {
073
074        if ((resource != null) && resource.isFile()) {
075            // only do something if the resource was found and is a file (that can be marked)
076            String mark = "";
077            try {
078                // read the property value
079                mark = cms.readPropertyObject(resource, PROPERTY_USERTRACKING_MARK, true).getValue(CmsStringUtil.FALSE);
080            } catch (CmsException e) {
081                // ignore, resource will not be marked at all
082            }
083            if (Boolean.valueOf(mark).booleanValue()
084                || (VALUE_ONLINE.equalsIgnoreCase(mark) && cms.getRequestContext().getCurrentProject().isOnlineProject())) {
085                // mark the resource as visited by the current user
086                try {
087                    OpenCms.getSubscriptionManager().markResourceAsVisitedBy(
088                        cms,
089                        resource,
090                        cms.getRequestContext().getCurrentUser());
091                } catch (CmsException e) {
092                    // error marking resource
093                    LOG.error(e);
094                }
095            }
096        }
097
098        return resource;
099    }
100
101}