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.item.BillableReceiptItem;
32 import org.mxchange.jfinancials.model.receipt.item.FinancialReceiptItem;
33 import org.mxchange.jfinancials.model.receipt_item.FinancialAdminReceiptItemSessionBeanRemote;
36 * An administrative backing bean for receipt items
38 * @author Roland Häder<roland@mxchange.org>
40 @Named ("adminReceiptItemController")
42 public class FinancialAdminReceiptItemWebRequestBean extends BaseFinancialsBean implements FinancialAdminReceiptItemWebRequestController {
47 private static final long serialVersionUID = 595_754_280_374_172L;
50 * Event being fired when administrator has added a new receipt item
54 private Event<ObservableReceiptItemAddedEvent> adminAddedReceiptItemEvent;
57 * EJB for general financial receipt item purposes
59 @EJB (lookup = "java:global/jfinancials-ejb/adminFinancialReceiptItem!org.mxchange.jfinancials.model.receipt.item.FinancialAdminReceiptItemSessionBeanRemote")
60 private FinancialAdminReceiptItemSessionBeanRemote adminReceiptItemBean;
63 * General receipt item controller
66 private FinancialsReceiptItemWebRequestController receiptItemController;
71 public FinancialAdminReceiptItemWebRequestBean () {
72 // Call super constructor
77 * Adds the completed receipt item to database by calling an EJB business
78 * method. If not all required fields are set, a proper exception is being
81 * @return Link outcome
83 public String addReceiptItem () {
84 // Are all required fields set?
86 // Prepare receipt item instance
87 final BillableReceiptItem receiptItem = this.createReceiptItemInstance();
89 // Is the receipt already there?
90 if (this.receiptItemController.isReceiptItemAdded(receiptItem)) {
91 // Receipt has already been added
92 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
96 final BillableReceiptItem updatedReceiptItem;
98 // All is set, then try to call EJB
101 updatedReceiptItem = this.adminReceiptItemBean.addReceiptItem(receiptItem);
102 } catch (final ReceiptItemAlreadyAddedException ex) {
104 throw new FaceletException(ex);
107 // Fire event with updated instance
108 this.adminAddedReceiptItemEvent.fire(new ReceiptItemAddedEvent(updatedReceiptItem));
113 // Return redirect outcome
114 return "add_receipt_item?faces-redirect=true"; //NOI18N
120 private void clear () {
125 * Creates a new instance from all available data of this bean.
127 * @return Receipt item instance
129 private BillableReceiptItem createReceiptItemInstance () {
130 // Create new instance with minimum required data
131 final BillableReceiptItem receiptItem = new FinancialReceiptItem();
135 // Return prepared instance