franta-hg@28
|
1 |
package cz.frantovo.nekurak.ejb;
|
franta-hg@28
|
2 |
|
franta-hg@39
|
3 |
import cz.frantovo.nekurak.dao.PodnikDAO;
|
franta-hg@170
|
4 |
import cz.frantovo.nekurak.dto.Kategorie;
|
franta-hg@130
|
5 |
import cz.frantovo.nekurak.dto.Komentar;
|
franta-hg@171
|
6 |
import cz.frantovo.nekurak.dto.Kurackost;
|
franta-hg@28
|
7 |
import cz.frantovo.nekurak.dto.Podnik;
|
franta-hg@107
|
8 |
import cz.frantovo.nekurak.dto.VysledekHlasovani;
|
franta-hg@119
|
9 |
import cz.frantovo.nekurak.ext.Geo;
|
franta-hg@119
|
10 |
import cz.frantovo.nekurak.ext.Geo.Souradnice;
|
franta-hg@130
|
11 |
import cz.frantovo.nekurak.ext.Texy;
|
franta-hg@130
|
12 |
import cz.frantovo.nekurak.util.Komentare;
|
franta-hg@132
|
13 |
import cz.frantovo.nekurak.vyjimky.KomentarovaVyjimka;
|
franta-hg@132
|
14 |
import cz.frantovo.nekurak.vyjimky.TexyVyjimka;
|
franta-hg@28
|
15 |
import java.util.Collection;
|
franta-hg@107
|
16 |
import java.util.logging.Logger;
|
franta-hg@63
|
17 |
import javax.annotation.Resource;
|
franta-hg@40
|
18 |
import javax.annotation.security.RolesAllowed;
|
franta-hg@39
|
19 |
import javax.ejb.EJB;
|
franta-hg@63
|
20 |
import javax.ejb.SessionContext;
|
franta-hg@28
|
21 |
import javax.ejb.Stateless;
|
franta-hg@28
|
22 |
|
franta-hg@28
|
23 |
/**
|
franta-hg@28
|
24 |
*
|
franta-hg@28
|
25 |
* @author fiki
|
franta-hg@28
|
26 |
*/
|
franta-hg@28
|
27 |
@Stateless
|
franta-hg@39
|
28 |
public class PodnikEJB implements PodnikRemote {
|
franta-hg@28
|
29 |
|
franta-hg@145
|
30 |
private static final Logger log = Logger.getLogger(PodnikEJB.class.getSimpleName());
|
franta-hg@145
|
31 |
@EJB
|
franta-hg@145
|
32 |
private PodnikDAO podnikDAO;
|
franta-hg@145
|
33 |
@Resource
|
franta-hg@145
|
34 |
private SessionContext ctx;
|
franta-hg@28
|
35 |
|
franta-hg@145
|
36 |
public Collection<Podnik> getPodniky() {
|
franta-hg@145
|
37 |
Collection<Podnik> vysledek = podnikDAO.getPodniky();
|
franta-hg@145
|
38 |
return vysledek;
|
franta-hg@100
|
39 |
}
|
franta-hg@100
|
40 |
|
franta-hg@170
|
41 |
public Collection<Podnik> getPodniky(Kategorie kategorie) {
|
franta-hg@170
|
42 |
Collection<Podnik> vysledek = podnikDAO.getPodniky(kategorie);
|
franta-hg@170
|
43 |
return vysledek;
|
franta-hg@170
|
44 |
}
|
franta-hg@170
|
45 |
|
franta-hg@171
|
46 |
public Collection<Podnik> getPodniky(Kurackost kurackost) {
|
franta-hg@171
|
47 |
Collection<Podnik> vysledek = podnikDAO.getPodniky(kurackost);
|
franta-hg@171
|
48 |
return vysledek;
|
franta-hg@171
|
49 |
}
|
franta-hg@171
|
50 |
|
franta-hg@145
|
51 |
public Podnik getPodnik(int id) {
|
franta-hg@145
|
52 |
return podnikDAO.getPodnik(id);
|
franta-hg@142
|
53 |
}
|
franta-hg@142
|
54 |
|
franta-hg@145
|
55 |
@RolesAllowed("opravneny")
|
franta-hg@145
|
56 |
public void zalozPodnik(Podnik p) {
|
franta-hg@173
|
57 |
/**
|
franta-hg@173
|
58 |
* Podnik zakládáme jako neschválený a je potřeba ho dodatečně schválit,
|
franta-hg@173
|
59 |
* to může udělat jen správce a ne uživatel (TODO)
|
franta-hg@173
|
60 |
*/
|
franta-hg@173
|
61 |
p.setSchvaleny(false);
|
franta-hg@145
|
62 |
p.setSpravce(ctx.getCallerPrincipal().getName());
|
franta-hg@145
|
63 |
podnikDAO.zaloz(p);
|
franta-hg@176
|
64 |
/** TODO: asynchronní počítání */
|
franta-hg@176
|
65 |
dopocitejSouradnice(p, new Geo());
|
franta-hg@130
|
66 |
}
|
franta-hg@130
|
67 |
|
franta-hg@145
|
68 |
@RolesAllowed("opravneny")
|
franta-hg@145
|
69 |
public void upravPodnik(Podnik p) {
|
franta-hg@145
|
70 |
podnikDAO.uloz(p);
|
franta-hg@145
|
71 |
}
|
franta-hg@130
|
72 |
|
franta-hg@145
|
73 |
public int dopocitejSouradnice() {
|
franta-hg@145
|
74 |
/**
|
franta-hg@145
|
75 |
* TODO: refaktorovat, změnit datové typy, souřadnice…
|
franta-hg@145
|
76 |
*/
|
franta-hg@145
|
77 |
Geo g = new Geo();
|
franta-hg@145
|
78 |
int pocetAktualizovanych = 0;
|
franta-hg@145
|
79 |
|
franta-hg@145
|
80 |
for (Podnik p : podnikDAO.getPodnikyBezSouradnic()) {
|
franta-hg@176
|
81 |
if (dopocitejSouradnice(p, g)) {
|
franta-hg@145
|
82 |
pocetAktualizovanych++;
|
franta-hg@145
|
83 |
}
|
franta-hg@145
|
84 |
}
|
franta-hg@145
|
85 |
|
franta-hg@145
|
86 |
return pocetAktualizovanych;
|
franta-hg@142
|
87 |
}
|
franta-hg@145
|
88 |
|
franta-hg@176
|
89 |
/**
|
franta-hg@176
|
90 |
* Na základě adresy (ulice, číslo, město) se pokusí zjistit souřadnice a uloží je k podniku.
|
franta-hg@176
|
91 |
* @param p podnik
|
franta-hg@176
|
92 |
* @param g poskytovatel geografických služeb
|
franta-hg@176
|
93 |
* @return zda se souřadnice podařilo dopočítat
|
franta-hg@176
|
94 |
*/
|
franta-hg@176
|
95 |
private boolean dopocitejSouradnice(Podnik p, Geo g) {
|
franta-hg@176
|
96 |
Souradnice s = g.getSouradnice(p.getUlice() + " " + p.getCisloPopisne() + ", " + p.getMesto());
|
franta-hg@176
|
97 |
if (s != null) {
|
franta-hg@176
|
98 |
p.setSirka(s.getSirka());
|
franta-hg@176
|
99 |
p.setDelka(s.getDelka());
|
franta-hg@176
|
100 |
podnikDAO.uloz(p);
|
franta-hg@176
|
101 |
return true;
|
franta-hg@176
|
102 |
} else {
|
franta-hg@176
|
103 |
return false;
|
franta-hg@176
|
104 |
}
|
franta-hg@176
|
105 |
}
|
franta-hg@176
|
106 |
|
franta-hg@145
|
107 |
public void hlasuj(int podnik, boolean hlas, String ipAdresa) {
|
franta-hg@145
|
108 |
|
franta-hg@145
|
109 |
podnikDAO.hlasuj(podnik, hlas, ipAdresa);
|
franta-hg@145
|
110 |
}
|
franta-hg@145
|
111 |
|
franta-hg@164
|
112 |
public VysledekHlasovani getVysledekHlasovani(Podnik podnik) {
|
franta-hg@145
|
113 |
return podnikDAO.getVysledekHlasovani(podnik);
|
franta-hg@145
|
114 |
}
|
franta-hg@145
|
115 |
|
franta-hg@145
|
116 |
private void kontrolaKomentare(Komentar k) throws TexyVyjimka, KomentarovaVyjimka {
|
franta-hg@145
|
117 |
k.setUzivatel(ctx.getCallerPrincipal().getName());
|
franta-hg@145
|
118 |
k.setDatum(null);
|
franta-hg@145
|
119 |
|
franta-hg@145
|
120 |
if (k.getKomentar() == null || k.getKomentar().length() < 1) {
|
franta-hg@145
|
121 |
throw new KomentarovaVyjimka("Nechceme prázdné komentáře.", null);
|
franta-hg@145
|
122 |
}
|
franta-hg@145
|
123 |
|
franta-hg@145
|
124 |
/** Převedeme na XML */
|
franta-hg@145
|
125 |
switch (k.getTyp()) {
|
franta-hg@145
|
126 |
case PROSTY_TEXT:
|
franta-hg@145
|
127 |
k.setKomentar(Komentare.upravProstyText(k.getKomentar()));
|
franta-hg@145
|
128 |
break;
|
franta-hg@145
|
129 |
case TEXY:
|
franta-hg@145
|
130 |
Texy t = new Texy();
|
franta-hg@145
|
131 |
k.setKomentar(t.preved(k.getKomentar()));
|
franta-hg@145
|
132 |
break;
|
franta-hg@145
|
133 |
case XHTML:
|
franta-hg@145
|
134 |
k.setKomentar(Komentare.upravXHTML(k.getKomentar()));
|
franta-hg@145
|
135 |
break;
|
franta-hg@145
|
136 |
}
|
franta-hg@145
|
137 |
/** Přidáme kořenový element */
|
franta-hg@145
|
138 |
k.setKomentar(Komentare.obal(k.getKomentar()));
|
franta-hg@145
|
139 |
|
franta-hg@145
|
140 |
/** Zkontrolujeme XML */
|
franta-hg@145
|
141 |
Komentare.zkontroluj(k.getKomentar());
|
franta-hg@145
|
142 |
}
|
franta-hg@145
|
143 |
|
franta-hg@145
|
144 |
@RolesAllowed("opravneny")
|
franta-hg@145
|
145 |
public Komentar komentuj(Komentar k, boolean uloz) throws KomentarovaVyjimka, TexyVyjimka {
|
franta-hg@145
|
146 |
kontrolaKomentare(k);
|
franta-hg@145
|
147 |
if (uloz) {
|
franta-hg@145
|
148 |
podnikDAO.zaloz(k);
|
franta-hg@145
|
149 |
}
|
franta-hg@145
|
150 |
return k;
|
franta-hg@145
|
151 |
}
|
franta-hg@28
|
152 |
}
|