View Javadoc

1   // $Id: Table.java,v 1.13 2004/07/21 22:13:11 powerpete Exp $
2   // [JMP, 02.03.2004] Created this file.
3   package com.pnpconsult.zeiterfassung.table;
4   
5   import java.util.Collection;
6   import java.util.Iterator;
7   import java.util.SortedMap;
8   import java.util.TreeMap;
9   
10  import net.sf.hibernate.HibernateException;
11  
12  import org.apache.commons.lang.builder.CompareToBuilder;
13  
14  import com.pnpconsult.zeiterfassung.helper.BillManager;
15  import com.pnpconsult.zeiterfassung.model.AbstractBaseEntry;
16  import com.pnpconsult.zeiterfassung.model.Bill;
17  import com.pnpconsult.zeiterfassung.model.BillEntry;
18  import com.pnpconsult.zeiterfassung.model.Project;
19  import com.pnpconsult.zeiterfassung.model.UserEntry;
20  import com.pnpconsult.zeiterfassung.util.DateUtils;
21  import com.pnpconsult.zeiterfassung.util.MapUtils;
22  import com.pnpconsult.zeiterfassung.util.NumberUtils;
23  
24  /***
25   * @author <a href="mailto:powerpete@users.sf.net">M. Petersen </a>
26   * @version $Id: Table.java,v 1.13 2004/07/21 22:13:11 powerpete Exp $
27   */
28  public class Table
29  {
30      private SortedMap rows;
31      private float tax;
32      private Bill bill;
33  
34      public Table(Bill bill) throws HibernateException
35      {
36          this.bill = bill;
37          reload();
38      }
39  
40      public Bill getBill()
41      {
42          return bill;
43      }
44  
45      private void addUserEntry(UserEntry entry)
46      {
47          MapUtils.putMulti(rows, new Key(entry), entry, UserEntryRow.class);
48      }
49  
50      private void addBillEntry(BillEntry entry)
51      {
52          rows.put(new Key(entry), new BillEntryRow(entry));
53      }
54  
55      public Collection getRows()
56      {
57          return rows.values();
58      }
59  
60      private class Key implements Comparable
61      {
62          private AbstractBaseEntry entry;
63  
64          public Key(AbstractBaseEntry entry)
65          {
66              this.entry = entry;
67          }
68  
69          public int compareTo(Object o)
70          {
71              Key k = (Key) o;
72              CompareToBuilder builder = new CompareToBuilder();
73              builder
74                  .append(
75                      DateUtils.convertNullToReallyOld(entry.getTargetDate()),
76                      DateUtils.convertNullToReallyOld(k.entry.getTargetDate()))
77                      .append(
78                          entry.getBillDescription(),
79                          k.entry.getBillDescription());
80              if (entry instanceof BillEntry)
81              {
82                  builder.append(System.identityHashCode(entry), System
83                      .identityHashCode(k.entry));
84              }
85              return builder.toComparison();
86          }
87  
88          //        public boolean equals(Object o)
89          //        {
90          //            Key k = (Key) o;
91          //            return new EqualsBuilder()
92          //                .append(description, k.description)
93          //                .append(targetDate, k.targetDate)
94          //                .isEquals();
95          //        }
96          //
97          //        public int hashCode()
98          //        {
99          //            return new HashCodeBuilder()
100         //                .append(description)
101         //                .append(targetDate)
102         //                .toHashCode();
103         //        }
104     }
105 
106     public String getTotal()
107     {
108         return NumberUtils.formatLong(computeTotal());
109     }
110 
111     private float computeTotal()
112     {
113         float total = 0.0F;
114         for (Iterator it = rows.values().iterator(); it.hasNext();)
115         {
116             Row tableRow = (Row) it.next();
117             total += tableRow.computeTotal();
118         }
119         return total;
120     }
121 
122     public String getTotalIncludingTax()
123     {
124         return NumberUtils.formatLong(computeTotalIncludingTax());
125     }
126 
127     private float computeTotalIncludingTax()
128     {
129         return computeTotal() * (1.0F + tax / 100.0F);
130     }
131 
132     /***
133      * @return
134      */
135     public String getTax()
136     {
137         return NumberUtils.formatShort(tax) + " %";
138     }
139 
140     public String getTotalTax()
141     {
142         return NumberUtils.formatLong(computeTotalTax());
143     }
144 
145     private float computeTotalTax()
146     {
147         return computeTotal() * tax / 100.0F;
148     }
149 
150     /***
151      * @param i
152      * @return
153      */
154     public Row getRow(int index)
155     {
156         for (Iterator it = getRows().iterator(); it.hasNext();)
157         {
158             Row row = (Row) it.next();
159             if (index-- == 0)
160             {
161                 return row;
162             }
163         }
164         throw new ArrayIndexOutOfBoundsException(index);
165     }
166 
167     public String getCaption()
168     {
169         return bill.toString();
170     }
171 
172     public void reload() throws HibernateException
173     {
174         if (bill == null)
175         {
176             return;
177         }
178         Project project = bill.getProject();
179         if (project != null)
180         {
181             tax = project.getTax(bill.getStartDate());
182         }
183         rows = new TreeMap();
184         Collection userEntries = new BillManager().loadUserEntries(bill);
185         for (Iterator it = userEntries.iterator(); it.hasNext();)
186         {
187             addUserEntry((UserEntry) it.next());
188         }
189         Collection billEntries = bill.getBillEntries();
190         if (billEntries != null)
191         {
192             for (Iterator it = billEntries.iterator(); it.hasNext();)
193             {
194                 addBillEntry((BillEntry) it.next());
195             }
196         }
197     }
198 
199     public String getCurrency()
200     {
201         return bill.getProject().getCurrency();
202     }
203 }