]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/contact/JobsContactWebSessionBean.java
opps, now really fixed #2
[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 Contact createContactInstance () {
464                 // Is all required data set?
465                 if (!this.isRequiredPersonalDataSet()) {
466                         // No, then abort here
467                         throw new FaceletException(new IllegalArgumentException("Not all personal data is set, but createContactInstance() is called.")); //NOI18N
468                 }
469
470                 // Required personal data must be set
471                 assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N
472
473                 // Generate phone number
474                 DialableLandLineNumber phone = new LandLineNumber(this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber());
475                 DialableMobileNumber mobile = new MobileNumber(this.getMobileProvider(), this.getMobileNumber());
476                 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
477
478                 // Create new contact
479                 Contact contact = new UserContact(this.getPersonalTitle(), this.getFirstName(), this.getFamilyName());
480                 contact.setContactStreet(this.getStreet());
481                 contact.setContactHouseNumber(this.getHouseNumber());
482                 contact.setContactHouseNumberExtension(this.getHouseNumberExtension());
483                 contact.setContactZipCode(this.getZipCode());
484                 contact.setContactCity(this.getCity());
485                 contact.setContactCountry(this.getCountry());
486                 contact.setContactEmailAddress(this.getEmailAddress());
487                 contact.setContactBirthday(this.getBirthday());
488                 contact.setContactComment(this.getComment());
489
490                 // Don't set null or wrong references
491                 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getLandLineAreaCode() != null) && (this.getLandLineNumber() != null) && (this.getLandLineAreaCode() > 0) && (this.getLandLineNumber() > 0)) {
492                         // Now the number must be given
493                         if (phone.getPhoneAreaCode() == null) {
494                                 // Is null
495                                 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
496                         } else if (phone.getPhoneAreaCode() < 1) {
497                                 // Abort here
498                                 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
499                         } else if (phone.getPhoneNumber() == null) {
500                                 // Is null
501                                 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
502                         } else if (phone.getPhoneNumber() < 1) {
503                                 // Abort here
504                                 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
505                         }
506
507                         // Set phone number
508                         contact.setContactLandLineNumber(phone);
509                 }
510
511                 // Don't set null or wrong references
512                 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
513                         // Now the number must be given
514                         if (fax.getPhoneAreaCode() == null) {
515                                 // Is null
516                                 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
517                         } else if (fax.getPhoneAreaCode() < 1) {
518                                 // Abort here
519                                 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
520                         } else if (fax.getPhoneNumber() == null) {
521                                 // Is null
522                                 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
523                         } else if (fax.getPhoneNumber() < 1) {
524                                 // Abort here
525                                 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
526                         }
527
528                         // Set fax number
529                         contact.setContactFaxNumber(fax);
530                 }
531
532                 // Is the provider set?
533                 if ((mobile instanceof DialableMobileNumber) && (this.getMobileProvider() instanceof MobileProvider) && (this.getMobileNumber() != null) && (this.getMobileNumber() > 0)) {
534                         // Is the number set?
535                         if (mobile.getPhoneNumber() == null) {
536                                 // Is null
537                                 throw new NullPointerException("mobile.phoneNumber is null"); //NOI18N
538                         } else if (mobile.getPhoneNumber() < 1) {
539                                 // Abort here
540                                 throw new IllegalArgumentException("mobile.phoneNumber is zero or below."); //NOI18N
541                         }
542
543                         // Set mobile number
544                         contact.setContactMobileNumber(mobile);
545                 }
546
547                 // Return it
548                 return contact;
549         }
550
551         @Override
552         public String doChangePersonalContactData () {
553                 // This method shall only be called if the user is logged-in
554                 if (!this.userLoginController.isUserLoggedIn()) {
555                         // Not logged-in
556                         throw new IllegalStateException("User is not logged-in"); //NOI18N
557                 } else if (!this.isRequiredChangePersonalDataSet()) {
558                         // Not all required fields are set
559                         throw new FaceletException("Not all required fields are set."); //NOI18N
560                 } else if (!this.userLoginController.ifCurrentPasswordMatches()) {
561                         // Password not matching
562                         this.showFacesMessage("form_login_change_personal:currentPassword", new UserPasswordMismatchException(this.userLoginController.getLoggedInUser())); //NOI18N
563                         return ""; //NOI18N
564                 }
565
566                 // Get contact instance
567                 Contact contact = this.userLoginController.getLoggedInUser().getUserContact();
568
569                 // It should be there, so run some tests on it
570                 assert (contact instanceof Contact) : "Instance userLoginController.loggedInUser.userContact is null"; //NOI18N
571                 assert (contact.getContactId() instanceof Long) : "Instance userLoginController.userContact.contactId is null"; //NOI18N
572                 assert (contact.getContactId() > 0) : MessageFormat.format("Instance userLoginController.userContact.contactId={0} is invalid", contact.getContactId()); //NOI18N
573
574                 // Update all fields
575                 contact.setContactPersonalTitle(this.getPersonalTitle());
576                 contact.setContactFirstName(this.getFirstName());
577                 contact.setContactFamilyName(this.getFamilyName());
578                 contact.setContactStreet(this.getStreet());
579                 contact.setContactHouseNumber(this.getHouseNumber());
580                 contact.setContactHouseNumberExtension(this.getHouseNumberExtension());
581                 contact.setContactZipCode(this.getZipCode());
582                 contact.setContactCity(this.getCity());
583                 contact.setContactCountry(this.getCountry());
584
585                 // Update contact's mobile number
586                 this.isMobileUnlinked = ContactUtils.updateMobileNumber(contact, this.getMobileProvider(), this.getMobileNumber());
587
588                 // Update contact's land-line number
589                 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber());
590
591                 // Update contact's fax number
592                 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
593
594                 // Send it to the EJB
595                 this.contactBean.updateContactData(contact, this.isMobileUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
596
597                 // All fine
598                 return "contact_data_saved"; //NOI18N
599         }
600
601         @Override
602         public String getAcademicTitle () {
603                 return this.academicTitle;
604         }
605
606         @Override
607         public void setAcademicTitle (final String academicTitle) {
608                 this.academicTitle = academicTitle;
609         }
610
611         @Override
612         @SuppressWarnings ("ReturnOfDateField")
613         public Date getBirthday () {
614                 return this.birthday;
615         }
616
617         @Override
618         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
619         public void setBirthday (final Date birthday) {
620                 this.birthday = birthday;
621         }
622
623         @Override
624         public String getCity () {
625                 return this.city;
626         }
627
628         @Override
629         public void setCity (final String city) {
630                 this.city = city;
631         }
632
633         @Override
634         public String getComment () {
635                 return this.comment;
636         }
637
638         @Override
639         public void setComment (final String comment) {
640                 this.comment = comment;
641         }
642
643         @Override
644         public String getControllerType () {
645                 return "general"; //NOI18N
646         }
647
648         @Override
649         @Deprecated
650         public void setControllerType (final String controllerType) {
651                 throw new UnsupportedOperationException("Setting controller type is not supported."); //NOI18N
652         }
653
654         @Override
655         public Country getCountry () {
656                 return this.country;
657         }
658
659         @Override
660         public void setCountry (final Country country) {
661                 this.country = country;
662         }
663
664         @Override
665         public String getEmailAddress () {
666                 return this.emailAddress;
667         }
668
669         @Override
670         public void setEmailAddress (final String emailAddress) {
671                 this.emailAddress = emailAddress;
672         }
673
674         @Override
675         public String getEmailAddressRepeat () {
676                 return this.emailAddressRepeat;
677         }
678
679         @Override
680         public void setEmailAddressRepeat (final String emailAddressRepeat) {
681                 this.emailAddressRepeat = emailAddressRepeat;
682         }
683
684         @Override
685         public String getFamilyName () {
686                 return this.familyName;
687         }
688
689         @Override
690         public void setFamilyName (final String familyName) {
691                 this.familyName = familyName;
692         }
693
694         @Override
695         public Integer getFaxAreaCode () {
696                 return this.faxAreaCode;
697         }
698
699         @Override
700         public void setFaxAreaCode (final Integer faxAreaCode) {
701                 this.faxAreaCode = faxAreaCode;
702         }
703
704         @Override
705         public Country getFaxCountry () {
706                 return this.faxCountry;
707         }
708
709         @Override
710         public void setFaxCountry (final Country faxCountry) {
711                 this.faxCountry = faxCountry;
712         }
713
714         @Override
715         public Long getFaxNumber () {
716                 return this.faxNumber;
717         }
718
719         @Override
720         public void setFaxNumber (final Long faxNumber) {
721                 this.faxNumber = faxNumber;
722         }
723
724         @Override
725         public String getFirstName () {
726                 return this.firstName;
727         }
728
729         @Override
730         public void setFirstName (final String firstName) {
731                 this.firstName = firstName;
732         }
733
734         @Override
735         public Short getHouseNumber () {
736                 return this.houseNumber;
737         }
738
739         @Override
740         public void setHouseNumber (final Short houseNumber) {
741                 this.houseNumber = houseNumber;
742         }
743
744         @Override
745         public String getHouseNumberExtension () {
746                 return this.houseNumberExtension;
747         }
748
749         @Override
750         public void setHouseNumberExtension (final String houseNumberExtension) {
751                 this.houseNumberExtension = houseNumberExtension;
752         }
753
754         @Override
755         public Integer getLandLineAreaCode () {
756                 return this.landLineAreaCode;
757         }
758
759         @Override
760         public void setLandLineAreaCode (final Integer landLineAreaCode) {
761                 this.landLineAreaCode = landLineAreaCode;
762         }
763
764         @Override
765         public Country getLandLineCountry () {
766                 return this.landLineCountry;
767         }
768
769         @Override
770         public void setLandLineCountry (final Country landLineCountry) {
771                 this.landLineCountry = landLineCountry;
772         }
773
774         @Override
775         public Long getLandLineNumber () {
776                 return this.landLineNumber;
777         }
778
779         @Override
780         public void setLandLineNumber (final Long landLineNumber) {
781                 this.landLineNumber = landLineNumber;
782         }
783
784         @Override
785         public Long getMobileNumber () {
786                 return this.mobileNumber;
787         }
788
789         @Override
790         public void setMobileNumber (final Long mobileNumber) {
791                 this.mobileNumber = mobileNumber;
792         }
793
794         @Override
795         public MobileProvider getMobileProvider () {
796                 return this.mobileProvider;
797         }
798
799         @Override
800         public void setMobileProvider (final MobileProvider mobileProvider) {
801                 this.mobileProvider = mobileProvider;
802         }
803
804         @Override
805         public PersonalTitle getPersonalTitle () {
806                 return this.personalTitle;
807         }
808
809         @Override
810         public void setPersonalTitle (final PersonalTitle personalTitle) {
811                 this.personalTitle = personalTitle;
812         }
813
814         @Override
815         public String getStreet () {
816                 return this.street;
817         }
818
819         @Override
820         public void setStreet (final String street) {
821                 this.street = street;
822         }
823
824         @Override
825         public Integer getZipCode () {
826                 return this.zipCode;
827         }
828
829         @Override
830         public void setZipCode (final Integer zipCode) {
831                 this.zipCode = zipCode;
832         }
833
834         /**
835          * Post-construction method
836          */
837         @PostConstruct
838         public void init () {
839                 // Try it
840                 try {
841                         // Get initial context
842                         Context context = new InitialContext();
843
844                         // Try to lookup
845                         this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/jjobs-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
846                 } catch (final NamingException e) {
847                         // Throw again
848                         throw new FaceletException(e);
849                 }
850
851                 // Get full email address list for reducing EJB calls
852                 this.emailAddressList.addAll(this.contactBean.getEmailAddressList());
853
854                 // Get all contacts
855                 List<Contact> allContacts = this.contactBean.getAllContacts();
856
857                 // Get full contact list
858                 this.contactList.addAll(allContacts);
859
860                 // Get all users
861                 List<User> allUsers = this.userController.allUsers();
862
863                 // Get iterator
864                 Iterator<Contact> iterator = allContacts.iterator();
865
866                 // Loop through it
867                 while (iterator.hasNext()) {
868                         // Get next element
869                         Contact next = iterator.next();
870
871                         // Get iterator
872                         Iterator<User> userIterator = allUsers.iterator();
873
874                         // Loop through all users
875                         while (userIterator.hasNext()) {
876                                 // Get user instance
877                                 User nextUser = userIterator.next();
878
879                                 // Is contact same?
880                                 if (Objects.equals(next, nextUser.getUserContact())) {
881                                         // Found same
882                                         iterator.remove();
883                                         break;
884                                 }
885                         }
886                 }
887
888                 // Set contact list
889                 this.selectableContacts = allContacts;
890         }
891
892         @Override
893         public boolean isEmailAddressRegistered (final Contact contact) {
894                 // Cherck parameter
895                 if (null == contact) {
896                         // Throw NPE
897                         throw new NullPointerException("contact is null"); //NOI18N
898                 } else if (contact.getContactEmailAddress() == null) {
899                         // Throw again
900                         throw new NullPointerException("contact.contactEmailAddress is null"); //NOI18N
901                 } else if (contact.getContactEmailAddress().isEmpty()) {
902                         // Is empty
903                         throw new IllegalArgumentException("contact.contactEmailAddress is empty."); //NOI18N
904                 }
905
906                 // Determine it
907                 return ((this.emailAddressList instanceof List) && (this.emailAddressList.contains(contact.getContactEmailAddress())));
908         }
909
910         @Override
911         public boolean isRequiredChangePersonalDataSet () {
912                 return ((this.getPersonalTitle() != null) &&
913                                 (this.getFirstName() != null) &&
914                                 (this.getFamilyName() != null) &&
915                                 (this.getStreet() != null) &&
916                                 (this.getHouseNumber() != null) &&
917                                 (this.getZipCode() != null) &&
918                                 (this.getCity() != null));
919         }
920
921         @Override
922         public boolean isRequiredPersonalDataSet () {
923                 return ((this.getPersonalTitle() != null) &&
924                                 (this.getFirstName() != null) &&
925                                 (this.getFamilyName() != null) &&
926                                 (this.getStreet() != null) &&
927                                 (this.getHouseNumber() != null) &&
928                                 (this.getZipCode() != null) &&
929                                 (this.getCity() != null) &&
930                                 (this.getEmailAddress() != null) &&
931                                 (this.getEmailAddressRepeat() != null));
932         }
933
934         @Override
935         public boolean isSameEmailAddressEntered () {
936                 return (Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat()));
937         }
938
939         @Override
940         public Contact lookupContactById (final Long contactId) throws ContactNotFoundException {
941                 // Init variable
942                 Contact localContact = null;
943
944                 // Clear this bean
945                 this.clear();
946
947                 // Try to lookup it in visible user list
948                 for (final Iterator<Contact> iterator = this.contactList.iterator(); iterator.hasNext();) {
949                         // Get next user
950                         Contact next = iterator.next();
951
952                         // Is the user id found?
953                         if (Objects.equals(next.getContactId(), contactId)) {
954                                 // Copy to other variable
955                                 localContact = next;
956                                 break;
957                         }
958                 }
959
960                 // Is it still null?
961                 if (null == localContact) {
962                         // Not visible for the current user
963                         throw new ContactNotFoundException(contactId);
964                 }
965
966                 // Copy all data to this bean
967                 this.copyContact(localContact);
968
969                 // Return it
970                 return localContact;
971         }
972
973         @Override
974         public List<Contact> selectableContacts () {
975                 return Collections.unmodifiableList(this.selectableContacts);
976         }
977
978         @Override
979         public void updateContactDataFromController (final Contact contact) {
980                 // Is the instance valid?
981                 if (null == contact) {
982                         // Throw NPE
983                         throw new NullPointerException("contact is null"); //NOI18N
984                 } else if (contact.getContactId() == null) {
985                         // Throw NPE
986                         throw new NullPointerException("contact.contactId is null"); //NOI18N
987                 } else if (contact.getContactId() < 1) {
988                         // Not valid id number
989                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
990                 }
991
992                 // Set all
993                 this.copyContact(contact);
994         }
995
996         /**
997          * Adds email address to bean's internal list.
998          * <p>
999          * @param contact Contact instance
1000          */
1001         private void addUserNameEmailAddress (final Contact contact) {
1002                 // Make sure the entry is not added yet
1003                 if (this.emailAddressList.contains(contact.getContactEmailAddress())) {
1004                         // Already added
1005                         throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", contact.getContactEmailAddress())); //NOI18N
1006                 }
1007
1008                 // Add email addres
1009                 this.emailAddressList.add(contact.getContactEmailAddress());
1010         }
1011
1012         /**
1013          * Clears this bean
1014          */
1015         private void clear () {
1016                 // Clear all data
1017                 // - personal data
1018                 this.setPersonalTitle(null);
1019                 this.setAcademicTitle(null);
1020                 this.setFirstName(null);
1021                 this.setFamilyName(null);
1022                 this.setStreet(null);
1023                 this.setHouseNumber(null);
1024                 this.setHouseNumberExtension(null);
1025                 this.setZipCode(null);
1026                 this.setCity(null);
1027                 this.setCountry(null);
1028
1029                 // - contact data
1030                 this.setEmailAddress(null);
1031                 this.setEmailAddressRepeat(null);
1032                 this.setLandLineAreaCode(null);
1033                 this.setLandLineCountry(null);
1034                 this.setLandLineNumber(null);
1035                 this.setMobileProvider(null);
1036                 this.setMobileNumber(null);
1037                 this.setFaxAreaCode(null);
1038                 this.setFaxCountry(null);
1039                 this.setFaxNumber(null);
1040
1041                 // - other data
1042                 this.setBirthday(null);
1043                 this.setComment(null);
1044         }
1045
1046         /**
1047          * Copies given contact into the controller
1048          * <p>
1049          * @param contact Contact instance
1050          */
1051         private void copyContact (final Contact contact) {
1052                 // Is the instance valid?
1053                 if (null == contact) {
1054                         // Throw NPE
1055                         throw new NullPointerException("contact is null"); //NOI18N
1056                 } else if (contact.getContactId() == null) {
1057                         // Throw NPE
1058                         throw new NullPointerException("contact.contactId is null"); //NOI18N
1059                 } else if (contact.getContactId() < 1) {
1060                         // Not valid id number
1061                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1062                 }
1063
1064                 // Copy all fields:
1065                 // - base data
1066                 this.setPersonalTitle(contact.getContactPersonalTitle());
1067                 this.setAcademicTitle(contact.getContactTitle());
1068                 this.setFirstName(contact.getContactFirstName());
1069                 this.setFamilyName(contact.getContactFamilyName());
1070                 this.setStreet(contact.getContactStreet());
1071                 this.setHouseNumber(contact.getContactHouseNumber());
1072                 this.setHouseNumberExtension(contact.getContactHouseNumberExtension());
1073                 this.setZipCode(contact.getContactZipCode());
1074                 this.setCity(contact.getContactCity());
1075                 this.setCountry(contact.getContactCountry());
1076                 this.setEmailAddress(contact.getContactEmailAddress());
1077                 this.setBirthday(contact.getContactBirthday());
1078                 this.setComment(contact.getContactComment());
1079
1080                 // Get mobile, phone and fax instance
1081                 DialableMobileNumber mobile = contact.getContactMobileNumber();
1082                 DialableFaxNumber fax = contact.getContactFaxNumber();
1083                 DialableLandLineNumber phone = contact.getContactLandLineNumber();
1084
1085                 // - contact data
1086                 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneAreaCode() > 0)) {
1087                         this.setLandLineCountry(phone.getPhoneCountry());
1088                         this.setLandLineAreaCode(phone.getPhoneAreaCode());
1089                         this.setLandLineNumber(phone.getPhoneNumber());
1090                 }
1091
1092                 if ((mobile instanceof DialableMobileNumber) && (mobile.getMobileProvider() instanceof MobileProvider)) {
1093                         this.setMobileProvider(mobile.getMobileProvider());
1094                         this.setMobileNumber(mobile.getPhoneNumber());
1095                 }
1096
1097                 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneAreaCode() > 0)) {
1098                         this.setFaxCountry(fax.getPhoneCountry());
1099                         this.setFaxAreaCode(fax.getPhoneAreaCode());
1100                         this.setFaxNumber(fax.getPhoneNumber());
1101                 }
1102         }
1103
1104         /**
1105          * Removes given contact from all lists
1106          * <p>
1107          * @param contact Contact instance to remove
1108          */
1109         private void removeContact (final Contact contact) {
1110                 // Is the instance valid?
1111                 if (null == contact) {
1112                         // Throw NPE
1113                         throw new NullPointerException("contact is null"); //NOI18N
1114                 } else if (contact.getContactId() == null) {
1115                         // Throw NPE
1116                         throw new NullPointerException("contact.contactId is null"); //NOI18N
1117                 } else if (contact.getContactId() < 1) {
1118                         // Not valid id number
1119                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1120                 }
1121
1122                 // Remove from general list
1123                 if (!this.contactList.remove(contact)) {
1124                         // Did not remove contact
1125                         throw new IllegalStateException(MessageFormat.format("contact {0} was not removed.", contact.getContactId())); //NOI18N
1126                 }
1127
1128                 // Remove from other lists
1129                 this.emailAddressList.remove(contact.getContactEmailAddress());
1130         }
1131
1132         /**
1133          * Adds unique instance to contact list. First any existing instance is
1134          * being removed, then the new instance is added.
1135          * <p>
1136          * @param contact Contact instance to add uniquely
1137          */
1138         private void uniqueAddContact (final Contact contact) {
1139                 // Is the instance valid?
1140                 if (null == contact) {
1141                         // Throw NPE
1142                         throw new NullPointerException("contact is null"); //NOI18N
1143                 } else if (contact.getContactId() == null) {
1144                         // Throw NPE
1145                         throw new NullPointerException("contact.contactId is null"); //NOI18N
1146                 } else if (contact.getContactId() < 1) {
1147                         // Not valid id number
1148                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1149                 }
1150
1151                 // Get iterator from list
1152                 Iterator<Contact> iterator = this.contactList.iterator();
1153
1154                 // "Walk" through all entries
1155                 while (iterator.hasNext()) {
1156                         // Get next element
1157                         Contact next = iterator.next();
1158
1159                         // Is id number the same?
1160                         if (Objects.equals(contact.getContactId(), next.getContactId())) {
1161                                 // Found entry, so remove it and abort
1162                                 this.removeContact(next);
1163                                 break;
1164                         }
1165                 }
1166
1167                 // Add contact to list
1168                 this.contactList.add(contact);
1169         }
1170
1171 }