java/nekurak.net-ejb/src/java/cz/frantovo/nekurak/ejb/PodnikEJB.java
author František Kučera <franta-hg@frantovo.cz>
Tue Jun 08 10:42:58 2010 +0200 (2010-06-08)
changeset 131 0d1cba59734b
parent 130 1bbff3f5181f
child 132 1ca0d7fdbe51
permissions -rw-r--r--
XML schéma pro komentáře
     1 package cz.frantovo.nekurak.ejb;
     2 
     3 import cz.frantovo.nekurak.dao.PodnikDAO;
     4 import cz.frantovo.nekurak.dto.Komentar;
     5 import cz.frantovo.nekurak.dto.Podnik;
     6 import cz.frantovo.nekurak.dto.VysledekHlasovani;
     7 import cz.frantovo.nekurak.ext.Geo;
     8 import cz.frantovo.nekurak.ext.Geo.Souradnice;
     9 import cz.frantovo.nekurak.ext.Texy;
    10 import cz.frantovo.nekurak.util.Komentare;
    11 import java.util.Collection;
    12 import java.util.Date;
    13 import java.util.logging.Logger;
    14 import javax.annotation.Resource;
    15 import javax.annotation.security.RolesAllowed;
    16 import javax.ejb.EJB;
    17 import javax.ejb.SessionContext;
    18 import javax.ejb.Stateless;
    19 
    20 /**
    21  *
    22  * @author fiki
    23  */
    24 @Stateless
    25 public class PodnikEJB implements PodnikRemote {
    26 
    27     private static final Logger log = Logger.getLogger(PodnikEJB.class.getSimpleName());
    28     @EJB
    29     private PodnikDAO podnikDAO;
    30     @Resource
    31     private SessionContext ctx;
    32 
    33     public Collection<Podnik> getPodniky() {
    34 	Collection<Podnik> vysledek = podnikDAO.getPodniky();
    35 	return vysledek;
    36     }
    37 
    38     public Podnik getPodnik(int id) {
    39 	return podnikDAO.getPodnik(id);
    40     }
    41 
    42     @RolesAllowed("opravneny")
    43     public void zalozPodnik(Podnik p) {
    44 	p.setSpravce(ctx.getCallerPrincipal().getName());
    45 	podnikDAO.zaloz(p);
    46     }
    47 
    48     @RolesAllowed("opravneny")
    49     public void upravPodnik(Podnik p) {
    50 	podnikDAO.uloz(p);
    51     }
    52 
    53     public int dopocitejSouradnice() {
    54 	/**
    55 	 * TODO: refaktorovat, změnit datové typy, souřadnice…
    56 	 */
    57 	Geo g = new Geo();
    58 	int pocetAktualizovanych = 0;
    59 
    60 	for (Podnik p : podnikDAO.getPodnikyBezSouradnic()) {
    61 	    Souradnice s = g.getSouradnice(p.getUlice() + " " + p.getCisloPopisne() + ", " + p.getMesto());
    62 	    if (s != null) {
    63 		pocetAktualizovanych++;
    64 		p.setSirka(s.getSirka());
    65 		p.setDelka(s.getDelka());
    66 		podnikDAO.uloz(p);
    67 	    }
    68 	}
    69 
    70 	return pocetAktualizovanych;
    71     }
    72 
    73     public void hlasuj(int podnik, boolean hlas, String ipAdresa) {
    74 
    75 	podnikDAO.hlasuj(podnik, hlas, ipAdresa);
    76     }
    77 
    78     public VysledekHlasovani getVysledekHlasovani(int podnik) {
    79 	return podnikDAO.getVysledekHlasovani(podnik);
    80     }
    81 
    82     @RolesAllowed("opravneny")
    83     public void komentuj(Komentar k) {
    84 	k.setUzivatel(ctx.getCallerPrincipal().getName());
    85 	k.setDatum(new Date());
    86 
    87 	/** Převedeme na XML */
    88 	switch (k.getTyp()) {
    89 	    case PROSTY_TEXT:
    90 		k.setKomentar(Komentare.upravProstyText(k.getKomentar()));
    91 		k.setKomentar(Komentare.obal(k.getKomentar()));
    92 		break;
    93 	    case TEXY:
    94 		Texy t = new Texy();
    95 		k.setKomentar(t.preved(k.getKomentar()));
    96 		k.setKomentar(Komentare.obal(k.getKomentar()));
    97 		break;
    98 	}
    99 
   100 	/** Zkontrolujeme XML */
   101 	
   102 
   103 
   104 	log.severe("Komentář: " + k.getNadpis() + " | " + k.getKomentar() + " | " + k.getTyp());
   105     }
   106 }