import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.sql.PreparedStatement;
import java.sql.Timestamp;
@Repository("tripDao")
public class TripRepository implements TripDAO {
private final JdbcTemplate jdbcTemplate;
private final String QUERY_SAVE = "INSERT into drier.t_trip(trip_id, stop_id, stop_sequence, arrival_time, departure_time, created, updated, status) VALUES (?, ?, ?, ?, ?, ?, ?, ?);";
@Autowired
public TripRepository(
JdbcTemplate jdbcTemplate
) {
this.jdbcTemplate = jdbcTemplate;
}
@Override
public void saveAll(List<Trip> entity, int batchSize) {
jdbcTemplate.batchUpdate(QUERY_SAVE,
new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps, int i) throws SQLException {
ps.setString(1, entity.get(i).getTripId());
ps.setLong(2, entity.get(i).getStopId());
ps.setInt(3, entity.get(i).getStopSequence());
ps.setTimestamp(4, new Timestamp(entity.get(i).getArrivalTime().getTime()));
ps.setTimestamp(5, new Timestamp(entity.get(i).getDepartureTime().getTime()));
ps.setTimestamp(6, new Timestamp(entity.get(i).getCreated().getTime()));
ps.setTimestamp(7, new Timestamp(entity.get(i).getUpdated().getTime()));
ps.setBoolean(8, entity.get(i).getStatus());
}
@Override
public int getBatchSize() {
return batchSize;
}
});
}
}
@Override
public void save(Trip entity) {
jdbcTemplate.update(QUERY_SAVE,
entity.getTripId(),
entity.getStopId(),
entity.getStopSequence(),
new Timestamp(entity.getArrivalTime().getTime()),
new Timestamp(entity.getDepartureTime().getTime()),
new Timestamp(entity.getCreated().getTime()),
new Timestamp(entity.getUpdated().getTime()),
entity.getStatus()
);
}
@Override
public void deleteAll() {
String sql = "DELETE FROM drier.t_trip WHERE TRUE=TRUE;";
jdbcTemplate.execute(sql);
}
public interface EntityDAO<E,K> {
void save(E entity);
void saveAll(List<E> entity, int batchSize);
List<E> findAll();
E findById(K id);
E update (E entity);
void deleteById(K id);
void delete(E entity);
void deleteAll();
}
@Override
public List<RouteDrier> findAll() {
String sql = "SELECT * FROM drier.t_route";
return jdbcTemplate.query(sql, new RouteDrierMapper());
}
public Station findById(Long id) {
String sql = "SELECT * FROM public.t_station WHERE id = ?;";
try {
return (Station) jdbcTemplate.queryForObject(sql,
new Object[]{id},
new StationMapper());
} catch (EmptyResultDataAccessException e) {
return null;
}
}
private class RouteDrierMapper implements RowMapper {
@Override
public RouteDrier mapRow(ResultSet rs, int rowNum) throws SQLException {
RouteDrier routeDrier = new RouteDrier();
routeDrier.setId(rs.getLong("id"));
routeDrier.setRouteShortName(rs.getString("route_short_name"));
routeDrier.setRouteLongName(rs.getString("route_long_name"));
routeDrier.setRouteType(rs.getInt("route_type"));
routeDrier.setCreated(rs.getTimestamp("created"));
routeDrier.setUpdated(rs.getTimestamp("updated"));
routeDrier.setStatus(rs.getBoolean("status"));
return routeDrier;
}
}