java/HrisniciSpameri/src/java/cz/frantovo/hrisniciSpameri/DenniSouhrn.java
author František Kučera <franta-hg@frantovo.cz>
Fri Feb 13 00:26:59 2009 +0100 (2009-02-13)
changeset 20 90dc76051e56
parent 6 38625daa449b
child 21 2bc6c427894d
permissions -rw-r--r--
Výměnna proměnných mezi JSP a beanou
JavaScript pro zobrazování a skrývání seznamu cílů
Formulář pro výběr data a cíle
     1 package cz.frantovo.hrisniciSpameri;
     2 
     3 import cz.frantovo.hrisniciSpameri.dao.CilDAO;
     4 import cz.frantovo.hrisniciSpameri.dto.Cil;
     5 import cz.frantovo.hrisniciSpameri.dto.SitovaAdresa;
     6 import cz.frantovo.hrisniciSpameri.dto.Souhrn;
     7 import java.io.Serializable;
     8 import java.sql.Date;
     9 import java.text.DateFormat;
    10 import java.text.ParseException;
    11 import java.text.SimpleDateFormat;
    12 import java.util.ArrayList;
    13 import java.util.Collection;
    14 import java.util.logging.Level;
    15 import java.util.logging.Logger;
    16 
    17 /**
    18  * Beana zprostředkovávající denní souhrn hříšníků.
    19  * @author fiki
    20  */
    21 public class DenniSouhrn implements Serializable {
    22 
    23     private static final long serialVersionUID = 7932392366943861342L;
    24     private static final Logger log = Logger.getLogger(DenniSouhrn.class.getName());
    25     private static final long DEN = 1000 * 60 * 60 * 24;
    26     private static String formatDataVzor = "yyyy-MM-dd";
    27     public static DateFormat formatData = new SimpleDateFormat(formatDataVzor);
    28     private Date den;
    29     private int cil;
    30 
    31     public String getDenString() {
    32         return formatData.format(getDen());
    33     }
    34 
    35     public Date getDen() {
    36         if (den == null) {
    37             nastavVychoziDen();
    38         }
    39         return den;
    40     }
    41 
    42     public void setDen(Date den) {
    43         if (den == null) {
    44             nastavVychoziDen();
    45         } else {
    46             this.den = den;
    47         }
    48         log.log(Level.FINE, "Den nastaven na: " + formatData.format(den));
    49     }
    50 
    51     public void setDenString(String den) {
    52         if (den == null) {
    53             nastavVychoziDen();
    54         }
    55 
    56         try {
    57             this.den = new Date(formatData.parse(den).getTime());
    58         } catch (ParseException e) {
    59             log.log(Level.FINE, "Chybně zadané datum: " + den, e);
    60             nastavVychoziDen();
    61         }
    62     }
    63 
    64     public void setCilString(String cil) {
    65         try {
    66             this.cil = Integer.parseInt(cil);
    67         } catch (Exception e) {
    68             this.cil = 0;
    69         }
    70     }
    71 
    72     public void setCil(int cil) {
    73         this.cil = cil;
    74     }
    75 
    76     public String getCilString() {
    77         return String.valueOf(cil);
    78     }
    79 
    80     public int getCil() {
    81         return cil;
    82     }
    83 
    84     /**
    85      * Ve výchozím stavu zobrazujeme souhrn za předešlý den.
    86      * @return včerejšek
    87      */
    88     private static Date getVychoziDen() {
    89         return new Date(System.currentTimeMillis() - DEN);
    90     }
    91 
    92     private void nastavVychoziDen() {
    93         log.log(Level.FINE, "Nastavuji den na výchozí hodnotu.");
    94         setDen(getVychoziDen());
    95     }
    96 
    97     public Collection<Souhrn> getSouhrn() {
    98         Collection<Souhrn> souhrn = new ArrayList<Souhrn>();
    99 
   100         Cil c = new Cil(1, "Frantovo.cz", "http://frantovo.cz/blog/", null, null);
   101 
   102         for (int i = 1; i < 11; i++) {
   103             Souhrn s = new Souhrn(getVychoziDen(), new SitovaAdresa("10.0.0.8", i + ".frantovo.cz"), 100 + i, c);
   104             souhrn.add(s);
   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 }