View Javadoc

1   /*
2    * Copyright 2004 Carlos Sanchez.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.sf.oness.order.webapp.controller.action;
17  
18  import java.util.LinkedList;
19  import java.util.List;
20  
21  import javax.servlet.http.HttpServletRequest;
22  import javax.servlet.http.HttpServletResponse;
23  
24  import net.sf.oness.common.model.util.PaginatedList;
25  import net.sf.oness.common.webapp.controller.action.ActionUtils;
26  import net.sf.oness.common.webapp.controller.util.NonEmptyStringConverter;
27  import net.sf.oness.order.model.bo.Invoice;
28  import net.sf.oness.order.model.to.InvoiceWithParty;
29  import net.sf.oness.order.webapp.controller.actionform.MasterActionForm;
30  
31  import org.apache.commons.beanutils.BeanUtils;
32  import org.apache.struts.action.ActionForm;
33  import org.apache.struts.action.ActionForward;
34  import org.apache.struts.action.ActionMapping;
35  
36  /***
37   * Invoice actions
38   * 
39   * @author Carlos Sanchez
40   * @version $Revision: 1.4 $
41   */
42  public class InvoiceAction extends AbstractAction {
43  
44      /***
45       * Create an invoice.
46       * 
47       * @param mapping
48       * @param actionForm
49       * @param request
50       * @param response
51       * @return ActionForward success
52       * @throws Exception
53       */
54      public ActionForward create(ActionMapping mapping, ActionForm actionForm,
55              HttpServletRequest request, HttpServletResponse response)
56              throws Exception {
57  
58          List deliveryDocketIds = new LinkedList();
59          int length = Integer.parseInt(request
60                  .getParameter("deliveryDocketsSize"));
61          for (int i = 0; i < length; i++) {
62              String o = request.getParameter("deliveryDocket[" + i + "].id");
63              if (o != null)
64                  deliveryDocketIds.add(new Long(o));
65          }
66  
67          getFacade().createInvoices(deliveryDocketIds);
68  
69          /* Return ActionForward. */
70          return mapping.findForward("success");
71      }
72  
73      /***
74       * Find an invoice.
75       * 
76       * @param mapping
77       * @param actionForm
78       * @param request
79       * @param response
80       * @return ActionForward success
81       * @throws Exception
82       */
83      public ActionForward find(ActionMapping mapping, ActionForm actionForm,
84              HttpServletRequest request, HttpServletResponse response)
85              throws Exception {
86  
87          Invoice invoice = getInvoice(actionForm, request);
88          if (!getErrors(request).isEmpty())
89              return mapping.getInputForward();
90  
91          int firstElement = ActionUtils.getFirstElement(request);
92          int maxElements = ActionUtils.getMaxElements(request);
93  
94          PaginatedList results = getFacade().findInvoice(invoice, firstElement,
95                  maxElements);
96  
97          ActionUtils.setIteratorPageParameters(results, request);
98  
99          /* Return ActionForward. */
100         return mapping.findForward("success");
101     }
102 
103     /***
104      * Show a invoice.
105      * 
106      * @param mapping
107      * @param actionForm
108      * @param request
109      * @param response
110      * @return ActionForward success
111      * @throws Exception
112      */
113     public ActionForward show(ActionMapping mapping, ActionForm actionForm,
114             HttpServletRequest request, HttpServletResponse response)
115             throws Exception {
116 
117         InvoiceWithParty invoiceWithParty = getFacade().findInvoiceWithDetails(
118                 getId(actionForm));
119         request.setAttribute("invoice", invoiceWithParty.getInvoice());
120         request.setAttribute("amount", invoiceWithParty.getAmount());
121         request.setAttribute("party", invoiceWithParty.getParty());
122         request.setAttribute("items", invoiceWithParty.getItems());
123         request.setAttribute("itemsSize", Integer.toString(invoiceWithParty
124                 .getItems().size()));
125 
126         return mapping.findForward("success");
127     }
128 
129     private Invoice getInvoice(ActionForm actionForm, HttpServletRequest request)
130             throws Exception {
131         Invoice invoice = new Invoice();
132         MasterActionForm form = (MasterActionForm) actionForm;
133         form.setItems(null);
134         NonEmptyStringConverter.register();
135         BeanUtils.copyProperties(invoice, actionForm);
136         NonEmptyStringConverter.deregister();
137 
138         if (form.getId() == null)
139             invoice.setId(null);
140         if (form.getType() == null)
141             invoice.setType(null);
142 
143         return invoice;
144     }
145 
146     private Long getId(ActionForm actionForm) throws Exception {
147         MasterActionForm form = (MasterActionForm) actionForm;
148         return new Long(form.getId());
149     }
150 
151     private void populateForm(Invoice invoice, ActionForm actionForm) {
152         MasterActionForm form = (MasterActionForm) actionForm;
153         form.setDate(invoice.getDate());
154         form.setDateAsString(invoice.getDate().toString());
155         form.setComments(invoice.getComments());
156     }
157 
158 }