java/HrisniciSpameri/src/java/cz/frantovo/hrisniciSpameri/DenniSouhrn.java
author František Kučera <franta-hg@frantovo.cz>
Sat Feb 14 13:32:38 2009 +0100 (2009-02-14)
changeset 21 2bc6c427894d
parent 20 90dc76051e56
child 23 b22c06c8a55c
permissions -rw-r--r--
Denní souhr načítáme z databáze.
     1 package cz.frantovo.hrisniciSpameri;
     2 
     3 import cz.frantovo.hrisniciSpameri.dao.CilDAO;
     4 import cz.frantovo.hrisniciSpameri.dao.SouhrnDAO;
     5 import cz.frantovo.hrisniciSpameri.dto.Cil;
     6 import cz.frantovo.hrisniciSpameri.dto.SitovaAdresa;
     7 import cz.frantovo.hrisniciSpameri.dto.Souhrn;
     8 import java.io.Serializable;
     9 import java.sql.Date;
    10 import java.text.DateFormat;
    11 import java.text.ParseException;
    12 import java.text.SimpleDateFormat;
    13 import java.util.ArrayList;
    14 import java.util.Collection;
    15 import java.util.logging.Level;
    16 import java.util.logging.Logger;
    17 
    18 /**
    19  * Beana zprostředkovávající denní souhrn hříšníků.
    20  * @author fiki
    21  */
    22 public class DenniSouhrn implements Serializable {
    23 
    24     private static final long serialVersionUID = 7932392366943861342L;
    25     private static final Logger log = Logger.getLogger(DenniSouhrn.class.getName());
    26     private static final long DEN = 1000 * 60 * 60 * 24;
    27     private static String formatDataVzor = "yyyy-MM-dd";
    28     public static DateFormat formatData = new SimpleDateFormat(formatDataVzor);
    29     private Date den;
    30     private int cil;
    31 
    32     public String getDenString() {
    33         return formatData.format(getDen());
    34     }
    35 
    36     public Date getDen() {
    37         if (den == null) {
    38             nastavVychoziDen();
    39         }
    40         return den;
    41     }
    42 
    43     public void setDen(Date den) {
    44         if (den == null) {
    45             nastavVychoziDen();
    46         } else {
    47             this.den = den;
    48         }
    49         log.log(Level.FINE, "Den nastaven na: " + formatData.format(den));
    50     }
    51 
    52     public void setDenString(String den) {
    53         if (den == null) {
    54             nastavVychoziDen();
    55         }
    56 
    57         try {
    58             this.den = new Date(formatData.parse(den).getTime());
    59         } catch (ParseException e) {
    60             log.log(Level.FINE, "Chybně zadané datum: " + den, e);
    61             nastavVychoziDen();
    62         }
    63     }
    64 
    65     public void setCilString(String cil) {
    66         try {
    67             this.cil = Integer.parseInt(cil);
    68         } catch (Exception e) {
    69             this.cil = 0;
    70         }
    71     }
    72 
    73     public void setCil(int cil) {
    74         this.cil = cil;
    75     }
    76 
    77     public String getCilString() {
    78         return String.valueOf(cil);
    79     }
    80 
    81     public int getCil() {
    82         return cil;
    83     }
    84 
    85     /**
    86      * Ve výchozím stavu zobrazujeme souhrn za předešlý den.
    87      * @return včerejšek
    88      */
    89     private static Date getVychoziDen() {
    90         return new Date(System.currentTimeMillis() - DEN);
    91     }
    92 
    93     private void nastavVychoziDen() {
    94         log.log(Level.FINE, "Nastavuji den na výchozí hodnotu.");
    95         setDen(getVychoziDen());
    96     }
    97 
    98     public Collection<Souhrn> getSouhrn() {
    99         Collection<Souhrn> souhrn = new SouhrnDAO().getSouhrn(den, null, cil);
   100 
   101         /** ne všechno musíme posílat do JSP vrstvy */
   102         for (Souhrn s : souhrn) {
   103             s.getCil().setDatabaze(null);
   104             s.getCil().setSelekt(null);
   105         }
   106 
   107         return souhrn;
   108     }
   109 
   110     /**
   111      * @return Seznam všech cílů. Ale bez těchto hodnot: databáze a selekt.
   112      */
   113     public Collection<Cil> getSeznamCilu() {
   114         Collection<Cil> cile = new CilDAO().getCile();
   115 
   116         /** ne všechno musíme posílat do JSP vrstvy */
   117         for (Cil c : cile) {
   118             c.setDatabaze(null);
   119             c.setSelekt(null);
   120         }
   121 
   122         return cile;
   123     }
   124 }