konditer.client.dao.CustomerDao.java Source code

Java tutorial

Introduction

Here is the source code for konditer.client.dao.CustomerDao.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 konditer.client.dao;

import java.sql.Types;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.activation.DataSource;
import static konditer.client.Main.customerDao;
import konditer.client.bean.Customer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

/**
 *
 * @author 
 */
@Repository("customerDao")
public class CustomerDao implements CustomerDaoInterface {

    private DataSource dataSource = null;
    private JdbcTemplate jdbcTemplate = null;

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    @Autowired
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public void addCustomer(int customerId, int discountId, String customerLastName, String customerFirstName,
            String customerParentName, Date customerDateBorn, String customerInfo) {
        String SQL_QUERY = "INSERT INTO customers ( CUSTOMER_ID, " + "DISCOUNT_ID, " + "CUSTOMER_LAST_NAME, "
                + "CUSTOMER_FIRST_NAME, " + "CUSTOMER_PARENT_NAME, " + "CUSTOMER_DATE_BORN, " + "CUSTOMER_INFO ) "
                + "VALUES (?,?,?,?,?,?,?)";
        int rowCount = 0;
        try {
            rowCount = jdbcTemplate.update(SQL_QUERY,
                    new Object[] { customerId, discountId, customerLastName, customerFirstName, customerParentName,
                            customerDateBorn, customerInfo },
                    new int[] { Types.INTEGER, Types.INTEGER, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR,
                            Types.TIMESTAMP, Types.VARCHAR });
            Logger.getLogger(CustomerDao.class.getName()).log(Level.SEVERE,
                    " : {0} .", rowCount);
            System.out.println(customerDao.getCustomer(customerId).toString());
        } catch (DataAccessException e) {
            rowCount = 0;
            Logger.getLogger(CustomerDao.class.getName()).log(Level.SEVERE,
                    "    .  ?: {0} .",
                    rowCount);
        }
    }

    @Override
    public void addCustomer(int customerId, int discountId, String customerFirstName) {
        String SQL_QUERY = "INSERT INTO customers ( CUSTOMER_ID, " + "DISCOUNT_ID, " + "CUSTOMER_LAST_NAME, "
                + "CUSTOMER_FIRST_NAME, " + "CUSTOMER_PARENT_NAME, " + "CUSTOMER_DATE_BORN, " + "CUSTOMER_INFO ) "
                + "VALUES (?,?,?,?,?,?,?)";
        int rowCount = 0;
        try {
            rowCount = jdbcTemplate.update(SQL_QUERY,
                    new Object[] { customerId, discountId, "", customerFirstName, "", new Date(), "" },
                    new int[] { Types.INTEGER, Types.INTEGER, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR,
                            Types.TIMESTAMP, Types.VARCHAR });
            Logger.getLogger(CustomerDao.class.getName()).log(Level.SEVERE,
                    " : {0} .",
                    rowCount + "\n" + customerDao.getCustomer(customerId).toString());
        } catch (DataAccessException e) {
            rowCount = 0;
            Logger.getLogger(CustomerDao.class.getName()).log(Level.SEVERE,
                    "    .  ?: {0} .",
                    rowCount);
        }
    }

    @Override
    public void addCustomer(int customerId, String customerFirstName, String customerDopInfo) {
        String SQL_QUERY = "INSERT INTO customers ( CUSTOMER_ID, " + "DISCOUNT_ID, " + "CUSTOMER_LAST_NAME, "
                + "CUSTOMER_FIRST_NAME, " + "CUSTOMER_PARENT_NAME, " + "CUSTOMER_DATE_BORN, " + "CUSTOMER_INFO ) "
                + "VALUES (?,?,?,?,?,?,?)";
        int rowCount = 0;
        try {
            rowCount = jdbcTemplate.update(SQL_QUERY,
                    new Object[] { customerId, 1, "", customerFirstName, "", new Date(), customerDopInfo },
                    new int[] { Types.INTEGER, Types.INTEGER, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR,
                            Types.TIMESTAMP, Types.VARCHAR });
            Logger.getLogger(CustomerDao.class.getName()).log(Level.SEVERE,
                    " : {0} .",
                    rowCount + "\n" + customerDao.getCustomer(customerId).toString());
        } catch (DataAccessException e) {
            rowCount = 0;
            Logger.getLogger(CustomerDao.class.getName()).log(Level.SEVERE,
                    "    .  ?: {0} .",
                    rowCount);
        }
    }

