Hlasování: zobrazujeme grafy na základě hlasování uloženého v databázi.
authorFrantišek Kučera <franta-hg@frantovo.cz>
Tue, 27 Apr 2010 17:13:23 +0200
changeset 108f74a9fc683da
parent 107 e8371105fcc8
child 109 374ec52b5b69
Hlasování: zobrazujeme grafy na základě hlasování uloženého v databázi.
java/nekurak.net-ejb/nbproject/build-impl.xml
java/nekurak.net-ejb/nbproject/genfiles.properties
java/nekurak.net-ejb/nbproject/project.properties
java/nekurak.net-ejb/nbproject/project.xml
java/nekurak.net-ejb/src/java/cz/frantovo/nekurak/dao/PodnikDAO.java
java/nekurak.net-ejb/src/java/cz/frantovo/nekurak/dao/PodnikDAO.sql.xml
java/nekurak.net-lib/src/cz/frantovo/nekurak/dto/VysledekHlasovani.java
java/nekurak.net-web/src/java/cz/frantovo/nekurak/web/Hlasovani.java
java/nekurak.net-web/web/WEB-INF/tags/nekurak/hlasovani.tag
     1.1 --- a/java/nekurak.net-ejb/nbproject/build-impl.xml	Tue Apr 27 16:13:36 2010 +0200
     1.2 +++ b/java/nekurak.net-ejb/nbproject/build-impl.xml	Tue Apr 27 17:13:23 2010 +0200
     1.3 @@ -321,11 +321,17 @@
     1.4                  COMPILATION SECTION
     1.5              -->
     1.6      <target depends="init" if="no.dist.ear.dir" name="-deps-module-jar" unless="no.deps">
     1.7 +        <ant antfile="${project.SuperDAO}/build.xml" inheritall="false" target="jar">
     1.8 +            <property name="deploy.on.save" value="false"/>
     1.9 +        </ant>
    1.10          <ant antfile="${project.nekurak_net-lib}/build.xml" inheritall="false" target="jar">
    1.11              <property name="deploy.on.save" value="false"/>
    1.12          </ant>
    1.13      </target>
    1.14      <target depends="init" if="dist.ear.dir" name="-deps-ear-jar" unless="no.deps">
    1.15 +        <ant antfile="${project.SuperDAO}/build.xml" inheritall="false" target="jar">
    1.16 +            <property name="deploy.on.save" value="false"/>
    1.17 +        </ant>
    1.18          <ant antfile="${project.nekurak_net-lib}/build.xml" inheritall="false" target="jar">
    1.19              <property name="deploy.on.save" value="false"/>
    1.20          </ant>
    1.21 @@ -341,9 +347,11 @@
    1.22      </target>
    1.23      <target depends="compile" name="library-inclusion-in-archive">
    1.24          <copyfiles files="${reference.nekurak_net-lib.jar}" todir="${build.classes.dir}"/>
    1.25 +        <copyfiles files="${reference.SuperDAO.jar}" todir="${build.classes.dir}"/>
    1.26      </target>
    1.27      <target depends="compile" name="library-inclusion-in-manifest">
    1.28          <copyfiles files="${reference.nekurak_net-lib.jar}" todir="${dist.ear.dir}/lib"/>
    1.29 +        <copyfiles files="${reference.SuperDAO.jar}" todir="${dist.ear.dir}/lib"/>
    1.30          <manifest file="${build.ear.classes.dir}/META-INF/MANIFEST.MF" mode="update"/>
    1.31      </target>
    1.32      <target name="-copy-meta-inf">
    1.33 @@ -627,6 +635,7 @@
    1.34                  CLEANUP SECTION
    1.35              -->
    1.36      <target depends="init" if="no.dist.ear.dir" name="deps-clean" unless="no.deps">
    1.37 +        <ant antfile="${project.SuperDAO}/build.xml" inheritall="false" target="clean"/>
    1.38          <ant antfile="${project.nekurak_net-lib}/build.xml" inheritall="false" target="clean"/>
    1.39      </target>
    1.40      <target depends="init" name="-do-clean">
     2.1 --- a/java/nekurak.net-ejb/nbproject/genfiles.properties	Tue Apr 27 16:13:36 2010 +0200
     2.2 +++ b/java/nekurak.net-ejb/nbproject/genfiles.properties	Tue Apr 27 17:13:23 2010 +0200
     2.3 @@ -1,8 +1,8 @@
     2.4 -build.xml.data.CRC32=c812c3c4
     2.5 +build.xml.data.CRC32=764630c8
     2.6  build.xml.script.CRC32=d7a2a0ed
     2.7  build.xml.stylesheet.CRC32=e5f4c432@1.19.2
     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=c812c3c4
    2.11 -nbproject/build-impl.xml.script.CRC32=9437cdd7
    2.12 +nbproject/build-impl.xml.data.CRC32=764630c8
    2.13 +nbproject/build-impl.xml.script.CRC32=45c5a458
    2.14  nbproject/build-impl.xml.stylesheet.CRC32=b9c9cfe1@1.19.2
     3.1 --- a/java/nekurak.net-ejb/nbproject/project.properties	Tue Apr 27 16:13:36 2010 +0200
     3.2 +++ b/java/nekurak.net-ejb/nbproject/project.properties	Tue Apr 27 17:13:23 2010 +0200
     3.3 @@ -23,7 +23,8 @@
     3.4  jar.compress=false
     3.5  jar.name=nekurak.net-ejb.jar
     3.6  javac.classpath=\
     3.7 -    ${reference.nekurak_net-lib.jar}
     3.8 +    ${reference.nekurak_net-lib.jar}:\
     3.9 +    ${reference.SuperDAO.jar}
    3.10  javac.debug=true
    3.11  javac.deprecation=false
    3.12  javac.source=1.5
    3.13 @@ -49,7 +50,9 @@
    3.14  meta.inf.excludes=sun-cmp-mappings.xml
    3.15  platform.active=default_platform
    3.16  project.nekurak_net-lib=../nekurak.net-lib
    3.17 +project.SuperDAO=../../../SuperDAO/java/SuperDAO
    3.18  reference.nekurak_net-lib.jar=${project.nekurak_net-lib}/dist/nekurak.net-lib.jar
    3.19 +reference.SuperDAO.jar=${project.SuperDAO}/dist/SuperDAO.jar
    3.20  resource.dir=setup
    3.21  run.test.classpath=\
    3.22      ${javac.test.classpath}:\
     4.1 --- a/java/nekurak.net-ejb/nbproject/project.xml	Tue Apr 27 16:13:36 2010 +0200
     4.2 +++ b/java/nekurak.net-ejb/nbproject/project.xml	Tue Apr 27 17:13:23 2010 +0200
     4.3 @@ -6,6 +6,7 @@
     4.4              <name>nekurak.net-ejb</name>
     4.5              <minimum-ant-version>1.6.5</minimum-ant-version>
     4.6              <included-library dirs="200">reference.nekurak_net-lib.jar</included-library>
     4.7 +            <included-library dirs="200">reference.SuperDAO.jar</included-library>
     4.8              <source-roots>
     4.9                  <root id="src.dir" name="Source Packages"/>
    4.10              </source-roots>
    4.11 @@ -15,6 +16,14 @@
    4.12          </data>
    4.13          <references xmlns="http://www.netbeans.org/ns/ant-project-references/1">
    4.14              <reference>
    4.15 +                <foreign-project>SuperDAO</foreign-project>
    4.16 +                <artifact-type>jar</artifact-type>
    4.17 +                <script>build.xml</script>
    4.18 +                <target>jar</target>
    4.19 +                <clean-target>clean</clean-target>
    4.20 +                <id>jar</id>
    4.21 +            </reference>
    4.22 +            <reference>
    4.23                  <foreign-project>nekurak_net-lib</foreign-project>
    4.24                  <artifact-type>jar</artifact-type>
    4.25                  <script>build.xml</script>
     5.1 --- a/java/nekurak.net-ejb/src/java/cz/frantovo/nekurak/dao/PodnikDAO.java	Tue Apr 27 16:13:36 2010 +0200
     5.2 +++ b/java/nekurak.net-ejb/src/java/cz/frantovo/nekurak/dao/PodnikDAO.java	Tue Apr 27 17:13:23 2010 +0200
     5.3 @@ -2,9 +2,11 @@
     5.4  
     5.5  import cz.frantovo.nekurak.dto.Podnik;
     5.6  import cz.frantovo.nekurak.dto.VysledekHlasovani;
     5.7 +import cz.frantovo.superDAO.SuperDAO;
     5.8 +import java.math.BigInteger;
     5.9  import java.util.Collection;
    5.10  import java.util.Date;
    5.11 -import java.util.logging.Logger;
    5.12 +import java.util.List;
    5.13  import javax.ejb.LocalBean;
    5.14  import javax.ejb.Stateless;
    5.15  import javax.persistence.EntityManager;
    5.16 @@ -17,12 +19,17 @@
    5.17   */
    5.18  @Stateless
    5.19  @LocalBean
    5.20 -public class PodnikDAO {
    5.21 +public class PodnikDAO extends SuperDAO {
    5.22  
    5.23 -    private static final Logger log = Logger.getLogger(PodnikDAO.class.getSimpleName());
    5.24      @PersistenceContext(unitName = DAO.PU)
    5.25      private EntityManager em;
    5.26  
    5.27 +    private enum SQL {
    5.28 +
    5.29 +	HLASOVANI_INSERT,
    5.30 +	HLASOVANI_SELECT
    5.31 +    }
    5.32 +
    5.33      public Collection<Podnik> getPodniky() {
    5.34  	Query dotaz = em.createQuery("FROM " + DAO.t(Podnik.class) + " o ORDER BY datum DESC");
    5.35  	return dotaz.getResultList();
    5.36 @@ -57,7 +64,7 @@
    5.37      }
    5.38  
    5.39      public void hlasuj(int podnik, boolean hlas, String ipAdresa) {
    5.40 -	Query insert = em.createNativeQuery("INSERT INTO hlasovani (podnik, hlas, ip_adresa) VALUES (:podnik, :hlas, :ip_adresa);");
    5.41 +	Query insert = em.createNativeQuery(getSQL(SQL.HLASOVANI_INSERT));
    5.42  	insert.setParameter("podnik", podnik);
    5.43  	insert.setParameter("hlas", hlas);
    5.44  	insert.setParameter("ip_adresa", ipAdresa);
    5.45 @@ -65,7 +72,22 @@
    5.46      }
    5.47  
    5.48      public VysledekHlasovani getVysledekHlasovani(int podnik) {
    5.49 -	log.severe("TODO: vracet skutečné výsledky z databáze");
    5.50 -	return new VysledekHlasovani(10, 50);
    5.51 +	VysledekHlasovani vysledek = new VysledekHlasovani();
    5.52 +	Query select = em.createNativeQuery(getSQL(SQL.HLASOVANI_SELECT));
    5.53 +	select.setParameter("podnik", podnik);
    5.54 +	List<Object[]> vysledekDotazu = select.getResultList();
    5.55 +
    5.56 +	log.severe("Vracím výsledky pro podnik: " + podnik);
    5.57 +
    5.58 +	for (Object[] radek : vysledekDotazu) {
    5.59 +	    /** Transponujeme výsledek dotazu */
    5.60 +	    if ((Boolean) radek[0]) {
    5.61 +		vysledek.setHlasuAno((Integer) radek[1]);
    5.62 +	    } else {
    5.63 +		vysledek.setHlasuNe((Integer) radek[1]);
    5.64 +	    }
    5.65 +	}
    5.66 +
    5.67 +	return vysledek;
    5.68      }
    5.69  }
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/java/nekurak.net-ejb/src/java/cz/frantovo/nekurak/dao/PodnikDAO.sql.xml	Tue Apr 27 17:13:23 2010 +0200
     6.3 @@ -0,0 +1,25 @@
     6.4 +<?xml version="1.0" encoding="UTF-8"?>
     6.5 +<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
     6.6 +<properties>
     6.7 +    <entry key="HLASOVANI_INSERT">
     6.8 +	INSERT INTO hlasovani
     6.9 +	(podnik, hlas, ip_adresa)
    6.10 +	VALUES (:podnik, :hlas, :ip_adresa);
    6.11 +    </entry>
    6.12 +    <entry key="HLASOVANI_SELECT">
    6.13 +	<!--
    6.14 +	    Poznámka k přetypování int4(count(*)):
    6.15 +	    maximum je: 2 147 483 647 (tolik hlasů snad mít nikdy nebudeme :-)
    6.16 +	-->
    6.17 +	SELECT	hlas,
    6.18 +	    int4(count(*))
    6.19 +	FROM (
    6.20 +	    SELECT DISTINCT ON (ip_adresa)
    6.21 +		hlas
    6.22 +	    FROM hlasovani
    6.23 +	    WHERE podnik = :podnik
    6.24 +	    ORDER BY ip_adresa, id DESC
    6.25 +	) AS hlasy
    6.26 +	GROUP BY hlas;
    6.27 +    </entry>
    6.28 +</properties>
    6.29 \ No newline at end of file
     7.1 --- a/java/nekurak.net-lib/src/cz/frantovo/nekurak/dto/VysledekHlasovani.java	Tue Apr 27 16:13:36 2010 +0200
     7.2 +++ b/java/nekurak.net-lib/src/cz/frantovo/nekurak/dto/VysledekHlasovani.java	Tue Apr 27 17:13:23 2010 +0200
     7.3 @@ -10,11 +10,6 @@
     7.4      private int hlasuAno;
     7.5      private int hlasuNe;
     7.6  
     7.7 -    public VysledekHlasovani(int hlasuAno, int hlasuNe) {
     7.8 -	this.hlasuAno = hlasuAno;
     7.9 -	this.hlasuNe = hlasuNe;
    7.10 -    }
    7.11 -
    7.12      /**
    7.13       * @return počet hlasů pro kuřácký podnik
    7.14       */
     8.1 --- a/java/nekurak.net-web/src/java/cz/frantovo/nekurak/web/Hlasovani.java	Tue Apr 27 16:13:36 2010 +0200
     8.2 +++ b/java/nekurak.net-web/src/java/cz/frantovo/nekurak/web/Hlasovani.java	Tue Apr 27 17:13:23 2010 +0200
     8.3 @@ -14,6 +14,7 @@
     8.4  
     8.5      public void setPodnik(int podnik) {
     8.6  	this.podnik = podnik;
     8.7 +	vysledek = null;
     8.8      }
     8.9  
    8.10      public VysledekHlasovani getVysledek() {
     9.1 --- a/java/nekurak.net-web/web/WEB-INF/tags/nekurak/hlasovani.tag	Tue Apr 27 16:13:36 2010 +0200
     9.2 +++ b/java/nekurak.net-web/web/WEB-INF/tags/nekurak/hlasovani.tag	Tue Apr 27 17:13:23 2010 +0200
     9.3 @@ -16,7 +16,7 @@
     9.4      </c:if>
     9.5  
     9.6      <!-- hodnoty nejsou uvedeny → načteme si je -->
     9.7 -    <c:if test="${hlasuAno == null || hlasuNe == null}">
     9.8 +    <c:if test="${hlasuAno == null || hlasuNe == null || (hlasuAno == 0 &amp;&amp; hlasuNe == 0)}">
     9.9  	<jsp:useBean id="hlasovani" class="cz.frantovo.nekurak.web.Hlasovani" scope="request"/>
    9.10  	<jsp:setProperty name="hlasovani" property="podnik" value="${podnik}"/>
    9.11  	<c:set var="hlasuAno" value="${hlasovani.vysledek.hlasuAno}"/>