2 * Copyright (C) 2016 - 2020 Free Software Foundation
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.FacesException;
25 import javax.inject.Inject;
26 import javax.inject.Named;
27 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
28 import org.mxchange.jfinancials.beans.financial.model.receipt_item.list.FinancialsReceiptItemListWebViewController;
29 import org.mxchange.jfinancials.beans.user.login.FinancialsUserLoginWebSessionController;
30 import org.mxchange.jfinancials.events.receipt_item.added.ObservableReceiptItemAddedEvent;
31 import org.mxchange.jfinancials.events.receipt_item.added.ReceiptItemAddedEvent;
32 import org.mxchange.jfinancials.exceptions.receipt_item.ReceiptItemAlreadyAddedException;
33 import org.mxchange.jfinancials.model.receipt_item.BillableReceiptItem;
34 import org.mxchange.jfinancials.model.receipt_item.FinancialReceiptItem;
35 import org.mxchange.jfinancials.model.receipt_item.FinancialReceiptItemSessionBeanRemote;
38 * A general financial receipt item bean (controller)
40 * @author Roland Häder<roland@mxchange.org>
42 @Named ("receiptItemController")
44 public class FinancialsReceiptItemWebRequestBean extends BaseFinancialsBean implements FinancialsReceiptItemWebRequestController {
49 private static final long serialVersionUID = 56_189_028_928_372L;
52 * Event being fired when user has added a new receipt
56 private Event<ObservableReceiptItemAddedEvent> addedReceiptItemEvent;
59 * EJB for general financial receipt purposes
61 @EJB (lookup = "java:global/jfinancials-ejb/financialReceiptItem!org.mxchange.jfinancials.model.receipt_item.FinancialReceiptItemSessionBeanRemote")
62 private FinancialReceiptItemSessionBeanRemote receiptItemBean;
65 * Receipt item list controller
68 private FinancialsReceiptItemListWebViewController receiptItemListController;
74 private FinancialsUserLoginWebSessionController userLoginController;
79 @SuppressWarnings ("CollectionWithoutInitialCapacity")
80 public FinancialsReceiptItemWebRequestBean () {
81 // Call super constructor
86 * Adds the completed receipt item to database by calling an EJB business
87 * method. If not all required fields are set, a proper exception is being
91 public void addReceiptItem () {
92 // Are all required fields set?
93 if (!this.userLoginController.isUserLoggedIn()) {
95 throw new IllegalStateException("User is not logged-in"); //NOI18N
98 // Prepare receipt instance
99 final BillableReceiptItem receiptItem = this.createReceiptInstance();
101 // Is the receipt already there?
102 if (this.receiptItemListController.isReceiptItemAdded(receiptItem)) {
103 // Receipt has already been added
104 throw new FacesException(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
108 final BillableReceiptItem updatedReceiptItem;
110 // All is set, then try to call EJB
113 updatedReceiptItem = this.receiptItemBean.addReceiptItem(receiptItem);
114 } catch (final ReceiptItemAlreadyAddedException ex) {
116 throw new FacesException(ex);
119 // Fire event with updated instance
120 this.addedReceiptItemEvent.fire(new ReceiptItemAddedEvent(updatedReceiptItem));
129 private void clear () {
134 * Returns a fully created receipt item instance (except primary key, of
137 * @return Receipt item instance
139 private BillableReceiptItem createReceiptInstance () {
140 // Init receipt instance
142 final BillableReceiptItem receiptItem = new FinancialReceiptItem();
144 // Set optional fields