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