]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/helper/PizzaWebRequestHelper.java
renamed methods
[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                 // Log message
103                 //* NOISY-DEBUG: */ System.out.println("AdminHelper::copyContactToController - CALLED!"); //NOI18N
104
105                 // Validate user instance
106                 if (this.getContact() == null) {
107                         // Throw NPE
108                         throw new NullPointerException("this.contact is null"); //NOI18N
109                 } else if (this.getContact().getContactId() == null) {
110                         // Throw NPE again
111                         throw new NullPointerException("this.contact.contactId is null"); //NOI18N
112                 } else if (this.getContact().getContactId() < 1) {
113                         // Not valid
114                         throw new IllegalStateException(MessageFormat.format("this.contact.contactId={0} is not valid.", this.getContact().getContactId())); //NOI18N
115                 }
116
117                 // Set all phone instances
118                 this.setPhoneInstances(this.getContact());
119
120                 // Set all fields: user
121                 this.adminContactController.copyContactToController(this.getContact());
122
123                 // Log message
124                 //* NOISY-DEBUG: */ System.out.println("AdminHelper::copyContactToController - EXIT!"); //NOI18N
125         }
126
127         @Override
128         public void copyCustomerToController () {
129                 // Log message
130                 //* NOISY-DEBUG: */ System.out.println("AdminHelper::copyCustomerToController - CALLED!"); //NOI18N
131
132                 // Validate user instance
133                 if (this.getCustomer() == null) {
134                         // Throw NPE
135                         throw new NullPointerException("this.customer is null"); //NOI18N
136                 } else if (this.getCustomer().getCustomerId() == null) {
137                         // Throw NPE again
138                         throw new NullPointerException("this.customer.customerId is null"); //NOI18N
139                 } else if (this.getCustomer().getCustomerId() < 1) {
140                         // Not valid
141                         throw new IllegalStateException(MessageFormat.format("this.customer.customerId={0} is not valid.", this.getCustomer().getCustomerId())); //NOI18N
142                 }
143
144                 // Set all fields: user
145                 this.adminCustomerController.copyCustomerToController(this.getCustomer());
146
147                 // Log message
148                 //* NOISY-DEBUG: */ System.out.println("AdminHelper::copyCustomerToController - EXIT!"); //NOI18N
149         }
150
151         @Override
152         public void copyUserToController () {
153                 // Log message
154                 //* NOISY-DEBUG: */ System.out.println("AdminHelper::copyUserToController - CALLED!"); //NOI18N
155
156                 // Validate user instance
157                 if (this.getUser() == null) {
158                         // Throw NPE
159                         throw new NullPointerException("this.user is null"); //NOI18N
160                 } else if (this.getUser().getUserId() == null) {
161                         // Throw NPE again
162                         throw new NullPointerException("this.user.userId is null"); //NOI18N
163                 } else if (this.getUser().getUserId() < 1) {
164                         // Not valid
165                         throw new IllegalStateException(MessageFormat.format("this.user.userId={0} is not valid.", this.getUser().getUserId())); //NOI18N
166                 }
167
168                 // Get contact
169                 Contact userContact = this.getUser().getUserContact();
170
171                 // Set contact here, too. This avoids parameters that cannot auto-complete in IDEs.
172                 this.setContact(userContact);
173
174                 // Set all phone instances
175                 this.setPhoneInstances(userContact);
176
177                 // Set all fields: user
178                 this.userController.setUserName(this.getUser().getUserName());
179
180                 // Log message
181                 //* NOISY-DEBUG: */ System.out.println("AdminHelper::copyUserToController - EXIT!"); //NOI18N
182         }
183
184         @Override
185         public Contact getContact () {
186                 return this.contact;
187         }
188
189         @Override
190         public void setContact (final Contact contact) {
191                 this.contact = contact;
192         }
193
194         @Override
195         public String getContactUsageMessageKey (final Contact contact) {
196                 // The contact must be valid
197                 if (null == contact) {
198                         // Throw NPE
199                         throw new NullPointerException("contact is null"); //NOI18N
200                 } else if (contact.getContactId() == null) {
201                         // Throw again ...
202                         throw new NullPointerException("contact.contactId is null"); //NOI18N
203                 } else if (contact.getContactId() < 1) {
204                         // Not valid
205                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
206                 }
207
208                 // Default key is "unused"
209                 String messageKey = "CONTACT_IS_UNUSED"; //NOI18N
210
211                 // Check user/customer
212                 boolean isUserContact = this.userController.isContactFound(contact);
213                 boolean isCustomerContact = this.customerController.isContactFound(contact);
214
215                 // Check user first
216                 if (isUserContact && isCustomerContact) {
217                         // Is both
218                         messageKey = "CONTACT_IS_USER_USER"; //NOI18N
219                 } else if (isUserContact) {
220                         // Only user
221                         messageKey = "CONTACT_IS_USER"; //NOI18N
222                 } else if (isCustomerContact) {
223                         // Only customer
224                         messageKey = "CONTACT_IS_USER"; //NOI18N
225                 }
226
227                 // Return message key
228                 return messageKey;
229         }
230
231         @Override
232         public Customer getCustomer () {
233                 return this.customer;
234         }
235
236         @Override
237         public void setCustomer (final Customer customer) {
238                 this.customer = customer;
239         }
240
241         @Override
242         public User getUser () {
243                 return this.user;
244         }
245
246         @Override
247         public void setUser (final User user) {
248                 this.user = user;
249         }
250
251         /**
252          * Set's all given contact's phone instances: land-line, cellphone and fax
253          * <p>
254          * @param contact Contact to set phone instances for
255          */
256         private void setPhoneInstances (final Contact contact) {
257                 // The contact must be valid
258                 if (null == contact) {
259                         // Throw NPE
260                         throw new NullPointerException("contact is null"); //NOI18N
261                 } else if (contact.getContactId() == null) {
262                         // Throw again ...
263                         throw new NullPointerException("contact.contactId is null"); //NOI18N
264                 } else if (contact.getContactId() < 1) {
265                         // Not valid
266                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
267                 }
268
269                 // Is cellphone set?
270                 if (contact.getContactMobileNumber() instanceof DialableMobileNumber) {
271                         // Yes, then set it in admin controller
272                         this.adminPhoneController.setMobileNumber(contact.getContactMobileNumber());
273                 }
274
275                 // Is land-line set?
276                 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
277                         // Yes, then set it in admin controller
278                         this.adminPhoneController.setLandLineNumber(contact.getContactLandLineNumber());
279                 }
280
281                 // Is fax set?
282                 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
283                         // Yes, then set it in admin controller
284                         this.adminPhoneController.setFaxNumber(contact.getContactFaxNumber());
285                 }
286         }
287
288 }