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