View Javadoc

1   /**
2    * Copyright 2005-2012 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.web.struts.action.KualiTransactionalDocumentActionBase;
26  import org.kuali.rice.kns.web.struts.form.KualiForm;
27  import org.kuali.rice.krad.service.KRADServiceLocator;
28  
29  import javax.servlet.http.HttpServletRequest;
30  import javax.servlet.http.HttpServletResponse;
31  
32  /*
33   * BookOrderAction class file for BookOrder maintenance Object
34   * Actions prior to submit and post-Submit processes are handled.  
35   */
36  
37  public class BookOrderAction extends KualiTransactionalDocumentActionBase {
38  
39  	public ActionForward addBookOrder(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception {
40  		BookOrderForm form = (BookOrderForm) actionForm;
41          BookOrderDocument document = form.getBookOrderDocument();
42  
43          BookOrder newBookEntry = form.getNewBookOrder();       
44          document.addBookOrder(newBookEntry);
45          
46  		for (BookOrder entry : document.getBookOrders()) {
47  			if (entry.getBookId() != null) {
48  				Book book = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(Book.class, entry.getBookId());
49  
50  				entry.setUnitPrice(book.getPrice());
51  				Double totalPrice = 0.0d;
52  				if (book.getPrice() != null && entry.getQuantity() != null) {
53  					totalPrice = book.getPrice().doubleValue() * entry.getQuantity().intValue();
54  					if (entry.getDiscount() != null	&& entry.getDiscount().doubleValue() > 0) {
55  						totalPrice = totalPrice - (totalPrice * entry.getDiscount().doubleValue() / 100);
56  					}
57  				}
58  				entry.setTotalPrice(new KualiDecimal(totalPrice));
59  			}
60  		}
61  
62          // clear the used book order entry
63          form.setNewBookOrder(new BookOrder());
64  
65          return mapping.findForward("basic");
66      }
67  	
68  	public ActionForward deleteBookOrder(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception {
69          BookOrderForm form = (BookOrderForm) actionForm;
70          BookOrderDocument document = form.getBookOrderDocument();
71          
72          int deleteIndex = getLineToDelete(request);
73          document.removeBookOrder(deleteIndex);
74  
75          return mapping.findForward("basic");        
76      }
77  
78  	@Override
79  	protected void doProcessingAfterPost(KualiForm actionForm, HttpServletRequest request) {
80  		super.doProcessingAfterPost(actionForm, request);
81  		BookOrderForm form = (BookOrderForm) actionForm;
82          BookOrderDocument document = form.getBookOrderDocument();
83          for (BookOrder entry : document.getBookOrders()) {
84          	if(entry.getBookId() != null){
85          	Book book = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(Book.class, entry.getBookId());
86          	entry.setUnitPrice(book.getPrice());
87  			Double totalPrice = 0.0d;
88  			if (book.getPrice() != null && entry.getQuantity() != null) {
89  				totalPrice = book.getPrice().doubleValue() * entry.getQuantity().intValue();
90  				if (entry.getDiscount() != null && entry.getDiscount().doubleValue() > 0) {
91  					totalPrice = totalPrice	- (totalPrice * entry.getDiscount().doubleValue() / 100);
92  				}
93  			}
94  			entry.setTotalPrice(new KualiDecimal(totalPrice));
95          	entry.setBook(book);
96          	}
97          }
98  	}
99  
100 	
101     
102     
103 	
104 }