java/nekurak.net-ejb/src/java/cz/frantovo/nekurak/ejb/PodnikEJB.java
author František Kučera <franta-hg@frantovo.cz>
Tue Jan 25 20:28:32 2011 +0100 (2011-01-25)
changeset 176 2472d9e9c0aa
parent 173 2b458ac09044
child 192 f106b3043c0c
permissions -rw-r--r--
Souřadnice podniku (pro mapu) budeme počítat hned při založení.
     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.logging.Logger;
    17 import javax.annotation.Resource;
    18 import javax.annotation.security.RolesAllowed;
    19 import javax.ejb.EJB;
    20 import javax.ejb.SessionContext;
    21 import javax.ejb.Stateless;
    22 
    23 /**
    24  *
    25  * @author fiki
    26  */
    27 @Stateless
    28 public class PodnikEJB implements PodnikRemote {
    29 
    30 	private static final Logger log = Logger.getLogger(PodnikEJB.class.getSimpleName());
    31 	@EJB
    32 	private PodnikDAO podnikDAO;
    33 	@Resource
    34 	private SessionContext ctx;
    35 
    36 	public Collection<Podnik> getPodniky() {
    37 		Collection<Podnik> vysledek = podnikDAO.getPodniky();
    38 		return vysledek;
    39 	}
    40 
    41 	public Collection<Podnik> getPodniky(Kategorie kategorie) {
    42 		Collection<Podnik> vysledek = podnikDAO.getPodniky(kategorie);
    43 		return vysledek;
    44 	}
    45 
    46 	public Collection<Podnik> getPodniky(Kurackost kurackost) {
    47 		Collection<Podnik> vysledek = podnikDAO.getPodniky(kurackost);
    48 		return vysledek;
    49 	}
    50 
    51 	public Podnik getPodnik(int id) {
    52 		return podnikDAO.getPodnik(id);
    53 	}
    54 
    55 	@RolesAllowed("opravneny")
    56 	public void zalozPodnik(Podnik p) {
    57 		/**
    58 		 * Podnik zakládáme jako neschválený a je potřeba ho dodatečně schválit,
    59 		 * to může udělat jen správce a ne uživatel (TODO)
    60 		 */
    61 		p.setSchvaleny(false);
    62 		p.setSpravce(ctx.getCallerPrincipal().getName());
    63 		podnikDAO.zaloz(p);
    64 		/** TODO: asynchronní počítání */
    65 		dopocitejSouradnice(p, new Geo());
    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 			if (dopocitejSouradnice(p, g)) {
    82 				pocetAktualizovanych++;
    83 			}
    84 		}
    85 
    86 		return pocetAktualizovanych;
    87 	}
    88 
    89 	/**
    90 	 * Na základě adresy (ulice, číslo, město) se pokusí zjistit souřadnice a uloží je k podniku.
    91 	 * @param p podnik
    92 	 * @param g poskytovatel geografických služeb
    93 	 * @return zda se souřadnice podařilo dopočítat
    94 	 */
    95 	private boolean dopocitejSouradnice(Podnik p, Geo g) {
    96 		Souradnice s = g.getSouradnice(p.getUlice() + " " + p.getCisloPopisne() + ", " + p.getMesto());
    97 		if (s != null) {
    98 			p.setSirka(s.getSirka());
    99 			p.setDelka(s.getDelka());
   100 			podnikDAO.uloz(p);
   101 			return true;
   102 		} else {
   103 			return false;
   104 		}
   105 	}
   106 
   107 	public void hlasuj(int podnik, boolean hlas, String ipAdresa) {
   108 
   109 		podnikDAO.hlasuj(podnik, hlas, ipAdresa);
   110 	}
   111 
   112 	public VysledekHlasovani getVysledekHlasovani(Podnik podnik) {
   113 		return podnikDAO.getVysledekHlasovani(podnik);
   114 	}
   115 
   116 	private void kontrolaKomentare(Komentar k) throws TexyVyjimka, KomentarovaVyjimka {
   117 		k.setUzivatel(ctx.getCallerPrincipal().getName());
   118 		k.setDatum(null);
   119 
   120 		if (k.getKomentar() == null || k.getKomentar().length() < 1) {
   121 			throw new KomentarovaVyjimka("Nechceme prázdné komentáře.", null);
   122 		}
   123 
   124 		/** Převedeme na XML */
   125 		switch (k.getTyp()) {
   126 			case PROSTY_TEXT:
   127 				k.setKomentar(Komentare.upravProstyText(k.getKomentar()));
   128 				break;
   129 			case TEXY:
   130 				Texy t = new Texy();
   131 				k.setKomentar(t.preved(k.getKomentar()));
   132 				break;
   133 			case XHTML:
   134 				k.setKomentar(Komentare.upravXHTML(k.getKomentar()));
   135 				break;
   136 		}
   137 		/** Přidáme kořenový element */
   138 		k.setKomentar(Komentare.obal(k.getKomentar()));
   139 
   140 		/** Zkontrolujeme XML */
   141 		Komentare.zkontroluj(k.getKomentar());
   142 	}
   143 
   144 	@RolesAllowed("opravneny")
   145 	public Komentar komentuj(Komentar k, boolean uloz) throws KomentarovaVyjimka, TexyVyjimka {
   146 		kontrolaKomentare(k);
   147 		if (uloz) {
   148 			podnikDAO.zaloz(k);
   149 		}
   150 		return k;
   151 	}
   152 }