]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/helper/PizzaWebRequestHelper.java
82fd2019b69c6287fde2141d956dee76ce52a089
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / helper / PizzaWebRequestHelper.java
1 /*
2  * Copyright (C) 2016 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.pizzaapplication.beans.helper;
18
19 import java.text.MessageFormat;
20 import javax.enterprise.context.RequestScoped;
21 import javax.inject.Inject;
22 import javax.inject.Named;
23 import org.mxchange.jcontacts.contact.Contact;
24 import org.mxchange.jcustomercore.model.customer.Customer;
25 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
26 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
27 import org.mxchange.jphone.phonenumbers.mobile.DialableMobileNumber;
28 import org.mxchange.jusercore.model.user.User;
29 import org.mxchange.pizzaapplication.beans.contact.PizzaAdminContactWebRequestController;
30 import org.mxchange.pizzaapplication.beans.customer.PizzaAdminCustomerWebRequestController;
31 import org.mxchange.pizzaapplication.beans.customer.PizzaCustomerWebSessionController;
32 import org.mxchange.pizzaapplication.beans.phone.PizzaAdminPhoneWebRequestController;
33 import org.mxchange.pizzaapplication.beans.user.PizzaUserWebSessionController;
34
35 /**
36  * A general helper for beans
37  * <p>
38  * @author Roland Häder<roland@mxchange.org>
39  */
40 @Named ("beanHelper")
41 @RequestScoped
42 public class PizzaWebRequestHelper implements PizzaWebRequestController {
43
44         /**
45          * Serial number
46          */
47         private static final long serialVersionUID = 17_258_793_567_145_701L;
48
49         /**
50          * Administrative contact controller
51          */
52         @Inject
53         private PizzaAdminContactWebRequestController adminContactController;
54
55         /**
56          * Administrative user controller
57          */
58         @Inject
59         private PizzaAdminCustomerWebRequestController adminCustomerController;
60
61         /**
62          * Administrative phone controller
63          */
64         @Inject
65         private PizzaAdminPhoneWebRequestController adminPhoneController;
66
67         /**
68          * Contact instance
69          */
70         private Contact contact;
71
72         /**
73          * Contact instance
74          */
75         private Customer customer;
76
77         /**
78          * General user controller
79          */
80         @Inject
81         private PizzaCustomerWebSessionController customerController;
82
83         /**
84          * User instance
85          */
86         private User user;
87
88         /**
89          * Regular user controller
90          */
91         @Inject
92         private PizzaUserWebSessionController userController;
93
94         /**
95          * Default constructor
96          */
97         public PizzaWebRequestHelper () {
98         }
99
100         @Override
101         public void copyContactToController () {
102                 // Validate user instance
103                 if (this.getContact() == null) {
104                         // Throw NPE
105                         throw new NullPointerException("this.contact is null"); //NOI18N
106                 } else if (this.getContact().getContactId() == null) {
107                         // Throw NPE again
108                         throw new NullPointerException("this.contact.contactId is null"); //NOI18N
109                 } else if (this.getContact().getContactId() < 1) {
110                         // Not valid
111                         throw new IllegalStateException(MessageFormat.format("this.contact.contactId={0} is not valid.", this.getContact().getContactId())); //NOI18N
112                 }
113
114                 // Set all phone instances
115                 this.setPhoneInstances(this.getContact());
116
117                 // Set all fields: user
118                 this.adminContactController.copyContactToController(this.getContact());
119         }
120
121         @Override
122         public void copyCustomerToController () {
123                 // Log message
124                 //* NOISY-DEBUG: */ System.out.println("AdminHelper::copyCustomerToController - CALLED!"); //NOI18N
125
126                 // Validate user instance
127                 if (this.getCustomer() == null) {
128                         // Throw NPE
129                         throw new NullPointerException("this.customer is null"); //NOI18N
130                 } else if (this.getCustomer().getCustomerId() == null) {
131                         // Throw NPE again
132                         throw new NullPointerException("this.customer.customerId is null"); //NOI18N
133                 } else if (this.getCustomer().getCustomerId() < 1) {
134                         // Not valid
135                         throw new IllegalStateException(MessageFormat.format("this.customer.customerId={0} is not valid.", this.getCustomer().getCustomerId())); //NOI18N
136                 }
137
138                 // Set all fields: user
139                 this.adminCustomerController.copyCustomerToController(this.getCustomer());
140
141                 // Log message
142                 //* NOISY-DEBUG: */ System.out.println("AdminHelper::copyCustomerToController - EXIT!"); //NOI18N
143         }
144
145         @Override
146         public void copyUserToController () {
147                 // Validate user instance
148                 if (this.getUser() == null) {
149                         // Throw NPE
150                         throw new NullPointerException("this.user is null"); //NOI18N
151                 } else if (this.getUser().getUserId() == null) {
152                         // Throw NPE again
153                         throw new NullPointerException("this.user.userId is null"); //NOI18N
154                 } else if (this.getUser().getUserId() < 1) {
155                         // Not valid
156                         throw new IllegalStateException(MessageFormat.format("this.user.userId={0} is not valid.", this.getUser().getUserId())); //NOI18N
157                 }
158
159                 // Get contact
160                 Contact userContact = this.getUser().getUserContact();
161
162                 // Set contact here, too. This avoids parameters that cannot auto-complete in IDEs.
163                 this.setContact(userContact);
164
165                 // Set all phone instances
166                 this.setPhoneInstances(userContact);
167
168                 // Set all fields: user
169                 this.userController.setUserName(this.getUser().getUserName());
170         }
171
172         @Override
173         public Contact getContact () {
174                 return this.contact;
175         }
176
177         @Override
178         public void setContact (final Contact contact) {
179                 this.contact = contact;
180         }
181
182         @Override
183         public String getContactUsageMessageKey (final Contact contact) {
184                 // The contact must be valid
185                 if (null == contact) {
186                         // Throw NPE
187                         throw new NullPointerException("contact is null"); //NOI18N
188                 } else if (contact.getContactId() == null) {
189                         // Throw again ...
190                         throw new NullPointerException("contact.contactId is null"); //NOI18N
191                 } else if (contact.getContactId() < 1) {
192                         // Not valid
193                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
194                 }
195
196                 // Default key is "unused"
197                 String messageKey = "CONTACT_IS_UNUSED"; //NOI18N
198
199                 // Check user/customer
200                 boolean isUserContact = this.userController.isContactFound(contact);
201                 boolean isCustomerContact = this.customerController.isContactFound(contact);
202
203                 // Check user first
204                 if (isUserContact && isCustomerContact) {
205                         // Is both
206                         messageKey = "CONTACT_IS_USER_USER"; //NOI18N
207                 } else if (isUserContact) {
208                         // Only user
209                         messageKey = "CONTACT_IS_USER"; //NOI18N
210                 } else if (isCustomerContact) {
211                         // Only customer
212                         messageKey = "CONTACT_IS_USER"; //NOI18N
213                 }
214
215                 // Return message key
216                 return messageKey;
217         }
218
219         @Override
220         public Customer getCustomer () {
221                 return this.customer;
222         }
223
224         @Override
225         public void setCustomer (final Customer customer) {
226                 this.customer = customer;
227         }
228
229         @Override
230         public User getUser () {
231                 return this.user;
232         }
233
234         @Override
235         public void setUser (final User user) {
236                 this.user = user;
237         }
238
239         /**
240          * Set's all given contact's phone instances: land-line, cellphone and fax
241          * <p>
242          * @param contact Contact to set phone instances for
243          */
244         private void setPhoneInstances (final Contact contact) {
245                 // The contact must be valid
246                 if (null == contact) {
247                         // Throw NPE
248                         throw new NullPointerException("contact is null"); //NOI18N
249                 } else if (contact.getContactId() == null) {
250                         // Throw again ...
251                         throw new NullPointerException("contact.contactId is null"); //NOI18N
252                 } else if (contact.getContactId() < 1) {
253                         // Not valid
254                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
255                 }
256
257                 // Is cellphone set?
258                 if (contact.getContactMobileNumber() instanceof DialableMobileNumber) {
259                         // Yes, then set it in admin controller
260                         this.adminPhoneController.setMobileNumber(contact.getContactMobileNumber());
261                 }
262
263                 // Is land-line set?
264                 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
265                         // Yes, then set it in admin controller
266                         this.adminPhoneController.setLandLineNumber(contact.getContactLandLineNumber());
267                 }
268
269                 // Is fax set?
270                 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
271                         // Yes, then set it in admin controller
272                         this.adminPhoneController.setFaxNumber(contact.getContactFaxNumber());
273                 }
274         }
275
276 }