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.oracle;
029
030import org.opencms.db.CmsDbConsistencyException;
031import org.opencms.db.CmsDbContext;
032import org.opencms.db.CmsDbSqlException;
033import org.opencms.db.CmsVisitEntry;
034import org.opencms.db.CmsVisitEntryFilter;
035import org.opencms.db.generic.CmsSqlManager;
036import org.opencms.db.generic.Messages;
037import org.opencms.file.CmsDataAccessException;
038import org.opencms.file.CmsResource;
039import org.opencms.file.CmsUser;
040import org.opencms.main.OpenCms;
041
042import java.sql.Connection;
043import java.sql.PreparedStatement;
044import java.sql.ResultSet;
045import java.sql.SQLException;
046
047/**
048 * Oracle implementation of the subscription driver.<p>
049 *
050 *  @since 8.0.0
051 */
052public class CmsSubscriptionDriver extends org.opencms.db.generic.CmsSubscriptionDriver {
053
054    /**
055     * @see org.opencms.db.generic.CmsSubscriptionDriver#initSqlManager(java.lang.String)
056     */
057    @Override
058    public org.opencms.db.generic.CmsSqlManager initSqlManager(String classname) {
059
060        return CmsSqlManager.getInstance(classname);
061    }
062
063    /**
064     * @see org.opencms.db.generic.CmsSubscriptionDriver#markResourceAsVisitedBy(org.opencms.db.CmsDbContext, java.lang.String, org.opencms.file.CmsResource, org.opencms.file.CmsUser)
065     */
066    @Override
067    public void markResourceAsVisitedBy(CmsDbContext dbc, String poolName, CmsResource resource, CmsUser user)
068    throws CmsDataAccessException {
069
070        boolean entryExists = false;
071        CmsVisitEntryFilter filter = CmsVisitEntryFilter.ALL.filterResource(resource.getStructureId()).filterUser(
072            user.getId());
073        // delete existing visited entry for the resource
074        if (readVisits(dbc, OpenCms.getSubscriptionManager().getPoolName(), filter).size() > 0) {
075            entryExists = true;
076            deleteVisits(dbc, OpenCms.getSubscriptionManager().getPoolName(), filter);
077        }
078
079        // create new entry
080
081        CmsVisitEntry entry = new CmsVisitEntry(user.getId(), System.currentTimeMillis(), resource.getStructureId());
082        addVisit(dbc, poolName, entry);
083
084        if (!entryExists) {
085            // new entry, check if maximum number of stored visited resources is exceeded
086            PreparedStatement stmt = null;
087            Connection conn = null;
088            ResultSet res = null;
089            int count = 0;
090
091            try {
092                conn = m_sqlManager.getConnection(poolName);
093                stmt = m_sqlManager.getPreparedStatement(conn, dbc.currentProject(), "C_VISITED_USER_COUNT_1");
094
095                stmt.setString(1, user.getId().toString());
096                res = stmt.executeQuery();
097
098                if (res.next()) {
099                    count = res.getInt(1);
100                    while (res.next()) {
101                        // do nothing only move through all rows because of mssql odbc driver
102                    }
103                } else {
104                    throw new CmsDbConsistencyException(
105                        Messages.get().container(Messages.ERR_COUNTING_VISITED_RESOURCES_1, user.getName()));
106                }
107
108                int maxCount = OpenCms.getSubscriptionManager().getMaxVisitedCount();
109                if (count > maxCount) {
110                    // delete old visited log entries
111                    m_sqlManager.closeAll(dbc, null, stmt, res);
112                    stmt = m_sqlManager.getPreparedStatement(
113                        conn,
114                        dbc.currentProject(),
115                        "C_ORACLE_VISITED_USER_DELETE_3");
116
117                    stmt.setString(1, user.getId().toString());
118                    stmt.setString(2, user.getId().toString());
119                    stmt.setInt(3, count - maxCount);
120                    stmt.executeUpdate();
121                }
122            } catch (SQLException e) {
123                throw new CmsDbSqlException(
124                    Messages.get().container(Messages.ERR_GENERIC_SQL_1, CmsDbSqlException.getErrorQuery(stmt)),
125                    e);
126            } finally {
127                m_sqlManager.closeAll(dbc, conn, stmt, res);
128            }
129        }
130    }
131
132}