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.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.JobsUserWebSessionController;
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 Haeder<roland@mxchange.org>
38 @Named ("adminHelper")
40 public class JobsWebViewHelperBean extends BaseJobsController implements JobsWebViewHelperController {
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 JobsAdminContactWebRequestController adminContactController;
59 * Administrative phone controller
62 private JobsAdminPhoneWebRequestController adminPhoneController;
67 private Contact contact;
72 private DialableFaxNumber faxNumber;
77 private DialableLandLineNumber landLineNumber;
82 private DialableMobileNumber mobileNumber;
90 * Regular user controller
93 private JobsUserWebSessionController userController;
98 public JobsWebViewHelperBean () {
99 // String caller = MessageFormat.format("{0}.{1}", Thread.currentThread().getStackTrace()[3].getClassName(), Thread.currentThread().getStackTrace()[3].getMethodName());
100 // System.out.println(MessageFormat.format("{0}: Constructed, caller: {1}", this.getClass().getSimpleName(), caller));
104 public void copyContactToController () {
105 // String caller = MessageFormat.format("{0}.{1}", Thread.currentThread().getStackTrace()[THREAD_STACK].getClassName(), Thread.currentThread().getStackTrace()[THREAD_STACK].getMethodName());
106 // System.out.println(MessageFormat.format("{0}.copyContactToController: CALLED, caller: {2}", this.getClass().getSimpleName(), this.contact, caller));
108 // Validate contact instance
109 if (this.getContact() == null) {
111 throw new NullPointerException("this.contact is null"); //NOI18N
112 } else if (this.getContact().getContactId() == null) {
114 throw new NullPointerException("this.contact.contactId is null"); //NOI18N
115 } else if (this.getContact().getContactId() < 1) {
117 throw new IllegalStateException(MessageFormat.format("this.contact.contactId={0} is not valid.", this.getContact().getContactId())); //NOI18N
120 // Set all phone instances
121 this.setPhoneInstances(this.getContact());
123 // Set all fields: user
124 this.adminContactController.copyContactToController(this.getContact());
128 public void copyFaxNumberToController () {
129 // Validate fax instance
130 if (this.getFaxNumber() == null) {
132 throw new NullPointerException("this.faxNumber is null");
133 } else if (this.getFaxNumber().getPhoneId() == null) {
135 throw new NullPointerException("this.faxNumber.phoneId is null");
136 } else if (this.getFaxNumber().getPhoneId() < 1) {
138 throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneId={0} is not valid", this.getFaxNumber().getPhoneId()));
139 } else if (this.getFaxNumber().getPhoneAreaCode() == null) {
141 throw new NullPointerException("this.faxNumber.phoneAreaCode is null");
142 } else if (this.getFaxNumber().getPhoneAreaCode() < 1) {
144 throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneAreaCode={0} is not valid", this.getFaxNumber().getPhoneAreaCode()));
145 } else if (this.getFaxNumber().getPhoneCountry() == null) {
147 throw new NullPointerException("this.faxNumber.phoneCountry is null");
148 } else if (this.getFaxNumber().getPhoneCountry().getCountryId() == null) {
150 throw new NullPointerException("this.faxNumber.phoneCountry.countryId is null");
151 } else if (this.getFaxNumber().getPhoneCountry().getCountryId() < 1) {
153 throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneCountry.countryId={0} is invalid", this.getFaxNumber().getPhoneCountry().getCountryId()));
154 } else if (this.getFaxNumber().getPhoneNumber() == null) {
155 // Throw NPE again ...
156 throw new NullPointerException("this.faxNumber.phoneNumber is null");
157 } else if (this.getFaxNumber().getPhoneNumber() < 1) {
159 throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneNumber={0} is not valid", this.getFaxNumber().getPhoneNumber()));
162 // Copy all (changeable) data fields to admin controller
163 this.adminPhoneController.setPhoneAreaCode(this.getFaxNumber().getPhoneAreaCode());
164 this.adminPhoneController.setPhoneCountry(this.getFaxNumber().getPhoneCountry());
165 this.adminPhoneController.setPhoneNumber(this.getFaxNumber().getPhoneNumber());
169 public void copyLandLineNumberToController () {
170 // Validate land-line instance
171 if (this.getLandLineNumber() == null) {
173 throw new NullPointerException("this.landLineNumber is null");
174 } else if (this.getLandLineNumber().getPhoneId() == null) {
176 throw new NullPointerException("this.landLineNumber.phoneId is null");
177 } else if (this.getLandLineNumber().getPhoneId() < 1) {
179 throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneId={0} is not valid", this.getLandLineNumber().getPhoneId()));
180 } else if (this.getLandLineNumber().getPhoneAreaCode() == null) {
182 throw new NullPointerException("this.landLineNumber.phoneAreaCode is null");
183 } else if (this.getLandLineNumber().getPhoneAreaCode() < 1) {
185 throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneAreaCode={0} is not valid", this.getFaxNumber().getPhoneAreaCode()));
186 } else if (this.getLandLineNumber().getPhoneCountry() == null) {
188 throw new NullPointerException("this.landLineNumber.phoneCountry is null");
189 } else if (this.getLandLineNumber().getPhoneCountry().getCountryId() == null) {
191 throw new NullPointerException("this.landLineNumber.phoneCountry.countryId is null");
192 } else if (this.getLandLineNumber().getPhoneCountry().getCountryId() < 1) {
194 throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneCountry.countryId={0} is invalid", this.getLandLineNumber().getPhoneCountry().getCountryId()));
195 } else if (this.getLandLineNumber().getPhoneNumber() == null) {
196 // Throw NPE again ...
197 throw new NullPointerException("this.landLineNumber.phoneNumber is null");
198 } else if (this.getLandLineNumber().getPhoneNumber() < 1) {
200 throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneNumber={0} is not valid", this.getLandLineNumber().getPhoneNumber()));
203 // Copy all (changeable) data fields to admin controller
204 this.adminPhoneController.setPhoneAreaCode(this.getLandLineNumber().getPhoneAreaCode());
205 this.adminPhoneController.setPhoneCountry(this.getLandLineNumber().getPhoneCountry());
206 this.adminPhoneController.setPhoneNumber(this.getLandLineNumber().getPhoneNumber());
210 public void copyMobileNumberToController () {
211 // Validate mobile instance
212 if (this.getMobileNumber() == null) {
214 throw new NullPointerException("this.mobileNumber is null");
215 } else if (this.getMobileNumber().getPhoneId() == null) {
217 throw new NullPointerException("this.mobileNumber.phoneId is null");
218 } else if (this.getMobileNumber().getPhoneId() < 1) {
220 throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber.phoneId={0} is not valid", this.getMobileNumber().getPhoneId()));
221 } else if (this.getMobileNumber().getMobileProvider() == null) {
223 throw new NullPointerException("this.mobileNumber.mobileProvider is null");
224 } else if (this.getMobileNumber().getMobileProvider().getProviderId() == null) {
226 throw new NullPointerException("this.mobileNumber.mobileProvider.providerId is null");
227 } else if (this.getMobileNumber().getMobileProvider().getProviderId() < 1) {
229 throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber.mobileProvider.providerId={0} is invalid", this.getMobileNumber().getMobileProvider().getProviderId()));
230 } else if (this.getMobileNumber().getPhoneNumber() == null) {
231 // Throw NPE again ...
232 throw new NullPointerException("this.mobileNumber.phoneNumber is null");
233 } else if (this.getMobileNumber().getPhoneNumber() < 1) {
235 throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber.phoneNumber={0} is not valid", this.getMobileNumber().getPhoneNumber()));
238 // Copy all (changeable) data fields to admin controller
239 this.adminPhoneController.setMobileProvider(this.getMobileNumber().getMobileProvider());
240 this.adminPhoneController.setPhoneNumber(this.getMobileNumber().getPhoneNumber());
244 public void copyUserToController () {
246 //* NOISY-DEBUG: */ System.out.println("AdminHelper::copyUserToController - CALLED!"); //NOI18N
248 // Validate user instance
249 if (this.getUser() == null) {
251 throw new NullPointerException("this.user is null");
252 } else if (this.getUser().getUserId() == null) {
254 throw new NullPointerException("this.user.userId is null");
255 } else if (this.getUser().getUserId() < 1) {
257 throw new IllegalStateException(MessageFormat.format("this.user.userId={0} is not valid.", this.getUser().getUserId()));
261 Contact userContact = this.getUser().getUserContact();
263 // Set contact here, too. This avoids parameters that cannot auto-complete in IDEs.
264 this.setContact(userContact);
266 // Set all phone instances
267 this.setPhoneInstances(userContact);
269 // Set all fields: user
270 this.userController.setUserName(this.getUser().getUserName());
274 public Contact getContact () {
275 // String caller = MessageFormat.format("{0}.{1}", Thread.currentThread().getStackTrace()[THREAD_STACK].getClassName(), Thread.currentThread().getStackTrace()[THREAD_STACK].getMethodName());
276 // System.out.println(MessageFormat.format("{0}: Returning this.contact={1}, caller: {2}", this.getClass().getSimpleName(), this.contact, caller));
281 public void setContact (final Contact contact) {
282 // String caller = MessageFormat.format("{0}.{1}", Thread.currentThread().getStackTrace()[THREAD_STACK].getClassName(), Thread.currentThread().getStackTrace()[THREAD_STACK].getMethodName());
283 // System.out.println(MessageFormat.format("{0}: Setting contact={1}, previous: {2}, caller: {3}", this.getClass().getSimpleName(), contact, this.contact, caller));
284 this.contact = contact;
288 public String getContactUsageMessageKey (final Contact contact) {
289 // The contact must be valid
290 if (null == contact) {
292 throw new NullPointerException("contact is null"); //NOI18N
293 } else if (contact.getContactId() == null) {
295 throw new NullPointerException("contact.contactId is null"); //NOI18N
296 } else if (contact.getContactId() < 1) {
298 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
301 // Default key is "unused"
302 String messageKey = "CONTACT_IS_UNUSED"; //NOI18N
305 boolean isUserContact = this.userController.isContactFound(contact);
310 messageKey = "CONTACT_IS_USER"; //NOI18N
313 // Return message key
318 public DialableFaxNumber getFaxNumber () {
319 return this.faxNumber;
323 public void setFaxNumber (final DialableFaxNumber faxNumber) {
324 this.faxNumber = faxNumber;
328 public DialableLandLineNumber getLandLineNumber () {
329 return this.landLineNumber;
333 public void setLandLineNumber (final DialableLandLineNumber landLineNumber) {
334 this.landLineNumber = landLineNumber;
338 public DialableMobileNumber getMobileNumber () {
339 return this.mobileNumber;
343 public void setMobileNumber (final DialableMobileNumber mobileNumber) {
344 this.mobileNumber = mobileNumber;
348 public User getUser () {
353 public void setUser (final User user) {
358 * Set's all given contact's phone instances: land-line, mobile and
361 * @param contact Contact to set phone instances for
363 private void setPhoneInstances (final Contact contact) {
364 // The contact must be valid
365 if (null == contact) {
367 throw new NullPointerException("contact is null"); //NOI18N
368 } else if (contact.getContactId() == null) {
370 throw new NullPointerException("contact.contactId is null"); //NOI18N
371 } else if (contact.getContactId() < 1) {
373 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
377 if (contact.getContactMobileNumber() instanceof DialableMobileNumber) {
378 // Yes, then set it in admin controller
379 this.setMobileNumber(contact.getContactMobileNumber());
383 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
384 // Yes, then set it in admin controller
385 this.setLandLineNumber(contact.getContactLandLineNumber());
389 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
390 // Yes, then set it in admin controller
391 this.setFaxNumber(contact.getContactFaxNumber());