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.mysql; 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 * MySQL 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 CmsVisitEntry entry = new CmsVisitEntry(user.getId(), System.currentTimeMillis(), resource.getStructureId()); 080 081 addVisit(dbc, poolName, entry); 082 083 if (!entryExists) { 084 // new entry, check if maximum number of stored visited resources is exceeded 085 PreparedStatement stmt = null; 086 Connection conn = null; 087 ResultSet res = null; 088 int count = 0; 089 090 try { 091 conn = m_sqlManager.getConnection(poolName); 092 stmt = m_sqlManager.getPreparedStatement(conn, dbc.currentProject(), "C_VISITED_USER_COUNT_1"); 093 094 stmt.setString(1, user.getId().toString()); 095 res = stmt.executeQuery(); 096 097 if (res.next()) { 098 count = res.getInt(1); 099 while (res.next()) { 100 // do nothing only move through all rows because of mssql odbc driver 101 } 102 } else { 103 throw new CmsDbConsistencyException( 104 Messages.get().container(Messages.ERR_COUNTING_VISITED_RESOURCES_1, user.getName())); 105 } 106 107 int maxCount = OpenCms.getSubscriptionManager().getMaxVisitedCount(); 108 if (count > maxCount) { 109 // delete old visited log entries 110 m_sqlManager.closeAll(dbc, null, stmt, res); 111 stmt = m_sqlManager.getPreparedStatement( 112 conn, 113 dbc.currentProject(), 114 "C_MYSQL_VISITED_USER_DELETE_2"); 115 116 stmt.setString(1, user.getId().toString()); 117 stmt.setInt(2, count - maxCount); 118 stmt.executeUpdate(); 119 } 120 } catch (SQLException e) { 121 throw new CmsDbSqlException( 122 Messages.get().container(Messages.ERR_GENERIC_SQL_1, CmsDbSqlException.getErrorQuery(stmt)), 123 e); 124 } finally { 125 m_sqlManager.closeAll(dbc, conn, stmt, res); 126 } 127 } 128 } 129 130}