1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.oness.order.webapp.controller.action;
17
18 import java.math.BigDecimal;
19 import java.util.ArrayList;
20 import java.util.Collection;
21
22 import javax.servlet.http.HttpServletRequest;
23 import javax.servlet.http.HttpServletResponse;
24 import javax.servlet.http.HttpSession;
25
26 import net.sf.oness.common.model.util.PaginatedList;
27 import net.sf.oness.common.webapp.controller.action.ActionUtils;
28 import net.sf.oness.common.webapp.controller.util.NonEmptyStringConverter;
29 import net.sf.oness.order.model.bo.Order;
30 import net.sf.oness.order.model.bo.OrderItem;
31 import net.sf.oness.order.model.to.OrderWithParty;
32 import net.sf.oness.order.webapp.controller.actionform.ItemActionForm;
33 import net.sf.oness.order.webapp.controller.actionform.MasterActionForm;
34
35 import org.apache.commons.beanutils.BeanUtils;
36 import org.apache.struts.action.ActionForm;
37 import org.apache.struts.action.ActionForward;
38 import org.apache.struts.action.ActionMapping;
39 import org.apache.struts.action.ActionMessage;
40 import org.apache.struts.action.ActionMessages;
41
42 /***
43 * Order actions
44 *
45 * @author Carlos Sanchez
46 * @version $Revision: 1.12 $
47 */
48 public class OrderAction extends AbstractAction {
49
50 /***
51 * Create an order.
52 *
53 * @param mapping
54 * @param actionForm
55 * @param request
56 * @param response
57 * @return ActionForward success
58 * @throws Exception
59 */
60 public ActionForward create(ActionMapping mapping, ActionForm actionForm,
61 HttpServletRequest request, HttpServletResponse response)
62 throws Exception {
63
64 Order order = getOrder(actionForm, request);
65 if (!getErrors(request).isEmpty())
66 return mapping.getInputForward();
67
68 order.setId(null);
69 OrderWithParty orderWithParty = getFacade().createOrder(order);
70 request.setAttribute("order", orderWithParty.getOrder());
71 request.setAttribute("amount", orderWithParty.getAmount());
72 request.setAttribute("party", orderWithParty.getParty());
73 request.setAttribute("itemsSize", "0");
74 setCurrentOrder(request, orderWithParty.getOrder());
75 populateForm(orderWithParty.getOrder(), actionForm);
76
77
78 return mapping.findForward("success");
79 }
80
81 /***
82 * Update an order.
83 *
84 * @param mapping
85 * @param actionForm
86 * @param request
87 * @param response
88 * @return ActionForward success
89 * @throws Exception
90 */
91 public ActionForward update(ActionMapping mapping, ActionForm actionForm,
92 HttpServletRequest request, HttpServletResponse response)
93 throws Exception {
94
95 Collection items = getItems(actionForm, request, mapping);
96 Order order = getOrder(actionForm, request);
97 if (!getErrors(request).isEmpty())
98 return mapping.getInputForward();
99
100 OrderWithParty orderWithParty = getFacade().updateOrder(order, items);
101 request.setAttribute("order", orderWithParty.getOrder());
102 request.setAttribute("amount", orderWithParty.getAmount());
103 request.setAttribute("party", orderWithParty.getParty());
104 request.setAttribute("items", orderWithParty.getItems());
105 request.setAttribute("itemsSize", Integer.toString(orderWithParty
106 .getItems().size()));
107 setCurrentOrder(request, orderWithParty.getOrder());
108 return mapping.findForward("success");
109 }
110
111 /***
112 * Find an order.
113 *
114 * @param mapping
115 * @param actionForm
116 * @param request
117 * @param response
118 * @return ActionForward success
119 * @throws Exception
120 */
121 public ActionForward find(ActionMapping mapping, ActionForm actionForm,
122 HttpServletRequest request, HttpServletResponse response)
123 throws Exception {
124
125 Order order = getOrder(actionForm, request);
126 if (!getErrors(request).isEmpty())
127 return mapping.getInputForward();
128
129 int firstElement = ActionUtils.getFirstElement(request);
130 int maxElements = ActionUtils.getMaxElements(request);
131
132 PaginatedList results = getFacade().findOrder(order, firstElement,
133 maxElements);
134
135 ActionUtils.setIteratorPageParameters(results, request);
136
137
138 return mapping.findForward("success");
139 }
140
141 /***
142 * Show a order.
143 *
144 * @param mapping
145 * @param actionForm
146 * @param request
147 * @param response
148 * @return ActionForward success
149 * @throws Exception
150 */
151 public ActionForward show(ActionMapping mapping, ActionForm actionForm,
152 HttpServletRequest request, HttpServletResponse response)
153 throws Exception {
154
155 OrderWithParty orderWithParty = getFacade().findOrderWithDetails(
156 getId(actionForm));
157 request.setAttribute("order", orderWithParty.getOrder());
158 request.setAttribute("amount", orderWithParty.getAmount());
159 request.setAttribute("party", orderWithParty.getParty());
160 request.setAttribute("items", orderWithParty.getItems());
161 request.setAttribute("itemsSize", Integer.toString(orderWithParty
162 .getItems().size()));
163
164 return mapping.findForward("success");
165 }
166
167 /***
168 * Edit a order.
169 *
170 * @param mapping
171 * @param actionForm
172 * @param request
173 * @param response
174 * @return ActionForward success
175 * @throws Exception
176 */
177 public ActionForward edit(ActionMapping mapping, ActionForm actionForm,
178 HttpServletRequest request, HttpServletResponse response)
179 throws Exception {
180
181 OrderWithParty orderWithParty = getFacade().findOrderWithDetails(
182 getId(actionForm));
183 request.setAttribute("order", orderWithParty.getOrder());
184 request.setAttribute("amount", orderWithParty.getAmount());
185 request.setAttribute("party", orderWithParty.getParty());
186 request.setAttribute("items", orderWithParty.getItems());
187 request.setAttribute("itemsSize", Integer.toString(orderWithParty
188 .getItems().size()));
189 populateForm(orderWithParty.getOrder(), actionForm);
190
191 Order order = orderWithParty.getOrder();
192 setCurrentOrder(request, order);
193 Collection c = order.getItems();
194 order.setItems(null);
195 BeanUtils.copyProperties(actionForm, order);
196 order.setItems(c);
197 return mapping.findForward("success");
198 }
199
200 /***
201 * Delete a order.
202 *
203 * @param mapping
204 * @param actionForm
205 * @param request
206 * @param response
207 * @return ActionForward success
208 * @throws Exception
209 */
210 public ActionForward delete(ActionMapping mapping, ActionForm actionForm,
211 HttpServletRequest request, HttpServletResponse response)
212 throws Exception {
213
214 getFacade().deleteOrder(getId(actionForm));
215 return mapping.findForward("success");
216 }
217
218 private Order getOrder(ActionForm actionForm, HttpServletRequest request)
219 throws Exception {
220 Order order = new Order();
221 MasterActionForm form = (MasterActionForm) actionForm;
222 form.setItems(null);
223 NonEmptyStringConverter.register();
224 BeanUtils.copyProperties(order, actionForm);
225 NonEmptyStringConverter.deregister();
226
227 if (form.getId() == null)
228 order.setId(null);
229 if (form.getType() == null)
230 order.setType(null);
231
232 return order;
233 }
234
235 private Collection getItems(ActionForm actionForm,
236 HttpServletRequest request, ActionMapping mapping) throws Exception {
237
238 MasterActionForm form = (MasterActionForm) actionForm;
239 ItemActionForm[] orderItemActionForms = (ItemActionForm[]) form
240 .getItems();
241
242 Collection orderItems = new ArrayList(orderItemActionForms.length);
243 for (int i = 0; i < orderItemActionForms.length; i++) {
244 OrderItem orderItem = new OrderItem();
245 if (orderItemActionForms[i].getId() != null)
246 orderItem.setId(new Long(orderItemActionForms[i].getId()));
247 orderItem.setProductId(new Long(orderItemActionForms[i]
248 .getProductId()));
249 orderItem.setPrice(new BigDecimal(orderItemActionForms[i]
250 .getPrice()));
251 try {
252 orderItem.setQuantity(new BigDecimal(orderItemActionForms[i]
253 .getQuantity()));
254 } catch (NumberFormatException e) {
255 ActionMessages errors = getErrors(request);
256 if (errors == null)
257 errors = new ActionMessages();
258 errors.add(ActionMessages.GLOBAL_MESSAGE,
259 new ActionMessage("errors.invalid",
260 orderItemActionForms[i].getQuantity()));
261 saveErrors(request, errors);
262 return null;
263 }
264 orderItems.add(orderItem);
265 }
266
267 return orderItems;
268 }
269
270 private Long getId(ActionForm actionForm) throws Exception {
271 MasterActionForm form = (MasterActionForm) actionForm;
272 return new Long(form.getId());
273 }
274
275 /***
276 * Set current order in session
277 *
278 * @param request
279 */
280 private void setCurrentOrder(HttpServletRequest request, Order order) {
281 HttpSession session = request.getSession(true);
282 session.setAttribute(CURRENT_ORDER_ID_SESSION_ATTRIBUTE, order.getId());
283 }
284
285 private void populateForm(Order order, ActionForm actionForm) {
286 MasterActionForm form = (MasterActionForm) actionForm;
287 form.setDate(order.getDate());
288 form.setDateAsString(order.getDate().toString());
289 form.setComments(order.getComments());
290 }
291
292 }