]> git.mxchange.org Git - jfinancials-war.git/blob
b9d399c8a16c781591619f3a80cb5db3b0c16b0e
[jfinancials-war.git] /
1 /*
2  * Copyright (C) 2017 Roland Häder
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.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;
36
37 /**
38  * An administrative backing bean for receipt items
39  * <p>
40  * @author Roland Häder<roland@mxchange.org>
41  */
42 @Named ("adminReceiptItemController")
43 @RequestScoped
44 public class FinancialAdminReceiptItemWebRequestBean extends BaseFinancialsBean implements FinancialAdminReceiptItemWebRequestController {
45
46         /**
47          * Serial number
48          */
49         private static final long serialVersionUID = 595_754_280_374_172L;
50
51         /**
52          * Event being fired when administrator has added a new receipt item
53          */
54         @Inject
55         @Any
56         private Event<ObservableReceiptItemAddedEvent> adminAddedReceiptItemEvent;
57
58         /**
59          * EJB for general financial receipt item purposes
60          */
61         @EJB (lookup = "java:global/jfinancials-ejb/adminFinancialReceiptItem!org.mxchange.jfinancials.model.receipt_item.FinancialAdminReceiptItemSessionBeanRemote")
62         private FinancialAdminReceiptItemSessionBeanRemote adminReceiptItemBean;
63
64         /**
65          * Discount on product price (if any) Valid: values 0...1 (1=100% discount)
66          */
67         private Float itemDiscount;
68
69         /**
70          * Item product
71          */
72         private Product itemProduct;
73
74         /**
75          * Quantity of item
76          */
77         private Long itemQuantity;
78
79         /**
80          * Assigned receipt
81          */
82         private BillableReceipt itemReceipt;
83
84         /**
85          * General receipt item controller
86          */
87         @Inject
88         private FinancialsReceiptItemWebRequestController receiptItemController;
89
90         /**
91          * Default constructor
92          */
93         public FinancialAdminReceiptItemWebRequestBean () {
94                 // Call super constructor
95                 super();
96         }
97
98         /**
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
101          * thrown.
102          * <p>
103          * @return Link outcome
104          */
105         public String addReceiptItem () {
106                 // Are all required fields set?
107
108                 // Prepare receipt item instance
109                 final BillableReceiptItem receiptItem = this.createReceiptItemInstance();
110
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
115                 }
116
117                 // Init variable
118                 final BillableReceiptItem updatedReceiptItem;
119
120                 // All is set, then try to call EJB
121                 try {
122                         // Add it
123                         updatedReceiptItem = this.adminReceiptItemBean.addReceiptItem(receiptItem);
124                 } catch (final ReceiptItemAlreadyAddedException ex) {
125                         // Throw it again
126                         throw new FaceletException(ex);
127                 }
128
129                 // Fire event with updated instance
130                 this.adminAddedReceiptItemEvent.fire(new ReceiptItemAddedEvent(updatedReceiptItem));
131
132                 // Clear bean
133                 this.clear();
134
135                 // Return redirect outcome
136                 return "add_receipt_item?faces-redirect=true"; //NOI18N
137         }
138
139         /**
140          * Getter for item discount
141          * <p>
142          * @return Item discount
143          */
144         public Float getItemDiscount () {
145                 return this.itemDiscount;
146         }
147
148         /**
149          * Setter for item discount
150          * <p>
151          * @param itemDiscount Item discount
152          */
153         public void setItemDiscount (final Float itemDiscount) {
154                 this.itemDiscount = itemDiscount;
155         }
156
157         /**
158          * Getter for assigned product
159          * <p>
160          * @return Assigned product
161          */
162         public Product getItemProduct () {
163                 return this.itemProduct;
164         }
165
166         /**
167          * Setter for assigned product
168          * <p>
169          * @param itemProduct Assigned product
170          */
171         public void setItemProduct (final Product itemProduct) {
172                 this.itemProduct = itemProduct;
173         }
174
175         /**
176          * Getter for item quantity
177          * <p>
178          * @return Item quantity
179          */
180         public Long getItemQuantity () {
181                 return this.itemQuantity;
182         }
183
184         /**
185          * Setter for item quantity
186          * <p>
187          * @param itemQuantity Item quantity
188          */
189         public void setItemQuantity (final Long itemQuantity) {
190                 this.itemQuantity = itemQuantity;
191         }
192
193         /**
194          * Getter for assigned receipt
195          * <p>
196          * @return Assigned receipt
197          */
198         public BillableReceipt getItemReceipt () {
199                 return this.itemReceipt;
200         }
201
202         /**
203          * Setter for assigned receipt
204          * <p>
205          * @param itemReceipt Assigned receipt
206          */
207         public void setItemReceipt (final BillableReceipt itemReceipt) {
208                 this.itemReceipt = itemReceipt;
209         }
210
211         /**
212          * Clears this bean
213          */
214         private void clear () {
215                 // Clear all fields
216                 this.setItemDiscount(null);
217                 this.setItemProduct(null);
218                 this.setItemQuantity(null);
219                 this.setItemReceipt(null);
220         }
221
222         /**
223          * Creates a new instance from all available data of this bean.
224          * <p>
225          * @return Receipt item instance
226          */
227         private BillableReceiptItem createReceiptItemInstance () {
228                 // Create new instance with minimum required data
229                 final BillableReceiptItem receiptItem = new FinancialReceiptItem(this.getItemProduct(), this.getItemQuantity(), this.getItemReceipt());
230
231                 // Set optional data
232                 receiptItem.setItemProductDiscount(this.getItemDiscount());
233
234                 // Return prepared instance
235                 return receiptItem;
236         }
237
238 }