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