]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/customer/PizzaAdminCustomerWebRequestBean.java
catch newly thrown exception
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / customer / PizzaAdminCustomerWebRequestBean.java
1 /*
2  * Copyright (C) 2016 Roland Haeder
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.pizzaapplication.beans.customer;
18
19 import java.util.Collections;
20 import java.util.List;
21 import javax.annotation.PostConstruct;
22 import javax.enterprise.context.RequestScoped;
23 import javax.enterprise.event.Event;
24 import javax.enterprise.inject.Any;
25 import javax.faces.view.facelets.FaceletException;
26 import javax.inject.Inject;
27 import javax.inject.Named;
28 import javax.naming.Context;
29 import javax.naming.InitialContext;
30 import javax.naming.NamingException;
31 import org.mxchange.jcontacts.contact.Contact;
32 import org.mxchange.jcontacts.contact.gender.Gender;
33 import org.mxchange.jcontacts.exceptions.ContactAlreadyAddedException;
34 import org.mxchange.jcustomercore.events.AdminAddedCustomerEvent;
35 import org.mxchange.jcustomercore.events.CustomerAdminAddedEvent;
36 import org.mxchange.jcustomercore.exceptions.CustomerAlreadyRegisteredException;
37 import org.mxchange.jcustomercore.model.customer.Customer;
38 import org.mxchange.jcustomercore.model.customer.status.CustomerAccountStatus;
39 import org.mxchange.pizzaapplication.beans.contact.PizzaAdminContactWebRequestController;
40 import org.mxchange.pizzaapplication.model.customer.PizzaAdminCustomerSessionBeanRemote;
41
42 /**
43  * Administrative customer bean (controller)
44  * <p>
45  * @author Roland Haeder<roland@mxchange.org>
46  */
47 @Named ("adminCustomerController")
48 @RequestScoped
49 public class PizzaAdminCustomerWebRequestBean implements PizzaAdminCustomerWebRequestController {
50
51         /**
52          * Serial number
53          */
54         private static final long serialVersionUID = 12_487_062_487_527_913L;
55
56         /**
57          * Administrative contact controller (for personal data)
58          */
59         @Inject
60         private PizzaAdminContactWebRequestController adminContactController;
61
62         /**
63          * Administrative customer EJB
64          */
65         private PizzaAdminCustomerSessionBeanRemote adminCustomerBean;
66
67         /**
68          * An event being fired when an administrator has added a new customer
69          */
70         @Inject
71         @Any
72         private Event<AdminAddedCustomerEvent> customerAddedEvent;
73
74         /**
75          * A list of all customers
76          */
77         private List<Customer> customerList;
78
79         /**
80          * Default constructor
81          */
82         public PizzaAdminCustomerWebRequestBean () {
83                 // Try it
84                 try {
85                         // Get initial context
86                         Context context = new InitialContext();
87
88                         // Try to lookup
89                         this.adminCustomerBean = (PizzaAdminCustomerSessionBeanRemote) context.lookup("java:global/PizzaService-ejb/admincustomer!org.mxchange.pizzaapplication.model.customer.PizzaAdminCustomerSessionBeanRemote"); //NOI18N
90                 } catch (final NamingException e) {
91                         // Throw again
92                         throw new FaceletException(e);
93                 }
94         }
95
96         @Override
97         public String addCustomer () {
98                 // Are at least some fields added?
99                 if (!this.isCustomerDataSet()) {
100                         // Not all customer data is set
101                         throw new FaceletException("Please provide minimum personal data: gender, first_name, family_name"); //NOI18N
102                 }
103
104                 // Get contact instance
105                 Contact contact = this.adminContactController.createContactInstance();
106
107                 // Ask the EJB for a free customer number
108                 String customerNumber = this.adminCustomerBean.createCustomerNumber();
109
110                 // Create new customer instance
111                 Customer customer = new PizzaCustomer(CustomerAccountStatus.CONFIRMED, contact, customerNumber);
112
113                 // Init instance
114                 Customer updatedCustomer;
115
116                 try {
117                         // Add customer and return updated
118                         updatedCustomer = this.adminCustomerBean.addCustomer(customer);
119                 } catch (final CustomerAlreadyRegisteredException | ContactAlreadyAddedException ex) {
120                         // Throw again
121                         throw new FaceletException(ex);
122                 }
123
124                 // Add customer to list
125                 this.customerList.add(updatedCustomer);
126
127                 // Fire event
128                 this.customerAddedEvent.fire(new CustomerAdminAddedEvent(updatedCustomer));
129
130                 // Redirect
131                 return "admin_list_customer"; //NOI18N
132         }
133
134         @Override
135         public List<Customer> allCustomers () {
136                 // Return it
137                 return Collections.unmodifiableList(this.customerList);
138         }
139
140         @Override
141         public boolean hasCustomers () {
142                 return (!this.allCustomers().isEmpty());
143         }
144
145         /**
146          * Post-initialization of this class
147          */
148         @PostConstruct
149         public void init () {
150                 // Initialize customer list
151                 this.customerList = this.adminCustomerBean.allCustomers();
152         }
153
154         /**
155          * Checks whether ther minimum customer data is set
156          * <p>
157          * @return Whether minimum data is set
158          */
159         private boolean isCustomerDataSet () {
160                 // Check all
161                 return ((this.adminContactController.getGender() instanceof Gender) &&
162                                 (this.adminContactController.getFirstName() != null) &&
163                                 (!this.adminContactController.getFirstName().isEmpty()) &&
164                                 (this.adminContactController.getFamilyName() != null) &&
165                                 (!this.adminContactController.getFamilyName().isEmpty()));
166         }
167
168 }