com.stehno.sanctuary.core.local.JdbcLocalStore.java Source code

Java tutorial

Introduction

Here is the source code for com.stehno.sanctuary.core.local.JdbcLocalStore.java

Source

/*
 * Copyright (c) 2011 Christopher J. Stehno
 *
 *    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.stehno.sanctuary.core.local;

import com.stehno.sanctuary.core.FileStatus;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreatorFactory;
import org.springframework.jdbc.core.simple.ParameterizedSingleColumnRowMapper;

import java.io.File;
import java.sql.Types;
import java.util.List;

/**
 *
 * Tables:
 *
 *  store_files
 *      varchar(1000) path
 *      bigint
 *
 * @author cjstehno
 */
public class JdbcLocalStore implements LocalStore {

    private final JdbcTemplate jdbcTemplate;
    private final PreparedStatementCreatorFactory storeFileFactory = new PreparedStatementCreatorFactory(
            "insert into store_files (path,lastmod) values (?,?)", new int[] { Types.VARCHAR, Types.BIGINT });
    private final PreparedStatementCreatorFactory removeFileFactory = new PreparedStatementCreatorFactory(
            "delete from store_files where path=?", new int[] { Types.VARCHAR });
    private final PreparedStatementCreatorFactory fileStatusFactory = new PreparedStatementCreatorFactory(
            "select lastmod from store_files where path=?", new int[] { Types.VARCHAR });
    private final String listFilesSql = "select path from store_files";

    public JdbcLocalStore(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public void init() throws Exception {
        jdbcTemplate.execute(
                "CREATE TABLE IF NOT EXISTS STORE_FILES ( path VARCHAR(1000) primary key, lastmod bigint default 0)");
    }

    @Override
    public void destroy() {
        /* nothing */
    }

    @Override
    public void storeFile(File file) {
        removeFile(file);

        jdbcTemplate.update(
                storeFileFactory.newPreparedStatementCreator(new Object[] { file.getPath(), file.lastModified() }));
    }

    @Override
    public void removeFile(File file) {
        jdbcTemplate.update(removeFileFactory.newPreparedStatementCreator(new Object[] { file.getPath() }));
    }

    @Override
    public FileStatus fileStatus(File file) {
        final FileStatus status;

        List<Long> results = jdbcTemplate.query(
                fileStatusFactory.newPreparedStatementCreator(new Object[] { file.getPath() }),
                new ParameterizedSingleColumnRowMapper<Long>());
        if (results.isEmpty()) {
            status = FileStatus.NEW;
        } else {
            status = file.lastModified() > results.get(0) ? FileStatus.MODIFIED : FileStatus.UNMODIFIED;
        }

        return status;
    }

    @Override
    public Iterable<String> listFilePaths() {
        return jdbcTemplate.queryForList(listFilesSql, String.class);
    }
}