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