]> git.mxchange.org Git - jfinancials-war.git/blob
df8336da5de48786e72bd9f7fea2500319a4d396
[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.item.BillableReceiptItem;
32 import org.mxchange.jfinancials.model.receipt.item.FinancialReceiptItem;
33 import org.mxchange.jfinancials.model.receipt_item.FinancialAdminReceiptItemSessionBeanRemote;
34
35 /**
36  * An administrative backing bean for receipt items
37  * <p>
38  * @author Roland Häder<roland@mxchange.org>
39  */
40 @Named ("adminReceiptItemController")
41 @RequestScoped
42 public class FinancialAdminReceiptItemWebRequestBean extends BaseFinancialsBean implements FinancialAdminReceiptItemWebRequestController {
43
44         /**
45          * Serial number
46          */
47         private static final long serialVersionUID = 595_754_280_374_172L;
48
49         /**
50          * Event being fired when administrator has added a new receipt item
51          */
52         @Inject
53         @Any
54         private Event<ObservableReceiptItemAddedEvent> adminAddedReceiptItemEvent;
55
56         /**
57          * EJB for general financial receipt item purposes
58          */
59         @EJB (lookup = "java:global/jfinancials-ejb/adminFinancialReceiptItem!org.mxchange.jfinancials.model.receipt.item.FinancialAdminReceiptItemSessionBeanRemote")
60         private FinancialAdminReceiptItemSessionBeanRemote adminReceiptItemBean;
61
62         /**
63          * General receipt item controller
64          */
65         @Inject
66         private FinancialsReceiptItemWebRequestController receiptItemController;
67
68         /**
69          * Default constructor
70          */
71         public FinancialAdminReceiptItemWebRequestBean () {
72                 // Call super constructor
73                 super();
74         }
75
76         /**
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
79          * thrown.
80          * <p>
81          * @return Link outcome
82          */
83         public String addReceiptItem () {
84                 // Are all required fields set?
85
86                 // Prepare receipt item instance
87                 final BillableReceiptItem receiptItem = this.createReceiptItemInstance();
88
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
93                 }
94
95                 // Init variable
96                 final BillableReceiptItem updatedReceiptItem;
97
98                 // All is set, then try to call EJB
99                 try {
100                         // Add it
101                         updatedReceiptItem = this.adminReceiptItemBean.addReceiptItem(receiptItem);
102                 } catch (final ReceiptItemAlreadyAddedException ex) {
103                         // Throw it again
104                         throw new FaceletException(ex);
105                 }
106
107                 // Fire event with updated instance
108                 this.adminAddedReceiptItemEvent.fire(new ReceiptItemAddedEvent(updatedReceiptItem));
109
110                 // Clear bean
111                 this.clear();
112
113                 // Return redirect outcome
114                 return "add_receipt_item?faces-redirect=true"; //NOI18N
115         }
116
117         /**
118          * Clears this bean
119          */
120         private void clear () {
121                 // Clear all fields
122         }
123
124         /**
125          * Creates a new instance from all available data of this bean.
126          * <p>
127          * @return Receipt item instance
128          */
129         private BillableReceiptItem createReceiptItemInstance () {
130                 // Create new instance with minimum required data
131                 final BillableReceiptItem receiptItem = new FinancialReceiptItem();
132
133                 // Set optional data
134
135                 // Return prepared instance
136                 return receiptItem;
137         }
138
139 }