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