1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/java/nekurak.net-web/src/java/cz/frantovo/nekurak/servlet/PodnikServlet.java Wed Jan 26 03:23:32 2011 +0100
1.3 @@ -0,0 +1,55 @@
1.4 +package cz.frantovo.nekurak.servlet;
1.5 +
1.6 +import cz.frantovo.nekurak.dto.Podnik;
1.7 +import cz.frantovo.nekurak.ejb.PodnikRemote;
1.8 +import java.io.IOException;
1.9 +import java.io.UnsupportedEncodingException;
1.10 +import java.net.URLEncoder;
1.11 +import javax.ejb.EJB;
1.12 +import javax.servlet.ServletException;
1.13 +import javax.servlet.http.HttpServletRequest;
1.14 +import javax.servlet.http.HttpServletResponse;
1.15 +
1.16 +/**
1.17 + *
1.18 + * @author fiki
1.19 + */
1.20 +public class PodnikServlet extends SuperServlet {
1.21 +
1.22 + @EJB
1.23 + PodnikRemote ejb;
1.24 +
1.25 + @Override
1.26 + protected void doGet(HttpServletRequest požadavek, HttpServletResponse odpověď) throws ServletException, IOException {
1.27 + super.doGet(požadavek, odpověď);
1.28 +
1.29 + String[] parametryCesty = (požadavek.getPathInfo().split("/"));
1.30 + int id = Integer.parseInt(parametryCesty[1]);
1.31 +
1.32 + Podnik p = ejb.getPodnik(id);
1.33 +
1.34 + if (p == null) {
1.35 + odpověď.sendError(HttpServletResponse.SC_NOT_FOUND);
1.36 + } else {
1.37 + požadavek.setAttribute("podnik", p);
1.38 + požadavek.setAttribute("mapa", getMapa(p));
1.39 + zpracujJSP("detail.jsp", požadavek, odpověď);
1.40 + }
1.41 + }
1.42 +
1.43 + /**
1.44 + * @return URL mapy – obrázku nebo null při chybě nebo absenci souřadnic
1.45 + */
1.46 + private String getMapa(Podnik p) {
1.47 + if (p == null || p.getSirka() == null || p.getDelka() == null) {
1.48 + return null;
1.49 + } else {
1.50 + try {
1.51 + String souradnice = URLEncoder.encode(p.getSirka() + ", " + p.getDelka(), "UTF-8");
1.52 + return "http://maps.google.com/maps/api/staticmap?size=400x400&sensor=false&markers=size:big|color:green|" + souradnice;
1.53 + } catch (UnsupportedEncodingException e) {
1.54 + return null;
1.55 + }
1.56 + }
1.57 + }
1.58 +}