]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/helper/JobsWebRequestHelper.java
Renaming season has started:
[jjobs-war.git] / src / java / org / mxchange / jjobs / beans / helper / JobsWebRequestHelper.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.jjobs.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.jjobs.beans.contact.JobsAdminContactWebRequestController;
25 import org.mxchange.jjobs.beans.phone.JobsAdminPhoneWebRequestController;
26 import org.mxchange.jjobs.beans.user.JobsUserWebSessionController;
27 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
28 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
29 import org.mxchange.jphone.phonenumbers.mobile.DialableMobileNumber;
30 import org.mxchange.jusercore.model.user.User;
31
32 /**
33  * A general helper for beans
34  * <p>
35  * @author Roland Haeder<roland@mxchange.org>
36  */
37 @Named ("adminHelper")
38 @RequestScoped
39 public class JobsWebRequestHelper implements JobsWebRequestController {
40
41         /**
42          * Serial number
43          */
44         private static final long serialVersionUID = 17_258_793_567_145_701L;
45
46         /**
47          * Administrative contact controller
48          */
49         @Inject
50         private JobsAdminContactWebRequestController adminContactController;
51
52         /**
53          * Administrative phone controller
54          */
55         @Inject
56         private JobsAdminPhoneWebRequestController adminPhoneController;
57
58         /**
59          * Contact instance
60          */
61         private Contact contact;
62
63         /**
64          * Fax number
65          */
66         private DialableFaxNumber faxNumber;
67
68         /**
69          * Land-line number
70          */
71         private DialableLandLineNumber landLineNumber;
72
73         /**
74          * Cell phone number
75          */
76         private DialableMobileNumber mobileNumber;
77
78         /**
79          * User instance
80          */
81         private User user;
82
83         /**
84          * Regular user controller
85          */
86         @Inject
87         private JobsUserWebSessionController userController;
88
89         /**
90          * Default constructor
91          */
92         public JobsWebRequestHelper () {
93         }
94
95         @Override
96         public void copyContactToController () {
97                 // Validate contact instance
98                 if (this.getContact() == null) {
99                         // Throw NPE
100                         throw new NullPointerException("this.contact is null"); //NOI18N
101                 } else if (this.getContact().getContactId() == null) {
102                         // Throw NPE again
103                         throw new NullPointerException("this.contact.contactId is null"); //NOI18N
104                 } else if (this.getContact().getContactId() < 1) {
105                         // Not valid
106                         throw new IllegalStateException(MessageFormat.format("this.contact.contactId={0} is not valid.", this.getContact().getContactId())); //NOI18N
107                 }
108
109                 // Set all phone instances
110                 this.setPhoneInstances(this.getContact());
111
112                 // Set all fields: user
113                 this.adminContactController.copyContactToController(this.getContact());
114
115                 // Log message
116                 //* NOISY-DEBUG: */ System.out.println("AdminHelper::copyContactToController - EXIT!"); //NOI18N
117         }
118
119         @Override
120         public void copyMobileNumberToController () {
121                 // Validate mobile instance
122                 if (this.getMobileNumber() == null) {
123                         // Throw NPE
124                         throw new NullPointerException("this.cellPhoneNumber is null");
125                 } else if (this.getMobileNumber().getPhoneId() == null) {
126                         // Throw again
127                         throw new NullPointerException("this.cellPhoneNumber.phoneId is null");
128                 } else if (this.getMobileNumber().getPhoneId() < 1) {
129                         // Invalid id number
130                         throw new IllegalArgumentException(MessageFormat.format("this.cellPhoneNumber.phoneId={0} is not valid", this.getMobileNumber().getPhoneId()));
131                 } else if (this.getMobileNumber().getMobileProvider() == null) {
132                         // Throw NPE again
133                         throw new NullPointerException("this.cellPhoneNumber.mobileProvider is null");
134                 } else if (this.getMobileNumber().getMobileProvider().getProviderId() == null) {
135                         // ... throw again
136                         throw new NullPointerException("this.cellPhoneNumber.mobileProvider.providerId is null");
137                 } else if (this.getMobileNumber().getMobileProvider().getProviderId() < 1) {
138                         // Invalid id
139                         throw new IllegalArgumentException(MessageFormat.format("this.cellPhoneNumber.mobileProvider.providerId={0} is invalid", this.getMobileNumber().getMobileProvider().getProviderId()));
140                 } else if (this.getMobileNumber().getPhoneNumber() == null) {
141                         // Throw NPE again ...
142                         throw new NullPointerException("this.cellPhoneNumber.phoneNumber is null");
143                 } else if (this.getMobileNumber().getPhoneNumber() < 1) {
144                         // Invalid id number
145                         throw new IllegalArgumentException(MessageFormat.format("this.cellPhoneNumber.phoneNumber={0} is not valid", this.getMobileNumber().getPhoneNumber()));
146                 }
147
148                 // Copy all (changeable) data fields to admin controller
149                 this.adminPhoneController.setMobileProvider(this.getMobileNumber().getMobileProvider());
150                 this.adminPhoneController.setPhoneNumber(this.getMobileNumber().getPhoneNumber());
151         }
152
153         @Override
154         public void copyUserToController () {
155                 // Log message
156                 //* NOISY-DEBUG: */ System.out.println("AdminHelper::copyUserToController - CALLED!"); //NOI18N
157
158                 // Validate user instance
159                 if (this.getUser() == null) {
160                         // Throw NPE
161                         throw new NullPointerException("this.user is null");
162                 } else if (this.getUser().getUserId() == null) {
163                         // Throw NPE again
164                         throw new NullPointerException("this.user.userId is null");
165                 } else if (this.getUser().getUserId() < 1) {
166                         // Not valid
167                         throw new IllegalStateException(MessageFormat.format("this.user.userId={0} is not valid.", this.getUser().getUserId()));
168                 }
169
170                 // Get contact
171                 Contact userContact = this.getUser().getUserContact();
172
173                 // Set contact here, too. This avoids parameters that cannot auto-complete in IDEs.
174                 this.setContact(userContact);
175
176                 // Set all phone instances
177                 this.setPhoneInstances(userContact);
178
179                 // Set all fields: user
180                 this.userController.setUserName(this.getUser().getUserName());
181
182                 // Log message
183                 //* NOISY-DEBUG: */ System.out.println("AdminHelper::copyUserToController - EXIT!"); //NOI18N
184         }
185
186         @Override
187         public Contact getContact () {
188                 return this.contact;
189         }
190
191         @Override
192         public void setContact (final Contact contact) {
193                 this.contact = contact;
194         }
195
196         @Override
197         public String getContactUsageMessageKey (final Contact contact) {
198                 // The contact must be valid
199                 if (null == contact) {
200                         // Throw NPE
201                         throw new NullPointerException("contact is null"); //NOI18N
202                 } else if (contact.getContactId() == null) {
203                         // Throw again ...
204                         throw new NullPointerException("contact.contactId is null"); //NOI18N
205                 } else if (contact.getContactId() < 1) {
206                         // Not valid
207                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
208                 }
209
210                 // Default key is "unused"
211                 String messageKey = "CONTACT_IS_UNUSED"; //NOI18N
212
213                 // Check user
214                 boolean isUserContact = this.userController.isContactFound(contact);
215
216                 // Check user first
217                 if (isUserContact) {
218                         // Only user
219                         messageKey = "CONTACT_IS_USER"; //NOI18N
220                 }
221
222                 // Return message key
223                 return messageKey;
224         }
225
226         @Override
227         public DialableFaxNumber getFaxNumber () {
228                 return this.faxNumber;
229         }
230
231         @Override
232         public void setFaxNumber (final DialableFaxNumber faxNumber) {
233                 this.faxNumber = faxNumber;
234         }
235
236         @Override
237         public DialableLandLineNumber getLandLineNumber () {
238                 return this.landLineNumber;
239         }
240
241         @Override
242         public void setLandLineNumber (final DialableLandLineNumber landLineNumber) {
243                 this.landLineNumber = landLineNumber;
244         }
245
246         @Override
247         public DialableMobileNumber getMobileNumber () {
248                 return this.mobileNumber;
249         }
250
251         @Override
252         public void setMobileNumber (final DialableMobileNumber mobileNumber) {
253                 this.mobileNumber = mobileNumber;
254         }
255
256         @Override
257         public User getUser () {
258                 return this.user;
259         }
260
261         @Override
262         public void setUser (final User user) {
263                 this.user = user;
264         }
265
266         /**
267          * Set's all given contact's phone instances: land-line, mobile and
268          * faxNumber
269          * <p>
270          * @param contact Contact to set phone instances for
271          */
272         private void setPhoneInstances (final Contact contact) {
273                 // The contact must be valid
274                 if (null == contact) {
275                         // Throw NPE
276                         throw new NullPointerException("contact is null"); //NOI18N
277                 } else if (contact.getContactId() == null) {
278                         // Throw again ...
279                         throw new NullPointerException("contact.contactId is null"); //NOI18N
280                 } else if (contact.getContactId() < 1) {
281                         // Not valid
282                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
283                 }
284
285                 // Is mobile set?
286                 if (contact.getContactMobileNumber() instanceof DialableMobileNumber) {
287                         // Yes, then set it in admin controller
288                         this.setMobileNumber(contact.getContactMobileNumber());
289                 }
290
291                 // Is land-line set?
292                 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
293                         // Yes, then set it in admin controller
294                         this.setLandLineNumber(contact.getContactLandLineNumber());
295                 }
296
297                 // Is faxNumber set?
298                 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
299                         // Yes, then set it in admin controller
300                         this.setFaxNumber(contact.getContactFaxNumber());
301                 }
302         }
303
304 }