Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package be.esi.alg2.ebiblio.db;
- import be.esi.alg2.ebiblio.dto.EditionDto;
- import be.esi.alg2.ebiblio.dto.LecteurDto;
- import be.esi.alg2.ebiblio.dto.LivreDto;
- import be.esi.alg2.ebiblio.exception.EbiblioDbException;
- import be.esi.alg2.ebiblio.seldto.EditionSel;
- import be.esi.alg2.ebiblio.seldto.LivreSel;
- import be.esi.alg2.ebiblio.Main;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.Date;
- import java.util.List;
- /**
- * Classe d'accès au gestionnaire de persistance pour les Livres
- */
- public class LivreDB {
- public static Collection<LivreDto> getLivres(LivreSel sel) throws EbiblioDbException {
- List<LivreDto> al = new ArrayList<>();
- try {
- String query = "SELECT livId, dateAcquisition, empDepuis, retourPrevu, nbRappels, perdu, empruntable, edition, lecteur FROM Livre ";
- java.sql.Connection connexion = DBManager.getConnection();
- java.sql.PreparedStatement stmt;
- String where = "";
- if (sel.getLivId() != 0) {
- where = where + " livid = ? ";
- } else {
- if (sel.getEdition() != null ) {
- if (!where.equals("")) {
- where = where + " AND ";
- }
- where = where + " edition = ? ";
- }
- if (sel.getLecteur() != null ) {
- if (!where.equals("")) {
- where = where + " AND ";
- }
- where = where + " lecteur = ? ";
- }
- }
- if (where.length() != 0) {
- query = query + " WHERE "+where;
- stmt = connexion.prepareStatement(query);
- if (sel.getLivId() != 0) {
- stmt.setInt(1, sel.getLivId());
- } else {
- int i = 1;
- if (sel.getEdition()!=null) {
- stmt.setInt(i++, sel.getEdition().getId());
- }
- if (sel.getLecteur() != null) {
- stmt.setInt(i, sel.getLecteur().getId());
- }
- }
- } else {
- stmt = connexion.prepareStatement(query);
- }
- java.sql.ResultSet rs = stmt.executeQuery();
- while (rs.next()) {
- al.add(new LivreDto(rs.getInt("livId"), rs.getInt("edition"), rs.getBoolean("perdu"),
- rs.getBoolean("empruntable"), rs.getDate("empDepuis"), rs.getDate("retourPrevu"), rs.getInt("lecteur"), rs.getDate("dateAcquisition")));
- }
- } catch (java.sql.SQLException eSQL) {
- throw new EbiblioDbException("Instanciation de livre(s) impossible:\rSQLException: " + eSQL.getMessage());
- }
- return al;
- }
- public static LivreDto getLivre(int livId) throws EbiblioDbException {
- Collection<LivreDto> col = getLivres(new LivreSel(livId));
- if(!col.isEmpty())
- return col.iterator().next();
- else
- return null;
- }
- public static Collection<LivreDto> getLivresEmpruntePar(LecteurDto lecteur, Date dateRetard) throws EbiblioDbException {
- if (dateRetard != null) Main.erreurVolontaire(": la date n'est pas prise en compte!");
- List<LivreDto> al = new ArrayList<>();
- try {
- String query = "SELECT livId, dateAcquisition, empDepuis, retourPrevu, nbRappels, perdu, empruntable, edition, lecteur FROM Livre where lecteur = ? ";
- java.sql.Connection connexion = DBManager.getConnection();
- java.sql.PreparedStatement stmt;
- stmt = connexion.prepareStatement(query);
- stmt.setInt(1, lecteur.getId());
- java.sql.ResultSet rs = stmt.executeQuery();
- while (rs.next()) {
- LivreDto liv = new LivreDto(rs.getInt("livId"), rs.getInt("edition"), rs.getBoolean("perdu"),
- rs.getBoolean("empruntable"), rs.getDate("empDepuis"), rs.getDate("retourPrevu"), lecteur, rs.getDate("dateAcquisition"));
- liv.setEdition(EditionDB.getEditions(new EditionSel(liv.getEdId()), false).iterator().next());
- al.add(liv);
- }
- } catch (java.sql.SQLException eSQL) {
- throw new EbiblioDbException("Instanciation de livre(s) impossible:\rSQLException: " + eSQL.getMessage());
- }
- return al;
- }
- public static Collection<LivreDto> getLivresEdition(int edId) throws EbiblioDbException {
- return getLivres(new LivreSel(new EditionDto(edId, null, null, null,null)));
- }
- public static void retourLivre(LivreDto livre, LecteurDto lecteur) throws EbiblioDbException {
- try {
- java.sql.Connection connexion = DBManager.getConnection();
- int num = SequenceDB.getNumSuivant(Sequences.HISTORIQUE);
- String query = "INSERT INTO historique(hId, livre, lecteur, dateEmprunt, dateRetour) VALUES (?,?,?,?,?)";
- java.sql.PreparedStatement update = connexion.prepareStatement(query);
- Date d = new Date();
- update.setInt(1, num);
- update.setInt(2, livre.getId());
- update.setInt(3, lecteur.getId());
- update.setDate(4, new java.sql.Date(livre.getEmpDepuis().getTime()));
- update.setDate(5, new java.sql.Date(d.getTime()));
- update.execute();
- query = "UPDATE Livre SET lecteur=null, empDepuis=null, nbRappels=null, retourPrevu=null WHERE livId = ? ";
- update = connexion.prepareStatement(query);
- update.setInt(1, livre.getId());
- update.execute();
- } catch (SQLException ex) {
- throw new EbiblioDbException("Retour de livre impossible:\rSQLException: " + ex.getMessage());
- }
- }
- public static void emprunte(int livid, int lecid, Date depuis, Date jusque) throws EbiblioDbException {
- try {
- java.sql.Connection connexion = DBManager.getConnection();
- String query = "UPDATE Livre SET lecteur = ?, empDepuis=?, nbRappels=0, retourPrevu=? WHERE livId = ? ";
- java.sql.PreparedStatement update = connexion.prepareStatement(query);
- update.setInt(1, lecid);
- update.setDate(2, new java.sql.Date(depuis.getTime()));
- update.setDate(3, new java.sql.Date(jusque.getTime()));
- update.setInt(4, livid);
- update.execute();
- } catch (SQLException ex) {
- throw new EbiblioDbException("Impossible de noter l'emprunt \n" + ex.getMessage());
- }
- }
- public static Collection<LecteurDto> getLecteursRetard(Date dateRetard) throws EbiblioDbException {
- Main.erreurVolontaire("Recherche des lecteurs avec retard non implémentée");
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement