1
2 package com.pnpconsult.zeiterfassung.actions.manager;
3
4 import java.util.Collection;
5 import java.util.Date;
6
7 import javax.servlet.http.HttpServletRequest;
8
9 import jface.util.factory.FactoryException;
10 import net.sf.hibernate.HibernateException;
11
12 import org.apache.commons.logging.Log;
13 import org.apache.commons.logging.LogFactory;
14 import org.apache.struts.action.ActionError;
15 import org.apache.struts.action.ActionErrors;
16 import org.apache.struts.action.ActionForm;
17 import org.apache.struts.action.ActionMapping;
18
19 import com.pnpconsult.zeiterfassung.helper.BillManager;
20 import com.pnpconsult.zeiterfassung.helper.ProjectManager;
21 import com.pnpconsult.zeiterfassung.helper.TableManager;
22 import com.pnpconsult.zeiterfassung.model.Bill;
23 import com.pnpconsult.zeiterfassung.model.Project;
24 import com.pnpconsult.zeiterfassung.table.Table;
25 import com.pnpconsult.zeiterfassung.util.DateUtils;
26
27 /***
28 * @author <a href="mailto:powerpete@users.sf.net">M. Petersen</a>
29 * @version $Id: NewBillForm.java,v 1.6 2004/05/23 16:59:42 powerpete Exp $
30 *
31 * @struts.form name="newBillForm"
32 */
33 public class NewBillForm extends ActionForm
34 {
35 private static final Log LOG = LogFactory.getLog(NewBillForm.class);
36 private static final int YEARS_AFTER = 3;
37 private static final int YEARS_BEFORE = 3;
38 private Bill bill;
39 private String billingMonth;
40 private boolean billingMonthSelected;
41 private String billingYear;
42 private String endMonth;
43 private String endYear;
44 private Project project;
45 private String startMonth;
46 private String startYear;
47
48 private Bill createBill() throws HibernateException, FactoryException
49 {
50
51 if (project == null)
52 {
53 return null;
54 }
55 Bill bill = new Bill();
56 bill.setProject(project);
57 if (billingMonthSelected)
58 {
59 bill.setStartDate(DateUtils.createDate(
60 billingMonth,
61 billingYear,
62 currentDate()));
63 bill.setEndDate(DateUtils.createDate(
64 billingMonth,
65 billingYear,
66 currentDate()));
67 }
68 else
69 {
70 bill.setStartDate(DateUtils.createDate(
71 startMonth,
72 startYear,
73 currentDate()));
74 bill.setEndDate(DateUtils.createDate(
75 endMonth,
76 endYear,
77 currentDate()));
78 }
79 if (bill.getStartDate() == null && bill.getEndDate() == null)
80 {
81 return null;
82 }
83 if (bill.getStartDate() == null)
84 {
85 bill.setStartDate(DateUtils.toFirst(bill.getEndDate()));
86 }
87 if (bill.getEndDate() == null)
88 {
89 bill.setEndDate(DateUtils.toLast(bill.getStartDate()));
90 }
91 Bill sameBill = BillManager.getInstance().loadSame(bill);
92 if (sameBill != null)
93 {
94 return sameBill;
95 }
96 return bill;
97 }
98
99 protected Date currentDate()
100 {
101 return new Date();
102 }
103
104 public Bill getBill()
105 {
106 return bill;
107 }
108
109 public String getBillingMonth()
110 {
111 return billingMonth;
112 }
113
114 public String getBillingYear()
115 {
116 return billingYear;
117 }
118
119 public String getEndMonth()
120 {
121 return endMonth;
122 }
123
124 public String getEndYear()
125 {
126 return endYear;
127 }
128
129 public Collection getMonthFromCollection()
130 {
131 return DateUtils.monthCollection();
132 }
133
134 public Collection getMonthToCollection()
135 {
136 return DateUtils.monthCollection();
137 }
138
139 public String getProjectId()
140 {
141 if (project == null)
142 {
143 return null;
144 }
145 return String.valueOf(project.getId());
146 }
147
148 public String getStartMonth()
149 {
150 return startMonth;
151 }
152
153 public String getStartYear()
154 {
155 return startYear;
156 }
157
158 public Collection getYearFromCollection()
159 {
160 return DateUtils.yearCollection(
161 currentDate(),
162 YEARS_BEFORE,
163 YEARS_AFTER);
164 }
165
166 public Collection getYearToCollection()
167 {
168 return DateUtils.yearCollection(
169 currentDate(),
170 YEARS_BEFORE,
171 YEARS_AFTER);
172 }
173
174 public boolean isBillingMonthSelected()
175 {
176 return billingMonthSelected;
177 }
178
179 public void reset(ActionMapping arg0, HttpServletRequest arg1)
180 {
181 billingMonth = "--";
182 billingMonthSelected = false;
183 billingYear = "--";
184 endMonth = "--";
185 endYear = "--";
186 project = null;
187 startMonth = "--";
188 startYear = "--";
189 }
190
191 public void setBillingMonth(String billingMonth)
192 {
193 this.billingMonth = billingMonth;
194 }
195
196 public void setBillingMonthSelected(boolean billingMonthSelected)
197 {
198 this.billingMonthSelected = billingMonthSelected;
199 }
200
201 public void setBillingYear(String billingYear)
202 {
203 this.billingYear = billingYear;
204 }
205
206 public void setEndMonth(String monthTo)
207 {
208 this.endMonth = monthTo;
209 }
210
211 public void setEndYear(String yearTo)
212 {
213 this.endYear = yearTo;
214 }
215
216 public void setProjectId(String projectId) throws HibernateException,
217 FactoryException
218 {
219 project = ProjectManager.getInstance().load(Long.parseLong(projectId));
220 }
221
222 public void setStartMonth(String monthFrom)
223 {
224 this.startMonth = monthFrom;
225 }
226
227 public void setStartYear(String yearFrom)
228 {
229 this.startYear = yearFrom;
230 }
231
232 public ActionErrors validate(
233 ActionMapping mapping,
234 HttpServletRequest request)
235 {
236 ActionErrors errors = new ActionErrors();
237 try
238 {
239 bill = createBill();
240 if (bill == null)
241 {
242 errors.add(
243 "secure.manager.input.error.newBill",
244 new ActionError("secure.manager.input.error.newBill"));
245 }
246 else
247 {
248 TableManager.getInstance().put(request, createTable(bill));
249 }
250 }
251 catch (HibernateException e)
252 {
253 LOG.fatal(e.getMessage(), e);
254 errors.add("secure.manager.input.error.newBill", new ActionError(
255 "secure.manager.input.error.newBill"));
256 }
257 catch (FactoryException e)
258 {
259 LOG.fatal(e.getMessage(), e);
260 errors.add("secure.manager.input.error.newBill", new ActionError(
261 "secure.manager.input.error.newBill"));
262 }
263 return errors;
264 }
265
266 /***
267 * This method is used for prototyping and mocking. Overwrite it in
268 * tests to avoid database access.
269 *
270 * @return
271 * @throws HibernateException
272 * @throws FactoryException
273 */
274 protected Table createTable(Bill bill) throws HibernateException,
275 FactoryException
276 {
277 return new Table(bill);
278 }
279 }