com.github.dbourdette.glass.log.joblog.jdbc.JdbcJobLogStore.java Source code

Java tutorial

Introduction

Here is the source code for com.github.dbourdette.glass.log.joblog.jdbc.JdbcJobLogStore.java

Source

/*
 * Copyright 2011 Damien Bourdette
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.github.dbourdette.glass.log.joblog.jdbc;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;

import com.github.dbourdette.glass.configuration.Configuration;
import com.github.dbourdette.glass.log.joblog.JobLog;
import com.github.dbourdette.glass.log.joblog.JobLogLevel;
import com.github.dbourdette.glass.log.joblog.JobLogStore;
import com.github.dbourdette.glass.util.Page;
import com.github.dbourdette.glass.util.Query;

/**
 * @author damien bourdette
 */
public class JdbcJobLogStore implements JobLogStore {
    private static final String TABLE_SUFFIX = "job_log";

    private NamedParameterJdbcTemplate jdbcTemplate;

    private Configuration configuration;

    public JdbcJobLogStore(DataSource dataSource, Configuration configuration) {
        this.configuration = configuration;
        this.jdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
    }

    @Override
    public void add(JobLog jobLog) {
        String sql = "insert into " + getTableName()
                + " (id, executionId, logLevel, logDate, jobClass, jobGroup, jobName, triggerGroup, triggerName, message, stackTrace, rootCause)"
                + " values (" + configuration.getTablePrefix()
                + "sequence.nextval, :executionId, :logLevel, :logDate, :jobClass, :jobGroup, :jobName, :triggerGroup, :triggerName, :message, :stackTrace, :rootCause)";

        SqlParameterSource params = new MapSqlParameterSource().addValue("executionId", jobLog.getExecutionId())
                .addValue("logLevel", jobLog.getLevel().name()).addValue("logDate", jobLog.getDate())
                .addValue("jobClass", jobLog.getJobClass()).addValue("jobGroup", jobLog.getJobGroup())
                .addValue("jobName", jobLog.getJobName()).addValue("triggerGroup", jobLog.getTriggerGroup())
                .addValue("triggerName", jobLog.getTriggerName()).addValue("message", jobLog.getMessage())
                .addValue("stackTrace", jobLog.getStackTrace()).addValue("rootCause", jobLog.getRootCause());

        jdbcTemplate.update(sql, params);
    }

    @Override
    public Page<JobLog> getLogs(Query query) {
        String sql = "from " + getTableName();

        return getLogs(sql, new MapSqlParameterSource(), query);
    }

    @Override
    public Page<JobLog> getLogs(Long executionId, Query query) {
        String sql = "from " + configuration.getTablePrefix() + "log where executionId = :executionId";

        SqlParameterSource source = new MapSqlParameterSource().addValue("executionId", executionId);

        return getLogs(sql, source, query);
    }

    @Override
    public synchronized void clear() {
        String sql = "truncate table " + getTableName();

        jdbcTemplate.getJdbcOperations().execute(sql);
    }

    private Page<JobLog> getLogs(String sqlBase, SqlParameterSource params, Query query) {
        String sql = query.applySqlLimit("select * " + sqlBase + " order by logDate asc");

        List<JobLog> jobLogs = jdbcTemplate.query(sql, params, new RowMapper<JobLog>() {
            @Override
            public JobLog mapRow(ResultSet rs, int rowNum) throws SQLException {
                return doMapRow(rs, rowNum);
            }
        });

        String countSql = "select count(*) " + sqlBase;

        Page<JobLog> page = Page.fromQuery(query);

        page.setItems(jobLogs);
        page.setTotalCount(jdbcTemplate.queryForInt(countSql, params));

        return page;
    }

    private List<JobLog> getLogs(String sqlBase, SqlParameterSource params) {
        String sql = "select * " + sqlBase + " order by logDate asc";

        return jdbcTemplate.query(sql, params, new RowMapper<JobLog>() {
            @Override
            public JobLog mapRow(ResultSet rs, int rowNum) throws SQLException {
                return doMapRow(rs, rowNum);
            }
        });
    }

    private JobLog doMapRow(ResultSet rs, int rowNum) throws SQLException {
        JobLog jobLog = new JobLog();

        jobLog.setExecutionId(rs.getLong("executionId"));
        jobLog.setLevel(JobLogLevel.valueOf(rs.getString("logLevel")));
        jobLog.setDate(rs.getTimestamp("logDate"));
        jobLog.setJobClass(rs.getString("jobClass"));
        jobLog.setJobGroup(rs.getString("jobGroup"));
        jobLog.setJobName(rs.getString("jobName"));
        jobLog.setTriggerGroup(rs.getString("triggerGroup"));
        jobLog.setTriggerName(rs.getString("triggerName"));
        jobLog.setMessage(rs.getString("message"));
        jobLog.setStackTrace(rs.getString("stackTrace"));
        jobLog.setRootCause(rs.getString("rootCause"));

        return jobLog;
    }

    private String getTableName() {
        return configuration.getTablePrefix() + TABLE_SUFFIX;
    }
}