View Javadoc

1   // $Id: ManagerMenuForm.java,v 1.11 2004/06/25 09:08:48 powerpete Exp $
2   // [JMP, 02.06.2004] created this file.
3   package com.pnpconsult.zeiterfassung.actions.manager;
4   
5   import java.text.ParseException;
6   import java.util.ArrayList;
7   import java.util.Calendar;
8   import java.util.Collection;
9   import java.util.Date;
10  
11  import javax.servlet.http.HttpServletRequest;
12  
13  import net.sf.hibernate.HibernateException;
14  
15  import org.apache.commons.logging.Log;
16  import org.apache.commons.logging.LogFactory;
17  import org.apache.struts.action.ActionErrors;
18  import org.apache.struts.action.ActionForm;
19  import org.apache.struts.action.ActionMapping;
20  import org.apache.struts.util.LabelValueBean;
21  
22  import com.pnpconsult.zeiterfassung.helper.BillManager;
23  import com.pnpconsult.zeiterfassung.helper.ProjectManager;
24  import com.pnpconsult.zeiterfassung.helper.TableManager;
25  import com.pnpconsult.zeiterfassung.helper.UserEntryManager;
26  import com.pnpconsult.zeiterfassung.model.Bill;
27  import com.pnpconsult.zeiterfassung.table.Table;
28  import com.pnpconsult.zeiterfassung.util.ActionErrorUtils;
29  import com.pnpconsult.zeiterfassung.util.DateUtils;
30  import com.pnpconsult.zeiterfassung.util.MinMaxDates;
31  
32  /***
33   * @author <a href="mailto:powerpete@users.sf.net">Moritz Petersen</a>
34   * @version $Id: ManagerMenuForm.java,v 1.11 2004/06/25 09:08:48 powerpete Exp $
35   * 
36   * @struts.form name="managerMenuForm"
37   */
38  public class ManagerMenuForm extends ActionForm
39  {
40      private static final Log LOG = LogFactory.getLog(ManagerMenuForm.class);
41      private Collection dates;
42      private Collection months = DateUtils.monthCollection();
43      private Collection years = DateUtils.yearCollection(new Date(), 2, 2);
44      private Date dateFrom;
45      private Date dateTo;
46      private int billingMonth;
47      private int billingYear;
48      private boolean showInBill;
49      private long projectId;
50      private Collection projects;
51      private boolean createBill;
52      private long temporaryBillId;
53      private Collection temporaryBills;
54      private boolean openTemporaryBill;
55      private boolean deleteTemporaryBill;
56      private long printedBillId;
57      private Collection printedBills;
58      private boolean openPrintedBill;
59      private boolean printTemporaryBill;
60  
61      public void reset(ActionMapping mapping, HttpServletRequest request)
62      {
63          resetDates();
64          resetProjects();
65          resetTemporaryBills();
66          resetPrintedBills();
67          showInBill = false;
68          Calendar cal = Calendar.getInstance();
69          cal.set(Calendar.DAY_OF_MONTH, 1);
70          //cal.add(Calendar.MONTH, -1);
71          dateFrom = cal.getTime();
72          dateTo = cal.getTime();
73          billingMonth = -1;
74          billingYear = -1;
75      }
76  
77      public ActionErrors validate(
78          ActionMapping mapping,
79          HttpServletRequest request)
80      {
81          ActionErrors errors = new ActionErrors();
82          try
83          {
84              BillManager billManager = new BillManager();
85              TableManager tableManager = new TableManager();
86              if (userHasClickedCreateBill())
87              {
88                  Bill bill = new Bill();
89                  bill.setStartDate(dateFrom);
90                  bill.setEndDate(dateTo);
91                  bill.setProject(new ProjectManager().load(projectId));
92                  if (showInBill)
93                  {
94                      bill.setBillingDate(DateUtils.createDate(
95                          billingMonth,
96                          billingYear));
97                  }
98  
99                  Bill sameBill = billManager.loadSame(bill);
100                 if (sameBill != null)
101                 {
102                     bill = sameBill;
103                 }
104                 else
105                 {
106                     billManager.save(bill);
107                 }
108                 if (bill.isArchived())
109                 {
110                     ActionErrorUtils
111                         .add(
112                             errors,
113                             "errors.secure.manager.managerMenu.bill_already_archived");
114                 }
115                 else if (bill.isPrinted())
116                 {
117                     ActionErrorUtils
118                         .add(
119                             errors,
120                             "errors.secure.manager.managerMenu.bill_already_printed");
121                 }
122                 else
123                 {
124                     tableManager.put(request, new Table(bill));
125                 }
126             }
127             else if (userHasClickedDeleteTemporaryBill())
128             {
129                 // TODO Refactor this. no if (userHasClicked..) in the action!
130             }
131             else if (userHasClickedPrintTemporaryBill())
132             {
133                 Bill bill = billManager.load(temporaryBillId);
134                 tableManager.put(request, new Table(bill));
135                 bill.setPrinted(true);
136                 billManager.update(bill);
137             }
138             else if (userHasClickedOpenPrintedBill())
139             {
140                 Bill bill = billManager.load(printedBillId);
141                 tableManager.put(request, new Table(bill));
142             }
143             else if (userHasClickedOpenTemporaryBill())
144             {
145                 Bill bill = billManager.load(temporaryBillId);
146                 tableManager.put(request, new Table(bill));
147             }
148             else
149             {
150                 ActionErrorUtils.add(
151                     errors,
152                     "secure.manager.managerMenu.error.unknown_input");
153             }
154         }
155         catch (HibernateException e)
156         {
157             LOG.fatal(e.getMessage(), e);
158             ActionErrorUtils.add(
159                 errors,
160                 "secure.manager.managerMenu.error.hibernate");
161         }
162         return errors;
163     }
164 
165     private void resetPrintedBills()
166     {
167         if (printedBills == null)
168         {
169             try
170             {
171                 printedBills = new BillManager().loadPrintedAndNotArchived();
172             }
173             catch (HibernateException e)
174             {
175                 LOG.fatal(e.getMessage(), e);
176             }
177         }
178     }
179 
180     void resetBillCollections()
181     {
182         printedBills = null;
183         resetPrintedBills();
184         temporaryBills = null;
185         resetTemporaryBills();
186     }
187 
188     //    void removeTemporaryBill()
189     //    {
190     //        if (temporaryBills == null)
191     //        {
192     //            return;
193     //        }
194     //        for (Iterator it = temporaryBills.iterator(); it.hasNext();)
195     //        {
196     //            Bill bill = (Bill) it.next();
197     //            if (bill.getId() == temporaryBillId)
198     //            {
199     //                it.remove();
200     //                break;
201     //            }
202     //        }
203     //    }
204 
205     private void resetTemporaryBills()
206     {
207         if (temporaryBills == null)
208         {
209             try
210             {
211                 temporaryBills = new BillManager().loadNotPrinted();
212             }
213             catch (HibernateException e)
214             {
215                 LOG.fatal(e.getMessage(), e);
216             }
217         }
218     }
219 
220     private void resetProjects()
221     {
222         if (projects == null)
223         {
224             try
225             {
226                 projects = new ProjectManager().loadNotArchived();
227             }
228             catch (HibernateException e)
229             {
230                 LOG.fatal(e.getMessage(), e);
231             }
232         }
233     }
234 
235     private void resetDates()
236     {
237         if (dates == null)
238         {
239             try
240             {
241                 MinMaxDates minMaxDates = new UserEntryManager()
242                     .loadMinMaxDates();
243                 Date min = minMaxDates.getMin();
244                 long maxInMillis = minMaxDates.getMax().getTime();
245                 dates = new ArrayList();
246                 Calendar cal = Calendar.getInstance();
247                 cal.setTime(min);
248                 cal.set(Calendar.DAY_OF_MONTH, 1);
249                 while (cal.getTimeInMillis() <= maxInMillis)
250                 {
251                     Date date = cal.getTime();
252                     String value = DateUtils.format(date);
253                     String label = DateUtils.formatWithoutDay(date);
254                     dates.add(new LabelValueBean(label, value));
255                     cal.add(Calendar.MONTH, 1);
256                 }
257             }
258             catch (HibernateException e)
259             {
260                 LOG.fatal(e.getMessage(), e);
261             }
262         }
263     }
264 
265     //    public ActionErrors validate(
266     //        ActionMapping mapping,
267     //        HttpServletRequest request)
268     //    {
269     //        ActionErrors errors = new ActionErrors();
270     //        errors.add(null, null);
271     //        return errors;
272     //    }
273 
274     public int getBillingMonth()
275     {
276         return billingMonth;
277     }
278 
279     public void setBillingMonth(int billingMonth)
280     {
281         this.billingMonth = billingMonth;
282     }
283 
284     public int getBillingYear()
285     {
286         return billingYear;
287     }
288 
289     public void setBillingYear(int billingYear)
290     {
291         this.billingYear = billingYear;
292     }
293 
294     public boolean userHasClickedCreateBill()
295     {
296         return createBill;
297     }
298 
299     public void setCreateBill(String createBill)
300     {
301         this.createBill = createBill != null;
302     }
303 
304     public String getDateFrom()
305     {
306         String tmp = DateUtils.format(dateFrom);
307         return tmp;
308     }
309 
310     public void setDateFrom(String dateFrom)
311     {
312         try
313         {
314             this.dateFrom = DateUtils.parse(dateFrom);
315         }
316         catch (ParseException e)
317         {
318             LOG.fatal(e.getMessage(), e);
319         }
320     }
321 
322     public String getDateTo()
323     {
324         String tmp = DateUtils.format(dateTo);
325         return tmp;
326     }
327 
328     public void setDateTo(String dateTo)
329     {
330         try
331         {
332             this.dateTo = DateUtils.parse(dateTo);
333         }
334         catch (ParseException e)
335         {
336             LOG.fatal(e.getMessage(), e);
337         }
338     }
339 
340     public boolean userHasClickedDeleteTemporaryBill()
341     {
342         return deleteTemporaryBill;
343     }
344 
345     public void setDeleteTemporaryBill(String deleteTemporaryBill)
346     {
347         this.deleteTemporaryBill = deleteTemporaryBill != null;
348     }
349 
350     public boolean userHasClickedPrintTemporaryBill()
351     {
352         return printTemporaryBill;
353     }
354 
355     public void setPrintTemporaryBill(String printTemporaryBill)
356     {
357         this.printTemporaryBill = printTemporaryBill != null;
358     }
359 
360     public boolean userHasClickedOpenPrintedBill()
361     {
362         return openPrintedBill;
363     }
364 
365     public void setOpenPrintedBill(String openPrintedBill)
366     {
367         this.openPrintedBill = openPrintedBill != null;
368     }
369 
370     public boolean userHasClickedOpenTemporaryBill()
371     {
372         return openTemporaryBill;
373     }
374 
375     public void setOpenTemporaryBill(String openTemporaryBill)
376     {
377         this.openTemporaryBill = openTemporaryBill != null;
378     }
379 
380     public long getPrintedBillId()
381     {
382         return printedBillId;
383     }
384 
385     public void setPrintedBillId(long printedBillId)
386     {
387         this.printedBillId = printedBillId;
388     }
389 
390     public Collection getPrintedBills()
391     {
392         return printedBills;
393     }
394 
395     public long getProjectId()
396     {
397         return projectId;
398     }
399 
400     public void setProjectId(long projectId)
401     {
402         this.projectId = projectId;
403     }
404 
405     public boolean isShowInBill()
406     {
407         return showInBill;
408     }
409 
410     public void setShowInBill(boolean showInBill)
411     {
412         this.showInBill = showInBill;
413     }
414 
415     public long getTemporaryBillId()
416     {
417         return temporaryBillId;
418     }
419 
420     public void setTemporaryBillId(long temporaryBillId)
421     {
422         this.temporaryBillId = temporaryBillId;
423     }
424 
425     public Collection getTemporaryBills()
426     {
427         return temporaryBills;
428     }
429 
430     public Collection getDates()
431     {
432         return dates;
433     }
434 
435     public Collection getMonths()
436     {
437         return months;
438     }
439 
440     public Collection getProjects()
441     {
442         return projects;
443     }
444 
445     public Collection getYears()
446     {
447         return years;
448     }
449 }