konditer.client.dao.CakeTypeDao.java Source code

Java tutorial

Introduction

Here is the source code for konditer.client.dao.CakeTypeDao.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.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;
import javax.activation.DataSource;
import konditer.client.bean.CakeType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

/**
 *
 * @author 
 */
@Repository("cakeTypeDao")
public class CakeTypeDao implements CakeTypeDaoInterface {

    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 addCakeType(int cakeTypeId, String cakeTypeName) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

@Override
public CakeType getCakeType(int cakeTypeId) {
    String SQL_QUERY = "SELECT CAKE_TYPE_ID, CAKE_TYPE_NAME, TIMESTAMP "
                     + "FROM cake_types "
                     + "WHERE CAKE_TYPE_ID = ?";
    CakeType ?akeType = (CakeType) jdbcTemplate.queryForObject(SQL_QUERY,
            new Object[]{cakeTypeId},
            new BeanPropertyRowMapper(CakeType.class));
    return ?akeType;
    /*
    String SQL_QUERY = "SELECT CAKE_ID, CUSTOMER_ID, CAKE_PRICE, ORDER_ID, "
                            + "CAKE_NAME, CAKE_WEIGHT, CAKE_PHOTO_MINI, CAKE_PHOTO_MAXI, "
                            + "CAKE_KEYWORDS, CAKE_STATUS, TIMESTAMP "
                     + "FROM cakes "
                     + "WHERE CAKE_ID = ?";
    Cake cake = (Cake) jdbcTemplate.queryForObject(SQL_QUERY,
            new Object[]{cakeId},
            new BeanPropertyRowMapper(Cake.class));
    return cake;
    */
}

@Override
public CakeType getCakeTypeByName(String cakeTypeName) {
    String SQL_QUERY = "SELECT CAKE_TYPE_ID, CAKE_TYPE_NAME, TIMESTAMP "
                     + "FROM cake_types "
                     + "WHERE CAKE_TYPE_NAME = ?";
    CakeType ?akeType = (CakeType) jdbcTemplate.queryForObject(SQL_QUERY,
            new Object[]{cakeTypeName},
            new BeanPropertyRowMapper(CakeType.class));
    return ?akeType;
}

    @Override
    public ArrayList<CakeType> getAllCakeTypes() {
        String SQL_QUERY = "SELECT CAKE_TYPE_ID, CAKE_TYPE_NAME, TIMESTAMP "
                + "  FROM cake_types ORDER BY CAKE_TYPE_NAME";
        List<Map<String, Object>> rows = jdbcTemplate.queryForList(SQL_QUERY);
        ArrayList<CakeType> cakeTypeList = getCakeTypesList(rows);
        return cakeTypeList;
    }

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

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

    private ArrayList<CakeType> getCakeTypesList(List<Map<String, Object>> rows) {
        ArrayList<CakeType> cakeTypeList = new ArrayList<>();
        for (Map row : rows) {
            CakeType cakeType = new CakeType();
            cakeType.setCakeTypeId(Integer.parseInt(String.valueOf(row.get("CAKE_TYPE_ID"))));
            cakeType.setCakeTypeName(String.valueOf(row.get("CAKE_TYPE_NAME")));
            ////
            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]));
            cakeType.setTimestamp(new GregorianCalendar(year, month, day, hour, minut, (int) second).getTime());
            ///
            cakeTypeList.add(cakeType);
        }
        return cakeTypeList;
    }

    @Override
    public ArrayList<CakeType> getTimestamp() {
        String SQL_QUERY = "SELECT CAKE_TYPE_ID, TIMESTAMP " + "FROM cake_types ";
        List<Map<String, Object>> rows = jdbcTemplate.queryForList(SQL_QUERY);
        ArrayList<CakeType> cakeTypeList = new ArrayList<>();
        for (Map row : rows) {
            CakeType cakeType = new CakeType();
            cakeType.setCakeTypeId(Integer.parseInt(String.valueOf(row.get("CAKE_TYPE_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]));
            cakeType.setTimestamp(new GregorianCalendar(year, month, day, hour, minut, (int) second).getTime());
            ///
            cakeTypeList.add(cakeType);
        }
        return cakeTypeList;
    }

}