franta-hg@28: package cz.frantovo.nekurak.ejb; franta-hg@28: franta-hg@39: import cz.frantovo.nekurak.dao.PodnikDAO; franta-hg@130: import cz.frantovo.nekurak.dto.Komentar; franta-hg@28: import cz.frantovo.nekurak.dto.Podnik; franta-hg@107: import cz.frantovo.nekurak.dto.VysledekHlasovani; franta-hg@119: import cz.frantovo.nekurak.ext.Geo; franta-hg@119: import cz.frantovo.nekurak.ext.Geo.Souradnice; franta-hg@130: import cz.frantovo.nekurak.ext.Texy; franta-hg@130: import cz.frantovo.nekurak.util.Komentare; franta-hg@132: import cz.frantovo.nekurak.vyjimky.KomentarovaVyjimka; franta-hg@132: import cz.frantovo.nekurak.vyjimky.TexyVyjimka; franta-hg@28: import java.util.Collection; franta-hg@107: import java.util.logging.Logger; franta-hg@63: import javax.annotation.Resource; franta-hg@40: import javax.annotation.security.RolesAllowed; franta-hg@39: import javax.ejb.EJB; franta-hg@63: import javax.ejb.SessionContext; franta-hg@28: import javax.ejb.Stateless; franta-hg@28: franta-hg@28: /** franta-hg@28: * franta-hg@28: * @author fiki franta-hg@28: */ franta-hg@28: @Stateless franta-hg@39: public class PodnikEJB implements PodnikRemote { franta-hg@28: franta-hg@107: private static final Logger log = Logger.getLogger(PodnikEJB.class.getSimpleName()); franta-hg@39: @EJB franta-hg@50: private PodnikDAO podnikDAO; franta-hg@63: @Resource franta-hg@63: private SessionContext ctx; franta-hg@133: private Komentare komentare = new Komentare(); franta-hg@28: franta-hg@28: public Collection getPodniky() { franta-hg@39: Collection vysledek = podnikDAO.getPodniky(); franta-hg@39: return vysledek; franta-hg@28: } franta-hg@40: franta-hg@100: public Podnik getPodnik(int id) { franta-hg@100: return podnikDAO.getPodnik(id); franta-hg@100: } franta-hg@100: franta-hg@40: @RolesAllowed("opravneny") franta-hg@40: public void zalozPodnik(Podnik p) { franta-hg@63: p.setSpravce(ctx.getCallerPrincipal().getName()); franta-hg@56: podnikDAO.zaloz(p); franta-hg@40: } franta-hg@40: franta-hg@40: @RolesAllowed("opravneny") franta-hg@40: public void upravPodnik(Podnik p) { franta-hg@40: podnikDAO.uloz(p); franta-hg@40: } franta-hg@100: franta-hg@100: public int dopocitejSouradnice() { franta-hg@100: /** franta-hg@123: * TODO: refaktorovat, změnit datové typy, souřadnice… franta-hg@100: */ franta-hg@100: Geo g = new Geo(); franta-hg@100: int pocetAktualizovanych = 0; franta-hg@100: franta-hg@102: for (Podnik p : podnikDAO.getPodnikyBezSouradnic()) { franta-hg@100: Souradnice s = g.getSouradnice(p.getUlice() + " " + p.getCisloPopisne() + ", " + p.getMesto()); franta-hg@100: if (s != null) { franta-hg@100: pocetAktualizovanych++; franta-hg@100: p.setSirka(s.getSirka()); franta-hg@100: p.setDelka(s.getDelka()); franta-hg@100: podnikDAO.uloz(p); franta-hg@100: } franta-hg@100: } franta-hg@100: franta-hg@100: return pocetAktualizovanych; franta-hg@100: } franta-hg@107: franta-hg@107: public void hlasuj(int podnik, boolean hlas, String ipAdresa) { franta-hg@110: franta-hg@107: podnikDAO.hlasuj(podnik, hlas, ipAdresa); franta-hg@107: } franta-hg@107: franta-hg@107: public VysledekHlasovani getVysledekHlasovani(int podnik) { franta-hg@107: return podnikDAO.getVysledekHlasovani(podnik); franta-hg@107: } franta-hg@130: franta-hg@142: private void kontrolaKomentare(Komentar k) throws TexyVyjimka, KomentarovaVyjimka { franta-hg@130: k.setUzivatel(ctx.getCallerPrincipal().getName()); franta-hg@134: k.setDatum(null); franta-hg@130: franta-hg@142: if (k.getKomentar() == null || k.getKomentar().length() < 1) { franta-hg@142: throw new KomentarovaVyjimka("Nechceme prázdné komentáře.", null); franta-hg@142: } franta-hg@142: franta-hg@130: /** Převedeme na XML */ franta-hg@130: switch (k.getTyp()) { franta-hg@130: case PROSTY_TEXT: franta-hg@130: k.setKomentar(Komentare.upravProstyText(k.getKomentar())); franta-hg@130: break; franta-hg@130: case TEXY: franta-hg@130: Texy t = new Texy(); franta-hg@130: k.setKomentar(t.preved(k.getKomentar())); franta-hg@133: break; franta-hg@133: case XHTML: franta-hg@133: k.setKomentar(Komentare.upravXHTML(k.getKomentar())); franta-hg@130: break; franta-hg@130: } franta-hg@142: /** Přidáme kořenový element */ franta-hg@133: k.setKomentar(Komentare.obal(k.getKomentar())); franta-hg@130: franta-hg@131: /** Zkontrolujeme XML */ franta-hg@142: Komentare.zkontroluj(k.getKomentar()); franta-hg@142: } franta-hg@130: franta-hg@142: @RolesAllowed("opravneny") franta-hg@142: public Komentar komentuj(Komentar k, boolean uloz) throws KomentarovaVyjimka, TexyVyjimka { franta-hg@142: kontrolaKomentare(k); franta-hg@142: if (uloz) { franta-hg@142: podnikDAO.zaloz(k); franta-hg@142: } franta-hg@142: return k; franta-hg@130: } franta-hg@28: }