com.fileanalyzer.dao.impl.FilesDAOImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.fileanalyzer.dao.impl.FilesDAOImpl.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.fileanalyzer.dao.impl;

import com.fileanalyzer.dao.DAOMinimizer;
import com.fileanalyzer.dao.FilesDAO;
import com.fileanalyzer.dbconnector.DBConnector;
import com.fileanalyzer.domain.FileStatistic;
import com.fileanalyzer.domain.Files;
import com.fileanalyzer.util.SqlGenerator;
import static com.fileanalyzer.util.SqlGenerator.*;
import static com.fileanalyzer.util.LineStatisticCalculator.*;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

/**
 *
 * @author Dimon
 */
@Component
public class FilesDAOImpl implements FilesDAO, DAOMinimizer {
    private static final Logger log = Logger.getLogger(FilesDAOImpl.class);

    @Override
    public void add(Files fileInDb) {
        Connection con = null;
        PreparedStatement preparedStatement = null;
        try {
            con = DBConnector.getConnection();
            con.setAutoCommit(false);
            preparedStatement = con.prepareStatement(getInsertByFiles(fileInDb));
            preparedStatement.executeUpdate();
            con.commit();
        } catch (SQLException e) {
            handleException(e, con);
        } finally {
            doFinal(con, preparedStatement);
        }
    }

    @Override
    public void add(StringBuilder fStat) {
        Connection con = null;
        PreparedStatement preparedStatement = null;
        try {
            con = DBConnector.getConnection();
            con.setAutoCommit(false);
            preparedStatement = con.prepareStatement(fStat.toString());
            preparedStatement.executeUpdate();
            con.commit();
        } catch (SQLException e) {
            handleException(e, con);
        } finally {
            doFinal(con, preparedStatement);
        }
    }

    @Override
    public void add(List<Files> fileInDb) {
        for (Files f : fileInDb)
            add(f);
    }

    @Override
    public void update(Files fileInDb) {
        Connection con = null;
        PreparedStatement preparedStatement = null;
        try {
            con = DBConnector.getConnection();
            con.setAutoCommit(false);
            preparedStatement = con.prepareStatement(SqlGenerator.getUpdateByFiles(fileInDb));
            preparedStatement.executeUpdate();
            con.commit();
        } catch (SQLException e) {
            handleException(e, con);
        } finally {
            doFinal(con, preparedStatement);
        }
    }

    @Override
    public void delete(Files fileInDb) {
        Connection con = null;
        Statement statement = null;
        try {
            con = DBConnector.getConnection();
            con.setAutoCommit(false);
            statement = con.createStatement();
            statement.execute("delete from " + Files.FilesFieldsKey.TABLE + " where id=" + fileInDb.getId());
            con.commit();
        } catch (SQLException e) {
            handleException(e, con);
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    log.error(ex);
                }
            }
            try {
                con.setAutoCommit(true);
            } catch (SQLException ex) {
                log.error("setAutoCommit(true)", ex);
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException ex) {
                    log.error(ex);
                }
            }
        }
    }

    @Override
    public Files getFileById(Long id) {
        Connection con = null;
        PreparedStatement statement = null;
        String sql = "select * from " + Files.FilesFieldsKey.TABLE + " where id=?";
        Files fStat = new Files();
        try {
            con = DBConnector.getConnection();
            con.setAutoCommit(false);
            statement = con.prepareStatement(sql);
            statement.setLong(1, id);
            ResultSet rs = statement.executeQuery();
            ResultSetToFiles(rs, fStat);
            con.commit();
        } catch (SQLException e) {
            handleException(e, con);
        } finally {
            doFinal(con, statement);
        }
        return fStat;
    }

    @Override
    public List<Files> getAll() {
        Connection con = null;
        PreparedStatement statement = null;
        String sql = "select * from " + Files.FilesFieldsKey.TABLE;
        List<Files> listFStat = new ArrayList<>();
        try {
            con = DBConnector.getConnection();
            con.setAutoCommit(false);
            statement = con.prepareStatement(sql);
            ResultSet rs = statement.executeQuery();
            ResultSetToListFiles(rs, listFStat);
            con.commit();
        } catch (SQLException e) {
            handleException(e, con);
        } finally {
            doFinal(con, statement);
        }
        return listFStat;
    }

    @Override
    public Long getMaxId() {
        Connection con = null;
        PreparedStatement statement = null;
        String sql = "select max(id) from " + Files.FilesFieldsKey.TABLE;
        Long maxId = null;
        try {
            con = DBConnector.getConnection();
            con.setAutoCommit(false);
            statement = con.prepareStatement(sql);
            ResultSet rs = statement.executeQuery();
            if (rs.next())
                maxId = rs.getLong(1);
            con.commit();
        } catch (SQLException e) {
            handleException(e, con);
        } finally {
            doFinal(con, statement);
        }
        return maxId;
    }

    @Override
    public void handleException(final Exception e, final Connection con) {
        if (con != null) {
            try {
                log.error("Transaction is being rolled back", e);
                con.rollback();
            } catch (SQLException excep) {
                log.error(excep);
            }
        }
    }

    @Override
    public void doFinal(final Connection con, final PreparedStatement preparedStatement) {
        if (preparedStatement != null) {
            try {
                preparedStatement.close();
            } catch (SQLException ex) {
                log.error(ex);
            }
        }
        try {
            con.setAutoCommit(true);
        } catch (SQLException ex) {
            log.error("setAutoCommit(true)", ex);
        }
        if (con != null) {
            try {
                con.close();
            } catch (SQLException ex) {
                log.error(ex);
            }
        }
    }
}