Java tutorial
/** * Copyright (C) 2013 Seajas, the Netherlands. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License version 3, as * published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package com.seajas.search.utilities.logging.dao; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Timestamp; import java.util.Date; import java.util.List; import javax.sql.DataSource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DataAccessException; import org.springframework.jdbc.core.JdbcOperations; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import com.seajas.search.utilities.logging.model.Logging; /** * Simple data access object for logger data. * * @author Jasper van Veghel <jasper@seajas.com> */ @SuppressWarnings("deprecation") public class LoggingDAO { /** * The logger. */ private static final Logger logger = LoggerFactory.getLogger(LoggingDAO.class); /** * The JDBC template (for DDL operations. */ private JdbcOperations jdbc; /** * Initialize the database table. * * @param dataSource */ @Autowired public LoggingDAO(final DataSource dataSource) { this.jdbc = new JdbcTemplate(dataSource); try { jdbc.execute( "CREATE CACHED TABLE logging (id INT IDENTITY, level VARCHAR(255) NOT NULL, message VARCHAR(8196) NOT NULL, " + "creation_date DATETIME NOT NULL)"); } catch (DataAccessException e) { if (logger.isDebugEnabled()) logger.debug("Table 'logging' likely already exists as it could not be created"); } } /** * The row mapper. */ RowMapper<Logging> mapper = new RowMapper<Logging>() { @Override public Logging mapRow(final ResultSet resultSet, final int rowNum) throws SQLException { Integer id = resultSet.getInt("id"); String level = resultSet.getString("level"); String message = resultSet.getString("message"); Timestamp creationDate = resultSet.getTimestamp("creation_date"); return new Logging(id, level, message, creationDate); } }; /** * Find logging message by level. * * @param level * @param maxResults * @return List<Modifier> */ public List<Logging> findMessagesByLevel(final String level, final Integer maxResults) { return jdbc.query( "SELECT id, level, message, creation_date FROM logging WHERE level = ? ORDER BY creation_date DESC LIMIT ?", mapper, level, maxResults); } /** * Find all logging messages. * * @param maxResults * @return List<Modifier> */ public List<Logging> findMessages(final Integer maxResults) { return jdbc.query( "SELECT id, level, message, creation_date FROM logging ORDER BY creation_date DESC LIMIT ?", mapper, maxResults); } /** * Find all logging messages before the given date. * * @param date * @return List<Logging> */ public List<Logging> findMessagesBeforeDate(final Date date) { return jdbc.query( "SELECT id, level, message, creation_date FROM logging WHERE creation_date < ? ORDER BY creation_date ASC", mapper, date); } /** * Count all messages currently residing in the logging table. * * @return Integer */ public Integer countMessages() { return jdbc.queryForInt("SELECT COUNT(1) FROM logging"); } /** * Delete all logging messages before the given date. * * @param date * @return boolean */ public boolean deleteMessagesBeforeDate(final Date date) { return jdbc.update("DELETE FROM logging WHERE creation_date < ?", date) > 0; } }