    @Override
    public Customer getCustomer(int customerId) {
        String SQL_QUERY = "SELECT CUSTOMER_ID, " + "DISCOUNT_ID, " + "CUSTOMER_LAST_NAME, "
                + "CUSTOMER_FIRST_NAME, " + "CUSTOMER_PARENT_NAME, " + "CUSTOMER_DATE_BORN, " + "CUSTOMER_INFO, "
                + "TIMESTAMP " + "FROM customers " + "WHERE CUSTOMER_ID = ?";
        Customer customer = (Customer) jdbcTemplate.queryForObject(SQL_QUERY, new Object[] { customerId },
                new BeanPropertyRowMapper(Customer.class));
        return customer;
    }

    @Override
    public ArrayList<Customer> getAllCustomers() {
        String SQL_QUERY = "SELECT CUSTOMER_ID, " + "DISCOUNT_ID, " + "CUSTOMER_LAST_NAME, "
                + "CUSTOMER_FIRST_NAME, " + "CUSTOMER_PARENT_NAME, " + "CUSTOMER_DATE_BORN, " + "CUSTOMER_INFO, "
                + "TIMESTAMP " + "FROM customers " + "ORDER BY CUSTOMER_FIRST_NAME ASC ";
        List<Map<String, Object>> rows = jdbcTemplate.queryForList(SQL_QUERY);
        ArrayList<Customer> customerList = getCustomersList(rows);
        return customerList;
    }

    @Override
    public ArrayList<Customer> getAllCustomers(String searchValue) {
        String SQL_QUERY = searchValue;
        List<Map<String, Object>> rows = jdbcTemplate.queryForList(SQL_QUERY);
        ArrayList<Customer> customerList = getCustomersList(rows);
        return customerList;
    }

    @Override
    public ArrayList<Customer> getAllCustomersByDiscountId(int discountId) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public ArrayList<Customer> searchAllCustomers(String sqlQuery) {
        String SQL_QUERY = sqlQuery;
        List<Map<String, Object>> rows = jdbcTemplate.queryForList(SQL_QUERY);
        ArrayList<Customer> customerList = getCustomersList(rows);
        return customerList;
    }

    @Override
    public int updateCustomer(int customerId, int newDiscountId, String newCustomerLastName,
            String newCustomerFirstName, String newCustomerParentName, Date newCustomerDateBorn,
            String newCustomerInfo) {
        System.out.println("  CustomerDao.updateCustomer c : newCustomerDateBorn="
                + newCustomerDateBorn);
        String SQL_QUERY = "UPDATE customers " + "SET DISCOUNT_ID = ?, " + "CUSTOMER_LAST_NAME = ?, "
                + "CUSTOMER_FIRST_NAME = ?, " + "CUSTOMER_PARENT_NAME = ?, " + "CUSTOMER_DATE_BORN = ?, "
                + "CUSTOMER_INFO = ? " + "WHERE CUSTOMER_ID = ?";
        int rowCount = 0;
        try {
            rowCount = jdbcTemplate.update(SQL_QUERY,
                    new Object[] { newDiscountId, newCustomerLastName, newCustomerFirstName, newCustomerParentName,
                            newCustomerDateBorn, newCustomerInfo, customerId },
                    new int[] { Types.INTEGER, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.TIMESTAMP,
                            Types.VARCHAR, Types.INTEGER });
            Logger.getLogger(CustomerDao.class.getName()).log(Level.SEVERE,
                    "   .  ?: {0} .",
                    rowCount + "\n" + customerDao.getCustomer(customerId).toString());
        } catch (DataAccessException e) {
            rowCount = 0;
            Logger.getLogger(CustomerDao.class.getName()).log(Level.SEVERE,
                    "     .  ?: {0} .",
                    rowCount);
        }
        return rowCount;
    }

