java/nekurak.net-ejb/src/java/cz/frantovo/nekurak/dao/PodnikDAO.java
author František Kučera <franta-hg@frantovo.cz>
Tue, 27 Apr 2010 16:13:36 +0200
changeset 107 e8371105fcc8
parent 100 01be78803f73
child 108 f74a9fc683da
permissions -rw-r--r--
Hlasování: DAO a EJB vrstva pro ukládání hlasů, kostra pro načítání výsledků hlasování.
     1 package cz.frantovo.nekurak.dao;
     2 
     3 import cz.frantovo.nekurak.dto.Podnik;
     4 import cz.frantovo.nekurak.dto.VysledekHlasovani;
     5 import java.util.Collection;
     6 import java.util.Date;
     7 import java.util.logging.Logger;
     8 import javax.ejb.LocalBean;
     9 import javax.ejb.Stateless;
    10 import javax.persistence.EntityManager;
    11 import javax.persistence.PersistenceContext;
    12 import javax.persistence.Query;
    13 
    14 /**
    15  *
    16  * @author fiki
    17  */
    18 @Stateless
    19 @LocalBean
    20 public class PodnikDAO {
    21 
    22     private static final Logger log = Logger.getLogger(PodnikDAO.class.getSimpleName());
    23     @PersistenceContext(unitName = DAO.PU)
    24     private EntityManager em;
    25 
    26     public Collection<Podnik> getPodniky() {
    27 	Query dotaz = em.createQuery("FROM " + DAO.t(Podnik.class) + " o ORDER BY datum DESC");
    28 	return dotaz.getResultList();
    29     }
    30 
    31     /**
    32      * @return podniky, které nemají souřadnice (null, null)
    33      */
    34     public Collection<Podnik> getPodnikyBezSouradnic() {
    35 	Query dotaz = em.createQuery("FROM " + DAO.t(Podnik.class) + " o WHERE sirka IS NULL AND delka IS NULL");
    36 	return dotaz.getResultList();
    37     }
    38 
    39     public Podnik getPodnik(int id) {
    40 	return em.find(Podnik.class, id);
    41     }
    42 
    43     public void zaloz(Podnik p) {
    44 	if (p.getDatum() == null) {
    45 	    p.setDatum(new Date());
    46 	}
    47 
    48 	em.persist(p);
    49     }
    50 
    51     public void uloz(Podnik p) {
    52 	if (p.getDatum() == null) {
    53 	    p.setDatum(new Date());
    54 	}
    55 
    56 	em.merge(p);
    57     }
    58 
    59     public void hlasuj(int podnik, boolean hlas, String ipAdresa) {
    60 	Query insert = em.createNativeQuery("INSERT INTO hlasovani (podnik, hlas, ip_adresa) VALUES (:podnik, :hlas, :ip_adresa);");
    61 	insert.setParameter("podnik", podnik);
    62 	insert.setParameter("hlas", hlas);
    63 	insert.setParameter("ip_adresa", ipAdresa);
    64 	insert.executeUpdate();
    65     }
    66 
    67     public VysledekHlasovani getVysledekHlasovani(int podnik) {
    68 	log.severe("TODO: vracet skutečné výsledky z databáze");
    69 	return new VysledekHlasovani(10, 50);
    70     }
    71 }