1 package edu.sampleu.bookstore.document.web;
2
3 import edu.sampleu.bookstore.bo.Book;
4 import edu.sampleu.bookstore.bo.BookOrder;
5 import edu.sampleu.bookstore.document.BookOrderDocument;
6 import org.apache.struts.action.ActionForm;
7 import org.apache.struts.action.ActionForward;
8 import org.apache.struts.action.ActionMapping;
9 import org.kuali.rice.core.api.util.type.KualiDecimal;
10 import org.kuali.rice.kns.web.struts.action.KualiTransactionalDocumentActionBase;
11 import org.kuali.rice.kns.web.struts.form.KualiForm;
12 import org.kuali.rice.krad.service.KRADServiceLocator;
13
14 import javax.servlet.http.HttpServletRequest;
15 import javax.servlet.http.HttpServletResponse;
16
17
18
19
20
21
22 public class BookOrderAction extends KualiTransactionalDocumentActionBase {
23
24 public ActionForward addBookOrder(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception {
25 BookOrderForm form = (BookOrderForm) actionForm;
26 BookOrderDocument document = form.getBookOrderDocument();
27
28 BookOrder newBookEntry = form.getNewBookOrder();
29 document.addBookOrder(newBookEntry);
30
31 for (BookOrder entry : document.getBookOrders()) {
32 if (entry.getBookId() != null) {
33 Book book = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(Book.class, entry.getBookId());
34
35 entry.setUnitPrice(book.getPrice());
36 Double totalPrice = 0.0d;
37 if (book.getPrice() != null && entry.getQuantity() != null) {
38 totalPrice = book.getPrice().doubleValue() * entry.getQuantity().intValue();
39 if (entry.getDiscount() != null && entry.getDiscount().doubleValue() > 0) {
40 totalPrice = totalPrice - (totalPrice * entry.getDiscount().doubleValue() / 100);
41 }
42 }
43 entry.setTotalPrice(new KualiDecimal(totalPrice));
44 }
45 }
46
47
48 form.setNewBookOrder(new BookOrder());
49
50 return mapping.findForward("basic");
51 }
52
53 public ActionForward deleteBookOrder(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception {
54 BookOrderForm form = (BookOrderForm) actionForm;
55 BookOrderDocument document = form.getBookOrderDocument();
56
57 int deleteIndex = getLineToDelete(request);
58 document.removeBookOrder(deleteIndex);
59
60 return mapping.findForward("basic");
61 }
62
63 @Override
64 protected void doProcessingAfterPost(KualiForm actionForm, HttpServletRequest request) {
65 super.doProcessingAfterPost(actionForm, request);
66 BookOrderForm form = (BookOrderForm) actionForm;
67 BookOrderDocument document = form.getBookOrderDocument();
68 for (BookOrder entry : document.getBookOrders()) {
69 if(entry.getBookId() != null){
70 Book book = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(Book.class, entry.getBookId());
71 entry.setUnitPrice(book.getPrice());
72 Double totalPrice = 0.0d;
73 if (book.getPrice() != null && entry.getQuantity() != null) {
74 totalPrice = book.getPrice().doubleValue() * entry.getQuantity().intValue();
75 if (entry.getDiscount() != null && entry.getDiscount().doubleValue() > 0) {
76 totalPrice = totalPrice - (totalPrice * entry.getDiscount().doubleValue() / 100);
77 }
78 }
79 entry.setTotalPrice(new KualiDecimal(totalPrice));
80 entry.setBook(book);
81 }
82 }
83 }
84
85
86
87
88
89 }