]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/contact/JobsContactWebSessionBean.java
Continued with cell phone unlink: (please cherry-pick)
[jjobs-war.git] / src / java / org / mxchange / jjobs / beans / contact / JobsContactWebSessionBean.java
1 /*
2  * Copyright (C) 2016 Roland Haeder
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.UserContact;
38 import org.mxchange.jcontacts.contact.gender.Gender;
39 import org.mxchange.jcontacts.contact.utils.ContactUtils;
40 import org.mxchange.jcontacts.events.contact.add.AdminAddedContactEvent;
41 import org.mxchange.jcontacts.events.contact.update.AdminUpdatedContactEvent;
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.login.JobsUserLoginWebSessionController;
46 import org.mxchange.jjobs.beans.user.JobsUserWebSessionController;
47 import org.mxchange.jphone.phonenumbers.cellphone.CellphoneNumber;
48 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
49 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
50 import org.mxchange.jphone.phonenumbers.fax.FaxNumber;
51 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
52 import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
53 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
54 import org.mxchange.jusercore.events.confirmation.UserConfirmedAccountEvent;
55 import org.mxchange.jusercore.events.login.UserLoggedInEvent;
56 import org.mxchange.jusercore.events.registration.UserRegisteredEvent;
57 import org.mxchange.jusercore.events.user.add.AdminAddedUserEvent;
58 import org.mxchange.jusercore.events.user.linked.AdminLinkedUserEvent;
59 import org.mxchange.jusercore.exceptions.UserPasswordMismatchException;
60 import org.mxchange.jusercore.model.user.User;
61
62 /**
63  * A general contact controller (bean)
64  * <p>
65  * @author Roland Haeder<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          * Birth day
78          */
79         private Date birthday;
80
81         /**
82          * Cellphone number's carrier
83          */
84         private MobileProvider cellphoneCarrier;
85
86         /**
87          * Cellphone number
88          */
89         private Long cellphoneNumber;
90
91         /**
92          * City
93          */
94         private String city;
95
96         /**
97          * Optional comments
98          */
99         private String comment;
100
101         /**
102          * Remote contact bean
103          */
104         private final ContactSessionBeanRemote contactBean;
105
106         /**
107          * Contact list
108          */
109         private final List<Contact> contactList;
110
111         /**
112          * Country instance
113          */
114         private Country country;
115
116         /**
117          * Email address
118          */
119         private String emailAddress;
120
121         /**
122          * Email address list
123          */
124         private final List<String> emailAddressList;
125
126         /**
127          * Email address repeated
128          */
129         private String emailAddressRepeat;
130
131         /**
132          * Family name
133          */
134         private String familyName;
135
136         /**
137          * Fax number's area code
138          */
139         private Integer faxAreaCode;
140
141         /**
142          * Country instance for fax number
143          */
144         private Country faxCountry;
145
146         /**
147          * Fax number
148          */
149         private Long faxNumber;
150
151         /**
152          * First name
153          */
154         private String firstName;
155
156         /**
157          * Gender instance
158          */
159         private Gender gender;
160
161         /**
162          * House number
163          */
164         private Short houseNumber;
165
166         /**
167          * House number extension
168          */
169         private String houseNumberExtension;
170
171         /**
172          * Whether a cellphone entry has been unlinked
173          */
174         private boolean isCellphoneUnlinked;
175
176         /**
177          * Whether a fax entry has been unlinked
178          */
179         private boolean isFaxUnlinked;
180
181         /**
182          * Whether a land-line number has been unlinked
183          */
184         private boolean isLandLineUnlinked;
185
186         /**
187          * Phone number area code
188          */
189         private Integer phoneAreaCode;
190
191         /**
192          * Country instance for phone number
193          */
194         private Country phoneCountry;
195
196         /**
197          * Phone number
198          */
199         private Long phoneNumber;
200
201         /**
202          * A list of all selectable contacts
203          */
204         private List<Contact> selectableContacts;
205
206         /**
207          * Street
208          */
209         private String street;
210
211         /**
212          * Title
213          */
214         private String title;
215
216         /**
217          * Regular user controller
218          */
219         @Inject
220         private JobsUserWebSessionController userController;
221
222         /**
223          * Login controller (bean)
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                 // Try it
238                 try {
239                         // Get initial context
240                         Context context = new InitialContext();
241
242                         // Try to lookup
243                         this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/jratecalc-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
244                 } catch (final NamingException e) {
245                         // Throw again
246                         throw new FaceletException(e);
247                 }
248
249                 // Init lists/maps
250                 this.contactList = new LinkedList<>();
251                 this.emailAddressList = new LinkedList<>();
252         }
253
254         @Override
255         public void afterAdminAddedContact (@Observes final AdminAddedContactEvent event) {
256                 // The event must be valid
257                 if (null == event) {
258                         // Throw NPE
259                         throw new NullPointerException("event is null"); //NOI18N
260                 } else if (event.getAddedContact() == null) {
261                         // Throw again ...
262                         throw new NullPointerException("event.addedContact is null"); //NOI18N
263                 } else if (event.getAddedContact().getContactId() == null) {
264                         // ... and again
265                         throw new NullPointerException("event.addedContact.contactId is null"); //NOI18N
266                 } else if (event.getAddedContact().getContactId() < 1) {
267                         // Not valid
268                         throw new IllegalArgumentException(MessageFormat.format("event.addedContact.contactId={0} is not valid", event.getAddedContact().getContactId())); //NOI18N //NOI18N
269                 }
270
271                 // Clear this bean
272                 this.clear();
273
274                 // Call other method
275                 this.uniqueAddContact(event.getAddedContact());
276
277                 // Add to selectable contacts
278                 this.selectableContacts.add(event.getAddedContact());
279         }
280
281         @Override
282         public void afterAdminAddedUserEvent (@Observes final AdminAddedUserEvent event) {
283                 // event should not be null
284                 if (null == event) {
285                         // Throw NPE
286                         throw new NullPointerException("event is null"); //NOI18N
287                 } else if (event.getAddedUser() == null) {
288                         // Throw NPE again
289                         throw new NullPointerException("event.addedUser is null"); //NOI18N
290                 } else if (event.getAddedUser().getUserId() == null) {
291                         // userId is null
292                         throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
293                 } else if (event.getAddedUser().getUserId() < 1) {
294                         // Not avalid id
295                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
296                 }
297
298                 // Clear all data
299                 this.clear();
300         }
301
302         @Override
303         public void afterAdminLinkedUser (@Observes final AdminLinkedUserEvent event) {
304                 // event should not be null
305                 if (null == event) {
306                         // Throw NPE
307                         throw new NullPointerException("event is null"); //NOI18N
308                 } else if (event.getLinkedUser() == null) {
309                         // Throw NPE again
310                         throw new NullPointerException("event.linkedUser is null"); //NOI18N
311                 } else if (event.getLinkedUser().getUserContact() == null) {
312                         // Throw NPE again
313                         throw new NullPointerException("event.linkedUser.userContact is null"); //NOI18N
314                 } else if (event.getLinkedUser().getUserContact().getContactId() == null) {
315                         // userId is null
316                         throw new NullPointerException("event.linkedUser.userContact.contactId is null"); //NOI18N
317                 } else if (event.getLinkedUser().getUserContact().getContactId() < 1) {
318                         // Not avalid id
319                         throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getLinkedUser(), event.getLinkedUser().getUserContact().getContactId())); //NOI18N
320                 }
321
322                 // Remove contact from list available contacts list
323                 this.selectableContacts.remove(event.getLinkedUser().getUserContact());
324
325                 // Clear all data
326                 this.clear();
327         }
328
329         @Override
330         public void afterAdminUpdatedContactDataEvent (@Observes final AdminUpdatedContactEvent event) {
331                 // event should not be null
332                 if (null == event) {
333                         // Throw NPE
334                         throw new NullPointerException("event is null"); //NOI18N
335                 } else if (event.getUpdatedContact() == null) {
336                         // Throw NPE again
337                         throw new NullPointerException("event.updatedContact is null"); //NOI18N
338                 } else if (event.getUpdatedContact().getContactId() == null) {
339                         // userId is null
340                         throw new NullPointerException("event.updatedContact.contactId is null"); //NOI18N
341                 } else if (event.getUpdatedContact().getContactId() < 1) {
342                         // Not avalid id
343                         throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
344                 }
345
346                 // Add contact instance only once
347                 this.uniqueAddContact(event.getUpdatedContact());
348
349                 // Add email address to list
350                 this.emailAddressList.add(event.getUpdatedContact().getContactEmailAddress());
351         }
352
353         @Override
354         public void afterRegistrationEvent (@Observes final UserRegisteredEvent event) {
355                 // event should not be null
356                 if (null == event) {
357                         // Throw NPE
358                         throw new NullPointerException("event is null"); //NOI18N
359                 } else if (event.getRegisteredUser() == null) {
360                         // Throw NPE again
361                         throw new NullPointerException("event.registeredUser is null"); //NOI18N
362                 } else if (event.getRegisteredUser().getUserId() == null) {
363                         // userId is null
364                         throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
365                 } else if (event.getRegisteredUser().getUserId() < 1) {
366                         // Not avalid id
367                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
368                 }
369
370                 // Get user instance
371                 Contact registeredContact = event.getRegisteredUser().getUserContact();
372
373                 // Copy all data from registered->user
374                 this.copyContact(registeredContact);
375
376                 // Add contact instance only once
377                 this.uniqueAddContact(registeredContact);
378
379                 // Add user name and email address
380                 this.addUserNameEmailAddress(registeredContact);
381
382                 // Clear all data
383                 this.clear();
384         }
385
386         @Override
387         public void afterUserConfirmedAccount (@Observes final UserConfirmedAccountEvent event) {
388                 // event should not be null
389                 if (null == event) {
390                         // Throw NPE
391                         throw new NullPointerException("event is null"); //NOI18N
392                 } else if (event.getConfirmedUser() == null) {
393                         // Throw NPE again
394                         throw new NullPointerException("event.confirmedUser is null"); //NOI18N
395                 } else if (event.getConfirmedUser().getUserId() == null) {
396                         // userId is null
397                         throw new NullPointerException("event.confirmedUser.userId is null"); //NOI18N
398                 } else if (event.getConfirmedUser().getUserId() < 1) {
399                         // Not avalid id
400                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getConfirmedUser(), event.getConfirmedUser().getUserId())); //NOI18N
401                 }
402
403                 // Add contact instance only once
404                 this.uniqueAddContact(event.getConfirmedUser().getUserContact());
405         }
406
407         @Override
408         public void afterUserLogin (@Observes final UserLoggedInEvent event) {
409                 // event should not be null
410                 if (null == event) {
411                         // Throw NPE
412                         throw new NullPointerException("event is null"); //NOI18N
413                 } else if (event.getLoggedInUser() == null) {
414                         // Throw NPE again
415                         throw new NullPointerException("event.loggedInUser is null"); //NOI18N
416                 } else if (event.getLoggedInUser().getUserId() == null) {
417                         // userId is null
418                         throw new NullPointerException("event.loggedInUser.userId is null"); //NOI18N
419                 } else if (event.getLoggedInUser().getUserId() < 1) {
420                         // Not avalid id
421                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
422                 }
423
424                 // Copy all data to this bean
425                 this.copyContact(event.getLoggedInUser().getUserContact());
426         }
427
428         @Override
429         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
430         public List<Contact> allContacts () {
431                 // Debug message
432                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ContactController.allContacts: contactList.size()={0} - EXIT!", this.contactList.size()));
433
434                 // Return un-modified list
435                 return this.contactList;
436         }
437
438         @Override
439         public Contact createContactInstance () {
440                 // User message
441                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createContactInstance: CALLED!", this.getClass().getSimpleName()));
442
443                 // Is all required data set?
444                 if (!this.isRequiredPersonalDataSet()) {
445                         // No, then abort here
446                         throw new FaceletException(new IllegalArgumentException("Not all personal data is set, but createContactInstance() is called.")); //NOI18N
447                 }
448
449                 // Generate phone number
450                 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
451                 DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
452                 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
453
454                 // Create new contact
455                 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
456                 contact.setContactStreet(this.getStreet());
457                 contact.setContactHouseNumber(this.getHouseNumber());
458                 contact.setContactHouseNumberExtension(this.getHouseNumberExtension());
459                 contact.setContactZipCode(this.getZipCode());
460                 contact.setContactCity(this.getCity());
461                 contact.setContactCountry(this.getCountry());
462                 contact.setContactEmailAddress(this.getEmailAddress());
463                 contact.setContactBirthday(this.getBirthday());
464                 contact.setContactComment(this.getComment());
465
466                 // Debug message
467                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createContactInstance: this.emailAddress={1}", this.getClass().getSimpleName(), this.getEmailAddress()));
468                 // Don't set null or wrong references
469                 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getPhoneAreaCode() != null) && (this.getPhoneNumber() != null) && (this.getPhoneAreaCode() > 0) && (this.getPhoneNumber() > 0)) {
470                         // Now the number must be given
471                         if (phone.getPhoneAreaCode() == null) {
472                                 // Is null
473                                 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
474                         } else if (phone.getPhoneAreaCode() < 1) {
475                                 // Abort here
476                                 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
477                         } else if (phone.getPhoneNumber() == null) {
478                                 // Is null
479                                 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
480                         } else if (phone.getPhoneNumber() < 1) {
481                                 // Abort here
482                                 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
483                         }
484
485                         // Set phone number
486                         contact.setContactLandLineNumber(phone);
487                 }
488
489                 // Don't set null or wrong references
490                 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
491                         // Now the number must be given
492                         if (fax.getPhoneAreaCode() == null) {
493                                 // Is null
494                                 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
495                         } else if (fax.getPhoneAreaCode() < 1) {
496                                 // Abort here
497                                 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
498                         } else if (fax.getPhoneNumber() == null) {
499                                 // Is null
500                                 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
501                         } else if (fax.getPhoneNumber() < 1) {
502                                 // Abort here
503                                 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
504                         }
505
506                         // Set fax number
507                         contact.setContactFaxNumber(fax);
508                 }
509
510                 // Is the provider set?
511                 if ((cellphone instanceof DialableCellphoneNumber) && (this.getCellphoneCarrier() instanceof MobileProvider) && (this.getCellphoneNumber() != null) && (this.getCellphoneNumber() > 0)) {
512                         // Is the number set?
513                         if (cellphone.getPhoneNumber() == null) {
514                                 // Is null
515                                 throw new NullPointerException("cellphone.phoneNumber is null"); //NOI18N
516                         } else if (cellphone.getPhoneNumber() < 1) {
517                                 // Abort here
518                                 throw new IllegalArgumentException("cellphone.phoneNumber is zero or below."); //NOI18N
519                         }
520
521                         // Set cellphone number
522                         contact.setContactCellphoneNumber(cellphone);
523                 }
524
525                 // Trace message
526                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createContactInstance: contact={1} - EXIT!", this.getClass().getSimpleName(), contact));
527                 // Return it
528                 return contact;
529         }
530
531         @Override
532         public String doChangePersonalContactData () {
533                 // This method shall only be called if the user is logged-in
534                 if (!this.userLoginController.isUserLoggedIn()) {
535                         // Not logged-in
536                         throw new IllegalStateException("User is not logged-in"); //NOI18N
537                 } else if (!this.isRequiredChangePersonalDataSet()) {
538                         // Not all required fields are set
539                         throw new FaceletException("Not all required fields are set."); //NOI18N
540                 } else if (!this.userLoginController.ifCurrentPasswordMatches()) {
541                         // Password not matching
542                         this.showFacesMessage("form_login_change_personal:currentPassword", new UserPasswordMismatchException(this.userLoginController.getLoggedInUser())); //NOI18N
543                         return ""; //NOI18N
544                 }
545
546                 // Get contact instance
547                 Contact contact = this.userLoginController.getLoggedInUser().getUserContact();
548
549                 // It should be there, so run some tests on it
550                 assert (contact instanceof Contact) : "Instance userLoginController.loggedInUser.userContact is null"; //NOI18N
551                 assert (contact.getContactId() instanceof Long) : "Instance userLoginController.userContact.contactId is null"; //NOI18N
552                 assert (contact.getContactId() > 0) : MessageFormat.format("Instance userLoginController.userContact.contactId={0} is invalid", contact.getContactId()); //NOI18N
553
554                 // Update all fields
555                 contact.setContactGender(this.getGender());
556                 contact.setContactFirstName(this.getFirstName());
557                 contact.setContactFamilyName(this.getFamilyName());
558                 contact.setContactStreet(this.getStreet());
559                 contact.setContactHouseNumber(this.getHouseNumber());
560                 contact.setContactHouseNumberExtension(this.getHouseNumberExtension());
561                 contact.setContactZipCode(this.getZipCode());
562                 contact.setContactCity(this.getCity());
563                 contact.setContactCountry(this.getCountry());
564
565                 // Update contact's cellphone number
566                 this.isCellphoneUnlinked = ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
567
568                 // Update contact's land-line number
569                 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
570
571                 // Update contact's fax number
572                 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
573
574                 // Send it to the EJB
575                 this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
576
577                 // All fine
578                 return "contact_data_saved"; //NOI18N
579         }
580
581         @Override
582         @SuppressWarnings ("ReturnOfDateField")
583         public Date getBirthday () {
584                 return this.birthday;
585         }
586
587         @Override
588         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
589         public void setBirthday (final Date birthday) {
590                 this.birthday = birthday;
591         }
592
593         @Override
594         public MobileProvider getCellphoneCarrier () {
595                 return this.cellphoneCarrier;
596         }
597
598         @Override
599         public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
600                 this.cellphoneCarrier = cellphoneCarrier;
601         }
602
603         @Override
604         public Long getCellphoneNumber () {
605                 return this.cellphoneNumber;
606         }
607
608         @Override
609         public void setCellphoneNumber (Long cellphoneNumber) {
610                 this.cellphoneNumber = cellphoneNumber;
611         }
612
613         @Override
614         public String getCity () {
615                 return this.city;
616         }
617
618         @Override
619         public void setCity (final String city) {
620                 this.city = city;
621         }
622
623         @Override
624         public String getComment () {
625                 return this.comment;
626         }
627
628         @Override
629         public void setComment (final String comment) {
630                 this.comment = comment;
631         }
632
633         @Override
634         public String getControllerType () {
635                 return "general"; //NOI18N
636         }
637
638         @Override
639         @Deprecated
640         public void setControllerType (final String controllerType) {
641                 throw new UnsupportedOperationException("Setting controller type is not supported."); //NOI18N
642         }
643
644         @Override
645         public Country getCountry () {
646                 return this.country;
647         }
648
649         @Override
650         public void setCountry (final Country country) {
651                 this.country = country;
652         }
653
654         @Override
655         public String getEmailAddress () {
656                 return this.emailAddress;
657         }
658
659         @Override
660         public void setEmailAddress (final String emailAddress) {
661                 this.emailAddress = emailAddress;
662         }
663
664         @Override
665         public String getEmailAddressRepeat () {
666                 return this.emailAddressRepeat;
667         }
668
669         @Override
670         public void setEmailAddressRepeat (final String emailAddressRepeat) {
671                 this.emailAddressRepeat = emailAddressRepeat;
672         }
673
674         @Override
675         public String getFamilyName () {
676                 return this.familyName;
677         }
678
679         @Override
680         public void setFamilyName (final String familyName) {
681                 this.familyName = familyName;
682         }
683
684         @Override
685         public Integer getFaxAreaCode () {
686                 return this.faxAreaCode;
687         }
688
689         @Override
690         public void setFaxAreaCode (final Integer faxAreaCode) {
691                 this.faxAreaCode = faxAreaCode;
692         }
693
694         @Override
695         public Country getFaxCountry () {
696                 return this.faxCountry;
697         }
698
699         @Override
700         public void setFaxCountry (final Country faxCountry) {
701                 this.faxCountry = faxCountry;
702         }
703
704         @Override
705         public Long getFaxNumber () {
706                 return this.faxNumber;
707         }
708
709         @Override
710         public void setFaxNumber (final Long faxNumber) {
711                 this.faxNumber = faxNumber;
712         }
713
714         @Override
715         public String getFirstName () {
716                 return this.firstName;
717         }
718
719         @Override
720         public void setFirstName (final String firstName) {
721                 this.firstName = firstName;
722         }
723
724         @Override
725         public Gender getGender () {
726                 return this.gender;
727         }
728
729         @Override
730         public void setGender (final Gender gender) {
731                 this.gender = gender;
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 getPhoneAreaCode () {
756                 return this.phoneAreaCode;
757         }
758
759         @Override
760         public void setPhoneAreaCode (final Integer phoneAreaCode) {
761                 this.phoneAreaCode = phoneAreaCode;
762         }
763
764         @Override
765         public Country getPhoneCountry () {
766                 return this.phoneCountry;
767         }
768
769         @Override
770         public void setPhoneCountry (final Country phoneCountry) {
771                 this.phoneCountry = phoneCountry;
772         }
773
774         @Override
775         public Long getPhoneNumber () {
776                 return this.phoneNumber;
777         }
778
779         @Override
780         public void setPhoneNumber (final Long phoneNumber) {
781                 this.phoneNumber = phoneNumber;
782         }
783
784         @Override
785         public String getStreet () {
786                 return this.street;
787         }
788
789         @Override
790         public void setStreet (final String street) {
791                 this.street = street;
792         }
793
794         @Override
795         public String getTitle () {
796                 return this.title;
797         }
798
799         @Override
800         public void setTitle (final String title) {
801                 this.title = title;
802         }
803
804         @Override
805         public Integer getZipCode () {
806                 return this.zipCode;
807         }
808
809         @Override
810         public void setZipCode (final Integer zipCode) {
811                 this.zipCode = zipCode;
812         }
813
814         /**
815          * Post-initialization of this class
816          */
817         @PostConstruct
818         public void init () {
819                 // Get full email address list for reducing EJB calls
820                 this.emailAddressList.addAll(this.contactBean.getEmailAddressList());
821
822                 // Get full contact list
823                 this.contactList.addAll(this.contactBean.getAllContacts());
824
825                 // Get all users
826                 List<User> allUsers = this.userController.allUsers();
827
828                 // Get all contacts
829                 List<Contact> allContacts = this.contactBean.getAllContacts();
830
831                 // Get iterator
832                 Iterator<Contact> iterator = allContacts.iterator();
833
834                 // Loop through it
835                 while (iterator.hasNext()) {
836                         // Get next element
837                         Contact next = iterator.next();
838
839                         // Get iterator
840                         Iterator<User> userIterator = allUsers.iterator();
841
842                         // Loop through all users
843                         while (userIterator.hasNext()) {
844                                 // Get user instance
845                                 User nextUser = userIterator.next();
846
847                                 // Is contact same?
848                                 if (Objects.equals(next, nextUser.getUserContact())) {
849                                         // Found same
850                                         iterator.remove();
851                                         break;
852                                 }
853                         }
854                 }
855
856                 // Set contact list
857                 this.selectableContacts = allContacts;
858         }
859
860         @Override
861         public boolean isEmailAddressRegistered (final Contact contact) {
862                 // Cherck parameter
863                 if (null == contact) {
864                         // Throw NPE
865                         throw new NullPointerException("contact is null"); //NOI18N
866                 } else if (contact.getContactEmailAddress() == null) {
867                         // Throw again
868                         throw new NullPointerException("contact.contactEmailAddress is null"); //NOI18N
869                 } else if (contact.getContactEmailAddress().isEmpty()) {
870                         // Is empty
871                         throw new IllegalArgumentException("contact.contactEmailAddress is empty."); //NOI18N
872                 }
873
874                 // Determine it
875                 return ((this.emailAddressList instanceof List) && (this.emailAddressList.contains(contact.getContactEmailAddress())));
876         }
877
878         @Override
879         public boolean isRequiredChangePersonalDataSet () {
880                 return ((this.getGender() != null) &&
881                                 (this.getFirstName() != null) &&
882                                 (this.getFamilyName() != null) &&
883                                 (this.getStreet() != null) &&
884                                 (this.getHouseNumber() != null) &&
885                                 (this.getZipCode() != null) &&
886                                 (this.getCity() != null));
887         }
888
889         @Override
890         public boolean isRequiredPersonalDataSet () {
891                 return ((this.getGender() != null) &&
892                                 (this.getFirstName() != null) &&
893                                 (this.getFamilyName() != null) &&
894                                 (this.getStreet() != null) &&
895                                 (this.getHouseNumber() != null) &&
896                                 (this.getZipCode() != null) &&
897                                 (this.getCity() != null) &&
898                                 (this.getEmailAddress() != null) &&
899                                 (this.getEmailAddressRepeat() != null));
900         }
901
902         @Override
903         public boolean isSameEmailAddressEntered () {
904                 return (Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat()));
905         }
906
907         @Override
908         public Contact lookupContactById (final Long contactId) throws ContactNotFoundException {
909                 // Init variable
910                 Contact localContact = null;
911
912                 // Clear this bean
913                 this.clear();
914
915                 // Try to lookup it in visible user list
916                 for (final Iterator<Contact> iterator = this.contactList.iterator(); iterator.hasNext();) {
917                         // Get next user
918                         Contact next = iterator.next();
919
920                         // Is the user id found?
921                         if (Objects.equals(next.getContactId(), contactId)) {
922                                 // Copy to other variable
923                                 localContact = next;
924                                 break;
925                         }
926                 }
927
928                 // Is it still null?
929                 if (null == localContact) {
930                         // Not visible for the current user
931                         throw new ContactNotFoundException(contactId);
932                 }
933
934                 // Copy all data to this bean
935                 this.copyContact(localContact);
936
937                 // Return it
938                 return localContact;
939         }
940
941         @Override
942         public List<Contact> selectableContacts () {
943                 return Collections.unmodifiableList(this.selectableContacts);
944         }
945
946         @Override
947         public void updateContactDataFromController (final Contact contact) {
948                 // Is the instance valid?
949                 if (null == contact) {
950                         // Throw NPE
951                         throw new NullPointerException("contact is null"); //NOI18N
952                 } else if (contact.getContactId() == null) {
953                         // Throw NPE
954                         throw new NullPointerException("contact.contactId is null"); //NOI18N
955                 } else if (contact.getContactId() < 1) {
956                         // Not valid id number
957                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
958                 }
959
960                 // Set all
961                 this.copyContact(contact);
962         }
963
964         /**
965          * Adds email address to bean's internal list.
966          * <p>
967          * @param contact Contact instance
968          */
969         private void addUserNameEmailAddress (final Contact contact) {
970                 // Make sure the entry is not added yet
971                 if (this.emailAddressList.contains(contact.getContactEmailAddress())) {
972                         // Already added
973                         throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", contact.getContactEmailAddress())); //NOI18N
974                 }
975
976                 // Add email addres
977                 this.emailAddressList.add(contact.getContactEmailAddress());
978         }
979
980         /**
981          * Clears this bean
982          */
983         private void clear () {
984                 // Clear all data
985                 // - personal data
986                 this.setGender(null);
987                 this.setTitle(null);
988                 this.setFirstName(null);
989                 this.setFamilyName(null);
990                 this.setStreet(null);
991                 this.setHouseNumber(null);
992                 this.setHouseNumberExtension(null);
993                 this.setZipCode(null);
994                 this.setCity(null);
995                 this.setCountry(null);
996
997                 // - contact data
998                 this.setEmailAddress(null);
999                 this.setEmailAddressRepeat(null);
1000                 this.setPhoneAreaCode(null);
1001                 this.setPhoneCountry(null);
1002                 this.setPhoneNumber(null);
1003                 this.setCellphoneCarrier(null);
1004                 this.setCellphoneNumber(null);
1005                 this.setFaxAreaCode(null);
1006                 this.setFaxCountry(null);
1007                 this.setFaxNumber(null);
1008
1009                 // - other data
1010                 this.setBirthday(null);
1011                 this.setComment(null);
1012         }
1013
1014         /**
1015          * Copies given contact into the controller
1016          * <p>
1017          * @param contact Contact instance
1018          */
1019         private void copyContact (final Contact contact) {
1020                 // Is the instance valid?
1021                 if (null == contact) {
1022                         // Throw NPE
1023                         throw new NullPointerException("contact is null"); //NOI18N
1024                 } else if (contact.getContactId() == null) {
1025                         // Throw NPE
1026                         throw new NullPointerException("contact.contactId is null"); //NOI18N
1027                 } else if (contact.getContactId() < 1) {
1028                         // Not valid id number
1029                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1030                 }
1031
1032                 // Copy all fields:
1033                 // - base data
1034                 this.setGender(contact.getContactGender());
1035                 this.setTitle(contact.getContactTitle());
1036                 this.setFirstName(contact.getContactFirstName());
1037                 this.setFamilyName(contact.getContactFamilyName());
1038                 this.setStreet(contact.getContactStreet());
1039                 this.setHouseNumber(contact.getContactHouseNumber());
1040                 this.setHouseNumberExtension(contact.getContactHouseNumberExtension());
1041                 this.setZipCode(contact.getContactZipCode());
1042                 this.setCity(contact.getContactCity());
1043                 this.setCountry(contact.getContactCountry());
1044                 this.setEmailAddress(contact.getContactEmailAddress());
1045                 this.setBirthday(contact.getContactBirthday());
1046                 this.setComment(contact.getContactComment());
1047
1048                 // Get cellphone, phone and fax instance
1049                 DialableCellphoneNumber cellphone = contact.getContactCellphoneNumber();
1050                 DialableFaxNumber fax = contact.getContactFaxNumber();
1051                 DialableLandLineNumber phone = contact.getContactLandLineNumber();
1052
1053                 // - contact data
1054                 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneAreaCode() > 0)) {
1055                         this.setPhoneCountry(phone.getPhoneCountry());
1056                         this.setPhoneAreaCode(phone.getPhoneAreaCode());
1057                         this.setPhoneNumber(phone.getPhoneNumber());
1058                 }
1059
1060                 if ((cellphone instanceof DialableCellphoneNumber) && (cellphone.getCellphoneProvider() instanceof MobileProvider)) {
1061                         this.setCellphoneCarrier(cellphone.getCellphoneProvider());
1062                         this.setCellphoneNumber(cellphone.getPhoneNumber());
1063                 }
1064
1065                 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneAreaCode() > 0)) {
1066                         this.setFaxCountry(fax.getPhoneCountry());
1067                         this.setFaxAreaCode(fax.getPhoneAreaCode());
1068                         this.setFaxNumber(fax.getPhoneNumber());
1069                 }
1070         }
1071
1072         /**
1073          * Removes given contact from all lists
1074          * <p>
1075          * @param contact Contact instance to remove
1076          */
1077         private void removeContact (final Contact contact) {
1078                 // Is the instance valid?
1079                 if (null == contact) {
1080                         // Throw NPE
1081                         throw new NullPointerException("contact is null"); //NOI18N
1082                 } else if (contact.getContactId() == null) {
1083                         // Throw NPE
1084                         throw new NullPointerException("contact.contactId is null"); //NOI18N
1085                 } else if (contact.getContactId() < 1) {
1086                         // Not valid id number
1087                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1088                 }
1089
1090                 // Remove from general list
1091                 if (!this.contactList.remove(contact)) {
1092                         // Did not remove contact
1093                         throw new IllegalStateException(MessageFormat.format("contact {0} was not removed.", contact.getContactId())); //NOI18N
1094                 }
1095
1096                 // Remove from other lists
1097                 this.emailAddressList.remove(contact.getContactEmailAddress());
1098         }
1099
1100         /**
1101          * Adds unique instance to contact list. First any existing instance is
1102          * being removed, then the new instance is added.
1103          * <p>
1104          * @param contact Contact instance to add uniquely
1105          */
1106         private void uniqueAddContact (final Contact contact) {
1107                 // Is the instance valid?
1108                 if (null == contact) {
1109                         // Throw NPE
1110                         throw new NullPointerException("contact is null"); //NOI18N
1111                 } else if (contact.getContactId() == null) {
1112                         // Throw NPE
1113                         throw new NullPointerException("contact.contactId is null"); //NOI18N
1114                 } else if (contact.getContactId() < 1) {
1115                         // Not valid id number
1116                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1117                 }
1118
1119                 // Get iterator from list
1120                 Iterator<Contact> iterator = this.contactList.iterator();
1121
1122                 // "Walk" through all entries
1123                 while (iterator.hasNext()) {
1124                         // Get next element
1125                         Contact next = iterator.next();
1126
1127                         // Is id number the same?
1128                         if (Objects.equals(contact.getContactId(), next.getContactId())) {
1129                                 // Found entry, so remove it and abort
1130                                 this.removeContact(next);
1131                                 break;
1132                         }
1133                 }
1134
1135                 // Add contact to list
1136                 this.contactList.add(contact);
1137         }
1138
1139 }