001    /**
002     * Copyright 2005-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package edu.sampleu.bookstore.document.web;
017    
018    import edu.sampleu.bookstore.bo.Book;
019    import edu.sampleu.bookstore.bo.BookOrder;
020    import edu.sampleu.bookstore.document.BookOrderDocument;
021    import org.apache.struts.action.ActionForm;
022    import org.apache.struts.action.ActionForward;
023    import org.apache.struts.action.ActionMapping;
024    import org.kuali.rice.core.api.util.type.KualiDecimal;
025    import org.kuali.rice.kns.web.struts.action.KualiTransactionalDocumentActionBase;
026    import org.kuali.rice.kns.web.struts.form.KualiForm;
027    import org.kuali.rice.krad.service.KRADServiceLocator;
028    
029    import javax.servlet.http.HttpServletRequest;
030    import javax.servlet.http.HttpServletResponse;
031    
032    /*
033     * BookOrderAction class file for BookOrder maintenance Object
034     * Actions prior to submit and post-Submit processes are handled.  
035     */
036    
037    public class BookOrderAction extends KualiTransactionalDocumentActionBase {
038    
039            public ActionForward addBookOrder(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception {
040                    BookOrderForm form = (BookOrderForm) actionForm;
041            BookOrderDocument document = form.getBookOrderDocument();
042    
043            BookOrder newBookEntry = form.getNewBookOrder();       
044            document.addBookOrder(newBookEntry);
045            
046                    for (BookOrder entry : document.getBookOrders()) {
047                            if (entry.getBookId() != null) {
048                                    Book book = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(Book.class, entry.getBookId());
049    
050                                    entry.setUnitPrice(book.getPrice());
051                                    Double totalPrice = 0.0d;
052                                    if (book.getPrice() != null && entry.getQuantity() != null) {
053                                            totalPrice = book.getPrice().doubleValue() * entry.getQuantity().intValue();
054                                            if (entry.getDiscount() != null && entry.getDiscount().doubleValue() > 0) {
055                                                    totalPrice = totalPrice - (totalPrice * entry.getDiscount().doubleValue() / 100);
056                                            }
057                                    }
058                                    entry.setTotalPrice(new KualiDecimal(totalPrice));
059                            }
060                    }
061    
062            // clear the used book order entry
063            form.setNewBookOrder(new BookOrder());
064    
065            return mapping.findForward("basic");
066        }
067            
068            public ActionForward deleteBookOrder(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception {
069            BookOrderForm form = (BookOrderForm) actionForm;
070            BookOrderDocument document = form.getBookOrderDocument();
071            
072            int deleteIndex = getLineToDelete(request);
073            document.removeBookOrder(deleteIndex);
074    
075            return mapping.findForward("basic");        
076        }
077    
078            @Override
079            protected void doProcessingAfterPost(KualiForm actionForm, HttpServletRequest request) {
080                    super.doProcessingAfterPost(actionForm, request);
081                    BookOrderForm form = (BookOrderForm) actionForm;
082            BookOrderDocument document = form.getBookOrderDocument();
083            for (BookOrder entry : document.getBookOrders()) {
084                    if(entry.getBookId() != null){
085                    Book book = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(Book.class, entry.getBookId());
086                    entry.setUnitPrice(book.getPrice());
087                            Double totalPrice = 0.0d;
088                            if (book.getPrice() != null && entry.getQuantity() != null) {
089                                    totalPrice = book.getPrice().doubleValue() * entry.getQuantity().intValue();
090                                    if (entry.getDiscount() != null && entry.getDiscount().doubleValue() > 0) {
091                                            totalPrice = totalPrice - (totalPrice * entry.getDiscount().doubleValue() / 100);
092                                    }
093                            }
094                            entry.setTotalPrice(new KualiDecimal(totalPrice));
095                    entry.setBook(book);
096                    }
097            }
098            }
099    
100            
101        
102        
103            
104    }