Začali jsme používat SuperDAO
authorFrantišek Kučera <franta-hg@frantovo.cz>
Wed, 11 Feb 2009 22:25:09 +0100
changeset 638625daa449b
parent 5 62bddd9b51fb
child 7 29bb67aa99fd
Začali jsme používat SuperDAO
Umíme načítat data z databáze – zobrazujeme seznam cílů.
java/HrisniciSpameri/nbproject/build-impl.xml
java/HrisniciSpameri/nbproject/genfiles.properties
java/HrisniciSpameri/nbproject/private/private.xml
java/HrisniciSpameri/nbproject/project.properties
java/HrisniciSpameri/nbproject/project.xml
java/HrisniciSpameri/src/java/cz/frantovo/hrisniciSpameri/DenniSouhrn.java
java/HrisniciSpameri/src/java/cz/frantovo/hrisniciSpameri/GenerovaciServlet.java
java/HrisniciSpameri/src/java/cz/frantovo/hrisniciSpameri/dao/CilDAO.java
java/HrisniciSpameri/src/java/cz/frantovo/hrisniciSpameri/dao/CilDAO.sql.xml
java/HrisniciSpameri/src/java/cz/frantovo/hrisniciSpameri/dao/HrisniciSuperDAO.java
java/HrisniciSpameri/src/java/cz/frantovo/hrisniciSpameri/dao/HrisniciSuperDAO.xml
java/HrisniciSpameri/src/java/cz/frantovo/hrisniciSpameri/dao/SouhrnDAO.java
java/HrisniciSpameri/src/java/cz/frantovo/hrisniciSpameri/dao/SouhrnDAO.sql.xml
java/HrisniciSpameri/web/souhrn.jspx
     1.1 --- a/java/HrisniciSpameri/nbproject/build-impl.xml	Wed Feb 11 20:08:00 2009 +0100
     1.2 +++ b/java/HrisniciSpameri/nbproject/build-impl.xml	Wed Feb 11 22:25:09 2009 +0100
     1.3 @@ -360,8 +360,12 @@
     1.4      <!--
     1.5                  COMPILATION SECTION
     1.6              -->
     1.7 -    <target depends="init" if="no.dist.ear.dir" name="deps-module-jar" unless="no.deps"/>
     1.8 -    <target depends="init" if="dist.ear.dir" name="deps-ear-jar" unless="no.deps"/>
     1.9 +    <target depends="init" if="no.dist.ear.dir" name="deps-module-jar" unless="no.deps">
    1.10 +        <ant antfile="${project.SuperDAO}/build.xml" inheritall="false" target="jar"/>
    1.11 +    </target>
    1.12 +    <target depends="init" if="dist.ear.dir" name="deps-ear-jar" unless="no.deps">
    1.13 +        <ant antfile="${project.SuperDAO}/build.xml" inheritall="false" target="jar"/>
    1.14 +    </target>
    1.15      <target depends="init, deps-module-jar, deps-ear-jar" name="deps-jar" unless="no.deps"/>
    1.16      <target depends="init,deps-jar" name="-pre-pre-compile">
    1.17          <mkdir dir="${build.classes.dir}"/>
    1.18 @@ -498,10 +502,15 @@
    1.19      </target>
    1.20      <target depends="init,compile,compile-jsps,-pre-dist,-do-dist-with-manifest,-do-dist-without-manifest" name="do-dist"/>
    1.21      <target depends="init" if="dist.ear.dir" name="library-inclusion-in-manifest">
    1.22 +        <copyfiles files="${reference.SuperDAO.jar}" iftldtodir="${build.web.dir}/WEB-INF" manifestproperty="manifest.reference.SuperDAO.jar" todir="${dist.ear.dir}"/>
    1.23          <mkdir dir="${build.web.dir}/META-INF"/>
    1.24 -        <manifest file="${build.web.dir}/META-INF/MANIFEST.MF" mode="update"/>
    1.25 +        <manifest file="${build.web.dir}/META-INF/MANIFEST.MF" mode="update">
    1.26 +            <attribute name="Class-Path" value="${manifest.reference.SuperDAO.jar} "/>
    1.27 +        </manifest>
    1.28      </target>
    1.29 -    <target depends="init" name="library-inclusion-in-archive" unless="dist.ear.dir"/>
    1.30 +    <target depends="init" name="library-inclusion-in-archive" unless="dist.ear.dir">
    1.31 +        <copyfiles files="${reference.SuperDAO.jar}" todir="${build.web.dir}/WEB-INF/lib"/>
    1.32 +    </target>
    1.33      <target depends="init,compile,compile-jsps,-pre-dist,library-inclusion-in-manifest" name="do-ear-dist">
    1.34          <dirname file="${dist.ear.war}" property="dist.jar.dir"/>
    1.35          <mkdir dir="${dist.jar.dir}"/>
    1.36 @@ -777,7 +786,9 @@
    1.37                  
    1.38                  CLEANUP SECTION
    1.39              -->
    1.40 -    <target depends="init" if="no.dist.ear.dir" name="deps-clean" unless="no.deps"/>
    1.41 +    <target depends="init" if="no.dist.ear.dir" name="deps-clean" unless="no.deps">
    1.42 +        <ant antfile="${project.SuperDAO}/build.xml" inheritall="false" target="clean"/>
    1.43 +    </target>
    1.44      <target depends="init" name="do-clean">
    1.45          <condition property="build.dir.to.clean" value="${build.web.dir}">
    1.46              <isset property="dist.ear.dir"/>
     2.1 --- a/java/HrisniciSpameri/nbproject/genfiles.properties	Wed Feb 11 20:08:00 2009 +0100
     2.2 +++ b/java/HrisniciSpameri/nbproject/genfiles.properties	Wed Feb 11 22:25:09 2009 +0100
     2.3 @@ -1,8 +1,8 @@
     2.4 -build.xml.data.CRC32=0d6b07a9
     2.5 +build.xml.data.CRC32=c00e70e9
     2.6  build.xml.script.CRC32=20d691ac
     2.7  build.xml.stylesheet.CRC32=c0ebde35
     2.8  # This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
     2.9  # Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
    2.10 -nbproject/build-impl.xml.data.CRC32=0d6b07a9
    2.11 -nbproject/build-impl.xml.script.CRC32=cde5e1cb
    2.12 +nbproject/build-impl.xml.data.CRC32=c00e70e9
    2.13 +nbproject/build-impl.xml.script.CRC32=81a189fc
    2.14  nbproject/build-impl.xml.stylesheet.CRC32=174458fc
     3.1 --- a/java/HrisniciSpameri/nbproject/private/private.xml	Wed Feb 11 20:08:00 2009 +0100
     3.2 +++ b/java/HrisniciSpameri/nbproject/private/private.xml	Wed Feb 11 22:25:09 2009 +0100
     3.3 @@ -1,12 +1,4 @@
     3.4  <?xml version="1.0" encoding="UTF-8"?>
     3.5  <project-private xmlns="http://www.netbeans.org/ns/project-private/1">
     3.6      <editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/1"/>
     3.7 -    <open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/1">
     3.8 -        <file>file:/home/fiki/src/HrisniciSpameri/java/HrisniciSpameri/src/java/cz/frantovo/hrisniciSpameri/DenniSouhrn.java</file>
     3.9 -        <file>file:/home/fiki/src/HrisniciSpameri/java/HrisniciSpameri/src/java/cz/frantovo/hrisniciSpameri/dto/Cil.java</file>
    3.10 -        <file>file:/home/fiki/src/HrisniciSpameri/java/HrisniciSpameri/src/java/cz/frantovo/hrisniciSpameri/dto/Souhrn.java</file>
    3.11 -        <file>file:/home/fiki/src/HrisniciSpameri/java/HrisniciSpameri/web/index.jsp</file>
    3.12 -        <file>file:/home/fiki/src/HrisniciSpameri/java/HrisniciSpameri/web/souhrn.jspx</file>
    3.13 -        <file>file:/home/fiki/src/HrisniciSpameri/java/HrisniciSpameri/web/styl.css</file>
    3.14 -    </open-files>
    3.15  </project-private>
     4.1 --- a/java/HrisniciSpameri/nbproject/project.properties	Wed Feb 11 20:08:00 2009 +0100
     4.2 +++ b/java/HrisniciSpameri/nbproject/project.properties	Wed Feb 11 22:25:09 2009 +0100
     4.3 @@ -23,7 +23,8 @@
     4.4  j2ee.platform=1.5
     4.5  j2ee.server.type=J2EE
     4.6  jar.compress=false
     4.7 -javac.classpath=
     4.8 +javac.classpath=\
     4.9 +    ${reference.SuperDAO.jar}
    4.10  # Space-separated list of extra javac options
    4.11  javac.compilerargs=
    4.12  javac.debug=true
    4.13 @@ -51,6 +52,8 @@
    4.14  lib.dir=${web.docbase.dir}/WEB-INF/lib
    4.15  persistence.xml.dir=${conf.dir}
    4.16  platform.active=default_platform
    4.17 +project.SuperDAO=../../../SuperDAO/java/SuperDAO
    4.18 +reference.SuperDAO.jar=${project.SuperDAO}/dist/SuperDAO.jar
    4.19  resource.dir=setup
    4.20  run.test.classpath=\
    4.21      ${javac.test.classpath}:\
     5.1 --- a/java/HrisniciSpameri/nbproject/project.xml	Wed Feb 11 20:08:00 2009 +0100
     5.2 +++ b/java/HrisniciSpameri/nbproject/project.xml	Wed Feb 11 22:25:09 2009 +0100
     5.3 @@ -5,7 +5,12 @@
     5.4          <data xmlns="http://www.netbeans.org/ns/web-project/3">
     5.5              <name>HrisniciSpameri</name>
     5.6              <minimum-ant-version>1.6.5</minimum-ant-version>
     5.7 -            <web-module-libraries/>
     5.8 +            <web-module-libraries>
     5.9 +                <library>
    5.10 +                    <file>${reference.SuperDAO.jar}</file>
    5.11 +                    <path-in-war>WEB-INF/lib</path-in-war>
    5.12 +                </library>
    5.13 +            </web-module-libraries>
    5.14              <web-module-additional-libraries/>
    5.15              <source-roots>
    5.16                  <root id="src.dir"/>
    5.17 @@ -14,5 +19,15 @@
    5.18                  <root id="test.src.dir"/>
    5.19              </test-roots>
    5.20          </data>
    5.21 +        <references xmlns="http://www.netbeans.org/ns/ant-project-references/1">
    5.22 +            <reference>
    5.23 +                <foreign-project>SuperDAO</foreign-project>
    5.24 +                <artifact-type>jar</artifact-type>
    5.25 +                <script>build.xml</script>
    5.26 +                <target>jar</target>
    5.27 +                <clean-target>clean</clean-target>
    5.28 +                <id>jar</id>
    5.29 +            </reference>
    5.30 +        </references>
    5.31      </configuration>
    5.32  </project>
     6.1 --- a/java/HrisniciSpameri/src/java/cz/frantovo/hrisniciSpameri/DenniSouhrn.java	Wed Feb 11 20:08:00 2009 +0100
     6.2 +++ b/java/HrisniciSpameri/src/java/cz/frantovo/hrisniciSpameri/DenniSouhrn.java	Wed Feb 11 22:25:09 2009 +0100
     6.3 @@ -1,11 +1,10 @@
     6.4  package cz.frantovo.hrisniciSpameri;
     6.5  
     6.6 +import cz.frantovo.hrisniciSpameri.dao.CilDAO;
     6.7  import cz.frantovo.hrisniciSpameri.dto.Cil;
     6.8  import cz.frantovo.hrisniciSpameri.dto.SitovaAdresa;
     6.9  import cz.frantovo.hrisniciSpameri.dto.Souhrn;
    6.10  import java.io.Serializable;
    6.11 -import java.net.InetAddress;
    6.12 -import java.net.UnknownHostException;
    6.13  import java.sql.Date;
    6.14  import java.text.DateFormat;
    6.15  import java.text.ParseException;
    6.16 @@ -79,4 +78,19 @@
    6.17  
    6.18          return souhrn;
    6.19      }
    6.20 +
    6.21 +    /**
    6.22 +     * @return Seznam všech cílů. Ale bez těchto hodnot: databáze a selekt.
    6.23 +     */
    6.24 +    public Collection<Cil> getSeznamCilu() {
    6.25 +        Collection<Cil> cile = new CilDAO().getCile();
    6.26 +
    6.27 +        /** ne všechno musíme posílat do JSP vrstvy */
    6.28 +        for (Cil c : cile) {
    6.29 +            c.setDatabaze(null);
    6.30 +            c.setSelekt(null);
    6.31 +        }
    6.32 +
    6.33 +        return cile;
    6.34 +    }
    6.35  }
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/java/HrisniciSpameri/src/java/cz/frantovo/hrisniciSpameri/GenerovaciServlet.java	Wed Feb 11 22:25:09 2009 +0100
     7.3 @@ -0,0 +1,11 @@
     7.4 +package cz.frantovo.hrisniciSpameri;
     7.5 +
     7.6 +/**
     7.7 + * Servlet, který vygeneruje denní souhrn.
     7.8 + * Pokud pro daný den už souhr vygenerovaný byl, smaže ho.
     7.9 + * Vše probíhá v jedné transakci.
    7.10 + * @author fiki
    7.11 + */
    7.12 +public class GenerovaciServlet {
    7.13 +
    7.14 +}
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/java/HrisniciSpameri/src/java/cz/frantovo/hrisniciSpameri/dao/CilDAO.java	Wed Feb 11 22:25:09 2009 +0100
     8.3 @@ -0,0 +1,52 @@
     8.4 +package cz.frantovo.hrisniciSpameri.dao;
     8.5 +
     8.6 +import cz.frantovo.hrisniciSpameri.dto.Cil;
     8.7 +import java.sql.Connection;
     8.8 +import java.sql.PreparedStatement;
     8.9 +import java.sql.ResultSet;
    8.10 +import java.util.ArrayList;
    8.11 +import java.util.Collection;
    8.12 +import java.util.logging.Level;
    8.13 +
    8.14 +/**
    8.15 + *
    8.16 + * @author fiki
    8.17 + */
    8.18 +public class CilDAO extends HrisniciSuperDAO {
    8.19 +
    8.20 +    private enum SQL {
    8.21 +
    8.22 +        VSECHNY_CILE
    8.23 +    }
    8.24 +
    8.25 +    public Collection<Cil> getCile() {
    8.26 +        Collection<Cil> cile = new ArrayList<Cil>();
    8.27 +
    8.28 +        Connection db = getSpojeni(null);
    8.29 +        if (db == null) {
    8.30 +            return null;
    8.31 +        } else {
    8.32 +            PreparedStatement ps = null;
    8.33 +            ResultSet rs = null;
    8.34 +            try {
    8.35 +                ps = db.prepareStatement(getSQL(SQL.VSECHNY_CILE));
    8.36 +                rs = ps.executeQuery();
    8.37 +                while (rs.next()) {
    8.38 +                    Cil c = new Cil();
    8.39 +                    c.setId(rs.getInt("id"));
    8.40 +                    c.setNazev(rs.getString("nazev"));
    8.41 +                    c.setUrl(rs.getString("url"));
    8.42 +                    c.setSelekt(rs.getString("selekt"));
    8.43 +                    c.setDatabaze(rs.getString("databaze"));
    8.44 +                    cile.add(c);
    8.45 +                }
    8.46 +            } catch (Exception e) {
    8.47 +                log.log(Level.SEVERE, "Chyba při získávání seznamu cílů z databáze", e);
    8.48 +            } finally {
    8.49 +                zavri(db, ps, rs);
    8.50 +            }
    8.51 +        }
    8.52 +
    8.53 +        return cile;
    8.54 +    }
    8.55 +}
     9.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.2 +++ b/java/HrisniciSpameri/src/java/cz/frantovo/hrisniciSpameri/dao/CilDAO.sql.xml	Wed Feb 11 22:25:09 2009 +0100
     9.3 @@ -0,0 +1,11 @@
     9.4 +<?xml version="1.0" encoding="UTF-8"?>
     9.5 +<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
     9.6 +<properties>
     9.7 +    <entry key="VSECHNY_CILE">
     9.8 +        <![CDATA[
     9.9 +        SELECT *
    9.10 +        FROM cil
    9.11 +        ORDER BY nazev
    9.12 +        ]]>
    9.13 +    </entry>
    9.14 +</properties>
    9.15 \ No newline at end of file
    10.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    10.2 +++ b/java/HrisniciSpameri/src/java/cz/frantovo/hrisniciSpameri/dao/HrisniciSuperDAO.java	Wed Feb 11 22:25:09 2009 +0100
    10.3 @@ -0,0 +1,60 @@
    10.4 +package cz.frantovo.hrisniciSpameri.dao;
    10.5 +
    10.6 +import cz.frantovo.superDAO.SuperDAO;
    10.7 +import java.sql.Connection;
    10.8 +import java.sql.SQLException;
    10.9 +import java.util.logging.Level;
   10.10 +import javax.naming.InitialContext;
   10.11 +import javax.naming.NamingException;
   10.12 +import javax.sql.DataSource;
   10.13 +
   10.14 +/**
   10.15 + *
   10.16 + * @author fiki
   10.17 + */
   10.18 +public class HrisniciSuperDAO extends SuperDAO {
   10.19 +
   10.20 +    protected InitialContext kontext = null;
   10.21 +
   10.22 +    protected enum DATABAZE {
   10.23 +
   10.24 +        HRISNICI
   10.25 +    }
   10.26 +
   10.27 +    public HrisniciSuperDAO() {
   10.28 +        try {
   10.29 +            kontext = new InitialContext();
   10.30 +        } catch (NamingException ex) {
   10.31 +            log.log(Level.SEVERE, "InitialContext problem", ex);
   10.32 +        }
   10.33 +    }
   10.34 +
   10.35 +    /**
   10.36 +     * <ul>
   10.37 +     *  <li>Webová část zadává jako parametr null →
   10.38 +     *  dostane výchozí spojení do PostgreSQL databáze obsahující vygenerované souhrny.</li>
   10.39 +     *  <li>Generovací servlet zadává JNDI jméno databáze cíle →
   10.40 +     *  dostane zdrojovou databázi, ze které vygeneruje denní souhrny a výsledek uloží do výchozí databáze.</li>
   10.41 +     * </ul>
   10.42 +     * @param jndi JNDI jméno, pokud je null, vrací výchozí spojení.
   10.43 +     * @return Databázové spojení
   10.44 +     */
   10.45 +    protected Connection getSpojeni(String jndi) {
   10.46 +        if (jndi == null || jndi.length() < 1) {
   10.47 +            jndi = getVlastnost(DATABAZE.HRISNICI, HrisniciSuperDAO.class);
   10.48 +        }
   10.49 +        jndi = orizniJNDI(jndi);
   10.50 +        DataSource zdroj = null;
   10.51 +        try {
   10.52 +            System.out.println("XXXXXX:" + jndi + ":XXXXX");
   10.53 +            zdroj = (DataSource) kontext.lookup(jndi);
   10.54 +            return zdroj.getConnection();
   10.55 +        } catch (NamingException ex) {
   10.56 +            log.log(Level.SEVERE, "getSpojeni: lookup", ex);
   10.57 +            return null;
   10.58 +        } catch (SQLException ex) {
   10.59 +            log.log(Level.SEVERE, "getSpojeni: sql", ex);
   10.60 +            return null;
   10.61 +        }
   10.62 +    }
   10.63 +}
    11.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    11.2 +++ b/java/HrisniciSpameri/src/java/cz/frantovo/hrisniciSpameri/dao/HrisniciSuperDAO.xml	Wed Feb 11 22:25:09 2009 +0100
    11.3 @@ -0,0 +1,9 @@
    11.4 +<?xml version="1.0" encoding="UTF-8"?>
    11.5 +<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
    11.6 +<properties>
    11.7 +    <entry key="HRISNICI">
    11.8 +        <![CDATA[
    11.9 +        jdbc/HrisniciSpameri
   11.10 +        ]]>
   11.11 +    </entry>
   11.12 +</properties>
   11.13 \ No newline at end of file
    12.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    12.2 +++ b/java/HrisniciSpameri/src/java/cz/frantovo/hrisniciSpameri/dao/SouhrnDAO.java	Wed Feb 11 22:25:09 2009 +0100
    12.3 @@ -0,0 +1,9 @@
    12.4 +package cz.frantovo.hrisniciSpameri.dao;
    12.5 +
    12.6 +/**
    12.7 + *
    12.8 + * @author fiki
    12.9 + */
   12.10 +public class SouhrnDAO extends HrisniciSuperDAO {
   12.11 +    
   12.12 +}
    13.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    13.2 +++ b/java/HrisniciSpameri/src/java/cz/frantovo/hrisniciSpameri/dao/SouhrnDAO.sql.xml	Wed Feb 11 22:25:09 2009 +0100
    13.3 @@ -0,0 +1,9 @@
    13.4 +<?xml version="1.0" encoding="UTF-8"?>
    13.5 +<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
    13.6 +<properties>
    13.7 +    <entry key="AHOJ">
    13.8 +        <![CDATA[
    13.9 +        Tak co, jak to jde? ;-)
   13.10 +        ]]>
   13.11 +    </entry>
   13.12 +</properties>
   13.13 \ No newline at end of file
    14.1 --- a/java/HrisniciSpameri/web/souhrn.jspx	Wed Feb 11 20:08:00 2009 +0100
    14.2 +++ b/java/HrisniciSpameri/web/souhrn.jspx	Wed Feb 11 22:25:09 2009 +0100
    14.3 @@ -14,8 +14,25 @@
    14.4  
    14.5  
    14.6  
    14.7 +    <h2>Seznam cílů</h2>
    14.8 +    <ul>
    14.9 +        <c:forEach var="c" items="${denniSouhrn.seznamCilu}">
   14.10 +            <li>
   14.11 +                <c:choose>
   14.12 +                    <c:when test="${c.url == null}">
   14.13 +                        <c:out value="${c.nazev}"/>
   14.14 +                    </c:when>
   14.15 +                    <c:otherwise>
   14.16 +                        <a href="${f:escapeXml(c.url)}"><c:out value="${c.nazev}"/></a>
   14.17 +                    </c:otherwise>
   14.18 +                </c:choose>                
   14.19 +            </li>
   14.20 +        </c:forEach>
   14.21 +    </ul>
   14.22  
   14.23  
   14.24 +    <h2>Denní souhrn</h2>
   14.25 +
   14.26      <table>
   14.27          <thead>
   14.28              <td title="Jméno a příjmení">IP adresa</td>