2 * Copyright (C) 2016 Roland Häder
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.faces.view.ViewScoped;
21 import javax.inject.Inject;
22 import javax.inject.Named;
23 import org.mxchange.jcontacts.contact.Contact;
24 import org.mxchange.jjobs.beans.BaseJobsController;
25 import org.mxchange.jjobs.beans.contact.JobsAdminContactWebRequestController;
26 import org.mxchange.jjobs.beans.phone.JobsAdminPhoneWebRequestController;
27 import org.mxchange.jjobs.beans.user.JobsAdminUserWebRequestController;
28 import org.mxchange.jjobs.beans.user.JobsUserWebSessionController;
29 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
30 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
31 import org.mxchange.jphone.phonenumbers.mobile.DialableMobileNumber;
32 import org.mxchange.jusercore.model.user.User;
35 * A general helper for beans
37 * @author Roland Häder<roland@mxchange.org>
39 @Named ("adminHelper")
41 public class JobsWebViewHelperBean extends BaseJobsController implements JobsWebViewHelperController {
44 * Call-stack instance (5 may show BeanELResolver.getValue as caller)
46 private static final short THREAD_STACK = 5;
51 private static final long serialVersionUID = 17_258_793_567_145_701L;
54 * Administrative contact controller
57 private JobsAdminContactWebRequestController adminContactController;
60 * Administrative phone controller
63 private JobsAdminPhoneWebRequestController adminPhoneController;
66 * Administrative user controller
69 private JobsAdminUserWebRequestController adminUserController;
74 private Contact contact;
79 private DialableFaxNumber faxNumber;
84 private DialableLandLineNumber landLineNumber;
89 private DialableMobileNumber mobileNumber;
97 * Regular user controller
100 private JobsUserWebSessionController userController;
103 * Default constructor
105 public JobsWebViewHelperBean () {
106 // String caller = MessageFormat.format("{0}.{1}", Thread.currentThread().getStackTrace()[3].getClassName(), Thread.currentThread().getStackTrace()[3].getMethodName());
107 // System.out.println(MessageFormat.format("{0}: Constructed, caller: {1}", this.getClass().getSimpleName(), caller));
111 public void copyContactToController () {
112 // String caller = MessageFormat.format("{0}.{1}", Thread.currentThread().getStackTrace()[THREAD_STACK].getClassName(), Thread.currentThread().getStackTrace()[THREAD_STACK].getMethodName());
113 // System.out.println(MessageFormat.format("{0}.copyContactToController: CALLED, caller: {2}", this.getClass().getSimpleName(), this.contact, caller));
115 // Validate contact instance
116 if (this.getContact() == null) {
118 throw new NullPointerException("this.contact is null"); //NOI18N
119 } else if (this.getContact().getContactId() == null) {
121 throw new NullPointerException("this.contact.contactId is null"); //NOI18N
122 } else if (this.getContact().getContactId() < 1) {
124 throw new IllegalStateException(MessageFormat.format("this.contact.contactId={0} is not valid.", this.getContact().getContactId())); //NOI18N
127 // Set all phone instances
128 this.setPhoneInstances(this.getContact());
130 // Set all fields: user
131 this.adminContactController.copyContactToController(this.getContact());
135 public void copyFaxNumberToController () {
136 // Validate fax instance
137 if (this.getFaxNumber() == null) {
139 throw new NullPointerException("this.faxNumber is null");
140 } else if (this.getFaxNumber().getPhoneId() == null) {
142 throw new NullPointerException("this.faxNumber.phoneId is null");
143 } else if (this.getFaxNumber().getPhoneId() < 1) {
145 throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneId={0} is not valid", this.getFaxNumber().getPhoneId()));
146 } else if (this.getFaxNumber().getPhoneAreaCode() == null) {
148 throw new NullPointerException("this.faxNumber.phoneAreaCode is null");
149 } else if (this.getFaxNumber().getPhoneAreaCode() < 1) {
151 throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneAreaCode={0} is not valid", this.getFaxNumber().getPhoneAreaCode()));
152 } else if (this.getFaxNumber().getPhoneCountry() == null) {
154 throw new NullPointerException("this.faxNumber.phoneCountry is null");
155 } else if (this.getFaxNumber().getPhoneCountry().getCountryId() == null) {
157 throw new NullPointerException("this.faxNumber.phoneCountry.countryId is null");
158 } else if (this.getFaxNumber().getPhoneCountry().getCountryId() < 1) {
160 throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneCountry.countryId={0} is invalid", this.getFaxNumber().getPhoneCountry().getCountryId()));
161 } else if (this.getFaxNumber().getPhoneNumber() == null) {
162 // Throw NPE again ...
163 throw new NullPointerException("this.faxNumber.phoneNumber is null");
164 } else if (this.getFaxNumber().getPhoneNumber() < 1) {
166 throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneNumber={0} is not valid", this.getFaxNumber().getPhoneNumber()));
169 // Copy all (changeable) data fields to admin controller
170 this.adminPhoneController.setPhoneAreaCode(this.getFaxNumber().getPhoneAreaCode());
171 this.adminPhoneController.setPhoneCountry(this.getFaxNumber().getPhoneCountry());
172 this.adminPhoneController.setPhoneNumber(this.getFaxNumber().getPhoneNumber());
176 public void copyLandLineNumberToController () {
177 // Validate land-line instance
178 if (this.getLandLineNumber() == null) {
180 throw new NullPointerException("this.landLineNumber is null");
181 } else if (this.getLandLineNumber().getPhoneId() == null) {
183 throw new NullPointerException("this.landLineNumber.phoneId is null");
184 } else if (this.getLandLineNumber().getPhoneId() < 1) {
186 throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneId={0} is not valid", this.getLandLineNumber().getPhoneId()));
187 } else if (this.getLandLineNumber().getPhoneAreaCode() == null) {
189 throw new NullPointerException("this.landLineNumber.phoneAreaCode is null");
190 } else if (this.getLandLineNumber().getPhoneAreaCode() < 1) {
192 throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneAreaCode={0} is not valid", this.getFaxNumber().getPhoneAreaCode()));
193 } else if (this.getLandLineNumber().getPhoneCountry() == null) {
195 throw new NullPointerException("this.landLineNumber.phoneCountry is null");
196 } else if (this.getLandLineNumber().getPhoneCountry().getCountryId() == null) {
198 throw new NullPointerException("this.landLineNumber.phoneCountry.countryId is null");
199 } else if (this.getLandLineNumber().getPhoneCountry().getCountryId() < 1) {
201 throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneCountry.countryId={0} is invalid", this.getLandLineNumber().getPhoneCountry().getCountryId()));
202 } else if (this.getLandLineNumber().getPhoneNumber() == null) {
203 // Throw NPE again ...
204 throw new NullPointerException("this.landLineNumber.phoneNumber is null");
205 } else if (this.getLandLineNumber().getPhoneNumber() < 1) {
207 throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneNumber={0} is not valid", this.getLandLineNumber().getPhoneNumber()));
210 // Copy all (changeable) data fields to admin controller
211 this.adminPhoneController.setPhoneAreaCode(this.getLandLineNumber().getPhoneAreaCode());
212 this.adminPhoneController.setPhoneCountry(this.getLandLineNumber().getPhoneCountry());
213 this.adminPhoneController.setPhoneNumber(this.getLandLineNumber().getPhoneNumber());
217 public void copyMobileNumberToController () {
218 // Validate mobile instance
219 if (this.getMobileNumber() == null) {
221 throw new NullPointerException("this.mobileNumber is null");
222 } else if (this.getMobileNumber().getPhoneId() == null) {
224 throw new NullPointerException("this.mobileNumber.phoneId is null");
225 } else if (this.getMobileNumber().getPhoneId() < 1) {
227 throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber.phoneId={0} is not valid", this.getMobileNumber().getPhoneId()));
228 } else if (this.getMobileNumber().getMobileProvider() == null) {
230 throw new NullPointerException("this.mobileNumber.mobileProvider is null");
231 } else if (this.getMobileNumber().getMobileProvider().getProviderId() == null) {
233 throw new NullPointerException("this.mobileNumber.mobileProvider.providerId is null");
234 } else if (this.getMobileNumber().getMobileProvider().getProviderId() < 1) {
236 throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber.mobileProvider.providerId={0} is invalid", this.getMobileNumber().getMobileProvider().getProviderId()));
237 } else if (this.getMobileNumber().getPhoneNumber() == null) {
238 // Throw NPE again ...
239 throw new NullPointerException("this.mobileNumber.phoneNumber is null");
240 } else if (this.getMobileNumber().getPhoneNumber() < 1) {
242 throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber.phoneNumber={0} is not valid", this.getMobileNumber().getPhoneNumber()));
245 // Copy all (changeable) data fields to admin controller
246 this.adminPhoneController.setMobileProvider(this.getMobileNumber().getMobileProvider());
247 this.adminPhoneController.setPhoneNumber(this.getMobileNumber().getPhoneNumber());
251 public void copyUserToController () {
253 //* NOISY-DEBUG: */ System.out.println("AdminHelper::copyUserToController - CALLED!"); //NOI18N
255 // Validate user instance
256 if (this.getUser() == null) {
258 throw new NullPointerException("this.user is null");
259 } else if (this.getUser().getUserId() == null) {
261 throw new NullPointerException("this.user.userId is null");
262 } else if (this.getUser().getUserId() < 1) {
264 throw new IllegalStateException(MessageFormat.format("this.user.userId={0} is not valid.", this.getUser().getUserId()));
268 Contact userContact = this.getUser().getUserContact();
270 // Set contact here, too. This avoids parameters that cannot auto-complete in IDEs.
271 this.setContact(userContact);
273 // Set all phone instances
274 this.setPhoneInstances(userContact);
276 // Set all fields: user
277 this.userController.setUserName(this.getUser().getUserName());
278 this.adminUserController.setUserName(this.getUser().getUserName());
282 public Contact getContact () {
283 // String caller = MessageFormat.format("{0}.{1}", Thread.currentThread().getStackTrace()[THREAD_STACK].getClassName(), Thread.currentThread().getStackTrace()[THREAD_STACK].getMethodName());
284 // System.out.println(MessageFormat.format("{0}: Returning this.contact={1}, caller: {2}", this.getClass().getSimpleName(), this.contact, caller));
289 public void setContact (final Contact contact) {
290 // String caller = MessageFormat.format("{0}.{1}", Thread.currentThread().getStackTrace()[THREAD_STACK].getClassName(), Thread.currentThread().getStackTrace()[THREAD_STACK].getMethodName());
291 // System.out.println(MessageFormat.format("{0}: Setting contact={1}, previous: {2}, caller: {3}", this.getClass().getSimpleName(), contact, this.contact, caller));
292 this.contact = contact;
296 public String getContactUsageMessageKey (final Contact contact) {
297 // The contact must be valid
298 if (null == contact) {
300 throw new NullPointerException("contact is null"); //NOI18N
301 } else if (contact.getContactId() == null) {
303 throw new NullPointerException("contact.contactId is null"); //NOI18N
304 } else if (contact.getContactId() < 1) {
306 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
309 // Default key is "unused"
310 String messageKey = "CONTACT_IS_UNUSED"; //NOI18N
313 boolean isUserContact = this.userController.isContactFound(contact);
318 messageKey = "CONTACT_IS_USER"; //NOI18N
321 // Return message key
326 public DialableFaxNumber getFaxNumber () {
327 return this.faxNumber;
331 public void setFaxNumber (final DialableFaxNumber faxNumber) {
332 this.faxNumber = faxNumber;
336 public DialableLandLineNumber getLandLineNumber () {
337 return this.landLineNumber;
341 public void setLandLineNumber (final DialableLandLineNumber landLineNumber) {
342 this.landLineNumber = landLineNumber;
346 public DialableMobileNumber getMobileNumber () {
347 return this.mobileNumber;
351 public void setMobileNumber (final DialableMobileNumber mobileNumber) {
352 this.mobileNumber = mobileNumber;
356 public User getUser () {
361 public void setUser (final User user) {
366 * Set's all given contact's phone instances: land-line, mobile and
369 * @param contact Contact to set phone instances for
371 private void setPhoneInstances (final Contact contact) {
372 // The contact must be valid
373 if (null == contact) {
375 throw new NullPointerException("contact is null"); //NOI18N
376 } else if (contact.getContactId() == null) {
378 throw new NullPointerException("contact.contactId is null"); //NOI18N
379 } else if (contact.getContactId() < 1) {
381 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
385 if (contact.getContactMobileNumber() instanceof DialableMobileNumber) {
386 // Yes, then set it in admin controller
387 this.setMobileNumber(contact.getContactMobileNumber());
391 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
392 // Yes, then set it in admin controller
393 this.setLandLineNumber(contact.getContactLandLineNumber());
397 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
398 // Yes, then set it in admin controller
399 this.setFaxNumber(contact.getContactFaxNumber());