]> git.mxchange.org Git - jfinancials-war.git/blob
aebad4b08d739febbf890f5228233df53c55044b
[jfinancials-war.git] /
1 /*
2  * Copyright (C) 2016 - 2020 Free Software Foundation
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.jfinancials.beans.financial.model.receipt_item;
18
19 import java.text.MessageFormat;
20 import javax.ejb.EJB;
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;
36
37 /**
38  * A general financial receipt item bean (controller)
39  * <p>
40  * @author Roland Häder<roland@mxchange.org>
41  */
42 @Named ("receiptItemController")
43 @RequestScoped
44 public class FinancialsReceiptItemWebRequestBean extends BaseFinancialsBean implements FinancialsReceiptItemWebRequestController {
45
46         /**
47          * Serial number
48          */
49         private static final long serialVersionUID = 56_189_028_928_372L;
50
51         /**
52          * Event being fired when user has added a new receipt
53          */
54         @Inject
55         @Any
56         private Event<ObservableReceiptItemAddedEvent> addedReceiptItemEvent;
57
58         /**
59          * EJB for general financial receipt purposes
60          */
61         @EJB (lookup = "java:global/jfinancials-ejb/financialReceiptItem!org.mxchange.jfinancials.model.receipt_item.FinancialReceiptItemSessionBeanRemote")
62         private FinancialReceiptItemSessionBeanRemote receiptItemBean;
63
64         /**
65          * Receipt item list controller
66          */
67         @Inject
68         private FinancialsReceiptItemListWebViewController receiptItemListController;
69
70         /**
71          * User instance
72          */
73         @Inject
74         private FinancialsUserLoginWebSessionController userLoginController;
75
76         /**
77          * Constructor
78          */
79         @SuppressWarnings ("CollectionWithoutInitialCapacity")
80         public FinancialsReceiptItemWebRequestBean () {
81                 // Call super constructor
82                 super();
83         }
84
85         /**
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
88          * thrown.
89          * <p>
90          */
91         public void addReceiptItem () {
92                 // Are all required fields set?
93                 if (!this.userLoginController.isUserLoggedIn()) {
94                         // Not logged-in
95                         throw new IllegalStateException("User is not logged-in"); //NOI18N
96                 }
97
98                 // Prepare receipt instance
99                 final BillableReceiptItem receiptItem = this.createReceiptInstance();
100
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
105                 }
106
107                 // Init variable
108                 final BillableReceiptItem updatedReceiptItem;
109
110                 // All is set, then try to call EJB
111                 try {
112                         // Add it
113                         updatedReceiptItem = this.receiptItemBean.addReceiptItem(receiptItem);
114                 } catch (final ReceiptItemAlreadyAddedException ex) {
115                         // Throw it again
116                         throw new FacesException(ex);
117                 }
118
119                 // Fire event with updated instance
120                 this.addedReceiptItemEvent.fire(new ReceiptItemAddedEvent(updatedReceiptItem));
121
122                 // Clear bean
123                 this.clear();
124         }
125
126         /**
127          * Clears this bean
128          */
129         private void clear () {
130                 // Clear all fields
131         }
132
133         /**
134          * Returns a fully created receipt item instance (except primary key, of
135          * course)
136          * <p>
137          * @return Receipt item instance
138          */
139         private BillableReceiptItem createReceiptInstance () {
140                 // Init receipt instance
141                 // @TODO Unfinished:
142                 final BillableReceiptItem receiptItem = new FinancialReceiptItem();
143
144                 // Set optional fields
145                 // Return it
146                 return receiptItem;
147         }
148
149 }