2 * Copyright (C) 2017 Roland Häder
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License as
6 * published by the Free Software Foundation, either version 3 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Affero General Public License for more details.
14 * You should have received a copy of the GNU Affero General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package org.mxchange.jfinancials.beans.financial.model.receipt_item;
19 import java.text.MessageFormat;
21 import javax.enterprise.context.RequestScoped;
22 import javax.enterprise.event.Event;
23 import javax.enterprise.inject.Any;
24 import javax.faces.view.facelets.FaceletException;
25 import javax.inject.Inject;
26 import javax.inject.Named;
27 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
28 import org.mxchange.jfinancials.events.receipt_item.added.ObservableReceiptItemAddedEvent;
29 import org.mxchange.jfinancials.events.receipt_item.added.ReceiptItemAddedEvent;
30 import org.mxchange.jfinancials.exceptions.receipt_item.ReceiptItemAlreadyAddedException;
31 import org.mxchange.jfinancials.model.receipt.BillableReceipt;
32 import org.mxchange.jfinancials.model.receipt_item.BillableReceiptItem;
33 import org.mxchange.jfinancials.model.receipt_item.FinancialAdminReceiptItemSessionBeanRemote;
34 import org.mxchange.jfinancials.model.receipt_item.FinancialReceiptItem;
35 import org.mxchange.jproduct.model.product.Product;
38 * An administrative backing bean for receipt items
40 * @author Roland Häder<roland@mxchange.org>
42 @Named ("adminReceiptItemController")
44 public class FinancialAdminReceiptItemWebRequestBean extends BaseFinancialsBean implements FinancialAdminReceiptItemWebRequestController {
49 private static final long serialVersionUID = 595_754_280_374_172L;
52 * Event being fired when administrator has added a new receipt item
56 private Event<ObservableReceiptItemAddedEvent> adminAddedReceiptItemEvent;
59 * EJB for general financial receipt item purposes
61 @EJB (lookup = "java:global/jfinancials-ejb/adminFinancialReceiptItem!org.mxchange.jfinancials.model.receipt_item.FinancialAdminReceiptItemSessionBeanRemote")
62 private FinancialAdminReceiptItemSessionBeanRemote adminReceiptItemBean;
65 * Discount on product price (if any) Valid: values 0...1 (1=100% discount)
67 private Float itemDiscount;
72 private Product itemProduct;
77 private Long itemQuantity;
82 private BillableReceipt itemReceipt;
85 * General receipt item controller
88 private FinancialsReceiptItemWebRequestController receiptItemController;
93 public FinancialAdminReceiptItemWebRequestBean () {
94 // Call super constructor
99 * Adds the completed receipt item to database by calling an EJB business
100 * method. If not all required fields are set, a proper exception is being
103 * @return Link outcome
105 public String addReceiptItem () {
106 // Are all required fields set?
108 // Prepare receipt item instance
109 final BillableReceiptItem receiptItem = this.createReceiptItemInstance();
111 // Is the receipt already there?
112 if (this.receiptItemController.isReceiptItemAdded(receiptItem)) {
113 // Receipt has already been added
114 throw new FaceletException(MessageFormat.format("Receipt for itemReceipt.receiptId={0},itemProduct.productId={1},itemProductQuantity={2} has already been added.", receiptItem.getItemReceipt().getReceiptId(), receiptItem.getItemProduct().getProductId(), receiptItem.getItemProductQuantity())); //NOI18N
118 final BillableReceiptItem updatedReceiptItem;
120 // All is set, then try to call EJB
123 updatedReceiptItem = this.adminReceiptItemBean.addReceiptItem(receiptItem);
124 } catch (final ReceiptItemAlreadyAddedException ex) {
126 throw new FaceletException(ex);
129 // Fire event with updated instance
130 this.adminAddedReceiptItemEvent.fire(new ReceiptItemAddedEvent(updatedReceiptItem));
135 // Return redirect outcome
136 return "add_receipt_item?faces-redirect=true"; //NOI18N
140 * Getter for item discount
142 * @return Item discount
144 public Float getItemDiscount () {
145 return this.itemDiscount;
149 * Setter for item discount
151 * @param itemDiscount Item discount
153 public void setItemDiscount (final Float itemDiscount) {
154 this.itemDiscount = itemDiscount;
158 * Getter for assigned product
160 * @return Assigned product
162 public Product getItemProduct () {
163 return this.itemProduct;
167 * Setter for assigned product
169 * @param itemProduct Assigned product
171 public void setItemProduct (final Product itemProduct) {
172 this.itemProduct = itemProduct;
176 * Getter for item quantity
178 * @return Item quantity
180 public Long getItemQuantity () {
181 return this.itemQuantity;
185 * Setter for item quantity
187 * @param itemQuantity Item quantity
189 public void setItemQuantity (final Long itemQuantity) {
190 this.itemQuantity = itemQuantity;
194 * Getter for assigned receipt
196 * @return Assigned receipt
198 public BillableReceipt getItemReceipt () {
199 return this.itemReceipt;
203 * Setter for assigned receipt
205 * @param itemReceipt Assigned receipt
207 public void setItemReceipt (final BillableReceipt itemReceipt) {
208 this.itemReceipt = itemReceipt;
214 private void clear () {
216 this.setItemDiscount(null);
217 this.setItemProduct(null);
218 this.setItemQuantity(null);
219 this.setItemReceipt(null);
223 * Creates a new instance from all available data of this bean.
225 * @return Receipt item instance
227 private BillableReceiptItem createReceiptItemInstance () {
228 // Create new instance with minimum required data
229 final BillableReceiptItem receiptItem = new FinancialReceiptItem(this.getItemProduct(), this.getItemQuantity(), this.getItemReceipt());
232 receiptItem.setItemProductDiscount(this.getItemDiscount());
234 // Return prepared instance