]> git.mxchange.org Git - jfinancials-war.git/blob
dd921455a184268575dd531ebcd74bed43eba22a
[jfinancials-war.git] /
1 /*
2  * Copyright (C) 2016, 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.income;
18
19 import java.text.MessageFormat;
20 import java.util.Arrays;
21 import java.util.List;
22 import javax.annotation.PostConstruct;
23 import javax.enterprise.context.RequestScoped;
24 import javax.faces.view.facelets.FaceletException;
25 import javax.inject.Inject;
26 import javax.inject.Named;
27 import javax.naming.Context;
28 import javax.naming.InitialContext;
29 import javax.naming.NamingException;
30 import org.mxchange.jfinancials.beans.BaseFinancialsController;
31 import org.mxchange.jfinancials.beans.login.user.FinancialsUserLoginWebSessionController;
32 import org.mxchange.jfinancials.financial.income.FinancialIncomeSessionBeanRemote;
33 import org.mxchange.jfinancials.model.income.BillableIncome;
34 import org.mxchange.jfinancials.model.income.FinancialIncome;
35 import org.mxchange.jfinancials.model.income.interval.FinancialInterval;
36
37 /**
38  * An administrative financial income bean (controller)
39  * <p>
40  * @author Roland Häder<roland@mxchange.org>
41  */
42 @Named ("financialIncomeController")
43 @RequestScoped
44 public class FinancialsAdminFinancialIncomeWebRequestBean extends BaseFinancialsController implements FinancialsAdminFinancialIncomeWebRequestController {
45
46         /**
47          * Serial number
48          */
49         private static final long serialVersionUID = 56_189_028_928_371L;
50
51         /**
52          * Remote contact bean
53          */
54         private FinancialIncomeSessionBeanRemote financialBean;
55
56         /**
57          * Income interval
58          */
59         private FinancialInterval incomeInterval;
60
61         /**
62          * Income single amount
63          */
64         private Float incomeSingleAmount;
65
66         /**
67          * Income (type) title
68          */
69         private String incomeTitle;
70
71         /**
72          * User instance
73          */
74         @Inject
75         private FinancialsUserLoginWebSessionController userLoginController;
76
77         /**
78          * Constructor
79          */
80         public FinancialsAdminFinancialIncomeWebRequestBean () {
81                 // Call super constructor
82                 super();
83         }
84
85         /**
86          * Adds income data by calling proper business method of the EJB.
87          * <p>
88          * @return Redirect outcome
89          */
90         public String addFinancialIncome () {
91                 // Is all data valid?
92                 if (!this.userLoginController.isUserLoggedIn()) {
93                         // Not logged-in
94                         throw new IllegalStateException("User is not logged-in"); //NOI18N
95                 } else if (null == this.getIncomeInterval()) {
96                         // Throw NPE
97                         throw new NullPointerException("incomeInterval is null"); //NOI18N
98                 } else if (null == this.getIncomeSingleAmount()) {
99                         // Throw again
100                         throw new NullPointerException("incomeSingleAmount is null"); //NOI18N
101                 } else if (this.getIncomeSingleAmount() < 0) {
102                         // Not allowed value
103                         throw new IllegalArgumentException(MessageFormat.format("incomeSingleAmount={0} is not valid.", this.getIncomeSingleAmount())); //NOI18N
104                 } else if (null == this.getIncomeTitle()) {
105                         // Throw again
106                         throw new NullPointerException("incomeTitle is null"); //NOI18N
107                 } else if (this.getIncomeTitle().isEmpty()) {
108                         // Should not be empty
109                         throw new IllegalArgumentException("incomeTitle is empty"); //NOI18N
110                 }
111
112                 // Now that all required data has been entered, prepare new income instance
113                 BillableIncome income = new FinancialIncome(this.getIncomeTitle(), this.getIncomeSingleAmount(), this.getIncomeInterval(), this.userLoginController.getLoggedInUser());
114
115                 // Handle it over to the EJB
116                 // @TODO Use updated income instance, e.g. fire event
117                 BillableIncome updatedIncome = this.financialBean.addIncome(income);
118
119                 // All fine
120                 return "add_user_fiancial_income"; //NOI18N
121         }
122
123         @Override
124         public List<FinancialInterval> getAllIncomeIntervals () {
125                 // Init array
126                 List<FinancialInterval> incomeIntervals = Arrays.asList(FinancialInterval.values());
127
128                 // Return it
129                 return incomeIntervals;
130         }
131
132         @Override
133         public FinancialInterval getIncomeInterval () {
134                 return this.incomeInterval;
135         }
136
137         @Override
138         public void setIncomeInterval (final FinancialInterval incomeInterval) {
139                 this.incomeInterval = incomeInterval;
140         }
141
142         @Override
143         public Float getIncomeSingleAmount () {
144                 return this.incomeSingleAmount;
145         }
146
147         @Override
148         public void setIncomeSingleAmount (final Float incomeSingleAmount) {
149                 this.incomeSingleAmount = incomeSingleAmount;
150         }
151
152         @Override
153         public String getIncomeTitle () {
154                 return this.incomeTitle;
155         }
156
157         @Override
158         public void setIncomeTitle (final String incomeTitle) {
159                 this.incomeTitle = incomeTitle;
160         }
161
162         /**
163          * Post-initialization of this class
164          */
165         @PostConstruct
166         public void init () {
167                 // Try it
168                 try {
169                         // Get initial context
170                         Context context = new InitialContext();
171
172                         // Try to lookup
173                         this.financialBean = (FinancialIncomeSessionBeanRemote) context.lookup("java:global/jfinancials-ejb/financial!org.mxchange.jfinancials.financial.income.FinancialIncomeSessionBeanRemote"); //NOI18N
174                 } catch (final NamingException e) {
175                         // Throw again
176                         throw new FaceletException(e);
177                 }
178         }
179
180 }