]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/contact/JobsContactWebSessionBean.java
Please cherry-pick:
[jjobs-war.git] / src / java / org / mxchange / jjobs / beans / contact / JobsContactWebSessionBean.java
1 /*
2  * Copyright (C) 2016, 2017 Roland Häder
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.contact;
18
19 import java.text.MessageFormat;
20 import java.util.Collections;
21 import java.util.Date;
22 import java.util.Iterator;
23 import java.util.LinkedList;
24 import java.util.List;
25 import java.util.Objects;
26 import javax.annotation.PostConstruct;
27 import javax.enterprise.context.SessionScoped;
28 import javax.enterprise.event.Observes;
29 import javax.faces.view.facelets.FaceletException;
30 import javax.inject.Inject;
31 import javax.inject.Named;
32 import javax.naming.Context;
33 import javax.naming.InitialContext;
34 import javax.naming.NamingException;
35 import org.mxchange.jcontacts.contact.Contact;
36 import org.mxchange.jcontacts.contact.ContactSessionBeanRemote;
37 import org.mxchange.jcontacts.contact.ContactUtils;
38 import org.mxchange.jcontacts.contact.UserContact;
39 import org.mxchange.jcontacts.contact.title.PersonalTitle;
40 import org.mxchange.jcontacts.events.contact.add.ObservableAdminAddedContactEvent;
41 import org.mxchange.jcontacts.events.contact.update.ObservableAdminUpdatedContactEvent;
42 import org.mxchange.jcontacts.exceptions.ContactNotFoundException;
43 import org.mxchange.jcountry.data.Country;
44 import org.mxchange.jjobs.beans.BaseJobsController;
45 import org.mxchange.jjobs.beans.user.JobsUserWebSessionController;
46 import org.mxchange.jjobs.beans.user.login.JobsUserLoginWebSessionController;
47 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
48 import org.mxchange.jphone.phonenumbers.fax.FaxNumber;
49 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
50 import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
51 import org.mxchange.jphone.phonenumbers.mobile.DialableMobileNumber;
52 import org.mxchange.jphone.phonenumbers.mobile.MobileNumber;
53 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
54 import org.mxchange.jusercore.events.user.add.ObservableAdminAddedUserEvent;
55 import org.mxchange.jusercore.events.user.linked.ObservableAdminLinkedUserEvent;
56 import org.mxchange.jusercore.model.user.User;
57 import org.mxchange.juserlogincore.events.confirmation.ObservableUserConfirmedAccountEvent;
58 import org.mxchange.juserlogincore.events.login.ObservableUserLoggedInEvent;
59 import org.mxchange.juserlogincore.events.registration.ObservableUserRegisteredEvent;
60 import org.mxchange.juserlogincore.exceptions.UserPasswordMismatchException;
61
62 /**
63  * A general contact controller (bean)
64  * <p>
65  * @author Roland Häder<roland@mxchange.org>
66  */
67 @Named ("contactController")
68 @SessionScoped
69 public class JobsContactWebSessionBean extends BaseJobsController implements JobsContactWebSessionController {
70
71         /**
72          * Serial number
73          */
74         private static final long serialVersionUID = 542_145_347_916L;
75
76         /**
77          * Academic academicTitle
78          */
79         private String academicTitle;
80
81         /**
82          * Birth day
83          */
84         private Date birthday;
85
86         /**
87          * City
88          */
89         private String city;
90
91         /**
92          * Optional comments
93          */
94         private String comment;
95
96         /**
97          * Remote contact bean
98          */
99         private ContactSessionBeanRemote contactBean;
100
101         /**
102          * Contact list
103          */
104         private final List<Contact> contactList;
105
106         /**
107          * Country instance
108          */
109         private Country country;
110
111         /**
112          * Email address
113          */
114         private String emailAddress;
115
116         /**
117          * Email address list
118          */
119         private final List<String> emailAddressList;
120
121         /**
122          * Email address repeated
123          */
124         private String emailAddressRepeat;
125
126         /**
127          * Family name
128          */
129         private String familyName;
130
131         /**
132          * Fax number's area code
133          */
134         private Integer faxAreaCode;
135
136         /**
137          * Country instance for fax number
138          */
139         private Country faxCountry;
140
141         /**
142          * Fax number
143          */
144         private Long faxNumber;
145
146         /**
147          * First name
148          */
149         private String firstName;
150
151         /**
152          * House number
153          */
154         private Short houseNumber;
155
156         /**
157          * House number extension
158          */
159         private String houseNumberExtension;
160
161         /**
162          * Whether a fax entry has been unlinked
163          */
164         private boolean isFaxUnlinked;
165
166         /**
167          * Whether a land-line number has been unlinked
168          */
169         private boolean isLandLineUnlinked;
170
171         /**
172          * Whether a mobile entry has been unlinked
173          */
174         private boolean isMobileUnlinked;
175
176         /**
177          * Phone number area code
178          */
179         private Integer landLineAreaCode;
180
181         /**
182          * Country instance for phone number
183          */
184         private Country landLineCountry;
185
186         /**
187          * Phone number
188          */
189         private Long landLineNumber;
190
191         /**
192          * Mobile number
193          */
194         private Long mobileNumber;
195
196         /**
197          * Mobile number's carrier
198          */
199         private MobileProvider mobileProvider;
200
201         /**
202          * Personal academicTitle
203          */
204         private PersonalTitle personalTitle;
205
206         /**
207          * A list of all selectable contacts
208          */
209         private List<Contact> selectableContacts;
210
211         /**
212          * Street
213          */
214         private String street;
215
216         /**
217          * Regular user controller
218          */
219         @Inject
220         private JobsUserWebSessionController userController;
221
222         /**
223          * Login bean (controller)
224          */
225         @Inject
226         private JobsUserLoginWebSessionController userLoginController;
227
228         /**
229          * ZIP code
230          */
231         private Integer zipCode;
232
233         /**
234          * Default constructor
235          */
236         public JobsContactWebSessionBean () {
237                 // Call super constructor
238                 super();
239
240                 // Init lists/maps
241                 this.contactList = new LinkedList<>();
242                 this.emailAddressList = new LinkedList<>();
243         }
244
245         /**
246          * Observes events being fired when an administrator has added a new
247          * contact.
248          * <p>
249          * @param event Event being fired
250          */
251         public void afterAdminAddedContactEvent (@Observes final ObservableAdminAddedContactEvent event) {
252                 // The event must be valid
253                 if (null == event) {
254                         // Throw NPE
255                         throw new NullPointerException("event is null"); //NOI18N
256                 } else if (event.getAddedContact() == null) {
257                         // Throw again ...
258                         throw new NullPointerException("event.addedContact is null"); //NOI18N
259                 } else if (event.getAddedContact().getContactId() == null) {
260                         // ... and again
261                         throw new NullPointerException("event.addedContact.contactId is null"); //NOI18N
262                 } else if (event.getAddedContact().getContactId() < 1) {
263                         // Not valid
264                         throw new IllegalArgumentException(MessageFormat.format("event.addedContact.contactId={0} is not valid", event.getAddedContact().getContactId())); //NOI18N //NOI18N
265                 }
266
267                 // Clear this bean
268                 this.clear();
269
270                 // Call other method
271                 this.uniqueAddContact(event.getAddedContact());
272
273                 // Add to selectable contacts
274                 this.selectableContacts.add(event.getAddedContact());
275         }
276
277         /**
278          * Event observer for newly added users by administrator
279          * <p>
280          * @param event Event being fired
281          */
282         public void afterAdminAddedUserEvent (@Observes final ObservableAdminAddedUserEvent event) {
283                 // Trace message
284                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("contactController.afterAdminAddedUserEvent(): event={0} - CALLED!", event)); //NOI18N
285
286                 // event should not be null
287                 if (null == event) {
288                         // Throw NPE
289                         throw new NullPointerException("event is null"); //NOI18N
290                 } else if (event.getAddedUser() == null) {
291                         // Throw NPE again
292                         throw new NullPointerException("event.addedUser is null"); //NOI18N
293                 } else if (event.getAddedUser().getUserId() == null) {
294                         // userId is null
295                         throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
296                 } else if (event.getAddedUser().getUserId() < 1) {
297                         // Not avalid id
298                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
299                 }
300
301                 // Clear all data
302                 this.clear();
303         }
304
305         /**
306          * Observes events being fired when an administrator has linked a new user
307          * with existing contact data.
308          * <p>
309          * @param event Event being fired
310          */
311         public void afterAdminLinkedUserEvent (@Observes final ObservableAdminLinkedUserEvent event) {
312                 // event should not be null
313                 if (null == event) {
314                         // Throw NPE
315                         throw new NullPointerException("event is null"); //NOI18N
316                 } else if (event.getLinkedUser() == null) {
317                         // Throw NPE again
318                         throw new NullPointerException("event.linkedUser is null"); //NOI18N
319                 } else if (event.getLinkedUser().getUserContact() == null) {
320                         // Throw NPE again
321                         throw new NullPointerException("event.linkedUser.userContact is null"); //NOI18N
322                 } else if (event.getLinkedUser().getUserContact().getContactId() == null) {
323                         // userId is null
324                         throw new NullPointerException("event.linkedUser.userContact.contactId is null"); //NOI18N
325                 } else if (event.getLinkedUser().getUserContact().getContactId() < 1) {
326                         // Not avalid id
327                         throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getLinkedUser(), event.getLinkedUser().getUserContact().getContactId())); //NOI18N
328                 }
329
330                 // Remove contact from list available contacts list
331                 this.selectableContacts.remove(event.getLinkedUser().getUserContact());
332
333                 // Clear all data
334                 this.clear();
335         }
336
337         /**
338          * Event observer for updated contact data by administrators
339          * <p>
340          * @param event Updated contact data event
341          */
342         public void afterAdminUpdatedContactDataEvent (@Observes final ObservableAdminUpdatedContactEvent event) {
343                 // event should not be null
344                 if (null == event) {
345                         // Throw NPE
346                         throw new NullPointerException("event is null"); //NOI18N
347                 } else if (event.getUpdatedContact() == null) {
348                         // Throw NPE again
349                         throw new NullPointerException("event.updatedContact is null"); //NOI18N
350                 } else if (event.getUpdatedContact().getContactId() == null) {
351                         // userId is null
352                         throw new NullPointerException("event.updatedContact.contactId is null"); //NOI18N
353                 } else if (event.getUpdatedContact().getContactId() < 1) {
354                         // Not avalid id
355                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
356                 }
357
358                 // Add contact instance only once
359                 this.uniqueAddContact(event.getUpdatedContact());
360
361                 // Add email address to list
362                 this.emailAddressList.add(event.getUpdatedContact().getContactEmailAddress());
363
364                 // Trace message
365                 //* NOISY-DEBUG: */ System.out.println("contactController.afterAdminUpdatedContactDataEvent(): EXIT!"); //NOI18N
366         }
367
368         /**
369          * Event observer when user confirmed account.
370          * <p>
371          * @param event Event being fired
372          */
373         public void afterUserConfirmedAccount (@Observes final ObservableUserConfirmedAccountEvent event) {
374                 // event should not be null
375                 if (null == event) {
376                         // Throw NPE
377                         throw new NullPointerException("event is null"); //NOI18N
378                 } else if (event.getConfirmedUser() == null) {
379                         // Throw NPE again
380                         throw new NullPointerException("event.confirmedUser is null"); //NOI18N
381                 } else if (event.getConfirmedUser().getUserId() == null) {
382                         // userId is null
383                         throw new NullPointerException("event.confirmedUser.userId is null"); //NOI18N
384                 } else if (event.getConfirmedUser().getUserId() < 1) {
385                         // Not avalid id
386                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getConfirmedUser(), event.getConfirmedUser().getUserId())); //NOI18N
387                 }
388
389                 // Add contact instance only once
390                 this.uniqueAddContact(event.getConfirmedUser().getUserContact());
391         }
392
393         /**
394          * Event observer for logged-in user
395          * <p>
396          * @param event Event instance
397          */
398         public void afterUserLoginEvent (@Observes final ObservableUserLoggedInEvent event) {
399                 // event should not be null
400                 if (null == event) {
401                         // Throw NPE
402                         throw new NullPointerException("event is null"); //NOI18N
403                 } else if (event.getLoggedInUser() == null) {
404                         // Throw NPE again
405                         throw new NullPointerException("event.loggedInUser is null"); //NOI18N
406                 } else if (event.getLoggedInUser().getUserId() == null) {
407                         // userId is null
408                         throw new NullPointerException("event.loggedInUser.userId is null"); //NOI18N
409                 } else if (event.getLoggedInUser().getUserId() < 1) {
410                         // Not avalid id
411                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
412                 }
413
414                 // Copy all data to this bean
415                 this.copyContact(event.getLoggedInUser().getUserContact());
416         }
417
418         /**
419          * Event observer for new user registrations
420          * <p>
421          * @param event User registration event
422          */
423         public void afterUserRegistrationEvent (@Observes final ObservableUserRegisteredEvent event) {
424                 // event should not be null
425                 if (null == event) {
426                         // Throw NPE
427                         throw new NullPointerException("event is null"); //NOI18N
428                 } else if (event.getRegisteredUser() == null) {
429                         // Throw NPE again
430                         throw new NullPointerException("event.registeredUser is null"); //NOI18N
431                 } else if (event.getRegisteredUser().getUserId() == null) {
432                         // userId is null
433                         throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
434                 } else if (event.getRegisteredUser().getUserId() < 1) {
435                         // Not avalid id
436                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
437                 }
438
439                 // Get user instance
440                 Contact registeredContact = event.getRegisteredUser().getUserContact();
441
442                 // Copy all data from registered->user
443                 this.copyContact(registeredContact);
444
445                 // Add contact instance only once
446                 this.uniqueAddContact(registeredContact);
447
448                 // Add user name and email address
449                 this.addUserNameEmailAddress(registeredContact);
450
451                 // Clear all data
452                 this.clear();
453         }
454
455         @Override
456         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
457         public List<Contact> allContacts () {
458                 // Return un-modified list
459                 return this.contactList;
460         }
461
462         @Override
463         public void clearEmailAddresses () {
464                 // Clear both
465                 this.setEmailAddress(null);
466                 this.setEmailAddressRepeat(null);
467         }
468
469         @Override
470         public Contact createContactInstance () {
471                 // Is all required data set?
472                 if (!this.isRequiredPersonalDataSet()) {
473                         // No, then abort here
474                         throw new FaceletException(new IllegalArgumentException("Not all personal data is set, but createContactInstance() is called.")); //NOI18N
475                 }
476
477                 // Required personal data must be set
478                 assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N
479
480                 // Generate phone number
481                 DialableLandLineNumber phone = new LandLineNumber(this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber());
482                 DialableMobileNumber mobile = new MobileNumber(this.getMobileProvider(), this.getMobileNumber());
483                 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
484
485                 // Create new contact
486                 Contact contact = new UserContact(this.getPersonalTitle(), this.getFirstName(), this.getFamilyName());
487                 contact.setContactStreet(this.getStreet());
488                 contact.setContactHouseNumber(this.getHouseNumber());
489                 contact.setContactHouseNumberExtension(this.getHouseNumberExtension());
490                 contact.setContactZipCode(this.getZipCode());
491                 contact.setContactCity(this.getCity());
492                 contact.setContactCountry(this.getCountry());
493                 contact.setContactEmailAddress(this.getEmailAddress());
494                 contact.setContactBirthday(this.getBirthday());
495                 contact.setContactComment(this.getComment());
496
497                 // Don't set null or wrong references
498                 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getLandLineAreaCode() != null) && (this.getLandLineNumber() != null) && (this.getLandLineAreaCode() > 0) && (this.getLandLineNumber() > 0)) {
499                         // Now the number must be given
500                         if (phone.getPhoneAreaCode() == null) {
501                                 // Is null
502                                 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
503                         } else if (phone.getPhoneAreaCode() < 1) {
504                                 // Abort here
505                                 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
506                         } else if (phone.getPhoneNumber() == null) {
507                                 // Is null
508                                 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
509                         } else if (phone.getPhoneNumber() < 1) {
510                                 // Abort here
511                                 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
512                         }
513
514                         // Set phone number
515                         contact.setContactLandLineNumber(phone);
516                 }
517
518                 // Don't set null or wrong references
519                 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
520                         // Now the number must be given
521                         if (fax.getPhoneAreaCode() == null) {
522                                 // Is null
523                                 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
524                         } else if (fax.getPhoneAreaCode() < 1) {
525                                 // Abort here
526                                 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
527                         } else if (fax.getPhoneNumber() == null) {
528                                 // Is null
529                                 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
530                         } else if (fax.getPhoneNumber() < 1) {
531                                 // Abort here
532                                 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
533                         }
534
535                         // Set fax number
536                         contact.setContactFaxNumber(fax);
537                 }
538
539                 // Is the provider set?
540                 if ((mobile instanceof DialableMobileNumber) && (this.getMobileProvider() instanceof MobileProvider) && (this.getMobileNumber() != null) && (this.getMobileNumber() > 0)) {
541                         // Is the number set?
542                         if (mobile.getPhoneNumber() == null) {
543                                 // Is null
544                                 throw new NullPointerException("mobile.phoneNumber is null"); //NOI18N
545                         } else if (mobile.getPhoneNumber() < 1) {
546                                 // Abort here
547                                 throw new IllegalArgumentException("mobile.phoneNumber is zero or below."); //NOI18N
548                         }
549
550                         // Set mobile number
551                         contact.setContactMobileNumber(mobile);
552                 }
553
554                 // Return it
555                 return contact;
556         }
557
558         @Override
559         public String doChangePersonalContactData () {
560                 // This method shall only be called if the user is logged-in
561                 if (!this.userLoginController.isUserLoggedIn()) {
562                         // Not logged-in
563                         throw new IllegalStateException("User is not logged-in"); //NOI18N
564                 } else if (!this.isRequiredChangePersonalDataSet()) {
565                         // Not all required fields are set
566                         throw new FaceletException("Not all required fields are set."); //NOI18N
567                 } else if (!this.userLoginController.ifCurrentPasswordMatches()) {
568                         // Password not matching
569                         this.showFacesMessage("form_login_change_personal:currentPassword", new UserPasswordMismatchException(this.userLoginController.getLoggedInUser())); //NOI18N
570                         return ""; //NOI18N
571                 }
572
573                 // Get contact instance
574                 Contact contact = this.userLoginController.getLoggedInUser().getUserContact();
575
576                 // It should be there, so run some tests on it
577                 assert (contact instanceof Contact) : "Instance userLoginController.loggedInUser.userContact is null"; //NOI18N
578                 assert (contact.getContactId() instanceof Long) : "Instance userLoginController.userContact.contactId is null"; //NOI18N
579                 assert (contact.getContactId() > 0) : MessageFormat.format("Instance userLoginController.userContact.contactId={0} is invalid", contact.getContactId()); //NOI18N
580
581                 // Update all fields
582                 contact.setContactPersonalTitle(this.getPersonalTitle());
583                 contact.setContactFirstName(this.getFirstName());
584                 contact.setContactFamilyName(this.getFamilyName());
585                 contact.setContactStreet(this.getStreet());
586                 contact.setContactHouseNumber(this.getHouseNumber());
587                 contact.setContactHouseNumberExtension(this.getHouseNumberExtension());
588                 contact.setContactZipCode(this.getZipCode());
589                 contact.setContactCity(this.getCity());
590                 contact.setContactCountry(this.getCountry());
591
592                 // Update contact's mobile number
593                 this.isMobileUnlinked = ContactUtils.updateMobileNumber(contact, this.getMobileProvider(), this.getMobileNumber());
594
595                 // Update contact's land-line number
596                 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber());
597
598                 // Update contact's fax number
599                 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
600
601                 // Send it to the EJB
602                 this.contactBean.updateContactData(contact, this.isMobileUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
603
604                 // All fine
605                 return "contact_data_saved"; //NOI18N
606         }
607
608         /**
609          * Getter for academic title
610          * <p>
611          * @return Academic title
612          */
613         public String getAcademicTitle () {
614                 return this.academicTitle;
615         }
616
617         /**
618          * Setter for academic title
619          * <p>
620          * @param academicTitle Academic title
621          */
622         public void setAcademicTitle (final String academicTitle) {
623                 this.academicTitle = academicTitle;
624         }
625
626         /**
627          * Getter for birth day
628          * <p>
629          * @return Birth day
630          */
631         @SuppressWarnings ("ReturnOfDateField")
632         public Date getBirthday () {
633                 return this.birthday;
634         }
635
636         /**
637          * Setter for birth day
638          * <p>
639          * @param birthday Birth day
640          */
641         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
642         public void setBirthday (final Date birthday) {
643                 this.birthday = birthday;
644         }
645
646         /**
647          * Getter for city name
648          * <p>
649          * @return City name
650          */
651         public String getCity () {
652                 return this.city;
653         }
654
655         /**
656          * Setter for city name
657          * <p>
658          * @param city City name
659          */
660         public void setCity (final String city) {
661                 this.city = city;
662         }
663
664         /**
665          * Getter for comments
666          * <p>
667          * @return Comments
668          */
669         public String getComment () {
670                 return this.comment;
671         }
672
673         /**
674          * Setter for comment
675          * <p>
676          * @param comment Comments
677          */
678         public void setComment (final String comment) {
679                 this.comment = comment;
680         }
681
682         @Override
683         public String getControllerType () {
684                 return "general"; //NOI18N
685         }
686
687         @Override
688         @Deprecated
689         public void setControllerType (final String controllerType) {
690                 throw new UnsupportedOperationException("Setting controller type is not supported."); //NOI18N
691         }
692
693         /**
694          * Getter for country instance
695          * <p>
696          * @return Country instance
697          */
698         public Country getCountry () {
699                 return this.country;
700         }
701
702         /**
703          * Setter for country instance
704          * <p>
705          * @param country Country instance
706          */
707         public void setCountry (final Country country) {
708                 this.country = country;
709         }
710
711         @Override
712         public String getEmailAddress () {
713                 return this.emailAddress;
714         }
715
716         /**
717          * Setter for email address
718          * <p>
719          * @param emailAddress Email address
720          */
721         public void setEmailAddress (final String emailAddress) {
722                 this.emailAddress = emailAddress;
723         }
724
725         /**
726          * Getter for email address, repeated
727          * <p>
728          * @return the emailAddress, repeated
729          */
730         public String getEmailAddressRepeat () {
731                 return this.emailAddressRepeat;
732         }
733
734         /**
735          * Setter for email address repeated
736          * <p>
737          * @param emailAddressRepeat the emailAddress to set
738          */
739         public void setEmailAddressRepeat (final String emailAddressRepeat) {
740                 this.emailAddressRepeat = emailAddressRepeat;
741         }
742
743         /**
744          * Family name
745          * <p>
746          * @return the familyName
747          */
748         public String getFamilyName () {
749                 return this.familyName;
750         }
751
752         /**
753          * Family name
754          * <p>
755          * @param familyName the familyName to set
756          */
757         public void setFamilyName (final String familyName) {
758                 this.familyName = familyName;
759         }
760
761         /**
762          * Getter for fax number's area code
763          * <p>
764          * @return Fax number's area code
765          */
766         public Integer getFaxAreaCode () {
767                 return this.faxAreaCode;
768         }
769
770         /**
771          * Setter for fax number's area code
772          * <p>
773          * @param faxAreaCode Fax number's area code
774          */
775         public void setFaxAreaCode (final Integer faxAreaCode) {
776                 this.faxAreaCode = faxAreaCode;
777         }
778
779         /**
780          * Getter for fax's country instance
781          * <p>
782          * @return Fax' country instance
783          */
784         public Country getFaxCountry () {
785                 return this.faxCountry;
786         }
787
788         /**
789          * Setter for fax's country instance
790          * <p>
791          * @param faxCountry Fax' country instance
792          */
793         public void setFaxCountry (final Country faxCountry) {
794                 this.faxCountry = faxCountry;
795         }
796
797         /**
798          * Getter for fax number
799          * <p>
800          * @return Fax number
801          */
802         public Long getFaxNumber () {
803                 return this.faxNumber;
804         }
805
806         /**
807          * Setter for fax number
808          * <p>
809          * @param faxNumber Fax number
810          */
811         public void setFaxNumber (final Long faxNumber) {
812                 this.faxNumber = faxNumber;
813         }
814
815         /**
816          * First name
817          * <p>
818          * @return the first name
819          */
820         public String getFirstName () {
821                 return this.firstName;
822         }
823
824         /**
825          * First name
826          * <p>
827          * @param firstName the first name to set
828          */
829         public void setFirstName (final String firstName) {
830                 this.firstName = firstName;
831         }
832
833         /**
834          * House number
835          * <p>
836          * @return the houseNumber
837          */
838         public Short getHouseNumber () {
839                 return this.houseNumber;
840         }
841
842         /**
843          * House number
844          * <p>
845          * @param houseNumber the houseNumber to set
846          */
847         public void setHouseNumber (final Short houseNumber) {
848                 this.houseNumber = houseNumber;
849         }
850
851         /**
852          * Getter for house number extension, example: 123a 'a' is then the
853          * extension and 123 is the house number.
854          * <p>
855          * @return House number extension
856          */
857         public String getHouseNumberExtension () {
858                 return this.houseNumberExtension;
859         }
860
861         /**
862          * Setter for house number extension
863          * <p>
864          * @param houseNumberExtension House number extension
865          */
866         public void setHouseNumberExtension (final String houseNumberExtension) {
867                 this.houseNumberExtension = houseNumberExtension;
868         }
869
870         /**
871          * Getter for land-line number's area code
872          * <p>
873          * @return Land-line number's area code
874          */
875         public Integer getLandLineAreaCode () {
876                 return this.landLineAreaCode;
877         }
878
879         /**
880          * Setter for land-line number's area code
881          * <p>
882          * @param landLineAreaCode Land-line number's area code
883          */
884         public void setLandLineAreaCode (final Integer landLineAreaCode) {
885                 this.landLineAreaCode = landLineAreaCode;
886         }
887
888         /**
889          * Getter for land-line number's country instance
890          * <p>
891          * @return Land-line number's country instance
892          */
893         public Country getLandLineCountry () {
894                 return this.landLineCountry;
895         }
896
897         /**
898          * Setter for land-line number's country instance
899          * <p>
900          * @param landLineCountry Land-line number's country instance
901          */
902         public void setLandLineCountry (final Country landLineCountry) {
903                 this.landLineCountry = landLineCountry;
904         }
905
906         /**
907          * Getter for land-line number
908          * <p>
909          * @return Land-line number
910          */
911         public Long getLandLineNumber () {
912                 return this.landLineNumber;
913         }
914
915         /**
916          * Setter for land-line number
917          * <p>
918          * @param landLineNumber Land-line number
919          */
920         public void setLandLineNumber (final Long landLineNumber) {
921                 this.landLineNumber = landLineNumber;
922         }
923
924         /**
925          * Getter for mobile number
926          * <p>
927          * @return Mobile number
928          */
929         public Long getMobileNumber () {
930                 return this.mobileNumber;
931         }
932
933         /**
934          * Setter for mobile number
935          * <p>
936          * @param mobileNumber Mobile number
937          */
938         public void setMobileNumber (final Long mobileNumber) {
939                 this.mobileNumber = mobileNumber;
940         }
941
942         /**
943          * Getter for mobile number's carrier
944          * <p>
945          * @return Mobile number's carrier
946          */
947         public MobileProvider getMobileProvider () {
948                 return this.mobileProvider;
949         }
950
951         /**
952          * Setter for mobile number's provider
953          * <p>
954          * @param mobileProvider Mobile number's provider
955          */
956         public void setMobileProvider (final MobileProvider mobileProvider) {
957                 this.mobileProvider = mobileProvider;
958         }
959
960         /**
961          * Getter for personal title
962          * <p>
963          * @return Personal title
964          */
965         public PersonalTitle getPersonalTitle () {
966                 return this.personalTitle;
967         }
968
969         /**
970          * Setter for personal title
971          * <p>
972          * @param personalTitle Personal title
973          */
974         public void setPersonalTitle (final PersonalTitle personalTitle) {
975                 this.personalTitle = personalTitle;
976         }
977
978         /**
979          * Getter for street
980          * <p>
981          * @return Street
982          */
983         public String getStreet () {
984                 return this.street;
985         }
986
987         /**
988          * Setter for street
989          * <p>
990          * @param street Street
991          */
992         public void setStreet (final String street) {
993                 this.street = street;
994         }
995
996         /**
997          * Getter for ZIP code
998          * <p>
999          * @return ZIP code
1000          */
1001         public Integer getZipCode () {
1002                 return this.zipCode;
1003         }
1004
1005         /**
1006          * Setter for ZIP code
1007          * <p>
1008          * @param zipCode ZIP code
1009          */
1010         public void setZipCode (final Integer zipCode) {
1011                 this.zipCode = zipCode;
1012         }
1013
1014         /**
1015          * Post-construction method
1016          */
1017         @PostConstruct
1018         public void init () {
1019                 // Try it
1020                 try {
1021                         // Get initial context
1022                         Context context = new InitialContext();
1023
1024                         // Try to lookup
1025                         this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/jjobs-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
1026                 } catch (final NamingException e) {
1027                         // Throw again
1028                         throw new FaceletException(e);
1029                 }
1030
1031                 // Get full email address list for reducing EJB calls
1032                 this.emailAddressList.addAll(this.contactBean.getEmailAddressList());
1033
1034                 // Get all contacts
1035                 List<Contact> allContacts = this.contactBean.getAllContacts();
1036
1037                 // Get full contact list
1038                 this.contactList.addAll(allContacts);
1039
1040                 // Get all users
1041                 List<User> allUsers = this.userController.allUsers();
1042
1043                 // Get iterator
1044                 Iterator<Contact> iterator = allContacts.iterator();
1045
1046                 // Loop through it
1047                 while (iterator.hasNext()) {
1048                         // Get next element
1049                         Contact next = iterator.next();
1050
1051                         // Get iterator
1052                         Iterator<User> userIterator = allUsers.iterator();
1053
1054                         // Loop through all users
1055                         while (userIterator.hasNext()) {
1056                                 // Get user instance
1057                                 User nextUser = userIterator.next();
1058
1059                                 // Is contact same?
1060                                 if (Objects.equals(next, nextUser.getUserContact())) {
1061                                         // Found same
1062                                         iterator.remove();
1063                                         break;
1064                                 }
1065                         }
1066                 }
1067
1068                 // Set contact list
1069                 this.selectableContacts = allContacts;
1070         }
1071
1072         @Override
1073         public boolean isEmailAddressRegistered (final Contact contact) {
1074                 // Cherck parameter
1075                 if (null == contact) {
1076                         // Throw NPE
1077                         throw new NullPointerException("contact is null"); //NOI18N
1078                 } else if (contact.getContactEmailAddress() == null) {
1079                         // Throw again
1080                         throw new NullPointerException("contact.contactEmailAddress is null"); //NOI18N
1081                 } else if (contact.getContactEmailAddress().isEmpty()) {
1082                         // Is empty
1083                         throw new IllegalArgumentException("contact.contactEmailAddress is empty."); //NOI18N
1084                 }
1085
1086                 // Determine it
1087                 return ((this.emailAddressList instanceof List) && (this.emailAddressList.contains(contact.getContactEmailAddress())));
1088         }
1089
1090         @Override
1091         public boolean isRequiredChangePersonalDataSet () {
1092                 return ((this.getPersonalTitle() != null) &&
1093                                 (this.getFirstName() != null) &&
1094                                 (this.getFamilyName() != null) &&
1095                                 (this.getStreet() != null) &&
1096                                 (this.getHouseNumber() != null) &&
1097                                 (this.getZipCode() != null) &&
1098                                 (this.getCity() != null));
1099         }
1100
1101         @Override
1102         public boolean isRequiredPersonalDataSet () {
1103                 return ((this.getPersonalTitle() != null) &&
1104                                 (this.getFirstName() != null) &&
1105                                 (this.getFamilyName() != null) &&
1106                                 (this.getStreet() != null) &&
1107                                 (this.getHouseNumber() != null) &&
1108                                 (this.getZipCode() != null) &&
1109                                 (this.getCity() != null) &&
1110                                 (this.getEmailAddress() != null) &&
1111                                 (this.getEmailAddressRepeat() != null));
1112         }
1113
1114         @Override
1115         public boolean isSameEmailAddressEntered () {
1116                 return (Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat()));
1117         }
1118
1119         @Override
1120         public Contact lookupContactById (final Long contactId) throws ContactNotFoundException {
1121                 // Init variable
1122                 Contact localContact = null;
1123
1124                 // Clear this bean
1125                 this.clear();
1126
1127                 // Try to lookup it in visible user list
1128                 for (final Iterator<Contact> iterator = this.contactList.iterator(); iterator.hasNext();) {
1129                         // Get next user
1130                         Contact next = iterator.next();
1131
1132                         // Is the user id found?
1133                         if (Objects.equals(next.getContactId(), contactId)) {
1134                                 // Copy to other variable
1135                                 localContact = next;
1136                                 break;
1137                         }
1138                 }
1139
1140                 // Is it still null?
1141                 if (null == localContact) {
1142                         // Not visible for the current user
1143                         throw new ContactNotFoundException(contactId);
1144                 }
1145
1146                 // Copy all data to this bean
1147                 this.copyContact(localContact);
1148
1149                 // Return it
1150                 return localContact;
1151         }
1152
1153         @Override
1154         public List<Contact> selectableContacts () {
1155                 return Collections.unmodifiableList(this.selectableContacts);
1156         }
1157
1158         @Override
1159         public void updateContactDataFromController (final Contact contact) {
1160                 // Is the instance valid?
1161                 if (null == contact) {
1162                         // Throw NPE
1163                         throw new NullPointerException("contact is null"); //NOI18N
1164                 } else if (contact.getContactId() == null) {
1165                         // Throw NPE
1166                         throw new NullPointerException("contact.contactId is null"); //NOI18N
1167                 } else if (contact.getContactId() < 1) {
1168                         // Not valid id number
1169                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1170                 }
1171
1172                 // Set all
1173                 this.copyContact(contact);
1174         }
1175
1176         /**
1177          * Adds email address to bean's internal list.
1178          * <p>
1179          * @param contact Contact instance
1180          */
1181         private void addUserNameEmailAddress (final Contact contact) {
1182                 // Make sure the entry is not added yet
1183                 if (this.emailAddressList.contains(contact.getContactEmailAddress())) {
1184                         // Already added
1185                         throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", contact.getContactEmailAddress())); //NOI18N
1186                 }
1187
1188                 // Add email addres
1189                 this.emailAddressList.add(contact.getContactEmailAddress());
1190         }
1191
1192         /**
1193          * Clears this bean
1194          */
1195         private void clear () {
1196                 // Clear all data
1197                 // - personal data
1198                 this.setPersonalTitle(null);
1199                 this.setAcademicTitle(null);
1200                 this.setFirstName(null);
1201                 this.setFamilyName(null);
1202                 this.setStreet(null);
1203                 this.setHouseNumber(null);
1204                 this.setHouseNumberExtension(null);
1205                 this.setZipCode(null);
1206                 this.setCity(null);
1207                 this.setCountry(null);
1208
1209                 // - contact data
1210                 this.clearEmailAddresses();
1211                 this.setLandLineAreaCode(null);
1212                 this.setLandLineCountry(null);
1213                 this.setLandLineNumber(null);
1214                 this.setMobileProvider(null);
1215                 this.setMobileNumber(null);
1216                 this.setFaxAreaCode(null);
1217                 this.setFaxCountry(null);
1218                 this.setFaxNumber(null);
1219
1220                 // - other data
1221                 this.setBirthday(null);
1222                 this.setComment(null);
1223         }
1224
1225         /**
1226          * Copies given contact into the controller
1227          * <p>
1228          * @param contact Contact instance
1229          */
1230         private void copyContact (final Contact contact) {
1231                 // Is the instance valid?
1232                 if (null == contact) {
1233                         // Throw NPE
1234                         throw new NullPointerException("contact is null"); //NOI18N
1235                 } else if (contact.getContactId() == null) {
1236                         // Throw NPE
1237                         throw new NullPointerException("contact.contactId is null"); //NOI18N
1238                 } else if (contact.getContactId() < 1) {
1239                         // Not valid id number
1240                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1241                 }
1242
1243                 // Copy all fields:
1244                 // - base data
1245                 this.setPersonalTitle(contact.getContactPersonalTitle());
1246                 this.setAcademicTitle(contact.getContactTitle());
1247                 this.setFirstName(contact.getContactFirstName());
1248                 this.setFamilyName(contact.getContactFamilyName());
1249                 this.setStreet(contact.getContactStreet());
1250                 this.setHouseNumber(contact.getContactHouseNumber());
1251                 this.setHouseNumberExtension(contact.getContactHouseNumberExtension());
1252                 this.setZipCode(contact.getContactZipCode());
1253                 this.setCity(contact.getContactCity());
1254                 this.setCountry(contact.getContactCountry());
1255                 this.setEmailAddress(contact.getContactEmailAddress());
1256                 this.setBirthday(contact.getContactBirthday());
1257                 this.setComment(contact.getContactComment());
1258
1259                 // Get mobile, phone and fax instance
1260                 DialableMobileNumber mobile = contact.getContactMobileNumber();
1261                 DialableFaxNumber fax = contact.getContactFaxNumber();
1262                 DialableLandLineNumber phone = contact.getContactLandLineNumber();
1263
1264                 // - contact data
1265                 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneAreaCode() > 0)) {
1266                         this.setLandLineCountry(phone.getPhoneCountry());
1267                         this.setLandLineAreaCode(phone.getPhoneAreaCode());
1268                         this.setLandLineNumber(phone.getPhoneNumber());
1269                 }
1270
1271                 if ((mobile instanceof DialableMobileNumber) && (mobile.getMobileProvider() instanceof MobileProvider)) {
1272                         this.setMobileProvider(mobile.getMobileProvider());
1273                         this.setMobileNumber(mobile.getPhoneNumber());
1274                 }
1275
1276                 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneAreaCode() > 0)) {
1277                         this.setFaxCountry(fax.getPhoneCountry());
1278                         this.setFaxAreaCode(fax.getPhoneAreaCode());
1279                         this.setFaxNumber(fax.getPhoneNumber());
1280                 }
1281         }
1282
1283         /**
1284          * Removes given contact from all lists
1285          * <p>
1286          * @param contact Contact instance to remove
1287          */
1288         private void removeContact (final Contact contact) {
1289                 // Is the instance valid?
1290                 if (null == contact) {
1291                         // Throw NPE
1292                         throw new NullPointerException("contact is null"); //NOI18N
1293                 } else if (contact.getContactId() == null) {
1294                         // Throw NPE
1295                         throw new NullPointerException("contact.contactId is null"); //NOI18N
1296                 } else if (contact.getContactId() < 1) {
1297                         // Not valid id number
1298                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1299                 }
1300
1301                 // Remove from general list
1302                 if (!this.contactList.remove(contact)) {
1303                         // Did not remove contact
1304                         throw new IllegalStateException(MessageFormat.format("contact {0} was not removed.", contact.getContactId())); //NOI18N
1305                 }
1306
1307                 // Remove from other lists
1308                 this.emailAddressList.remove(contact.getContactEmailAddress());
1309         }
1310
1311         /**
1312          * Adds unique instance to contact list. First any existing instance is
1313          * being removed, then the new instance is added.
1314          * <p>
1315          * @param contact Contact instance to add uniquely
1316          */
1317         private void uniqueAddContact (final Contact contact) {
1318                 // Is the instance valid?
1319                 if (null == contact) {
1320                         // Throw NPE
1321                         throw new NullPointerException("contact is null"); //NOI18N
1322                 } else if (contact.getContactId() == null) {
1323                         // Throw NPE
1324                         throw new NullPointerException("contact.contactId is null"); //NOI18N
1325                 } else if (contact.getContactId() < 1) {
1326                         // Not valid id number
1327                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1328                 }
1329
1330                 // Get iterator from list
1331                 Iterator<Contact> iterator = this.contactList.iterator();
1332
1333                 // "Walk" through all entries
1334                 while (iterator.hasNext()) {
1335                         // Get next element
1336                         Contact next = iterator.next();
1337
1338                         // Is id number the same?
1339                         if (Objects.equals(contact.getContactId(), next.getContactId())) {
1340                                 // Found entry, so remove it and abort
1341                                 this.removeContact(next);
1342                                 break;
1343                         }
1344                 }
1345
1346                 // Add contact to list
1347                 this.contactList.add(contact);
1348         }
1349
1350 }