java/nekurak.net-ejb/src/java/cz/frantovo/nekurak/ejb/PodnikEJB.java
author František Kučera <franta-hg@frantovo.cz>
Thu Jul 21 15:20:32 2011 +0200 (2011-07-21)
changeset 192 f106b3043c0c
parent 176 2472d9e9c0aa
child 197 40b7aa9126d0
permissions -rw-r--r--
Doplněno @Override
     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 	@Override
    37 	public Collection<Podnik> getPodniky() {
    38 		Collection<Podnik> vysledek = podnikDAO.getPodniky();
    39 		return vysledek;
    40 	}
    41 
    42 	@Override
    43 	public Collection<Podnik> getPodniky(Kategorie kategorie) {
    44 		Collection<Podnik> vysledek = podnikDAO.getPodniky(kategorie);
    45 		return vysledek;
    46 	}
    47 
    48 	@Override
    49 	public Collection<Podnik> getPodniky(Kurackost kurackost) {
    50 		Collection<Podnik> vysledek = podnikDAO.getPodniky(kurackost);
    51 		return vysledek;
    52 	}
    53 
    54 	@Override
    55 	public Podnik getPodnik(int id) {
    56 		return podnikDAO.getPodnik(id);
    57 	}
    58 
    59 	@Override
    60 	@RolesAllowed("opravneny")
    61 	public void zalozPodnik(Podnik p) {
    62 		/**
    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)
    65 		 */
    66 		p.setSchvaleny(false);
    67 		p.setSpravce(ctx.getCallerPrincipal().getName());
    68 		podnikDAO.zaloz(p);
    69 		/** TODO: asynchronní počítání */
    70 		dopocitejSouradnice(p, new Geo());
    71 	}
    72 
    73 	@Override
    74 	@RolesAllowed("opravneny")
    75 	public void upravPodnik(Podnik p) {
    76 		podnikDAO.uloz(p);
    77 	}
    78 
    79 	@Override
    80 	public int dopocitejSouradnice() {
    81 		/**
    82 		 * TODO: refaktorovat, změnit datové typy, souřadnice…
    83 		 */
    84 		Geo g = new Geo();
    85 		int pocetAktualizovanych = 0;
    86 
    87 		for (Podnik p : podnikDAO.getPodnikyBezSouradnic()) {
    88 			if (dopocitejSouradnice(p, g)) {
    89 				pocetAktualizovanych++;
    90 			}
    91 		}
    92 
    93 		return pocetAktualizovanych;
    94 	}
    95 
    96 	/**
    97 	 * Na základě adresy (ulice, číslo, město) se pokusí zjistit souřadnice a uloží je k podniku.
    98 	 * @param p podnik
    99 	 * @param g poskytovatel geografických služeb
   100 	 * @return zda se souřadnice podařilo dopočítat
   101 	 */
   102 	private boolean dopocitejSouradnice(Podnik p, Geo g) {
   103 		Souradnice s = g.getSouradnice(p.getUlice() + " " + p.getCisloPopisne() + ", " + p.getMesto());
   104 		if (s != null) {
   105 			p.setSirka(s.getSirka());
   106 			p.setDelka(s.getDelka());
   107 			podnikDAO.uloz(p);
   108 			return true;
   109 		} else {
   110 			return false;
   111 		}
   112 	}
   113 
   114 	@Override
   115 	public void hlasuj(int podnik, boolean hlas, String ipAdresa) {
   116 
   117 		podnikDAO.hlasuj(podnik, hlas, ipAdresa);
   118 	}
   119 
   120 	@Override
   121 	public VysledekHlasovani getVysledekHlasovani(Podnik podnik) {
   122 		return podnikDAO.getVysledekHlasovani(podnik);
   123 	}
   124 
   125 	private void kontrolaKomentare(Komentar k) throws TexyVyjimka, KomentarovaVyjimka {
   126 		k.setUzivatel(ctx.getCallerPrincipal().getName());
   127 		k.setDatum(null);
   128 
   129 		if (k.getKomentar() == null || k.getKomentar().length() < 1) {
   130 			throw new KomentarovaVyjimka("Nechceme prázdné komentáře.", null);
   131 		}
   132 
   133 		/** Převedeme na XML */
   134 		switch (k.getTyp()) {
   135 			case PROSTY_TEXT:
   136 				k.setKomentar(Komentare.upravProstyText(k.getKomentar()));
   137 				break;
   138 			case TEXY:
   139 				Texy t = new Texy();
   140 				k.setKomentar(t.preved(k.getKomentar()));
   141 				break;
   142 			case XHTML:
   143 				k.setKomentar(Komentare.upravXHTML(k.getKomentar()));
   144 				break;
   145 		}
   146 		/** Přidáme kořenový element */
   147 		k.setKomentar(Komentare.obal(k.getKomentar()));
   148 
   149 		/** Zkontrolujeme XML */
   150 		Komentare.zkontroluj(k.getKomentar());
   151 	}
   152 
   153 	@Override
   154 	@RolesAllowed("opravneny")
   155 	public Komentar komentuj(Komentar k, boolean uloz) throws KomentarovaVyjimka, TexyVyjimka {
   156 		kontrolaKomentare(k);
   157 		if (uloz) {
   158 			podnikDAO.zaloz(k);
   159 		}
   160 		return k;
   161 	}
   162 }