1
2 package com.pnpconsult.zeiterfassung.model;
3
4 import java.util.Date;
5 import java.util.Set;
6 import java.util.TreeSet;
7
8 import org.apache.commons.lang.builder.CompareToBuilder;
9
10 import com.pnpconsult.zeiterfassung.util.DateUtils;
11 import com.pnpconsult.zeiterfassung.util.Description;
12
13 /***
14 * @author <a href="mailto:powerpete@users.sf.net">M. Petersen </a>
15 * @version $Id: Bill.java,v 1.10 2004/07/21 22:13:11 powerpete Exp $
16 *
17 * @hibernate.class table="bill"
18 */
19 public class Bill extends ArchivableObject
20 {
21 private Date endDate;
22 private long id = -1;
23 private boolean printed;
24 private Project project;
25 private Date startDate;
26 private Set billEntries;
27 private Date billingDate;
28
29 protected Description buildDescription()
30 {
31 Description description = new Description();
32 description.setFormatter(Date.class, new Description.Formatter()
33 {
34 public String format(Object obj)
35 {
36 return DateUtils.formatWithoutDay((Date) obj);
37 }
38 });
39 description.append(project);
40 if (billingDate != null)
41 {
42 description.append(" - ", "", billingDate, "");
43 }
44 else
45 {
46 description.append(" - ", "", startDate, "");
47 if (!DateUtils.equalsMonth(startDate, endDate))
48 {
49 description.append(" - ", "", endDate, "");
50 }
51 }
52 return description;
53 }
54
55 public int compareTo(Object obj)
56 {
57 Bill b = (Bill) obj;
58 CompareToBuilder builder = new CompareToBuilder();
59 Date thisDate = billingDate != null ? billingDate : startDate;
60 Date bDate = b.billingDate != null ? b.billingDate : b.startDate;
61 builder.append(bDate, thisDate);
62 builder.append(project, b.project);
63 return builder.toComparison();
64 }
65
66 /***
67 * @return
68 * @hibernate.property type="date"
69 */
70 public Date getEndDate()
71 {
72 return endDate;
73 }
74
75 /***
76 * @return Returns the id.
77 *
78 * @hibernate.id generator-class="increment" unsaved-value="-1"
79 */
80 public long getId()
81 {
82 return id;
83 }
84
85 /***
86 * @return Returns the project.
87 *
88 * @hibernate.many-to-one column="projectId"
89 */
90 public Project getProject()
91 {
92 return project;
93 }
94
95 /***
96 * @return
97 * @hibernate.property type="date"
98 */
99 public Date getStartDate()
100 {
101 return startDate;
102 }
103
104 /***
105 * @return Returns the printed.
106 *
107 * @hibernate.property
108 */
109 public boolean isPrinted()
110 {
111 return printed;
112 }
113
114 public void setEndDate(Date endDate)
115 {
116 this.endDate = endDate;
117 sortDates();
118 }
119
120 /***
121 * @param id
122 * The id to set.
123 */
124 public void setId(long id)
125 {
126 this.id = id;
127 }
128
129 /***
130 * @param printed
131 * The printed to set.
132 */
133 public void setPrinted(boolean printed)
134 {
135 this.printed = printed;
136 }
137
138 /***
139 * @param project
140 * The project to set.
141 */
142 public void setProject(Project project)
143 {
144 this.project = project;
145 }
146
147 public void setStartDate(Date startDate)
148 {
149 this.startDate = startDate;
150 sortDates();
151 }
152
153 private void sortDates()
154 {
155 if (startDate != null && endDate != null && startDate.after(endDate))
156 {
157 Date tmp = startDate;
158 startDate = endDate;
159 endDate = tmp;
160 }
161 startDate = DateUtils.toFirst(startDate);
162 endDate = DateUtils.toLast(endDate);
163 }
164
165 /***
166 * @hibernate.set cascade="delete" lazy="true"
167 * @hibernate.collection-key column="billId"
168 * @hibernate.collection-one-to-many class="com.pnpconsult.zeiterfassung.model.BillEntry"
169 */
170 public Set getBillEntries()
171 {
172 return billEntries;
173 }
174
175 public void setBillEntries(Set billEntries)
176 {
177 this.billEntries = billEntries;
178 }
179
180 public void addBillEntry(BillEntry entry)
181 {
182 if (billEntries == null)
183 {
184 billEntries = new TreeSet();
185 }
186 else
187 {
188 if (billEntries.contains(entry))
189 {
190 billEntries.remove(entry);
191 }
192 }
193 billEntries.add(entry);
194 }
195
196 public String getLabel()
197 {
198 return toString();
199 }
200
201 /***
202 * @hibernate.property type="date"
203 */
204 public Date getBillingDate()
205 {
206 return billingDate;
207 }
208
209 public void setBillingDate(Date billingDate)
210 {
211 this.billingDate = billingDate;
212 }
213 }