View Javadoc

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