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@197
|
11 |
import cz.frantovo.nekurak.ext.WikiSyntaxProcesor;
|
franta-hg@130
|
12 |
import cz.frantovo.nekurak.util.Komentare;
|
franta-hg@132
|
13 |
import cz.frantovo.nekurak.vyjimky.KomentarovaVyjimka;
|
franta-hg@197
|
14 |
import cz.frantovo.nekurak.vyjimky.WikiSyntaxVyjimka;
|
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@197
|
35 |
|
franta-hg@197
|
36 |
private WikiSyntaxProcesor wikiSyntaxProcesor = new WikiSyntaxProcesor();
|
franta-hg@28
|
37 |
|
franta-hg@192
|
38 |
@Override
|
franta-hg@145
|
39 |
public Collection<Podnik> getPodniky() {
|
franta-hg@145
|
40 |
Collection<Podnik> vysledek = podnikDAO.getPodniky();
|
franta-hg@145
|
41 |
return vysledek;
|
franta-hg@100
|
42 |
}
|
franta-hg@100
|
43 |
|
franta-hg@192
|
44 |
@Override
|
franta-hg@170
|
45 |
public Collection<Podnik> getPodniky(Kategorie kategorie) {
|
franta-hg@170
|
46 |
Collection<Podnik> vysledek = podnikDAO.getPodniky(kategorie);
|
franta-hg@170
|
47 |
return vysledek;
|
franta-hg@170
|
48 |
}
|
franta-hg@170
|
49 |
|
franta-hg@192
|
50 |
@Override
|
franta-hg@171
|
51 |
public Collection<Podnik> getPodniky(Kurackost kurackost) {
|
franta-hg@171
|
52 |
Collection<Podnik> vysledek = podnikDAO.getPodniky(kurackost);
|
franta-hg@171
|
53 |
return vysledek;
|
franta-hg@171
|
54 |
}
|
franta-hg@171
|
55 |
|
franta-hg@192
|
56 |
@Override
|
franta-hg@145
|
57 |
public Podnik getPodnik(int id) {
|
franta-hg@145
|
58 |
return podnikDAO.getPodnik(id);
|
franta-hg@142
|
59 |
}
|
franta-hg@142
|
60 |
|
franta-hg@192
|
61 |
@Override
|
franta-hg@145
|
62 |
@RolesAllowed("opravneny")
|
franta-hg@145
|
63 |
public void zalozPodnik(Podnik p) {
|
franta-hg@173
|
64 |
/**
|
franta-hg@173
|
65 |
* Podnik zakládáme jako neschválený a je potřeba ho dodatečně schválit,
|
franta-hg@173
|
66 |
* to může udělat jen správce a ne uživatel (TODO)
|
franta-hg@173
|
67 |
*/
|
franta-hg@173
|
68 |
p.setSchvaleny(false);
|
franta-hg@145
|
69 |
p.setSpravce(ctx.getCallerPrincipal().getName());
|
franta-hg@145
|
70 |
podnikDAO.zaloz(p);
|
franta-hg@176
|
71 |
/** TODO: asynchronní počítání */
|
franta-hg@176
|
72 |
dopocitejSouradnice(p, new Geo());
|
franta-hg@130
|
73 |
}
|
franta-hg@130
|
74 |
|
franta-hg@192
|
75 |
@Override
|
franta-hg@145
|
76 |
@RolesAllowed("opravneny")
|
franta-hg@145
|
77 |
public void upravPodnik(Podnik p) {
|
franta-hg@145
|
78 |
podnikDAO.uloz(p);
|
franta-hg@145
|
79 |
}
|
franta-hg@130
|
80 |
|
franta-hg@192
|
81 |
@Override
|
franta-hg@145
|
82 |
public int dopocitejSouradnice() {
|
franta-hg@145
|
83 |
/**
|
franta-hg@145
|
84 |
* TODO: refaktorovat, změnit datové typy, souřadnice…
|
franta-hg@145
|
85 |
*/
|
franta-hg@145
|
86 |
Geo g = new Geo();
|
franta-hg@145
|
87 |
int pocetAktualizovanych = 0;
|
franta-hg@145
|
88 |
|
franta-hg@145
|
89 |
for (Podnik p : podnikDAO.getPodnikyBezSouradnic()) {
|
franta-hg@176
|
90 |
if (dopocitejSouradnice(p, g)) {
|
franta-hg@145
|
91 |
pocetAktualizovanych++;
|
franta-hg@145
|
92 |
}
|
franta-hg@145
|
93 |
}
|
franta-hg@145
|
94 |
|
franta-hg@145
|
95 |
return pocetAktualizovanych;
|
franta-hg@142
|
96 |
}
|
franta-hg@145
|
97 |
|
franta-hg@176
|
98 |
/**
|
franta-hg@176
|
99 |
* Na základě adresy (ulice, číslo, město) se pokusí zjistit souřadnice a uloží je k podniku.
|
franta-hg@176
|
100 |
* @param p podnik
|
franta-hg@176
|
101 |
* @param g poskytovatel geografických služeb
|
franta-hg@176
|
102 |
* @return zda se souřadnice podařilo dopočítat
|
franta-hg@176
|
103 |
*/
|
franta-hg@176
|
104 |
private boolean dopocitejSouradnice(Podnik p, Geo g) {
|
franta-hg@176
|
105 |
Souradnice s = g.getSouradnice(p.getUlice() + " " + p.getCisloPopisne() + ", " + p.getMesto());
|
franta-hg@176
|
106 |
if (s != null) {
|
franta-hg@176
|
107 |
p.setSirka(s.getSirka());
|
franta-hg@176
|
108 |
p.setDelka(s.getDelka());
|
franta-hg@176
|
109 |
podnikDAO.uloz(p);
|
franta-hg@176
|
110 |
return true;
|
franta-hg@176
|
111 |
} else {
|
franta-hg@176
|
112 |
return false;
|
franta-hg@176
|
113 |
}
|
franta-hg@176
|
114 |
}
|
franta-hg@176
|
115 |
|
franta-hg@192
|
116 |
@Override
|
franta-hg@145
|
117 |
public void hlasuj(int podnik, boolean hlas, String ipAdresa) {
|
franta-hg@145
|
118 |
|
franta-hg@145
|
119 |
podnikDAO.hlasuj(podnik, hlas, ipAdresa);
|
franta-hg@145
|
120 |
}
|
franta-hg@145
|
121 |
|
franta-hg@192
|
122 |
@Override
|
franta-hg@164
|
123 |
public VysledekHlasovani getVysledekHlasovani(Podnik podnik) {
|
franta-hg@145
|
124 |
return podnikDAO.getVysledekHlasovani(podnik);
|
franta-hg@145
|
125 |
}
|
franta-hg@145
|
126 |
|
franta-hg@197
|
127 |
private void kontrolaKomentare(Komentar k) throws WikiSyntaxVyjimka, KomentarovaVyjimka {
|
franta-hg@145
|
128 |
k.setUzivatel(ctx.getCallerPrincipal().getName());
|
franta-hg@145
|
129 |
k.setDatum(null);
|
franta-hg@145
|
130 |
|
franta-hg@145
|
131 |
if (k.getKomentar() == null || k.getKomentar().length() < 1) {
|
franta-hg@145
|
132 |
throw new KomentarovaVyjimka("Nechceme prázdné komentáře.", null);
|
franta-hg@145
|
133 |
}
|
franta-hg@145
|
134 |
|
franta-hg@145
|
135 |
/** Převedeme na XML */
|
franta-hg@145
|
136 |
switch (k.getTyp()) {
|
franta-hg@145
|
137 |
case PROSTY_TEXT:
|
franta-hg@145
|
138 |
k.setKomentar(Komentare.upravProstyText(k.getKomentar()));
|
franta-hg@145
|
139 |
break;
|
franta-hg@197
|
140 |
case MARKDOWN:
|
franta-hg@197
|
141 |
k.setKomentar(wikiSyntaxProcesor.převeď(k.getKomentar(), Komentar.TYP.MARKDOWN));
|
franta-hg@197
|
142 |
break;
|
franta-hg@145
|
143 |
case TEXY:
|
franta-hg@197
|
144 |
k.setKomentar(wikiSyntaxProcesor.převeď(k.getKomentar(), Komentar.TYP.TEXY));
|
franta-hg@145
|
145 |
break;
|
franta-hg@145
|
146 |
case XHTML:
|
franta-hg@145
|
147 |
k.setKomentar(Komentare.upravXHTML(k.getKomentar()));
|
franta-hg@145
|
148 |
break;
|
franta-hg@145
|
149 |
}
|
franta-hg@145
|
150 |
/** Přidáme kořenový element */
|
franta-hg@145
|
151 |
k.setKomentar(Komentare.obal(k.getKomentar()));
|
franta-hg@145
|
152 |
|
franta-hg@145
|
153 |
/** Zkontrolujeme XML */
|
franta-hg@145
|
154 |
Komentare.zkontroluj(k.getKomentar());
|
franta-hg@145
|
155 |
}
|
franta-hg@145
|
156 |
|
franta-hg@192
|
157 |
@Override
|
franta-hg@145
|
158 |
@RolesAllowed("opravneny")
|
franta-hg@197
|
159 |
public Komentar komentuj(Komentar k, boolean uloz) throws KomentarovaVyjimka, WikiSyntaxVyjimka {
|
franta-hg@145
|
160 |
kontrolaKomentare(k);
|
franta-hg@145
|
161 |
if (uloz) {
|
franta-hg@145
|
162 |
podnikDAO.zaloz(k);
|
franta-hg@145
|
163 |
}
|
franta-hg@145
|
164 |
return k;
|
franta-hg@145
|
165 |
}
|
franta-hg@28
|
166 |
}
|