package com.pat.GestHairHome.dao; import java.util.List; import javax.ejb.Stateless; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.TypedQuery; import com.pat.GestHairHome.entities.Client; import com.pat.GestHairHome.entities.Prestation; import com.pat.GestHairHome.entities.Rendezvous; @Stateless public class RendezvousDAO { private static final String JPQL_SELECT_PAR_IDUTILISATEUR = "SELECT r FROM Rendezvous r WHERE r.idUtilisateur=:idUtilisateur ORDER BY r.id"; private static final String PARAM_IDUTILISATEUR = "idUtilisateur"; // Injection du manager, qui s'occupe de la connexion avec la BDD @PersistenceContext( unitName = "gesthair_PU" ) private EntityManager em; public void creer(Rendezvous rendezvous) throws DAOException { try { em.persist(rendezvous); } catch (Exception e) { throw new DAOException (e); } } public void editer (Rendezvous rendezvous, int ID) throws DAOException { Rendezvous rendezvousbdd = this.trouver(ID); try { em.persist(rendezvousbdd); if ( rendezvousbdd.getIdClient() != rendezvous.getIdClient()) { rendezvousbdd.setIdClient(rendezvous.getIdClient()); } if (! rendezvousbdd.getDateHeure().equals(rendezvous.getDateHeure())) { rendezvousbdd.setDateHeure(rendezvous.getDateHeure()); } if (rendezvousbdd.getIdPrestation1() != rendezvous.getIdPrestation1()) { rendezvousbdd.setIdPrestation1(rendezvous.getIdPrestation1()); } if (rendezvousbdd.getIdPrestation2() != rendezvous.getIdPrestation2()) { rendezvousbdd.setIdPrestation2(rendezvous.getIdPrestation2()); } if (rendezvousbdd.getIdPrestation3() != rendezvous.getIdPrestation3()) { rendezvousbdd.setIdPrestation3(rendezvous.getIdPrestation3()); } if (rendezvousbdd.getIdPrestation4() != rendezvous.getIdPrestation4()) { rendezvousbdd.setIdPrestation4(rendezvous.getIdPrestation4()); } } catch (Exception e) { throw new DAOException (e); } } public void supprimer(int ID) throws DAOException { Rendezvous rendezvous = this.trouver(ID); try { em.remove(em.merge(rendezvous)); } catch (Exception e) { throw new DAOException(e); } } public Rendezvous trouver(int ID) throws DAOException { try { return em.find(Rendezvous.class,ID); } catch (Exception e) { throw new DAOException("Rendez vous pas trouvé"); } } public Client trouverClient(int ID) throws DAOException { try { return em.find(Client.class,ID); } catch (Exception e) { throw new DAOException("Rendez vous pas trouvé"); } } public Prestation trouverPrestation (int ID )throws DAOException { try { return em.find(Prestation.class, ID); } catch (Exception e) { throw new DAOException ("Prestation non trouvée"); } } public List lister(int idUtilisateur) throws DAOException { try { TypedQuery requete = em.createQuery( JPQL_SELECT_PAR_IDUTILISATEUR, Rendezvous.class ); requete.setParameter(PARAM_IDUTILISATEUR, idUtilisateur) ; return (List) requete.getResultList(); } catch ( Exception e) { throw new DAOException(e); } } }