franta-hg@177: package cz.frantovo.nekurak.servlet; franta-hg@177: franta-hg@179: import cz.frantovo.nekurak.dto.Podnik; franta-hg@179: import cz.frantovo.nekurak.ejb.PodnikRemote; franta-hg@177: import java.io.IOException; franta-hg@179: import java.io.UnsupportedEncodingException; franta-hg@179: import java.net.URLEncoder; franta-hg@179: import javax.ejb.EJB; franta-hg@177: import javax.servlet.ServletException; franta-hg@177: import javax.servlet.http.HttpServletRequest; franta-hg@177: import javax.servlet.http.HttpServletResponse; franta-hg@177: franta-hg@177: /** franta-hg@177: * franta-hg@177: * @author fiki franta-hg@177: */ franta-hg@179: public class PodnikServlet extends SuperServlet { franta-hg@179: franta-hg@179: @EJB franta-hg@179: PodnikRemote ejb; franta-hg@177: franta-hg@177: @Override franta-hg@177: protected void doGet(HttpServletRequest požadavek, HttpServletResponse odpověď) throws ServletException, IOException { franta-hg@177: super.doGet(požadavek, odpověď); franta-hg@177: franta-hg@177: String[] parametryCesty = (požadavek.getPathInfo().split("/")); franta-hg@177: int id = Integer.parseInt(parametryCesty[1]); franta-hg@179: franta-hg@179: Podnik p = ejb.getPodnik(id); franta-hg@179: franta-hg@179: if (p == null) { franta-hg@179: odpověď.sendError(HttpServletResponse.SC_NOT_FOUND); franta-hg@179: } else { franta-hg@179: požadavek.setAttribute("podnik", p); franta-hg@179: požadavek.setAttribute("mapa", getMapa(p)); franta-hg@179: zpracujJSP("detail.jsp", požadavek, odpověď); franta-hg@179: } franta-hg@179: } franta-hg@179: franta-hg@179: /** franta-hg@179: * @return URL mapy – obrázku nebo null při chybě nebo absenci souřadnic franta-hg@179: */ franta-hg@179: private String getMapa(Podnik p) { franta-hg@179: if (p == null || p.getSirka() == null || p.getDelka() == null) { franta-hg@179: return null; franta-hg@179: } else { franta-hg@179: try { franta-hg@179: String souradnice = URLEncoder.encode(p.getSirka() + ", " + p.getDelka(), "UTF-8"); franta-hg@191: return "https://maps.google.com/maps/api/staticmap?size=640x400&sensor=false&markers=size:big|color:green|" + souradnice; franta-hg@179: } catch (UnsupportedEncodingException e) { franta-hg@179: return null; franta-hg@179: } franta-hg@179: } franta-hg@177: } franta-hg@177: }