Advertisement
Guest User

Untitled

a guest
Apr 1st, 2015
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.89 KB | None | 0 0
  1. package be.esi.alg2.ebiblio.db;
  2.  
  3. import be.esi.alg2.ebiblio.dto.EditionDto;
  4. import be.esi.alg2.ebiblio.dto.LecteurDto;
  5. import be.esi.alg2.ebiblio.dto.LivreDto;
  6. import be.esi.alg2.ebiblio.exception.EbiblioDbException;
  7. import be.esi.alg2.ebiblio.seldto.EditionSel;
  8. import be.esi.alg2.ebiblio.seldto.LivreSel;
  9. import be.esi.alg2.ebiblio.Main;
  10. import java.sql.SQLException;
  11. import java.util.ArrayList;
  12. import java.util.Collection;
  13. import java.util.Date;
  14. import java.util.List;
  15.  
  16. /**
  17.  * Classe d'accès au gestionnaire de persistance pour les Livres
  18.  */
  19. public class LivreDB {
  20.  
  21.     public static Collection<LivreDto> getLivres(LivreSel sel) throws EbiblioDbException {
  22.         List<LivreDto> al = new ArrayList<>();
  23.         try {
  24.             String query = "SELECT livId, dateAcquisition, empDepuis, retourPrevu, nbRappels, perdu, empruntable, edition, lecteur  FROM Livre ";
  25.             java.sql.Connection connexion = DBManager.getConnection();
  26.             java.sql.PreparedStatement stmt;
  27.             String where = "";
  28.  
  29.             if (sel.getLivId() != 0) {
  30.                 where = where + " livid = ? ";
  31.             } else {
  32.                 if (sel.getEdition() != null ) {
  33.                     if (!where.equals("")) {
  34.                         where = where + " AND ";
  35.                     }
  36.                     where = where + " edition = ? ";
  37.                 }
  38.  
  39.                 if (sel.getLecteur() != null ) {
  40.                     if (!where.equals("")) {
  41.                         where = where + " AND ";
  42.                     }
  43.                     where = where + " lecteur = ? ";
  44.                 }
  45.             }
  46.  
  47.             if (where.length() != 0) {
  48.                 query = query + " WHERE "+where;
  49.                 stmt = connexion.prepareStatement(query);
  50.                 if (sel.getLivId() != 0) {
  51.                     stmt.setInt(1, sel.getLivId());
  52.                 } else {
  53.                     int i = 1;
  54.  
  55.                     if (sel.getEdition()!=null) {
  56.                         stmt.setInt(i++, sel.getEdition().getId());
  57.                     }
  58.  
  59.                     if (sel.getLecteur() != null) {
  60.                         stmt.setInt(i, sel.getLecteur().getId());
  61.                     }
  62.                 }
  63.             } else {
  64.                 stmt = connexion.prepareStatement(query);
  65.             }
  66.  
  67.             java.sql.ResultSet rs = stmt.executeQuery();
  68.             while (rs.next()) {
  69.                 al.add(new LivreDto(rs.getInt("livId"), rs.getInt("edition"), rs.getBoolean("perdu"),
  70.                         rs.getBoolean("empruntable"), rs.getDate("empDepuis"), rs.getDate("retourPrevu"), rs.getInt("lecteur"), rs.getDate("dateAcquisition")));
  71.             }
  72.         } catch (java.sql.SQLException eSQL) {
  73.             throw new EbiblioDbException("Instanciation de livre(s) impossible:\rSQLException: " + eSQL.getMessage());
  74.         }
  75.         return al;
  76.     }
  77.  
  78.     public static LivreDto getLivre(int livId) throws EbiblioDbException {
  79.         Collection<LivreDto> col = getLivres(new LivreSel(livId));
  80.         if(!col.isEmpty())
  81.             return col.iterator().next();
  82.         else
  83.             return null;
  84.     }
  85.  
  86.     public static Collection<LivreDto> getLivresEmpruntePar(LecteurDto lecteur, Date dateRetard) throws EbiblioDbException {
  87.  
  88.         if (dateRetard != null) Main.erreurVolontaire(": la date n'est pas prise en compte!");
  89.         List<LivreDto> al = new ArrayList<>();
  90.         try {
  91.             String query = "SELECT livId, dateAcquisition, empDepuis, retourPrevu, nbRappels, perdu, empruntable, edition, lecteur FROM Livre where lecteur = ? ";
  92.             java.sql.Connection connexion = DBManager.getConnection();
  93.             java.sql.PreparedStatement stmt;
  94.             stmt = connexion.prepareStatement(query);
  95.             stmt.setInt(1, lecteur.getId());
  96.  
  97.             java.sql.ResultSet rs = stmt.executeQuery();
  98.  
  99.             while (rs.next()) {
  100.                 LivreDto liv = new LivreDto(rs.getInt("livId"), rs.getInt("edition"), rs.getBoolean("perdu"),
  101.                                rs.getBoolean("empruntable"), rs.getDate("empDepuis"), rs.getDate("retourPrevu"), lecteur, rs.getDate("dateAcquisition"));
  102.                 liv.setEdition(EditionDB.getEditions(new EditionSel(liv.getEdId()), false).iterator().next());
  103.                 al.add(liv);
  104.             }
  105.         } catch (java.sql.SQLException eSQL) {
  106.             throw new EbiblioDbException("Instanciation de livre(s) impossible:\rSQLException: " + eSQL.getMessage());
  107.         }
  108.         return al;
  109.     }
  110.  
  111.     public static Collection<LivreDto> getLivresEdition(int edId) throws EbiblioDbException {
  112.         return getLivres(new LivreSel(new EditionDto(edId, null, null, null,null)));
  113.     }
  114.  
  115.     public static void retourLivre(LivreDto livre, LecteurDto lecteur) throws EbiblioDbException {
  116.         try {
  117.             java.sql.Connection connexion = DBManager.getConnection();
  118.             int num = SequenceDB.getNumSuivant(Sequences.HISTORIQUE);
  119.             String query = "INSERT INTO historique(hId, livre, lecteur, dateEmprunt, dateRetour) VALUES (?,?,?,?,?)";
  120.             java.sql.PreparedStatement update = connexion.prepareStatement(query);
  121.             Date d = new Date();
  122.             update.setInt(1, num);
  123.             update.setInt(2, livre.getId());
  124.             update.setInt(3, lecteur.getId());
  125.             update.setDate(4, new java.sql.Date(livre.getEmpDepuis().getTime()));
  126.             update.setDate(5, new java.sql.Date(d.getTime()));
  127.             update.execute();
  128.  
  129.             query = "UPDATE Livre SET lecteur=null, empDepuis=null, nbRappels=null, retourPrevu=null WHERE livId = ? ";
  130.  
  131.             update = connexion.prepareStatement(query);
  132.             update.setInt(1, livre.getId());
  133.             update.execute();
  134.         } catch (SQLException ex) {
  135.             throw new EbiblioDbException("Retour de livre impossible:\rSQLException: " + ex.getMessage());
  136.         }
  137.     }
  138.  
  139.     public static void emprunte(int livid, int lecid, Date depuis, Date jusque) throws EbiblioDbException {
  140.         try {
  141.             java.sql.Connection connexion = DBManager.getConnection();
  142.             String query = "UPDATE Livre SET lecteur = ?, empDepuis=?, nbRappels=0, retourPrevu=? WHERE livId = ? ";
  143.             java.sql.PreparedStatement update = connexion.prepareStatement(query);
  144.             update.setInt(1, lecid);
  145.             update.setDate(2, new java.sql.Date(depuis.getTime()));
  146.             update.setDate(3, new java.sql.Date(jusque.getTime()));
  147.             update.setInt(4, livid);
  148.             update.execute();
  149.         } catch (SQLException ex) {
  150.             throw new EbiblioDbException("Impossible de noter l'emprunt \n" + ex.getMessage());
  151.         }
  152.     }
  153.  
  154.     public static Collection<LecteurDto> getLecteursRetard(Date dateRetard) throws EbiblioDbException {
  155.         Main.erreurVolontaire("Recherche des lecteurs avec retard non implémentée");
  156.         return null;
  157.     }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement