# HG changeset patch # User František Kučera # Date 1271196752 -7200 # Node ID 01be78803f73dc3c0418fa3b916fc92b37923c9a # Parent fd22f66e812d319326890debc394e8b517816328 Webové služby, zjišťování souřadnic podniků. diff -r fd22f66e812d -r 01be78803f73 java/nekurak.net-ejb/src/conf/Podnik.hbm.xml --- a/java/nekurak.net-ejb/src/conf/Podnik.hbm.xml Mon Apr 12 12:09:45 2010 +0200 +++ b/java/nekurak.net-ejb/src/conf/Podnik.hbm.xml Wed Apr 14 00:12:32 2010 +0200 @@ -21,6 +21,8 @@ + + \ No newline at end of file diff -r fd22f66e812d -r 01be78803f73 java/nekurak.net-ejb/src/java/cz/frantovo/nekurak/dao/PodnikDAO.java --- a/java/nekurak.net-ejb/src/java/cz/frantovo/nekurak/dao/PodnikDAO.java Mon Apr 12 12:09:45 2010 +0200 +++ b/java/nekurak.net-ejb/src/java/cz/frantovo/nekurak/dao/PodnikDAO.java Wed Apr 14 00:12:32 2010 +0200 @@ -25,6 +25,18 @@ return dotaz.getResultList(); } + /** + * @return podniky, které nemají souřadnice (null, null) + */ + public Collection getPodnikyBezSouradnic() { + Query dotaz = em.createQuery("FROM " + DAO.t(Podnik.class) + " o WHERE sirka IS NULL AND delka IS NULL"); + return dotaz.getResultList(); + } + + public Podnik getPodnik(int id) { + return em.find(Podnik.class, id); + } + public void zaloz(Podnik p) { if (p.getDatum() == null) { p.setDatum(new Date()); diff -r fd22f66e812d -r 01be78803f73 java/nekurak.net-ejb/src/java/cz/frantovo/nekurak/ejb/Geo.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/nekurak.net-ejb/src/java/cz/frantovo/nekurak/ejb/Geo.java Wed Apr 14 00:12:32 2010 +0200 @@ -0,0 +1,102 @@ +package cz.frantovo.nekurak.ejb; + +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +/** + * TODO: přesunout jinam a převést na EJB. + * @author fiki + */ +public class Geo { + + private static final Logger log = Logger.getLogger(Geo.class.getSimpleName()); + + /** + * Převede poštovní adresu na zeměpisné souřadnice. + * @param adresa + * @return souřadnice k dané adrese nebo null, v případě chyby. + */ + public Souradnice getSouradnice(String adresa) { + try { + + /** + * TODO: naprosto zprasené → předělat → + * http://code.google.com/intl/cs/apis/maps/documentation/geocoding/#XMLParsing + */ + DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); + Document d = db.parse(sestavURL(adresa)); + NodeList mista = d.getElementsByTagName("location"); + Node misto = mista.item(0); + NodeList potomci = misto.getChildNodes(); + String delka = null; + String sirka = null; + for (int i = 0; i < potomci.getLength(); i++) { + Node p = potomci.item(i); + System.out.println("XML:" + p.getNodeName() + "|" + p.getTextContent()); + if ("lat".equals(p.getNodeName())) { + sirka = p.getTextContent(); + } + if ("lng".equals(p.getNodeName())) { + delka = p.getTextContent(); + } + } + + if (sirka == null || delka == null) { + log.log(Level.WARNING, "Selhalo zjišťování souřadnic – šířka nebo délka jsou null – pro adresu: " + adresa); + return null; + } else { + return new Souradnice(Double.parseDouble(sirka), Double.parseDouble(delka)); + } + } catch (Exception e) { + log.log(Level.WARNING, "Selhalo zjišťování souřadnic pro adresu: " + adresa, e); + return null; + } + } + + private static String sestavURL(String adresa) throws UnsupportedEncodingException { + return "http://maps.google.com/maps/api/geocode/xml?sensor=false&address=" + URLEncoder.encode(adresa, "UTF-8"); + } + + public class Souradnice { + + private double sirka; + private double delka; + + @Override + public String toString() { + return "šířka = " + sirka + "; délka = " + delka + ";"; + } + + public Souradnice(double sirka, double delka) { + this.sirka = sirka; + this.delka = delka; + } + + public String getLoc() { + return "Loc: " + sirka + ", " + delka; + } + + public double getDelka() { + return delka; + } + + public double getSirka() { + return sirka; + } + + public void setDelka(double delka) { + this.delka = delka; + } + + public void setSirka(double sirka) { + this.sirka = sirka; + } + } +} diff -r fd22f66e812d -r 01be78803f73 java/nekurak.net-ejb/src/java/cz/frantovo/nekurak/ejb/PodnikEJB.java --- a/java/nekurak.net-ejb/src/java/cz/frantovo/nekurak/ejb/PodnikEJB.java Mon Apr 12 12:09:45 2010 +0200 +++ b/java/nekurak.net-ejb/src/java/cz/frantovo/nekurak/ejb/PodnikEJB.java Wed Apr 14 00:12:32 2010 +0200 @@ -2,6 +2,7 @@ import cz.frantovo.nekurak.dao.PodnikDAO; import cz.frantovo.nekurak.dto.Podnik; +import cz.frantovo.nekurak.ejb.Geo.Souradnice; import java.util.Collection; import javax.annotation.Resource; import javax.annotation.security.RolesAllowed; @@ -26,6 +27,10 @@ return vysledek; } + public Podnik getPodnik(int id) { + return podnikDAO.getPodnik(id); + } + @RolesAllowed("opravneny") public void zalozPodnik(Podnik p) { p.setSpravce(ctx.getCallerPrincipal().getName()); @@ -36,4 +41,25 @@ public void upravPodnik(Podnik p) { podnikDAO.uloz(p); } + + public int dopocitejSouradnice() { + /** + * TODO: refaktorovat, změnit datové typy, souřadnice… + * dopočítávat jen ty, které ještě nemají souřadnice + */ + Geo g = new Geo(); + int pocetAktualizovanych = 0; + + for (Podnik p : podnikDAO.getPodniky()) { + Souradnice s = g.getSouradnice(p.getUlice() + " " + p.getCisloPopisne() + ", " + p.getMesto()); + if (s != null) { + pocetAktualizovanych++; + p.setSirka(s.getSirka()); + p.setDelka(s.getDelka()); + podnikDAO.uloz(p); + } + } + + return pocetAktualizovanych; + } } diff -r fd22f66e812d -r 01be78803f73 java/nekurak.net-lib/src/cz/frantovo/nekurak/dto/Podnik.java --- a/java/nekurak.net-lib/src/cz/frantovo/nekurak/dto/Podnik.java Mon Apr 12 12:09:45 2010 +0200 +++ b/java/nekurak.net-lib/src/cz/frantovo/nekurak/dto/Podnik.java Wed Apr 14 00:12:32 2010 +0200 @@ -22,6 +22,8 @@ private String mesto; private String spravce; private List fotky = new ArrayList(); + private Double sirka; + private Double delka; public Podnik() { } @@ -110,4 +112,20 @@ public void setFotky(List fotky) { this.fotky = fotky; } + + public Double getSirka() { + return sirka; + } + + public void setSirka(Double sirka) { + this.sirka = sirka; + } + + public Double getDelka() { + return delka; + } + + public void setDelka(Double delka) { + this.delka = delka; + } } diff -r fd22f66e812d -r 01be78803f73 java/nekurak.net-lib/src/cz/frantovo/nekurak/ejb/PodnikRemote.java --- a/java/nekurak.net-lib/src/cz/frantovo/nekurak/ejb/PodnikRemote.java Mon Apr 12 12:09:45 2010 +0200 +++ b/java/nekurak.net-lib/src/cz/frantovo/nekurak/ejb/PodnikRemote.java Wed Apr 14 00:12:32 2010 +0200 @@ -17,4 +17,7 @@ public void upravPodnik(Podnik p); + public Podnik getPodnik(int id); + + public int dopocitejSouradnice(); } diff -r fd22f66e812d -r 01be78803f73 java/nekurak.net-web/src/java/cz/frantovo/nekurak/web/Detail.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/nekurak.net-web/src/java/cz/frantovo/nekurak/web/Detail.java Wed Apr 14 00:12:32 2010 +0200 @@ -0,0 +1,51 @@ +package cz.frantovo.nekurak.web; + +import cz.frantovo.nekurak.dto.Podnik; +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * + * @author fiki + */ +public class Detail { + + private HledacSluzby hledac = new HledacSluzby(); + private int id; + private Podnik podnik; + + /** + * @return URL mapy – obrázku nebo null při chybě nebo absenci souřadnic + */ + public String getMapa() { + Podnik p = getPodnik(); + + if (p == null || p.getSirka() == null || p.getDelka() == null) { + return null; + } else { + try { + String souradnice = URLEncoder.encode(p.getSirka() + ", " + p.getDelka(), "UTF-8"); + return "http://maps.google.com/maps/api/staticmap?size=400x400&sensor=false¢er=" + souradnice; + } catch (UnsupportedEncodingException e) { + return null; + } + } + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public Podnik getPodnik() { + if (podnik == null) { + podnik = hledac.getPodnikEJB().getPodnik(id); + } + return podnik; + } +} diff -r fd22f66e812d -r 01be78803f73 java/nekurak.net-web/web/WEB-INF/casti/detail.jsp --- a/java/nekurak.net-web/web/WEB-INF/casti/detail.jsp Mon Apr 12 12:09:45 2010 +0200 +++ b/java/nekurak.net-web/web/WEB-INF/casti/detail.jsp Wed Apr 14 00:12:32 2010 +0200 @@ -6,8 +6,36 @@ xmlns:nk="urn:jsptagdir:/WEB-INF/tags/nekurak" version="2.0"> - -

TODO: Detail podniku ().

+ + + + + + +

+ + + + +

+ Souřadnice tohoto podniku nejsou známé.
+ (možná ještě nebyl proveden jejich výpočet) +

+
+ +

+ Zeměpisná šířka: ${detail.podnik.sirka}
+ Zeměpisná délka: ${detail.podnik.sirka}
+ Loc: ${detail.podnik.sirka}, ${detail.podnik.delka}
+

+

+ mapa +

+
+
+ + +
diff -r fd22f66e812d -r 01be78803f73 java/nekurak.net-ws/nbproject/build-impl.xml --- a/java/nekurak.net-ws/nbproject/build-impl.xml Mon Apr 12 12:09:45 2010 +0200 +++ b/java/nekurak.net-ws/nbproject/build-impl.xml Wed Apr 14 00:12:32 2010 +0200 @@ -391,8 +391,16 @@ - - + + + + + + + + + + @@ -528,10 +536,13 @@ + - + + + @@ -827,7 +838,9 @@ CLEANUP SECTION --> - + + + diff -r fd22f66e812d -r 01be78803f73 java/nekurak.net-ws/nbproject/genfiles.properties --- a/java/nekurak.net-ws/nbproject/genfiles.properties Mon Apr 12 12:09:45 2010 +0200 +++ b/java/nekurak.net-ws/nbproject/genfiles.properties Wed Apr 14 00:12:32 2010 +0200 @@ -3,8 +3,8 @@ build.xml.stylesheet.CRC32=c0ebde35@1.21.2.1 # This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. # Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. -nbproject/build-impl.xml.data.CRC32=4cb22909 -nbproject/build-impl.xml.script.CRC32=2e1faad3 +nbproject/build-impl.xml.data.CRC32=e6e63e07 +nbproject/build-impl.xml.script.CRC32=222fc177 nbproject/build-impl.xml.stylesheet.CRC32=b139b33b@1.21.2.1 nbproject/wsit-deploy.xml.data.CRC32=4cb22909 nbproject/wsit-deploy.xml.script.CRC32=deb039de diff -r fd22f66e812d -r 01be78803f73 java/nekurak.net-ws/nbproject/jax-ws.xml --- a/java/nekurak.net-ws/nbproject/jax-ws.xml Mon Apr 12 12:09:45 2010 +0200 +++ b/java/nekurak.net-ws/nbproject/jax-ws.xml Wed Apr 14 00:12:32 2010 +0200 @@ -4,6 +4,9 @@ cz.frantovo.nekurak.ws.Pokus + + cz.frantovo.nekurak.ws.Podnik + diff -r fd22f66e812d -r 01be78803f73 java/nekurak.net-ws/nbproject/jaxws-build.xml --- a/java/nekurak.net-ws/nbproject/jaxws-build.xml Mon Apr 12 12:09:45 2010 +0200 +++ b/java/nekurak.net-ws/nbproject/jaxws-build.xml Wed Apr 14 00:12:32 2010 +0200 @@ -24,6 +24,11 @@ + + + + + +

Pokusná služba

+

Podnik – služba

+ +