View Javadoc
1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * BookOrderAction class file for BookOrder maintenance Object
35   * Actions prior to submit and post-Submit processes are handled.  
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          // clear the used book order entry
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 }