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