java/nekurak.net-ejb/src/java/cz/frantovo/nekurak/ejb/PodnikEJB.java
author František Kučera <franta-hg@frantovo.cz>
Sat Jan 15 14:27:02 2011 +0100 (2011-01-15)
changeset 173 2b458ac09044
parent 171 e619057bad66
child 176 2472d9e9c0aa
permissions -rw-r--r--
Zobrazování jen schválených podniků, SQL (kategorie, kuřáckost).
     1 package cz.frantovo.nekurak.ejb;
     2 
     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.HashSet;
    17 import java.util.Set;
    18 import java.util.logging.Logger;
    19 import javax.annotation.Resource;
    20 import javax.annotation.security.RolesAllowed;
    21 import javax.ejb.EJB;
    22 import javax.ejb.SessionContext;
    23 import javax.ejb.Stateless;
    24 
    25 /**
    26  *
    27  * @author fiki
    28  */
    29 @Stateless
    30 public class PodnikEJB implements PodnikRemote {
    31 
    32 	private static final Logger log = Logger.getLogger(PodnikEJB.class.getSimpleName());
    33 	@EJB
    34 	private PodnikDAO podnikDAO;
    35 	@Resource
    36 	private SessionContext ctx;
    37 
    38 	public Collection<Podnik> getPodniky() {
    39 		Collection<Podnik> vysledek = podnikDAO.getPodniky();
    40 		return vysledek;
    41 	}
    42 
    43 	public Collection<Podnik> getPodniky(Kategorie kategorie) {
    44 		Collection<Podnik> vysledek = podnikDAO.getPodniky(kategorie);
    45 		return vysledek;
    46 	}
    47 
    48 	public Collection<Podnik> getPodniky(Kurackost kurackost) {
    49 		Collection<Podnik> vysledek = podnikDAO.getPodniky(kurackost);
    50 		return vysledek;
    51 	}
    52 
    53 	public Podnik getPodnik(int id) {
    54 		return podnikDAO.getPodnik(id);
    55 	}
    56 
    57 	@RolesAllowed("opravneny")
    58 	public void zalozPodnik(Podnik p) {
    59 		/**
    60 		 * Podnik zakládáme jako neschválený a je potřeba ho dodatečně schválit,
    61 		 * to může udělat jen správce a ne uživatel (TODO)
    62 		 */
    63 		p.setSchvaleny(false);
    64 		p.setSpravce(ctx.getCallerPrincipal().getName());
    65 		podnikDAO.zaloz(p);
    66 	}
    67 
    68 	@RolesAllowed("opravneny")
    69 	public void upravPodnik(Podnik p) {
    70 		podnikDAO.uloz(p);
    71 	}
    72 
    73 	public int dopocitejSouradnice() {
    74 		/**
    75 		 * TODO: refaktorovat, změnit datové typy, souřadnice…
    76 		 */
    77 		Geo g = new Geo();
    78 		int pocetAktualizovanych = 0;
    79 
    80 		for (Podnik p : podnikDAO.getPodnikyBezSouradnic()) {
    81 			Souradnice s = g.getSouradnice(p.getUlice() + " " + p.getCisloPopisne() + ", " + p.getMesto());
    82 			if (s != null) {
    83 				pocetAktualizovanych++;
    84 				p.setSirka(s.getSirka());
    85 				p.setDelka(s.getDelka());
    86 				podnikDAO.uloz(p);
    87 			}
    88 		}
    89 
    90 		return pocetAktualizovanych;
    91 	}
    92 
    93 	public void hlasuj(int podnik, boolean hlas, String ipAdresa) {
    94 
    95 		podnikDAO.hlasuj(podnik, hlas, ipAdresa);
    96 	}
    97 
    98 	public VysledekHlasovani getVysledekHlasovani(Podnik podnik) {
    99 		return podnikDAO.getVysledekHlasovani(podnik);
   100 	}
   101 
   102 	private void kontrolaKomentare(Komentar k) throws TexyVyjimka, KomentarovaVyjimka {
   103 		k.setUzivatel(ctx.getCallerPrincipal().getName());
   104 		k.setDatum(null);
   105 
   106 		if (k.getKomentar() == null || k.getKomentar().length() < 1) {
   107 			throw new KomentarovaVyjimka("Nechceme prázdné komentáře.", null);
   108 		}
   109 
   110 		/** Převedeme na XML */
   111 		switch (k.getTyp()) {
   112 			case PROSTY_TEXT:
   113 				k.setKomentar(Komentare.upravProstyText(k.getKomentar()));
   114 				break;
   115 			case TEXY:
   116 				Texy t = new Texy();
   117 				k.setKomentar(t.preved(k.getKomentar()));
   118 				break;
   119 			case XHTML:
   120 				k.setKomentar(Komentare.upravXHTML(k.getKomentar()));
   121 				break;
   122 		}
   123 		/** Přidáme kořenový element */
   124 		k.setKomentar(Komentare.obal(k.getKomentar()));
   125 
   126 		/** Zkontrolujeme XML */
   127 		Komentare.zkontroluj(k.getKomentar());
   128 	}
   129 
   130 	@RolesAllowed("opravneny")
   131 	public Komentar komentuj(Komentar k, boolean uloz) throws KomentarovaVyjimka, TexyVyjimka {
   132 		kontrolaKomentare(k);
   133 		if (uloz) {
   134 			podnikDAO.zaloz(k);
   135 		}
   136 		return k;
   137 	}
   138 }