airport.database.dispatcher.airplane.FlyingMachineTypeDaoImpl.java Source code

Java tutorial

Introduction

Here is the source code for airport.database.dispatcher.airplane.FlyingMachineTypeDaoImpl.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 airport.database.dispatcher.airplane;

import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

/**
 *
 * @author mcdoker
 */
@Repository
public class FlyingMachineTypeDaoImpl implements FlyingMachineTypeDao {

    private final static Logger LOG = Logger.getLogger(FlyingMachineTypeDaoImpl.class);

    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;

    private final static String SQL_QUERY_SELECT_ONE = "SELECT type_machine, year_serial_release, wingspan, length, height, \n"
            + "       crew_amount, passenger_amount, cruising_speed, range_on_flight\n"
            + "  FROM flying_machine_type " + "WHERE type_machine = :typeMachine";

    private final static String SQL_QUERY_SELECT_RANDOM = "SELECT type_machine, year_serial_release, wingspan, length, height, \n"
            + "       crew_amount, passenger_amount, cruising_speed, range_on_flight\n"
            + "  FROM flying_machine_type" + " ORDER BY RANDOM()" + " LIMIT 1";

    @Override
    public FlyingMachineType getFlyingMachineType(String typeMachine) {
        MapSqlParameterSource parameter = new MapSqlParameterSource("typeMachine", typeMachine);
        FlyingMachineType result;

        try {
            result = jdbcTemplate.queryForObject(SQL_QUERY_SELECT_ONE, parameter, new FlyingMachineTypeMapper());
        } catch (EmptyResultDataAccessException e) {
            if (LOG.isInfoEnabled()) {
                LOG.info("Information on such type of the plane aren't present. Type plane : " + typeMachine);
            }

            return new FlyingMachineType();
        }

        if (LOG.isInfoEnabled()) {
            LOG.info("Information on such type of the plane are present. Type plane : " + typeMachine + ". Result "
                    + result);
        }

        return result;
    }

    @Override
    public FlyingMachineType getRandomFlyingMachineType() {
        FlyingMachineType result;

        result = jdbcTemplate.getJdbcOperations().queryForObject(SQL_QUERY_SELECT_RANDOM,
                new FlyingMachineTypeMapper());

        if (LOG.isInfoEnabled()) {
            LOG.info("get random type plane. Result : " + result);
        }

        return result;
    }

    private static class FlyingMachineTypeMapper implements RowMapper<FlyingMachineType> {

        @Override
        public FlyingMachineType mapRow(ResultSet rs, int rowNum) throws SQLException {
            FlyingMachineType result = new FlyingMachineType();

            result.setTypeMachine(rs.getString("type_machine"));
            result.setYearSerialRelease(rs.getInt("year_serial_release"));
            result.setWingspan(rs.getDouble("wingspan"));
            result.setLength(rs.getDouble("length"));
            result.setHeight(rs.getDouble("height"));
            result.setCrewAmount(rs.getInt("crew_amount"));
            result.setPassengerAmount(rs.getInt("passenger_amount"));
            result.setCruisingSpeed(rs.getInt("cruising_speed"));
            result.setRangeOnFlight(rs.getInt("range_on_flight"));

            return result;
        }

    }

}