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