001    package edu.sampleu.bookstore.document.web;
002    
003    import javax.servlet.http.HttpServletRequest;
004    import javax.servlet.http.HttpServletResponse;
005    
006    import org.apache.struts.action.ActionForm;
007    import org.apache.struts.action.ActionForward;
008    import org.apache.struts.action.ActionMapping;
009    import org.kuali.rice.kns.service.KNSServiceLocator;
010    import org.kuali.rice.core.util.type.KualiDecimal;
011    import org.kuali.rice.core.util.type.KualiPercent;
012    import org.kuali.rice.kns.web.struts.action.KualiTransactionalDocumentActionBase;
013    import org.kuali.rice.kns.web.struts.form.KualiForm;
014    import edu.sampleu.bookstore.bo.Book;
015    import edu.sampleu.bookstore.bo.BookOrder;
016    import edu.sampleu.bookstore.document.BookOrderDocument;
017    
018    /*
019     * BookOrderAction class file for BookOrder maintenance Object
020     * Actions prior to submit and post-Submit processes are handled.  
021     */
022    
023    public class BookOrderAction extends KualiTransactionalDocumentActionBase {
024    
025            public ActionForward addBookOrder(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception {
026                    BookOrderForm form = (BookOrderForm) actionForm;
027            BookOrderDocument document = form.getBookOrderDocument();
028    
029            BookOrder newBookEntry = form.getNewBookOrder();       
030            document.addBookOrder(newBookEntry);
031            
032                    for (BookOrder entry : document.getBookOrders()) {
033                            if (entry.getBookId() != null) {
034                                    Book book = KNSServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(Book.class, entry.getBookId());
035    
036                                    entry.setUnitPrice(book.getPrice());
037                                    Double totalPrice = 0.0d;
038                                    if (book.getPrice() != null && entry.getQuantity() != null) {
039                                            totalPrice = book.getPrice().doubleValue() * entry.getQuantity().intValue();
040                                            if (entry.getDiscount() != null && entry.getDiscount().doubleValue() > 0) {
041                                                    totalPrice = totalPrice - (totalPrice * entry.getDiscount().doubleValue() / 100);
042                                            }
043                                    }
044                                    entry.setTotalPrice(new KualiDecimal(totalPrice));
045                            }
046                    }
047    
048            // clear the used book order entry
049            form.setNewBookOrder(new BookOrder());
050    
051            return mapping.findForward("basic");
052        }
053            
054            public ActionForward deleteBookOrder(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception {
055            BookOrderForm form = (BookOrderForm) actionForm;
056            BookOrderDocument document = form.getBookOrderDocument();
057            
058            int deleteIndex = getLineToDelete(request);
059            document.removeBookOrder(deleteIndex);
060    
061            return mapping.findForward("basic");        
062        }
063    
064            @Override
065            protected void doProcessingAfterPost(KualiForm actionForm, HttpServletRequest request) {
066                    super.doProcessingAfterPost(actionForm, request);
067                    BookOrderForm form = (BookOrderForm) actionForm;
068            BookOrderDocument document = form.getBookOrderDocument();
069            for (BookOrder entry : document.getBookOrders()) {
070                    if(entry.getBookId() != null){
071                    Book book = KNSServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(Book.class, entry.getBookId());         
072                    entry.setUnitPrice(book.getPrice());
073                            Double totalPrice = 0.0d;
074                            if (book.getPrice() != null && entry.getQuantity() != null) {
075                                    totalPrice = book.getPrice().doubleValue() * entry.getQuantity().intValue();
076                                    if (entry.getDiscount() != null && entry.getDiscount().doubleValue() > 0) {
077                                            totalPrice = totalPrice - (totalPrice * entry.getDiscount().doubleValue() / 100);
078                                    }
079                            }
080                            entry.setTotalPrice(new KualiDecimal(totalPrice));
081                    entry.setBook(book);
082                    }
083            }
084            }
085    
086            
087        
088        
089            
090    }