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@108
|
5 |
import cz.frantovo.superDAO.SuperDAO;
|
franta-hg@39
|
6 |
import java.util.Collection;
|
franta-hg@49
|
7 |
import java.util.Date;
|
franta-hg@108
|
8 |
import java.util.List;
|
franta-hg@39
|
9 |
import javax.ejb.LocalBean;
|
franta-hg@39
|
10 |
import javax.ejb.Stateless;
|
franta-hg@39
|
11 |
import javax.persistence.EntityManager;
|
franta-hg@39
|
12 |
import javax.persistence.PersistenceContext;
|
franta-hg@39
|
13 |
import javax.persistence.Query;
|
franta-hg@39
|
14 |
|
franta-hg@39
|
15 |
/**
|
franta-hg@39
|
16 |
*
|
franta-hg@39
|
17 |
* @author fiki
|
franta-hg@39
|
18 |
*/
|
franta-hg@39
|
19 |
@Stateless
|
franta-hg@39
|
20 |
@LocalBean
|
franta-hg@108
|
21 |
public class PodnikDAO extends SuperDAO {
|
franta-hg@39
|
22 |
|
franta-hg@67
|
23 |
@PersistenceContext(unitName = DAO.PU)
|
franta-hg@39
|
24 |
private EntityManager em;
|
franta-hg@39
|
25 |
|
franta-hg@108
|
26 |
private enum SQL {
|
franta-hg@108
|
27 |
|
franta-hg@108
|
28 |
HLASOVANI_INSERT,
|
franta-hg@108
|
29 |
HLASOVANI_SELECT
|
franta-hg@108
|
30 |
}
|
franta-hg@108
|
31 |
|
franta-hg@39
|
32 |
public Collection<Podnik> getPodniky() {
|
franta-hg@67
|
33 |
Query dotaz = em.createQuery("FROM " + DAO.t(Podnik.class) + " o ORDER BY datum DESC");
|
franta-hg@39
|
34 |
return dotaz.getResultList();
|
franta-hg@39
|
35 |
}
|
franta-hg@39
|
36 |
|
franta-hg@100
|
37 |
/**
|
franta-hg@100
|
38 |
* @return podniky, které nemají souřadnice (null, null)
|
franta-hg@100
|
39 |
*/
|
franta-hg@100
|
40 |
public Collection<Podnik> getPodnikyBezSouradnic() {
|
franta-hg@100
|
41 |
Query dotaz = em.createQuery("FROM " + DAO.t(Podnik.class) + " o WHERE sirka IS NULL AND delka IS NULL");
|
franta-hg@100
|
42 |
return dotaz.getResultList();
|
franta-hg@100
|
43 |
}
|
franta-hg@100
|
44 |
|
franta-hg@100
|
45 |
public Podnik getPodnik(int id) {
|
franta-hg@100
|
46 |
return em.find(Podnik.class, id);
|
franta-hg@100
|
47 |
}
|
franta-hg@100
|
48 |
|
franta-hg@56
|
49 |
public void zaloz(Podnik p) {
|
franta-hg@49
|
50 |
if (p.getDatum() == null) {
|
franta-hg@49
|
51 |
p.setDatum(new Date());
|
franta-hg@49
|
52 |
}
|
franta-hg@49
|
53 |
|
franta-hg@40
|
54 |
em.persist(p);
|
franta-hg@40
|
55 |
}
|
franta-hg@40
|
56 |
|
franta-hg@56
|
57 |
public void uloz(Podnik p) {
|
franta-hg@56
|
58 |
if (p.getDatum() == null) {
|
franta-hg@56
|
59 |
p.setDatum(new Date());
|
franta-hg@56
|
60 |
}
|
franta-hg@56
|
61 |
|
franta-hg@56
|
62 |
em.merge(p);
|
franta-hg@56
|
63 |
}
|
franta-hg@107
|
64 |
|
franta-hg@119
|
65 |
/**
|
franta-hg@119
|
66 |
* Zaznamená hlas uživatele, zda se v podniku má nebo nemá kouřit
|
franta-hg@119
|
67 |
* @param podnik podnik o kterém se hlasuje
|
franta-hg@119
|
68 |
* @param hlas true = kouřit | false = nekouřit
|
franta-hg@119
|
69 |
* @param ipAdresa identifikujeme pomocí ní uživatele
|
franta-hg@119
|
70 |
*/
|
franta-hg@107
|
71 |
public void hlasuj(int podnik, boolean hlas, String ipAdresa) {
|
franta-hg@108
|
72 |
Query insert = em.createNativeQuery(getSQL(SQL.HLASOVANI_INSERT));
|
franta-hg@107
|
73 |
insert.setParameter("podnik", podnik);
|
franta-hg@107
|
74 |
insert.setParameter("hlas", hlas);
|
franta-hg@107
|
75 |
insert.setParameter("ip_adresa", ipAdresa);
|
franta-hg@107
|
76 |
insert.executeUpdate();
|
franta-hg@107
|
77 |
}
|
franta-hg@107
|
78 |
|
franta-hg@107
|
79 |
public VysledekHlasovani getVysledekHlasovani(int podnik) {
|
franta-hg@108
|
80 |
VysledekHlasovani vysledek = new VysledekHlasovani();
|
franta-hg@108
|
81 |
Query select = em.createNativeQuery(getSQL(SQL.HLASOVANI_SELECT));
|
franta-hg@108
|
82 |
select.setParameter("podnik", podnik);
|
franta-hg@108
|
83 |
List<Object[]> vysledekDotazu = select.getResultList();
|
franta-hg@108
|
84 |
|
franta-hg@108
|
85 |
for (Object[] radek : vysledekDotazu) {
|
franta-hg@108
|
86 |
/** Transponujeme výsledek dotazu */
|
franta-hg@108
|
87 |
if ((Boolean) radek[0]) {
|
franta-hg@108
|
88 |
vysledek.setHlasuAno((Integer) radek[1]);
|
franta-hg@108
|
89 |
} else {
|
franta-hg@108
|
90 |
vysledek.setHlasuNe((Integer) radek[1]);
|
franta-hg@108
|
91 |
}
|
franta-hg@108
|
92 |
}
|
franta-hg@108
|
93 |
|
franta-hg@108
|
94 |
return vysledek;
|
franta-hg@107
|
95 |
}
|
franta-hg@39
|
96 |
}
|