Commit 849eb751 authored by Thomas Le Gluher's avatar Thomas Le Gluher

ajout DAOs

parent 03a64020
package db;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import model.Choix;
public class ChoixDAO implements DAO<Choix> {
private ArrayList<Choix> choix = new ArrayList<Choix>();
@Override
public ArrayList<Choix> getAll() {
try {
choix.clear();
Connection connection = ConnectionDB.getConnexion();
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT id, process, answer FROM choix");
while(rs.next()) {
this.choix.add(new Choix(rs.getInt("id"), rs.getInt("processs"), rs.getString("answer")));
}
} catch (SQLException e) {
e.printStackTrace();
}
return this.choix;
}
@Override
public Choix get(HashMap<String, String> params) {
try {
Connection connection = ConnectionDB.getConnexion();
String id = params.get("id");
String sql = "SELECT id, process, answer WHERE id = ?";
PreparedStatement st = connection.prepareStatement(sql);
st.setString(1, id);
ResultSet rs = st.executeQuery();
if(rs.next()){
Choix u = new Choix(rs.getInt("id"), rs.getInt("process"), rs.getString("answer"));
return u;
}
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
@Override
public void save(Choix t) {
Connection connection = ConnectionDB.getConnexion();
try {
String sql = "INSERT INTO choix (id, process, answer) "
+ "VALUES (?, ?, ?, ?, ?);";
PreparedStatement preparedStmt = connection.prepareStatement(sql);
preparedStmt.setInt(1, t.getId());
preparedStmt.setInt(2, t.getProcess());
preparedStmt.setString(3, t.getAnswer());
preparedStmt.execute();
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void update(Choix t, String[] params) {
// TODO Auto-generated method stub
}
@Override
public void delete(Choix t) {
// TODO Auto-generated method stub
}
}
package db;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import model.Parcours;
public class ParcoursDAO implements DAO<Parcours> {
private ArrayList<Parcours> parcours = new ArrayList<Parcours>();
@Override
public ArrayList<Parcours> getAll() {
try {
parcours.clear();
Connection connection = ConnectionDB.getConnexion();
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT id, user, questionnaire, score, duree FROM parcours");
while(rs.next()) {
this.parcours.add(new Parcours(rs.getInt("id"), rs.getInt("firstName"), rs.getInt("questionnaire"), rs.getInt("score"), rs.getInt("duree")));
}
} catch (SQLException e) {
e.printStackTrace();
}
return this.parcours;
}
@Override
public Parcours get(HashMap<String, String> params) {
try {
Connection connection = ConnectionDB.getConnexion();
String id = params.get("id");
String sql = "SELECT id, user, questionnaire, score, duree FROM parcours WHERE id = ?";
PreparedStatement st = connection.prepareStatement(sql);
st.setString(1, id);
ResultSet rs = st.executeQuery();
if(rs.next()){
Parcours u = new Parcours(rs.getInt("id"), rs.getInt("firstName"), rs.getInt("questionnaire"), rs.getInt("score"), rs.getInt("duree"));
return u;
}
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
@Override
public void save(Parcours t) {
Connection connection = ConnectionDB.getConnexion();
try {
String sql = "INSERT INTO parcours (id, user, questionnaire, score, duree) "
+ "VALUES (?, ?, ?, ?, ?);";
PreparedStatement preparedStmt = connection.prepareStatement(sql);
preparedStmt.setInt(1, t.getId());
preparedStmt.setInt(2, t.getUser());
preparedStmt.setInt(3, t.getQuestionnaire());
preparedStmt.setInt(4, t.getScore());
preparedStmt.setInt(5, t.getDuree());
preparedStmt.execute();
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void update(Parcours t, String[] params) {
// TODO Auto-generated method stub
}
@Override
public void delete(Parcours t) {
// TODO Auto-generated method stub
}
}
......@@ -85,30 +85,6 @@ public class QuestionnaireDAO implements DAO<Questionnaire> {
st = connection.prepareStatement(sql);
st.setString(1, id);
ArrayList<Question> questionList = new ArrayList<Question>();
ResultSet QuestionRS = st.executeQuery();
if(QuestionRS.next()) {
Question question = new Question(QuestionRS.getString("heading"),
QuestionRS.getBoolean("active"), QuestionRS.getInt("numorder"));
questionList.add(question);
sql = "SELECT id, intitule, correct, active FROM answer WHERE question = ?";
PreparedStatement answersStmt = connection.prepareStatement(sql);
answersStmt.setString(1, QuestionRS.getString("id"));
ResultSet answerRS = answersStmt.executeQuery();
ArrayList<Answer> answers = new ArrayList<Answer>();
while(answerRS.next()) {
answers.add(new Answer(answerRS.getInt("id"), answerRS.getString("intitule"), answerRS.getBoolean("correct"), answerRS.getBoolean("active")));
}
question.setAnswers(answers);
questionList.add(question);
sql = "SELECT id, heading, active, numorder FROM question WHERE questionnaire = ?";
st = connection.prepareStatement(sql);
st.setString(1, id);
ArrayList<Question> questionList = new ArrayList<Question>();
ResultSet QuestionRS = st.executeQuery();
if(QuestionRS.next()) {
sql = "SELECT id, intitule, correct, active FROM answer WHERE question = ?";
......
......@@ -135,214 +135,4 @@ public class UserDAO implements DAO<User> {
// TODO Auto-generated method stub
}
// @Override
// public List<User> getAll(HashMap<String, String> params) {
// ArrayList<User> result = new ArrayList<User>();
//
// Connection connection = ConfigConnexion.getConnexion();
//
// String nomTable = this.getNomTable();
//
// String sql = "SELECT mail, nom, prenom, societe, telephone, statut, date_creation, type, cree_par, modifie_par, date_modification " +
// "FROM "+nomTable;
// PreparedStatement st = null;
// try {
// st = connection.prepareStatement(sql);
// ResultSet row = st.executeQuery();
// while(row.next()){
// Optional<User> u = hydrater(row);
// if(u.isPresent()){
// result.add(u.get());
// }
// }
// } catch (SQLException e) {
// e.printStackTrace();
// }
//
// return result;
//
// }
//
// @Override
// public Optional<User> get(HashMap<String, String> params) {
// if(params.containsKey("mail")){
//
// Connection connection = ConfigConnexion.getConnexion();
// String nomTable = this.getNomTable();
//
// String mail = params.get("mail");
// String sql = "SELECT mail, nom, prenom, societe, telephone, statut, date_creation, type, cree_par, modifie_par, date_modification " +
// "FROM "+nomTable+" " +
// "WHERE mail = ? ";
//
// boolean mdp = false;
// if(params.containsKey("mot_de_passe")){
// sql += "AND mot_de_passe = ? ";
// mdp = true;
// }
//
// PreparedStatement st = null;
// try {
// st = connection.prepareStatement(sql);
// st.setString(1, mail);
//
// if(mdp){
// st.setString(2, params.get("mot_de_passe"));
// }
//
// ResultSet row = st.executeQuery();
// if(row.next()){
// Optional<User> u = hydrater(row);
// if(u.isPresent()){
// return u;
// }
// }
// } catch (SQLException e) {
// e.printStackTrace();
// }
//
// }
// return Optional.empty();
// }
//
// @Override
// public boolean create(User User) {
// Connection connection = ConfigConnexion.getConnexion();
// String nomTable = this.getNomTable();
// String sql = "INSERT INTO "+nomTable+" (mail, mot_de_passe, type, nom, prenom, societe, telephone, statut, date_creation, cree_par) " +
// "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
// PreparedStatement st = null;
// try {
// st = connection.prepareStatement(sql);
// st.setString(1, User.getmail());
// st.setString(2, User.getMotdepasse());
// st.setString(3, User.getType());
// st.setString(4, User.getNom());
// st.setString(5, User.getPrenom());
// st.setString(6, User.getSociete());
// st.setString(7, User.getTelephone());
// st.setBoolean(8, User.getStatut());
//
// Timestamp timestamp = Timestamp.valueOf(User.getDateCreation());
// st.setTimestamp(9, timestamp);
//
// st.setString(10, User.getCreePar().getmail());
//
// int inserted = st.executeUpdate();
// if (inserted > 0){
// return true;
// }
// } catch (SQLException e) {
// e.printStackTrace();
// }
// return false;
// }
//
// @Override
// public boolean update(User User) {
// Connection connection = ConfigConnexion.getConnexion();
// String nomTable = this.getNomTable();
// String sql = "UPDATE "+nomTable+" " +
// "SET mot_de_passe = ?, nom = ?, prenom = ?, societe = ?, telephone = ?, statut = ?, modifie_par = ?, date_modification = ? " +
// "WHERE mail = ?";
// PreparedStatement st = null;
// try {
// st = connection.prepareStatement(sql);
// st.setString(1, User.getMotdepasse());
// st.setString(2, User.getNom());
// st.setString(3, User.getPrenom());
// st.setString(4, User.getSociete());
// st.setString(5, User.getTelephone());
// st.setBoolean(6, User.getStatut());
// st.setString(7, User.getModifiePar().getmail());
//
// Timestamp timestamp = Timestamp.valueOf(User.getDateModification());
// st.setTimestamp(8, timestamp);
//
// st.setString(9, User.getmail());
//
// int updated = st.executeUpdate();
// if (updated > 0){
// return true;
// }
// } catch (SQLException e) {
// e.printStackTrace();
// }
// return false;
// }
//
// @Override
// public boolean delete(User User) {
// Connection connection = ConfigConnexion.getConnexion();
// String nomTable = this.getNomTable();
// String sql = "DELETE FROM "+nomTable+" " +
// "WHERE mail = ?";
// PreparedStatement st = null;
// try {
// st = connection.prepareStatement(sql);
// st.setString(1, User.getmail());
// int deleted = st.executeUpdate();
// if (deleted > 0){
// return true;
// }
// } catch (SQLException e) {
// e.printStackTrace();
// }
// return false;
// }
//
// @Override
// public String getNomTable() {
// return "User";
// }
//
// /**
// * Retourne un User hydraté par une ligne récupérée en base
// * @param row, la ligne utilisée pour hydrater l'objet User qui va être retourner
// * @return
// */
// private Optional<User> hydrater(ResultSet row){
// try {
// String mail = row.getString("mail");
// String type = row.getString("type");
// String nom = row.getString("nom");
// String prenom = row.getString("prenom");
// String societe = row.getString("societe");
// String telephone = row.getString("telephone");
// boolean statut = row.getBoolean("statut");
// LocalDateTime date_creation = row.getTimestamp("date_creation").toLocalDateTime();
// String cree_par = row.getString("cree_par");
//
// Timestamp date_modification_tmstp = row.getTimestamp("date_modification");
// LocalDateTime date_modification = null;
// if(date_modification_tmstp != null){
// date_modification = date_modification_tmstp.toLocalDateTime();
// }
//
// String modifie_par = row.getString("modifie_par");
//
// User u = null;
// if(type.equals("admin")){
// u = new Administrateur(mail);
// }else{
// u = new Stagiaire(mail);
// }
// u.setNom(nom);
// u.setPrenom(prenom);
// u.setSociete(societe);
// u.setTelephone(telephone);
// u.setStatut(statut);
// u.setDateCreation(date_creation);
// u.setCreePar(new Administrateur(cree_par));
// u.setDateCreation(date_modification);
// u.setModifiePar(new Administrateur(modifie_par));
// return Optional.of(u);
//
// } catch (SQLException e) {
// e.printStackTrace();
// }
// return Optional.empty();
// }
}
\ No newline at end of file
package model;
public class Choix {
private int id;
private int process;
private String answer;
public Choix(int id, int process, String answer) {
super();
this.id = id;
this.process = process;
this.answer = answer;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getProcess() {
return process;
}
public void setProcess(int process) {
this.process = process;
}
public String getAnswer() {
return answer;
}
public void setAnswer(String answer) {
this.answer = answer;
}
}
package model;
public class Parcours {
private int id;
private int user;
private int questionnaire;
private int score;
private int duree;
public Parcours(int id, int user, int questionnaire, int score, int duree) {
super();
this.id = id;
this.user = user;
this.questionnaire = questionnaire;
this.score = score;
this.duree = duree;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getUser() {
return user;
}
public void setUser(int user) {
this.user = user;
}
public int getQuestionnaire() {
return questionnaire;
}
public void setQuestionnaire(int questionnaire) {
this.questionnaire = questionnaire;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public int getDuree() {
return duree;
}
public void setDuree(int duree) {
this.duree = duree;
}
}
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