View Javadoc

1   // $Id: EditBillEntryRowAction.java,v 1.3 2004/06/24 20:54:19 powerpete Exp $
2   package com.pnpconsult.zeiterfassung.actions.manager;
3   
4   import java.util.Iterator;
5   
6   import javax.servlet.http.HttpServletRequest;
7   import javax.servlet.http.HttpServletResponse;
8   
9   import org.apache.commons.logging.Log;
10  import org.apache.commons.logging.LogFactory;
11  import org.apache.struts.action.Action;
12  import org.apache.struts.action.ActionForm;
13  import org.apache.struts.action.ActionForward;
14  import org.apache.struts.action.ActionMapping;
15  
16  import com.pnpconsult.zeiterfassung.helper.BillEntryManager;
17  import com.pnpconsult.zeiterfassung.helper.TableManager;
18  import com.pnpconsult.zeiterfassung.model.Bill;
19  import com.pnpconsult.zeiterfassung.model.BillEntry;
20  import com.pnpconsult.zeiterfassung.table.BillEntryRow;
21  import com.pnpconsult.zeiterfassung.table.Table;
22  
23  /***
24   * This Action controls the edit of 
25   * {@link com.pnpconsult.zeiterfassung.model.BillEntry}s. It depends on
26   * the {@link EditBillEntryRowForm}'s data:
27   * <p>
28   * <ol>
29   * <li> The Table is loaded from the HttpSession.
30   * <li> The row is looked up and the BillEntry is extracted off the row.
31   * <li> If the row is not found (e.g. when creating a new BillEntry), 
32   *      <tt>null</tt> is passed instead.
33   * <li> After editing, the submit button is evaluated.
34   *      <ul>
35   *      <li> If the user clicked "cancel", nothing is done.
36   *      <li> If the user clicked "delete", the bill entry is deleted.
37   *      <li> If the user clicked "save", the changes are saved.
38   *      </ul>
39   * </ol>
40   * 
41   * @author <a href="mailto:powerpete@users.sf.net">Moritz Petersen</a>
42   * @version $Id: EditBillEntryRowAction.java,v 1.3 2004/06/24 20:54:19 powerpete Exp $
43   * 
44   * @struts.action 
45   *  path="/secure/manager/editBillEntryRow" 
46   *  name="editBillEntryRowForm" 
47   *  scope="request" 
48   *  validate="true"
49   *  input="/secure/manager/editBillEntryRow.jsp"
50   * 
51   * @struts.action-forward 
52   *  name="edit" 
53   *  path="/secure/manager/editBillEntryRow.jsp"
54   * 
55   * @struts.action-forward 
56   *  name="table" 
57   *  path="/secure/manager/table.jsp"
58   */
59  public class EditBillEntryRowAction extends Action
60  {
61      private static final Log LOG = LogFactory
62          .getLog(EditBillEntryRowAction.class);
63  
64      public ActionForward execute(
65          ActionMapping mapping,
66          ActionForm form,
67          HttpServletRequest request,
68          HttpServletResponse response) throws Exception
69      {
70          EditBillEntryRowForm editBillEntryRowForm = (EditBillEntryRowForm) form;
71          Table table = new TableManager().get(request);
72          Bill bill = table.getBill();
73          if (editBillEntryRowForm.userHasNotClicked())
74          {
75              int index = editBillEntryRowForm.getIndex();
76              if (index < 0)
77              {
78                  editBillEntryRowForm.setBillEntry(new BillEntry());
79              }
80              else
81              {
82                  BillEntryRow row = (BillEntryRow) table.getRow(index);
83                  BillEntry entry = row == null ? new BillEntry() : row
84                      .getEntry();
85                  editBillEntryRowForm.setBillEntry(entry);
86              }
87              return mapping.findForward("edit");
88          }
89          else if (editBillEntryRowForm.userHasClickedCancel())
90          {
91              return mapping.findForward("table");
92          }
93          else if (editBillEntryRowForm.userHasClickedDelete())
94          {
95              long billEntryId = editBillEntryRowForm.getBillEntryId();
96              new BillEntryManager().delete(billEntryId);
97              for (Iterator it = bill.getBillEntries().iterator(); it.hasNext();)
98              {
99                  BillEntry entry = (BillEntry) it.next();
100                 if (entry.getId() == billEntryId)
101                 {
102                     it.remove();
103                     break;
104                 }
105             }
106             table.reload();
107             return mapping.findForward("table");
108         }
109         else if (editBillEntryRowForm.userHasClickedOK())
110         {
111             BillEntry entry = editBillEntryRowForm.getBillEntry();
112             bill.addBillEntry(entry);
113             entry.setBill(bill);
114             entry.setProject(bill.getProject());
115             new BillEntryManager().update(entry);
116             table.reload();
117             return mapping.findForward("table");
118         }
119         LOG.error("Undefined user response.");
120         return null;
121     }
122 }