2 * Copyright (C) 2016 Roland Häder GmbH
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.addressbook.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.addressbook.beans.contact.AddressbookAdminContactWebRequestController;
24 import org.mxchange.addressbook.beans.phone.AddressbookAdminPhoneWebRequestController;
25 import org.mxchange.addressbook.beans.user.AddressbookAdminUserWebRequestController;
26 import org.mxchange.addressbook.beans.user.AddressbookUserWebSessionController;
27 import org.mxchange.jcontacts.contact.Contact;
28 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
29 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
30 import org.mxchange.jphone.phonenumbers.mobile.DialableMobileNumber;
31 import org.mxchange.jusercore.model.user.User;
34 * A general helper for beans
36 * @author Roland Häder<roland@mxchange.org>
38 @Named ("adminHelper")
40 public class AddressbookWebRequestHelperBean implements AddressbookWebRequestHelperController {
43 * Call-stack instance (5 may show BeanELResolver.getValue as caller)
45 private static final short THREAD_STACK = 5;
50 private static final long serialVersionUID = 17_258_793_567_145_701L;
53 * Administrative contact controller
56 private AddressbookAdminContactWebRequestController adminContactController;
59 * Administrative phone controller
62 private AddressbookAdminPhoneWebRequestController adminPhoneController;
65 * Administrative user controller
68 private AddressbookAdminUserWebRequestController adminUserController;
73 private Contact contact;
78 private DialableFaxNumber faxNumber;
83 private DialableLandLineNumber landLineNumber;
88 private DialableMobileNumber mobileNumber;
96 * Regular user controller
99 private AddressbookUserWebSessionController userController;
102 * Default constructor
104 public AddressbookWebRequestHelperBean () {
105 // Call super constructor
108 // String caller = MessageFormat.format("{0}.{1}", Thread.currentThread().getStackTrace()[3].getClassName(), Thread.currentThread().getStackTrace()[3].getMethodName());
109 // System.out.println(MessageFormat.format("{0}: Constructed, caller: {1}", this.getClass().getSimpleName(), caller));
113 public void copyContactToController () {
114 // String caller = MessageFormat.format("{0}.{1}", Thread.currentThread().getStackTrace()[THREAD_STACK].getClassName(), Thread.currentThread().getStackTrace()[THREAD_STACK].getMethodName());
115 // System.out.println(MessageFormat.format("{0}.copyContactToController: CALLED, caller: {2}", this.getClass().getSimpleName(), this.contact, caller));
117 // Validate contact instance
118 if (this.getContact() == null) {
120 throw new NullPointerException("this.contact is null"); //NOI18N
121 } else if (this.getContact().getContactId() == null) {
123 throw new NullPointerException("this.contact.contactId is null"); //NOI18N
124 } else if (this.getContact().getContactId() < 1) {
126 throw new IllegalStateException(MessageFormat.format("this.contact.contactId={0} is not valid.", this.getContact().getContactId())); //NOI18N
129 // Set all phone instances
130 this.setPhoneInstances(this.getContact());
132 // Set all fields: user
133 this.adminContactController.copyContactToController(this.getContact());
137 public void copyFaxNumberToController () {
138 // Validate fax instance
139 if (this.getFaxNumber() == null) {
141 throw new NullPointerException("this.faxNumber is null");
142 } else if (this.getFaxNumber().getPhoneId() == null) {
144 throw new NullPointerException("this.faxNumber.phoneId is null");
145 } else if (this.getFaxNumber().getPhoneId() < 1) {
147 throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneId={0} is not valid", this.getFaxNumber().getPhoneId()));
148 } else if (this.getFaxNumber().getPhoneAreaCode() == null) {
150 throw new NullPointerException("this.faxNumber.phoneAreaCode is null");
151 } else if (this.getFaxNumber().getPhoneAreaCode() < 1) {
153 throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneAreaCode={0} is not valid", this.getFaxNumber().getPhoneAreaCode()));
154 } else if (this.getFaxNumber().getPhoneCountry() == null) {
156 throw new NullPointerException("this.faxNumber.phoneCountry is null");
157 } else if (this.getFaxNumber().getPhoneCountry().getCountryId() == null) {
159 throw new NullPointerException("this.faxNumber.phoneCountry.countryId is null");
160 } else if (this.getFaxNumber().getPhoneCountry().getCountryId() < 1) {
162 throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneCountry.countryId={0} is invalid", this.getFaxNumber().getPhoneCountry().getCountryId()));
163 } else if (this.getFaxNumber().getPhoneNumber() == null) {
164 // Throw NPE again ...
165 throw new NullPointerException("this.faxNumber.phoneNumber is null");
166 } else if (this.getFaxNumber().getPhoneNumber() < 1) {
168 throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneNumber={0} is not valid", this.getFaxNumber().getPhoneNumber()));
171 // Copy all (changeable) data fields to admin controller
172 this.adminPhoneController.setPhoneAreaCode(this.getFaxNumber().getPhoneAreaCode());
173 this.adminPhoneController.setPhoneCountry(this.getFaxNumber().getPhoneCountry());
174 this.adminPhoneController.setPhoneNumber(this.getFaxNumber().getPhoneNumber());
178 public void copyLandLineNumberToController () {
179 // Validate land-line instance
180 if (this.getLandLineNumber() == null) {
182 throw new NullPointerException("this.landLineNumber is null");
183 } else if (this.getLandLineNumber().getPhoneId() == null) {
185 throw new NullPointerException("this.landLineNumber.phoneId is null");
186 } else if (this.getLandLineNumber().getPhoneId() < 1) {
188 throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneId={0} is not valid", this.getLandLineNumber().getPhoneId()));
189 } else if (this.getLandLineNumber().getPhoneAreaCode() == null) {
191 throw new NullPointerException("this.landLineNumber.phoneAreaCode is null");
192 } else if (this.getLandLineNumber().getPhoneAreaCode() < 1) {
194 throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneAreaCode={0} is not valid", this.getFaxNumber().getPhoneAreaCode()));
195 } else if (this.getLandLineNumber().getPhoneCountry() == null) {
197 throw new NullPointerException("this.landLineNumber.phoneCountry is null");
198 } else if (this.getLandLineNumber().getPhoneCountry().getCountryId() == null) {
200 throw new NullPointerException("this.landLineNumber.phoneCountry.countryId is null");
201 } else if (this.getLandLineNumber().getPhoneCountry().getCountryId() < 1) {
203 throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneCountry.countryId={0} is invalid", this.getLandLineNumber().getPhoneCountry().getCountryId()));
204 } else if (this.getLandLineNumber().getPhoneNumber() == null) {
205 // Throw NPE again ...
206 throw new NullPointerException("this.landLineNumber.phoneNumber is null");
207 } else if (this.getLandLineNumber().getPhoneNumber() < 1) {
209 throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneNumber={0} is not valid", this.getLandLineNumber().getPhoneNumber()));
212 // Copy all (changeable) data fields to admin controller
213 this.adminPhoneController.setPhoneAreaCode(this.getLandLineNumber().getPhoneAreaCode());
214 this.adminPhoneController.setPhoneCountry(this.getLandLineNumber().getPhoneCountry());
215 this.adminPhoneController.setPhoneNumber(this.getLandLineNumber().getPhoneNumber());
219 public void copyMobileNumberToController () {
220 // Validate mobile instance
221 if (this.getMobileNumber() == null) {
223 throw new NullPointerException("this.mobileNumber is null");
224 } else if (this.getMobileNumber().getPhoneId() == null) {
226 throw new NullPointerException("this.mobileNumber.phoneId is null");
227 } else if (this.getMobileNumber().getPhoneId() < 1) {
229 throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber.phoneId={0} is not valid", this.getMobileNumber().getPhoneId()));
230 } else if (this.getMobileNumber().getMobileProvider() == null) {
232 throw new NullPointerException("this.mobileNumber.mobileProvider is null");
233 } else if (this.getMobileNumber().getMobileProvider().getProviderId() == null) {
235 throw new NullPointerException("this.mobileNumber.mobileProvider.providerId is null");
236 } else if (this.getMobileNumber().getMobileProvider().getProviderId() < 1) {
238 throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber.mobileProvider.providerId={0} is invalid", this.getMobileNumber().getMobileProvider().getProviderId()));
239 } else if (this.getMobileNumber().getPhoneNumber() == null) {
240 // Throw NPE again ...
241 throw new NullPointerException("this.mobileNumber.phoneNumber is null");
242 } else if (this.getMobileNumber().getPhoneNumber() < 1) {
244 throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber.phoneNumber={0} is not valid", this.getMobileNumber().getPhoneNumber()));
247 // Copy all (changeable) data fields to admin controller
248 this.adminPhoneController.setMobileProvider(this.getMobileNumber().getMobileProvider());
249 this.adminPhoneController.setPhoneNumber(this.getMobileNumber().getPhoneNumber());
253 public void copyUserToController () {
255 //* NOISY-DEBUG: */ System.out.println("AdminHelper::copyUserToController - CALLED!"); //NOI18N
257 // Validate user instance
258 if (this.getUser() == null) {
260 throw new NullPointerException("this.user is null"); //NOI18N
261 } else if (this.getUser().getUserId() == null) {
263 throw new NullPointerException("this.user.userId is null"); //NOI18N
264 } else if (this.getUser().getUserId() < 1) {
266 throw new IllegalStateException(MessageFormat.format("this.user.userId={0} is not valid.", this.getUser().getUserId())); //NOI18N
270 Contact userContact = this.getUser().getUserContact();
272 // Set contact here, too. This avoids parameters that cannot auto-complete in IDEs.
273 this.setContact(userContact);
275 // Set all phone instances
276 this.setPhoneInstances(userContact);
278 // Set all fields: user
279 this.userController.setUserName(this.getUser().getUserName());
280 this.adminUserController.setUserName(this.getUser().getUserName());
284 public Contact getContact () {
285 // String caller = MessageFormat.format("{0}.{1}", Thread.currentThread().getStackTrace()[THREAD_STACK].getClassName(), Thread.currentThread().getStackTrace()[THREAD_STACK].getMethodName());
286 // System.out.println(MessageFormat.format("{0}: Returning this.contact={1}, caller: {2}", this.getClass().getSimpleName(), this.contact, caller));
291 public void setContact (final Contact contact) {
292 // String caller = MessageFormat.format("{0}.{1}", Thread.currentThread().getStackTrace()[THREAD_STACK].getClassName(), Thread.currentThread().getStackTrace()[THREAD_STACK].getMethodName());
293 // System.out.println(MessageFormat.format("{0}: Setting contact={1}, previous: {2}, caller: {3}", this.getClass().getSimpleName(), contact, this.contact, caller));
294 this.contact = contact;
298 public String getContactUsageMessageKey (final Contact contact) {
299 // The contact must be valid
300 if (null == contact) {
302 throw new NullPointerException("contact is null"); //NOI18N
303 } else if (contact.getContactId() == null) {
305 throw new NullPointerException("contact.contactId is null"); //NOI18N
306 } else if (contact.getContactId() < 1) {
308 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
311 // Default key is "unused"
312 String messageKey = "CONTACT_IS_UNUSED"; //NOI18N
314 // Check user/recruiter
315 boolean isUserContact = this.userController.isContactFound(contact);
320 messageKey = "CONTACT_IS_USER"; //NOI18N
323 // Return message key
328 public DialableFaxNumber getFaxNumber () {
329 return this.faxNumber;
333 public void setFaxNumber (final DialableFaxNumber faxNumber) {
334 this.faxNumber = faxNumber;
338 public DialableLandLineNumber getLandLineNumber () {
339 return this.landLineNumber;
343 public void setLandLineNumber (final DialableLandLineNumber landLineNumber) {
344 this.landLineNumber = landLineNumber;
348 public DialableMobileNumber getMobileNumber () {
349 return this.mobileNumber;
353 public void setMobileNumber (final DialableMobileNumber mobileNumber) {
354 this.mobileNumber = mobileNumber;
358 public User getUser () {
363 public void setUser (final User user) {
368 * Set's all given contact's phone instances: land-line, mobile and
371 * @param contact Contact to set phone instances for
373 private void setPhoneInstances (final Contact contact) {
374 // The contact must be valid
375 if (null == contact) {
377 throw new NullPointerException("contact is null"); //NOI18N
378 } else if (contact.getContactId() == null) {
380 throw new NullPointerException("contact.contactId is null"); //NOI18N
381 } else if (contact.getContactId() < 1) {
383 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
387 if (contact.getContactMobileNumber() instanceof DialableMobileNumber) {
388 // Yes, then set it in admin controller
389 this.setMobileNumber(contact.getContactMobileNumber());
393 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
394 // Yes, then set it in admin controller
395 this.setLandLineNumber(contact.getContactLandLineNumber());
399 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
400 // Yes, then set it in admin controller
401 this.setFaxNumber(contact.getContactFaxNumber());