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