2 * Copyright (C) 2016 Roland Haeder
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.
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.
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/>.
17 package org.mxchange.jjobs.beans.helper;
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;
33 * A general helper for beans
35 * @author Roland Haeder<roland@mxchange.org>
37 @Named ("adminHelper")
39 public class JobsWebRequestHelper implements JobsWebRequestController {
44 private static final long serialVersionUID = 17_258_793_567_145_701L;
47 * Administrative contact controller
50 private JobsAdminContactWebRequestController adminContactController;
53 * Administrative phone controller
56 private JobsAdminPhoneWebRequestController adminPhoneController;
61 private Contact contact;
66 private DialableFaxNumber faxNumber;
71 private DialableLandLineNumber landLineNumber;
76 private DialableMobileNumber mobileNumber;
84 * Regular user controller
87 private JobsUserWebSessionController userController;
92 public JobsWebRequestHelper () {
96 public void copyContactToController () {
97 // Validate contact instance
98 if (this.getContact() == null) {
100 throw new NullPointerException("this.contact is null"); //NOI18N
101 } else if (this.getContact().getContactId() == null) {
103 throw new NullPointerException("this.contact.contactId is null"); //NOI18N
104 } else if (this.getContact().getContactId() < 1) {
106 throw new IllegalStateException(MessageFormat.format("this.contact.contactId={0} is not valid.", this.getContact().getContactId())); //NOI18N
109 // Set all phone instances
110 this.setPhoneInstances(this.getContact());
112 // Set all fields: user
113 this.adminContactController.copyContactToController(this.getContact());
117 public void copyFaxNumberToController () {
118 // Validate fax instance
119 if (this.getFaxNumber() == null) {
121 throw new NullPointerException("this.faxNumber is null");
122 } else if (this.getFaxNumber().getPhoneId() == null) {
124 throw new NullPointerException("this.faxNumber.phoneId is null");
125 } else if (this.getFaxNumber().getPhoneId() < 1) {
127 throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneId={0} is not valid", this.getFaxNumber().getPhoneId()));
128 } else if (this.getFaxNumber().getPhoneAreaCode()== null) {
130 throw new NullPointerException("this.faxNumber.phoneAreaCode is null");
131 } else if (this.getFaxNumber().getPhoneAreaCode() < 1) {
133 throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneAreaCode={0} is not valid", this.getFaxNumber().getPhoneAreaCode()));
134 } else if (this.getFaxNumber().getPhoneCountry() == null) {
136 throw new NullPointerException("this.faxNumber.phoneCountry is null");
137 } else if (this.getFaxNumber().getPhoneCountry().getCountryId() == null) {
139 throw new NullPointerException("this.faxNumber.phoneCountry.countryId is null");
140 } else if (this.getFaxNumber().getPhoneCountry().getCountryId() < 1) {
142 throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneCountry.countryId={0} is invalid", this.getFaxNumber().getPhoneCountry().getCountryId()));
143 } else if (this.getFaxNumber().getPhoneNumber() == null) {
144 // Throw NPE again ...
145 throw new NullPointerException("this.faxNumber.phoneNumber is null");
146 } else if (this.getFaxNumber().getPhoneNumber() < 1) {
148 throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneNumber={0} is not valid", this.getFaxNumber().getPhoneNumber()));
151 // Copy all (changeable) data fields to admin controller
152 this.adminPhoneController.setPhoneAreaCode(this.getFaxNumber().getPhoneAreaCode());
153 this.adminPhoneController.setPhoneCountry(this.getFaxNumber().getPhoneCountry());
154 this.adminPhoneController.setPhoneNumber(this.getFaxNumber().getPhoneNumber());
158 public void copyLandLineNumberToController () {
159 // Validate land-line instance
160 if (this.getLandLineNumber() == null) {
162 throw new NullPointerException("this.landLineNumber is null");
163 } else if (this.getLandLineNumber().getPhoneId() == null) {
165 throw new NullPointerException("this.landLineNumber.phoneId is null");
166 } else if (this.getLandLineNumber().getPhoneId() < 1) {
168 throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneId={0} is not valid", this.getLandLineNumber().getPhoneId()));
169 } else if (this.getLandLineNumber().getPhoneAreaCode()== null) {
171 throw new NullPointerException("this.landLineNumber.phoneAreaCode is null");
172 } else if (this.getLandLineNumber().getPhoneAreaCode() < 1) {
174 throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneAreaCode={0} is not valid", this.getFaxNumber().getPhoneAreaCode()));
175 } else if (this.getLandLineNumber().getPhoneCountry() == null) {
177 throw new NullPointerException("this.landLineNumber.phoneCountry is null");
178 } else if (this.getLandLineNumber().getPhoneCountry().getCountryId() == null) {
180 throw new NullPointerException("this.landLineNumber.phoneCountry.countryId is null");
181 } else if (this.getLandLineNumber().getPhoneCountry().getCountryId() < 1) {
183 throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneCountry.countryId={0} is invalid", this.getLandLineNumber().getPhoneCountry().getCountryId()));
184 } else if (this.getLandLineNumber().getPhoneNumber() == null) {
185 // Throw NPE again ...
186 throw new NullPointerException("this.landLineNumber.phoneNumber is null");
187 } else if (this.getLandLineNumber().getPhoneNumber() < 1) {
189 throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneNumber={0} is not valid", this.getLandLineNumber().getPhoneNumber()));
192 // Copy all (changeable) data fields to admin controller
193 this.adminPhoneController.setPhoneAreaCode(this.getLandLineNumber().getPhoneAreaCode());
194 this.adminPhoneController.setPhoneCountry(this.getLandLineNumber().getPhoneCountry());
195 this.adminPhoneController.setPhoneNumber(this.getLandLineNumber().getPhoneNumber());
199 public void copyMobileNumberToController () {
200 // Validate mobile instance
201 if (this.getMobileNumber() == null) {
203 throw new NullPointerException("this.mobileNumber is null");
204 } else if (this.getMobileNumber().getPhoneId() == null) {
206 throw new NullPointerException("this.mobileNumber.phoneId is null");
207 } else if (this.getMobileNumber().getPhoneId() < 1) {
209 throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber.phoneId={0} is not valid", this.getMobileNumber().getPhoneId()));
210 } else if (this.getMobileNumber().getMobileProvider() == null) {
212 throw new NullPointerException("this.mobileNumber.mobileProvider is null");
213 } else if (this.getMobileNumber().getMobileProvider().getProviderId() == null) {
215 throw new NullPointerException("this.mobileNumber.mobileProvider.providerId is null");
216 } else if (this.getMobileNumber().getMobileProvider().getProviderId() < 1) {
218 throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber.mobileProvider.providerId={0} is invalid", this.getMobileNumber().getMobileProvider().getProviderId()));
219 } else if (this.getMobileNumber().getPhoneNumber() == null) {
220 // Throw NPE again ...
221 throw new NullPointerException("this.mobileNumber.phoneNumber is null");
222 } else if (this.getMobileNumber().getPhoneNumber() < 1) {
224 throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber.phoneNumber={0} is not valid", this.getMobileNumber().getPhoneNumber()));
227 // Copy all (changeable) data fields to admin controller
228 this.adminPhoneController.setMobileProvider(this.getMobileNumber().getMobileProvider());
229 this.adminPhoneController.setPhoneNumber(this.getMobileNumber().getPhoneNumber());
233 public void copyUserToController () {
235 //* NOISY-DEBUG: */ System.out.println("AdminHelper::copyUserToController - CALLED!"); //NOI18N
237 // Validate user instance
238 if (this.getUser() == null) {
240 throw new NullPointerException("this.user is null");
241 } else if (this.getUser().getUserId() == null) {
243 throw new NullPointerException("this.user.userId is null");
244 } else if (this.getUser().getUserId() < 1) {
246 throw new IllegalStateException(MessageFormat.format("this.user.userId={0} is not valid.", this.getUser().getUserId()));
250 Contact userContact = this.getUser().getUserContact();
252 // Set contact here, too. This avoids parameters that cannot auto-complete in IDEs.
253 this.setContact(userContact);
255 // Set all phone instances
256 this.setPhoneInstances(userContact);
258 // Set all fields: user
259 this.userController.setUserName(this.getUser().getUserName());
263 public Contact getContact () {
268 public void setContact (final Contact contact) {
269 this.contact = contact;
273 public String getContactUsageMessageKey (final Contact contact) {
274 // The contact must be valid
275 if (null == contact) {
277 throw new NullPointerException("contact is null"); //NOI18N
278 } else if (contact.getContactId() == null) {
280 throw new NullPointerException("contact.contactId is null"); //NOI18N
281 } else if (contact.getContactId() < 1) {
283 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
286 // Default key is "unused"
287 String messageKey = "CONTACT_IS_UNUSED"; //NOI18N
290 boolean isUserContact = this.userController.isContactFound(contact);
295 messageKey = "CONTACT_IS_USER"; //NOI18N
298 // Return message key
303 public DialableFaxNumber getFaxNumber () {
304 return this.faxNumber;
308 public void setFaxNumber (final DialableFaxNumber faxNumber) {
309 this.faxNumber = faxNumber;
313 public DialableLandLineNumber getLandLineNumber () {
314 return this.landLineNumber;
318 public void setLandLineNumber (final DialableLandLineNumber landLineNumber) {
319 this.landLineNumber = landLineNumber;
323 public DialableMobileNumber getMobileNumber () {
324 return this.mobileNumber;
328 public void setMobileNumber (final DialableMobileNumber mobileNumber) {
329 this.mobileNumber = mobileNumber;
333 public User getUser () {
338 public void setUser (final User user) {
343 * Set's all given contact's phone instances: land-line, mobile and
346 * @param contact Contact to set phone instances for
348 private void setPhoneInstances (final Contact contact) {
349 // The contact must be valid
350 if (null == contact) {
352 throw new NullPointerException("contact is null"); //NOI18N
353 } else if (contact.getContactId() == null) {
355 throw new NullPointerException("contact.contactId is null"); //NOI18N
356 } else if (contact.getContactId() < 1) {
358 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
362 if (contact.getContactMobileNumber() instanceof DialableMobileNumber) {
363 // Yes, then set it in admin controller
364 this.setMobileNumber(contact.getContactMobileNumber());
368 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
369 // Yes, then set it in admin controller
370 this.setLandLineNumber(contact.getContactLandLineNumber());
374 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
375 // Yes, then set it in admin controller
376 this.setFaxNumber(contact.getContactFaxNumber());