1 package cz.frantovo.nekurak.ejb;
3 import cz.frantovo.nekurak.dao.PodnikDAO;
4 import cz.frantovo.nekurak.dto.Kategorie;
5 import cz.frantovo.nekurak.dto.Komentar;
6 import cz.frantovo.nekurak.dto.Kurackost;
7 import cz.frantovo.nekurak.dto.Podnik;
8 import cz.frantovo.nekurak.dto.VysledekHlasovani;
9 import cz.frantovo.nekurak.ext.Geo;
10 import cz.frantovo.nekurak.ext.Geo.Souradnice;
11 import cz.frantovo.nekurak.ext.Texy;
12 import cz.frantovo.nekurak.util.Komentare;
13 import cz.frantovo.nekurak.vyjimky.KomentarovaVyjimka;
14 import cz.frantovo.nekurak.vyjimky.TexyVyjimka;
15 import java.util.Collection;
16 import java.util.logging.Logger;
17 import javax.annotation.Resource;
18 import javax.annotation.security.RolesAllowed;
20 import javax.ejb.SessionContext;
21 import javax.ejb.Stateless;
28 public class PodnikEJB implements PodnikRemote {
30 private static final Logger log = Logger.getLogger(PodnikEJB.class.getSimpleName());
32 private PodnikDAO podnikDAO;
34 private SessionContext ctx;
37 public Collection<Podnik> getPodniky() {
38 Collection<Podnik> vysledek = podnikDAO.getPodniky();
43 public Collection<Podnik> getPodniky(Kategorie kategorie) {
44 Collection<Podnik> vysledek = podnikDAO.getPodniky(kategorie);
49 public Collection<Podnik> getPodniky(Kurackost kurackost) {
50 Collection<Podnik> vysledek = podnikDAO.getPodniky(kurackost);
55 public Podnik getPodnik(int id) {
56 return podnikDAO.getPodnik(id);
60 @RolesAllowed("opravneny")
61 public void zalozPodnik(Podnik p) {
63 * Podnik zakládáme jako neschválený a je potřeba ho dodatečně schválit,
64 * to může udělat jen správce a ne uživatel (TODO)
66 p.setSchvaleny(false);
67 p.setSpravce(ctx.getCallerPrincipal().getName());
69 /** TODO: asynchronní počítání */
70 dopocitejSouradnice(p, new Geo());
74 @RolesAllowed("opravneny")
75 public void upravPodnik(Podnik p) {
80 public int dopocitejSouradnice() {
82 * TODO: refaktorovat, změnit datové typy, souřadnice…
85 int pocetAktualizovanych = 0;
87 for (Podnik p : podnikDAO.getPodnikyBezSouradnic()) {
88 if (dopocitejSouradnice(p, g)) {
89 pocetAktualizovanych++;
93 return pocetAktualizovanych;
97 * Na základě adresy (ulice, číslo, město) se pokusí zjistit souřadnice a uloží je k podniku.
99 * @param g poskytovatel geografických služeb
100 * @return zda se souřadnice podařilo dopočítat
102 private boolean dopocitejSouradnice(Podnik p, Geo g) {
103 Souradnice s = g.getSouradnice(p.getUlice() + " " + p.getCisloPopisne() + ", " + p.getMesto());
105 p.setSirka(s.getSirka());
106 p.setDelka(s.getDelka());
115 public void hlasuj(int podnik, boolean hlas, String ipAdresa) {
117 podnikDAO.hlasuj(podnik, hlas, ipAdresa);
121 public VysledekHlasovani getVysledekHlasovani(Podnik podnik) {
122 return podnikDAO.getVysledekHlasovani(podnik);
125 private void kontrolaKomentare(Komentar k) throws TexyVyjimka, KomentarovaVyjimka {
126 k.setUzivatel(ctx.getCallerPrincipal().getName());
129 if (k.getKomentar() == null || k.getKomentar().length() < 1) {
130 throw new KomentarovaVyjimka("Nechceme prázdné komentáře.", null);
133 /** Převedeme na XML */
134 switch (k.getTyp()) {
136 k.setKomentar(Komentare.upravProstyText(k.getKomentar()));
140 k.setKomentar(t.preved(k.getKomentar()));
143 k.setKomentar(Komentare.upravXHTML(k.getKomentar()));
146 /** Přidáme kořenový element */
147 k.setKomentar(Komentare.obal(k.getKomentar()));
149 /** Zkontrolujeme XML */
150 Komentare.zkontroluj(k.getKomentar());
154 @RolesAllowed("opravneny")
155 public Komentar komentuj(Komentar k, boolean uloz) throws KomentarovaVyjimka, TexyVyjimka {
156 kontrolaKomentare(k);