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