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