    @Override
    public int deleteCustomer(int customerId) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    private ArrayList<Customer> getCustomersList(List<Map<String, Object>> rows) {
        ArrayList<Customer> customerList = new ArrayList<>();
        for (Map row : rows) {
            Customer customer = new Customer();
            customer.setCustomerId(Integer.parseInt(String.valueOf(row.get("CUSTOMER_ID"))));
            customer.setDiscountId(Integer.parseInt(String.valueOf(row.get("DISCOUNT_ID"))));
            customer.setCustomerLastName(String.valueOf(row.get("CUSTOMER_LAST_NAME")));
            customer.setCustomerFirstName(String.valueOf(row.get("CUSTOMER_FIRST_NAME")));
            customer.setCustomerParentName(String.valueOf(row.get("CUSTOMER_PARENT_NAME")));
            ///
            String[] dateArr = (String.valueOf(row.get("CUSTOMER_DATE_BORN"))).substring(0, 10).split("-");
            int year = Integer.parseInt(String.valueOf(dateArr[0]))/*-1900*/;
            int month = Integer.parseInt(String.valueOf(dateArr[1])) - 1;
            int day = Integer.parseInt(String.valueOf(dateArr[2]));
            customer.setCustomerDateBorn(new GregorianCalendar(year, month, day).getTime());
            ///
            customer.setCustomerInfo(String.valueOf(row.get("CUSTOMER_INFO")));
            ////
            String[] dateArrFull = (String.valueOf(row.get("TIMESTAMP"))).split(" ");
            String[] dateArrDate = dateArrFull[0].split("-");
            year = Integer.parseInt(String.valueOf(dateArrDate[0]))/*-1900*/;
            month = Integer.parseInt(String.valueOf(dateArrDate[1])) - 1;
            day = Integer.parseInt(String.valueOf(dateArrDate[2]));
            String[] dateArrTime = dateArrFull[1].split(":");
            int hour = Integer.parseInt(String.valueOf(dateArrTime[0]));
            int minut = Integer.parseInt(String.valueOf(dateArrTime[1]));
            double second = Double.parseDouble(String.valueOf(dateArrTime[2]));
            customer.setTimestamp(new GregorianCalendar(year, month, day, hour, minut, (int) second).getTime());
            ///
            customerList.add(customer);
        }
        return customerList;
    }

    @Override
    public ArrayList<Customer> getTimestamp() {
        String SQL_QUERY = "SELECT CUSTOMER_ID, TIMESTAMP " + "FROM customers ";
        List<Map<String, Object>> rows = jdbcTemplate.queryForList(SQL_QUERY);
        ArrayList<Customer> customerList = new ArrayList<>();
        for (Map row : rows) {
            Customer customer = new Customer();
            customer.setCustomerId(Integer.parseInt(String.valueOf(row.get("CUSTOMER_ID"))));
            ////
            String[] dateArrFull = (String.valueOf(row.get("TIMESTAMP"))).split(" ");
            String[] dateArrDate = dateArrFull[0].split("-");
            int year = Integer.parseInt(String.valueOf(dateArrDate[0]))/*-1900*/;
            int month = Integer.parseInt(String.valueOf(dateArrDate[1])) - 1;
            int day = Integer.parseInt(String.valueOf(dateArrDate[2]));
            String[] dateArrTime = dateArrFull[1].split(":");
            int hour = Integer.parseInt(String.valueOf(dateArrTime[0]));
            int minut = Integer.parseInt(String.valueOf(dateArrTime[1]));
            double second = Double.parseDouble(String.valueOf(dateArrTime[2]));
            customer.setTimestamp(new GregorianCalendar(year, month, day, hour, minut, (int) second).getTime());
            ///
            customerList.add(customer);
        }
        return customerList;
    }
}