Commit a6c6bf66 authored by Maya RAHAL's avatar Maya RAHAL
Browse files

jdbc templates done

parent b17f979d
......@@ -51,7 +51,6 @@ public final class InternDao {
@Autowired
private InternDao(InternRowMapper mapper, NamedParameterJdbcTemplate template) {
this.mapper = mapper;
this.template = template;
}
......@@ -68,19 +67,23 @@ public final class InternDao {
public List<Intern> getListInternSort(int currentPageNumber, int nbPerPage, SortParameter parameter, SortPattern pattern){
String query = null;
switch (pattern) {
case ASC :
query = LIST_INTERNS_SORTED_ASC_QUERY.substring(0, 73) + " ORDER BY " + parameter.getValue() + LIST_INTERNS_SORTED_ASC_QUERY.substring(73);
query = LIST_INTERNS_SORTED_ASC_QUERY.substring(0, 73) + " ORDER BY "
+ parameter.getValue() + LIST_INTERNS_SORTED_ASC_QUERY.substring(73);
break;
case DESC :
query = LIST_INTERNS_SORTED_DESC_QUERY.substring(0, 73) + " ORDER BY " + parameter.getValue() + LIST_INTERNS_SORTED_DESC_QUERY.substring(73);
query = LIST_INTERNS_SORTED_DESC_QUERY.substring(0, 73) + " ORDER BY "
+ parameter.getValue() + LIST_INTERNS_SORTED_DESC_QUERY.substring(73);
break;
}
List<Intern> internList = this.template.query(query, mapper);
int currentOffset = (currentPageNumber - 1)* nbPerPage;
SqlParameterSource namedParameters = new MapSqlParameterSource().addValue("offset", currentOffset).
addValue("limit", nbPerPage);
List<Intern> internList = this.template.query(query, namedParameters, mapper);
return internList;
}
......@@ -89,7 +92,7 @@ public final class InternDao {
int currentOffset = (currentPageNumber - 1)* nbPerPage;
SqlParameterSource namedParameters = new MapSqlParameterSource().addValue("offset", currentOffset).
addValue("limit", nbPerPage).addValue("search", search);
addValue("limit", nbPerPage).addValue("search", "%" + search + "%");
List<Intern> internList = this.template.query(LIST_INTERNS_FILTERED_QUERY, namedParameters, mapper);
return internList;
}
......@@ -104,7 +107,7 @@ public final class InternDao {
public int getTotalInternsFiltered(String search){
SqlParameterSource namedParameters = new MapSqlParameterSource().addValue("search", search);
SqlParameterSource namedParameters = new MapSqlParameterSource().addValue("search", "%" + search + "%");
int total = template.queryForObject(TOTAL_INTERNS_FILTERED_QUERY, namedParameters, Integer.class);
return total;
}
......@@ -120,7 +123,7 @@ public final class InternDao {
SqlParameterSource namedParameters = new MapSqlParameterSource().addValue("id", intern.getInternId()).
addValue("first_name", intern.getFirstName()).addValue("last_name", intern.getLastName()).
addValue("arrival", intern.getDateBegin()).addValue("fin_fomation", intern.getDateEnd()).
addValue("arrival", intern.getDateBegin()).addValue("formation_over", intern.getDateEnd()).
addValue("promotion_id", intern.getPromotion().getPromId());
template.update(ADD_INTERN_QUERY, namedParameters);
}
......@@ -130,7 +133,7 @@ public final class InternDao {
SqlParameterSource namedParameters = new MapSqlParameterSource().addValue("id", intern.getInternId()).
addValue("first_name", firstName).addValue("last_name", lastName).addValue("arrival", dateBegin).
addValue("fin_fomation", dateEnd).addValue("promotion_id", promotionId);
addValue("formation_over", dateEnd).addValue("promotion_id", promotionId);
template.update(EDIT_INTERN_QUERY, namedParameters);
}
......
......@@ -10,8 +10,15 @@ import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;
import mapper.InternRowMapper;
import mapper.PromotionMapperFromDb;
import mapper.PromotionRowMapper;
import model.Intern;
import model.Promotion;
import persistence.DataSource;
......@@ -19,41 +26,27 @@ import persistence.DataSource;
public class PromotionDao {
private static Logger logger = LoggerFactory.getLogger(PromotionDao.class);
private DataSource dataSource;
private PromotionMapperFromDb mapper;
private PromotionRowMapper mapper;
private NamedParameterJdbcTemplate template;
private static String LIST_PROMOTIONS_QUERY = "SELECT * FROM promotion";
private static String DETAILS_PROMOTION_QUERY = "SELECT * FROM promotion WHERE id = ?";
private static String DETAILS_PROMOTION_QUERY = "SELECT * FROM promotion WHERE id = :id";
@Autowired
private PromotionDao(DataSource dataSource, PromotionMapperFromDb mapper) {
this.dataSource = dataSource;
private PromotionDao(PromotionRowMapper mapper, NamedParameterJdbcTemplate template) {
this.template = template;
this.mapper = mapper;
}
public List<Promotion> getListPromotions(){
String query = LIST_PROMOTIONS_QUERY;
try (Connection connection = dataSource.getConnection() ; PreparedStatement prepState = connection.prepareStatement(query)) {
logger.info("Connexion établie ; préparation de la requête SQL pour la liste de promos");
ResultSet resultSet = prepState.executeQuery();
logger.info("Requête SQL pour la liste de promos réussie");
List<Promotion> promList = mapper.mapPromotionList(resultSet);
return promList;
} catch (SQLException e) {
logger.error("Requête SQL pour la liste de promos non aboutie");
return new ArrayList<>();
}
List<Promotion> promotionList = this.template.query(LIST_PROMOTIONS_QUERY, mapper);
return promotionList;
}
public Optional<Promotion> getDetailsPromotion(int id) {
return Optional.of(new Promotion(id, "test"));
SqlParameterSource namedParameters = new MapSqlParameterSource().addValue("id", id);
Promotion promotion = template.queryForObject(DETAILS_PROMOTION_QUERY, namedParameters, mapper);
return Optional.of(promotion);
}
}
package mapper;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;
import dao.PromotionDao;
import model.Intern;
import model.Intern.InternBuilder;
import model.Promotion;
@Component
public class PromotionRowMapper implements RowMapper<Promotion> {
@Autowired
public PromotionRowMapper() {
}
@Override
public Promotion mapRow(ResultSet rs, int rowNum) throws SQLException {
int id = rs.getInt("id");
String name = rs.getString("name");
Promotion promotion = new Promotion(id, name);
return promotion;
}
}
\ No newline at end of file
......@@ -59,12 +59,13 @@ public class ListInternsServlet extends HttpServlet {
int currentPage = request.getParameter("page") == null ? 1 : Integer.parseInt(request.getParameter("page"));
int totalLines = instance.totalInternsFilteredService(search);
Pages page = new Pages(nbLines, totalLines);
System.out.print(totalLines);
page.setCurrentPage(currentPage);
List<Intern> listInterns = instance.listInternsFilteredService(page.getCurrentPage(), nbLines, search);
request.setAttribute("search", search);
request.setAttribute("page", page);
request.setAttribute("nbPerPage",nbLines);
request.setAttribute("nbPerPage", nbLines);
request.setAttribute("listInterns", listInterns);
}
......@@ -80,7 +81,7 @@ public class ListInternsServlet extends HttpServlet {
List<Intern> listInterns = instance.listInternsService(page.getCurrentPage(), nbLines);
request.setAttribute("page", page);
request.setAttribute("nbPerPage",nbLines);
request.setAttribute("nbPerPage", nbLines);
request.setAttribute("listInterns", listInterns);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment