]> git.mxchange.org Git - addressbook-war.git/blob - src/java/org/mxchange/addressbook/beans/contact/AddressbookContactWebSessionBean.java
49c7ea75a4b140c3deb2e636bd27ccbb06fbb3e0
[addressbook-war.git] / src / java / org / mxchange / addressbook / beans / contact / AddressbookContactWebSessionBean.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.addressbook.beans.contact;
18
19 import java.text.MessageFormat;
20 import java.util.Date;
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.Objects;
24 import javax.annotation.PostConstruct;
25 import javax.enterprise.context.SessionScoped;
26 import javax.enterprise.event.Observes;
27 import javax.faces.view.facelets.FaceletException;
28 import javax.inject.Inject;
29 import javax.inject.Named;
30 import javax.naming.Context;
31 import javax.naming.InitialContext;
32 import javax.naming.NamingException;
33 import org.mxchange.addressbook.beans.login.AddressbookUserLoginWebSessionController;
34 import org.mxchange.jcontacts.contact.Contact;
35 import org.mxchange.jcontacts.contact.ContactSessionBeanRemote;
36 import org.mxchange.jcontacts.contact.UserContact;
37 import org.mxchange.jcontacts.contact.gender.Gender;
38 import org.mxchange.jcontacts.contact.utils.ContactUtils;
39 import org.mxchange.jcontacts.events.contact.update.AdminUpdatedContactEvent;
40 import org.mxchange.jcontacts.exceptions.ContactNotFoundException;
41 import org.mxchange.jcountry.data.Country;
42 import org.mxchange.jphone.phonenumbers.cellphone.CellphoneNumber;
43 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
44 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
45 import org.mxchange.jphone.phonenumbers.fax.FaxNumber;
46 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
47 import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
48 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
49 import org.mxchange.jusercore.events.login.UserLoggedInEvent;
50 import org.mxchange.jusercore.events.registration.UserRegisteredEvent;
51 import org.mxchange.jusercore.events.user.add.AdminAddedUserEvent;
52 import org.mxchange.jusercore.exceptions.UserPasswordMismatchException;
53
54 /**
55  * A general contact bean (controller)
56  * <p>
57  * @author Roland Haeder<roland@mxchange.org>
58  */
59 @Named ("contactController")
60 @SessionScoped
61 public class AddressbookContactWebSessionBean implements AddressbookContactWebSessionController {
62
63         /**
64          * Serial number
65          */
66         private static final long serialVersionUID = 542_145_347_916L;
67
68         /**
69          * Birth day
70          */
71         private Date birthday;
72
73         /**
74          * Cellphone number's carrier
75          */
76         private MobileProvider cellphoneCarrier;
77
78         /**
79          * Cellphone number
80          */
81         private Long cellphoneNumber;
82
83         /**
84          * City
85          */
86         private String city;
87
88         /**
89          * Optional comments
90          */
91         private String comment;
92
93         /**
94          * Remote contact bean
95          */
96         private final ContactSessionBeanRemote contactBean;
97
98         /**
99          * Contact list
100          */
101         private List<Contact> contactList;
102
103         /**
104          * Country instance
105          */
106         private Country country;
107
108         /**
109          * Email address
110          */
111         private String emailAddress;
112
113         /**
114          * Email address list
115          */
116         private List<String> emailAddressList;
117
118         /**
119          * Email address repeated
120          */
121         private String emailAddressRepeat;
122
123         /**
124          * Family name
125          */
126         private String familyName;
127
128         /**
129          * Fax number's area code
130          */
131         private Integer faxAreaCode;
132
133         /**
134          * Country instance for fax number
135          */
136         private Country faxCountry;
137
138         /**
139          * Fax number
140          */
141         private Long faxNumber;
142
143         /**
144          * First name
145          */
146         private String firstName;
147
148         /**
149          * Gender instance
150          */
151         private Gender gender;
152
153         /**
154          * House number
155          */
156         private Short houseNumber;
157
158         /**
159          * Whether a cellphone entry has been unlinked
160          */
161         private boolean isCellphoneUnlinked;
162
163         /**
164          * Whether a fax entry has been unlinked
165          */
166         private boolean isFaxUnlinked;
167
168         /**
169          * Whether a land-line number has been unlinked
170          */
171         private boolean isLandLineUnlinked;
172
173         /**
174          * Login bean (controller)
175          */
176         @Inject
177         private AddressbookUserLoginWebSessionController loginController;
178
179         /**
180          * Phone number area code
181          */
182         private Integer phoneAreaCode;
183
184         /**
185          * Country instance for phone number
186          */
187         private Country phoneCountry;
188
189         /**
190          * Phone number
191          */
192         private Long phoneNumber;
193
194         /**
195          * Street
196          */
197         private String street;
198
199         /**
200          * ZIP code
201          */
202         private Integer zipCode;
203
204         /**
205          * Default constructor
206          */
207         public AddressbookContactWebSessionBean () {
208                 // Try it
209                 try {
210                         // Get initial context
211                         Context context = new InitialContext();
212
213                         // Try to lookup
214                         this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/jratecalc-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
215                 } catch (final NamingException e) {
216                         // Throw again
217                         throw new FaceletException(e);
218                 }
219         }
220
221         @Override
222         public void afterAdminAddedContact (@Observes final AdminAddedContactEvent event) {
223                 // The event must be valid
224                 if (null == event) {
225                         // Throw NPE
226                         throw new NullPointerException("event is null"); //NOI18N
227                 } else if (event.getAddedContact() == null) {
228                         // Throw again ...
229                         throw new NullPointerException("event.addedContact is null"); //NOI18N
230                 } else if (event.getAddedContact().getContactId() == null) {
231                         // ... and again
232                         throw new NullPointerException("event.addedContact.customerId is null"); //NOI18N
233                 } else if (event.getAddedContact().getContactId() < 1) {
234                         // Not valid
235                         throw new IllegalArgumentException(MessageFormat.format("event.addedContact.customerId={0} is not valid", event.getAddedContact().getContactId())); //NOI18N //NOI18N
236                 }
237
238                 // Clear this bean
239                 this.clear();
240
241                 // Call other method
242                 this.contactList.add(event.getAddedContact());
243         }
244
245         @Override
246         public void afterAdminAddedUserEvent (@Observes final AdminAddedUserEvent event) {
247                 // Trace message
248                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterAdminAddedUserEvent: event={0} - CALLED!", event)); //NOI18N
249
250                 // event should not be null
251                 if (null == event) {
252                         // Throw NPE
253                         throw new NullPointerException("event is null"); //NOI18N
254                 } else if (event.getAddedUser() == null) {
255                         // Throw NPE again
256                         throw new NullPointerException("event.addedUser is null"); //NOI18N
257                 } else if (event.getAddedUser().getUserId() == null) {
258                         // userId is null
259                         throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
260                 } else if (event.getAddedUser().getUserId() < 1) {
261                         // Not avalid id
262                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
263                 }
264
265                 // Clear all data
266                 this.clear();
267
268                 // Trace message
269                 //* NOISY-DEBUG: */ System.out.println("UserWebBean:afterAdminAddedUserEvent: EXIT!"); //NOI18N
270         }
271
272         @Override
273         public void afterAdminUpdatedContactDataEvent (@Observes final AdminUpdatedContactEvent event) {
274                 // Trace message
275                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ContactWebBean:afterAdminUpdatedContactDataEvent: event={0} - CALLED!", event)); //NOI18N
276
277                 // event should not be null
278                 if (null == event) {
279                         // Throw NPE
280                         throw new NullPointerException("event is null"); //NOI18N
281                 } else if (event.getUpdatedContact() == null) {
282                         // Throw NPE again
283                         throw new NullPointerException("event.user is null"); //NOI18N
284                 } else if (event.getUpdatedContact().getContactId() == null) {
285                         // userId is null
286                         throw new NullPointerException("event.user.userId is null"); //NOI18N
287                 } else if (event.getUpdatedContact().getContactId() < 1) {
288                         // Not avalid id
289                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
290                 }
291
292                 // Get iterator from list
293                 Iterator<Contact> iterator = this.contactList.iterator();
294
295                 // "Walk" through all entries
296                 while (iterator.hasNext()) {
297                         // Get next element
298                         Contact next = iterator.next();
299
300                         // Is id number the same?
301                         if (Objects.equals(event.getUpdatedContact().getContactId(), next.getContactId())) {
302                                 // Found entry, so remove it and abort
303                                 this.contactList.remove(next);
304
305                                 // Remove also email from list
306                                 this.emailAddressList.remove(next.getContactEmailAddress());
307                                 break;
308                         }
309                 }
310
311                 // Add contact to list
312                 this.contactList.add(event.getUpdatedContact());
313
314                 // Add email address to list
315                 this.emailAddressList.add(event.getUpdatedContact().getContactEmailAddress());
316         }
317
318         @Override
319         public void afterRegistrationEvent (final @Observes UserRegisteredEvent event) {
320                 // Trace message
321                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ContactWebBean:afterRegistration: event={0} - CALLED!", event)); //NOI18N
322
323                 // event should not be null
324                 if (null == event) {
325                         // Throw NPE
326                         throw new NullPointerException("event is null"); //NOI18N
327                 } else if (event.getRegisteredUser() == null) {
328                         // Throw NPE again
329                         throw new NullPointerException("event.user is null"); //NOI18N
330                 } else if (event.getRegisteredUser().getUserId() == null) {
331                         // userId is null
332                         throw new NullPointerException("event.user.userId is null"); //NOI18N
333                 } else if (event.getRegisteredUser().getUserId() < 1) {
334                         // Not avalid id
335                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
336                 }
337
338                 // Get user instance
339                 Contact registeredContact = event.getRegisteredUser().getUserContact();
340
341                 // Debug message
342                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ContactWebBean:afterRegistration: registeredContact={0}", registeredContact)); //NOI18N
343
344                 // Copy all data from registered->user
345                 this.copyContact(registeredContact);
346
347                 // Add user name and email address
348                 this.addUserNameEmailAddress(registeredContact);
349
350                 // Clear all data
351                 this.clear();
352
353                 // Trace message
354                 //* NOISY-DEBUG: */ System.out.println("ContactWebBean:afterRegistration: EXIT!"); //NOI18N
355         }
356
357         @Override
358         public void afterUserLogin (final @Observes UserLoggedInEvent event) {
359                 // Trace message
360                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ContactWebBean:afterUserLogin: event={0} - CALLED!", event)); //NOI18N
361
362                 // event should not be null
363                 if (null == event) {
364                         // Throw NPE
365                         throw new NullPointerException("event is null"); //NOI18N
366                 } else if (event.getLoggedInUser() == null) {
367                         // Throw NPE again
368                         throw new NullPointerException("event.user is null"); //NOI18N
369                 } else if (event.getLoggedInUser().getUserId() == null) {
370                         // userId is null
371                         throw new NullPointerException("event.user.userId is null"); //NOI18N
372                 } else if (event.getLoggedInUser().getUserId() < 1) {
373                         // Not avalid id
374                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
375                 }
376
377                 // Copy all data to this bean
378                 this.copyContact(event.getLoggedInUser().getUserContact());
379
380                 // Trace message
381                 //* NOISY-DEBUG: */ System.out.println("ContactWebBean:afterUserLogin - EXIT!"); //NOI18N
382         }
383
384         @Override
385         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
386         public List<Contact> allContacts () {
387                 return this.contactList;
388         }
389
390         @Override
391         public Contact createContactInstance () {
392                 // User message
393                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createContactInstance: CALLED!", this.getClass().getSimpleName()));
394
395                 // Is all required data set?
396                 if (!this.isRequiredPersonalDataSet()) {
397                         // No, then abort here
398                         throw new FaceletException(new IllegalArgumentException("Not all personal data is set, but createContactInstance() is called.")); //NOI18N
399                 }
400
401                 // Required personal data must be set
402                 assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N
403
404                 // Create new contact instance
405                 Contact localContact = new UserContact();
406
407                 // Generate phone number
408                 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
409                 DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
410                 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
411
412                 // Create new contact
413                 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
414                 contact.setContactStreet(this.getStreet());
415                 contact.setContactHouseNumber(this.getHouseNumber());
416                 contact.setContactZipCode(this.getZipCode());
417                 contact.setContactCity(this.getCity());
418                 contact.setContactCountry(this.getCountry());
419                 contact.setContactEmailAddress(this.getEmailAddress());
420                 contact.setContactBirthday(this.getBirthday());
421                 contact.setContactComment(this.getComment());
422
423                 // Debug message
424                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createContactInstance: this.emailAddress={1}", this.getClass().getSimpleName(), this.getEmailAddress()));
425                 // Don't set null or wrong references
426                 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getPhoneAreaCode() != null) && (this.getPhoneNumber() != null) && (this.getPhoneAreaCode() > 0) && (this.getPhoneNumber() > 0)) {
427                         // Now the number must be given
428                         if (phone.getPhoneAreaCode() == null) {
429                                 // Is null
430                                 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
431                         } else if (phone.getPhoneAreaCode() < 1) {
432                                 // Abort here
433                                 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
434                         } else if (phone.getPhoneNumber() == null) {
435                                 // Is null
436                                 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
437                         } else if (phone.getPhoneNumber() < 1) {
438                                 // Abort here
439                                 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
440                         }
441
442                         // Set phone number
443                         contact.setContactLandLineNumber(phone);
444                 }
445
446                 // Don't set null or wrong references
447                 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
448                         // Now the number must be given
449                         if (fax.getPhoneAreaCode() == null) {
450                                 // Is null
451                                 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
452                         } else if (fax.getPhoneAreaCode() < 1) {
453                                 // Abort here
454                                 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
455                         } else if (fax.getPhoneNumber() == null) {
456                                 // Is null
457                                 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
458                         } else if (fax.getPhoneNumber() < 1) {
459                                 // Abort here
460                                 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
461                         }
462
463                         // Set fax number
464                         contact.setContactFaxNumber(fax);
465                 }
466
467                 // Is the provider set?
468                 if ((cellphone instanceof DialableCellphoneNumber) && (this.getCellphoneCarrier() instanceof MobileProvider) && (this.getCellphoneNumber() != null) && (this.getCellphoneNumber() > 0)) {
469                         // Is the number set?
470                         if (cellphone.getPhoneNumber() == null) {
471                                 // Is null
472                                 throw new NullPointerException("cellphone.phoneNumber is null"); //NOI18N
473                         } else if (cellphone.getPhoneNumber() < 1) {
474                                 // Abort here
475                                 throw new IllegalArgumentException("cellphone.phoneNumber is zero or below."); //NOI18N
476                         }
477
478                         // Set cellphone number
479                         contact.setContactCellphoneNumber(cellphone);
480                 }
481
482                 // Trace message
483                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createContactInstance: contact={1} - EXIT!", this.getClass().getSimpleName(), contact));
484
485                 // Return it
486                 return localContact;
487         }
488
489         @Override
490         public String doChangePersonalContactData () {
491                 // This method shall only be called if the user is logged-in
492                 if (!this.loginController.isUserLoggedIn()) {
493                         // Not logged-in
494                         throw new IllegalStateException("User is not logged-in"); //NOI18N
495                 } else if (!this.isRequiredChangePersonalDataSet()) {
496                         // Not all required fields are set
497                         throw new FaceletException("Not all required fields are set."); //NOI18N
498                 } else if (!this.loginController.ifCurrentPasswordMatches()) {
499                         // Password not matching
500                         throw new FaceletException(new UserPasswordMismatchException(this.loginController.getLoggedInUser()));
501                 }
502
503                 // Get contact instance
504                 Contact contact = this.loginController.getLoggedInUser().getUserContact();
505
506                 // It should be there, so run some tests on it
507                 assert (contact instanceof Contact) : "Instance loginController.loggedInUser.userContact is null"; //NOI18N
508                 assert (contact.getContactId() instanceof Long) : "Instance loginController.userContact.contactId is null"; //NOI18N
509                 assert (contact.getContactId() > 0) : MessageFormat.format("Instance loginController.userContact.contactId={0} is invalid", contact.getContactId()); //NOI18N
510
511                 // Update all fields
512                 contact.setContactGender(this.getGender());
513                 contact.setContactFirstName(this.getFirstName());
514                 contact.setContactFamilyName(this.getFamilyName());
515                 contact.setContactStreet(this.getStreet());
516                 contact.setContactHouseNumber(this.getHouseNumber());
517                 contact.setContactZipCode(this.getZipCode());
518                 contact.setContactCity(this.getCity());
519                 contact.setContactCountry(this.getCountry());
520
521                 // Update contact's cellphone number
522                 this.isCellphoneUnlinked = ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
523
524                 // Update contact's land-line number
525                 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
526
527                 // Update contact's fax number
528                 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
529
530                 // Send it to the EJB
531                 this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
532
533                 // All fine
534                 return "contact_data_saved"; //NOI18N
535         }
536
537         @Override
538         @SuppressWarnings ("ReturnOfDateField")
539         public Date getBirthday () {
540                 return this.birthday;
541         }
542
543         @Override
544         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
545         public void setBirthday (final Date birthday) {
546                 this.birthday = birthday;
547         }
548
549         @Override
550         public MobileProvider getCellphoneCarrier () {
551                 return this.cellphoneCarrier;
552         }
553
554         @Override
555         public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
556                 this.cellphoneCarrier = cellphoneCarrier;
557         }
558
559         @Override
560         public Long getCellphoneNumber () {
561                 return this.cellphoneNumber;
562         }
563
564         @Override
565         public void setCellphoneNumber (Long cellphoneNumber) {
566                 this.cellphoneNumber = cellphoneNumber;
567         }
568
569         @Override
570         public String getCity () {
571                 return this.city;
572         }
573
574         @Override
575         public void setCity (final String city) {
576                 this.city = city;
577         }
578
579         @Override
580         public String getComment () {
581                 return this.comment;
582         }
583
584         @Override
585         public void setComment (final String comment) {
586                 this.comment = comment;
587         }
588
589         @Override
590         public Country getCountry () {
591                 return this.country;
592         }
593
594         @Override
595         public void setCountry (final Country country) {
596                 this.country = country;
597         }
598
599         @Override
600         public String getEmailAddress () {
601                 return this.emailAddress;
602         }
603
604         @Override
605         public void setEmailAddress (final String emailAddress) {
606                 this.emailAddress = emailAddress;
607         }
608
609         @Override
610         public String getEmailAddressRepeat () {
611                 return this.emailAddressRepeat;
612         }
613
614         @Override
615         public void setEmailAddressRepeat (final String emailAddressRepeat) {
616                 this.emailAddressRepeat = emailAddressRepeat;
617         }
618
619         @Override
620         public String getFamilyName () {
621                 return this.familyName;
622         }
623
624         @Override
625         public void setFamilyName (final String familyName) {
626                 this.familyName = familyName;
627         }
628
629         @Override
630         public Integer getFaxAreaCode () {
631                 return this.faxAreaCode;
632         }
633
634         @Override
635         public void setFaxAreaCode (final Integer faxAreaCode) {
636                 this.faxAreaCode = faxAreaCode;
637         }
638
639         @Override
640         public Country getFaxCountry () {
641                 return this.faxCountry;
642         }
643
644         @Override
645         public void setFaxCountry (final Country faxCountry) {
646                 this.faxCountry = faxCountry;
647         }
648
649         @Override
650         public Long getFaxNumber () {
651                 return this.faxNumber;
652         }
653
654         @Override
655         public void setFaxNumber (final Long faxNumber) {
656                 this.faxNumber = faxNumber;
657         }
658
659         @Override
660         public String getFirstName () {
661                 return this.firstName;
662         }
663
664         @Override
665         public void setFirstName (final String firstName) {
666                 this.firstName = firstName;
667         }
668
669         @Override
670         public Gender getGender () {
671                 return this.gender;
672         }
673
674         @Override
675         public void setGender (final Gender gender) {
676                 this.gender = gender;
677         }
678
679         @Override
680         public Short getHouseNumber () {
681                 return this.houseNumber;
682         }
683
684         @Override
685         public void setHouseNumber (final Short houseNumber) {
686                 this.houseNumber = houseNumber;
687         }
688
689         @Override
690         public Integer getPhoneAreaCode () {
691                 return this.phoneAreaCode;
692         }
693
694         @Override
695         public void setPhoneAreaCode (final Integer phoneAreaCode) {
696                 this.phoneAreaCode = phoneAreaCode;
697         }
698
699         @Override
700         public Country getPhoneCountry () {
701                 return this.phoneCountry;
702         }
703
704         @Override
705         public void setPhoneCountry (final Country phoneCountry) {
706                 this.phoneCountry = phoneCountry;
707         }
708
709         @Override
710         public Long getPhoneNumber () {
711                 return this.phoneNumber;
712         }
713
714         @Override
715         public void setPhoneNumber (final Long phoneNumber) {
716                 this.phoneNumber = phoneNumber;
717         }
718
719         @Override
720         public String getStreet () {
721                 return this.street;
722         }
723
724         @Override
725         public void setStreet (final String street) {
726                 this.street = street;
727         }
728
729         @Override
730         public Integer getZipCode () {
731                 return this.zipCode;
732         }
733
734         @Override
735         public void setZipCode (final Integer zipCode) {
736                 this.zipCode = zipCode;
737         }
738
739         @Override
740         public boolean hasContacts () {
741                 return (!this.allContacts().isEmpty());
742         }
743
744         /**
745          * Post-initialization of this class
746          */
747         @PostConstruct
748         public void init () {
749                 // Get full email address list for reducing EJB calls
750                 this.emailAddressList = this.contactBean.getEmailAddressList();
751
752                 // Get full contact list
753                 this.contactList = this.contactBean.getAllContacts();
754         }
755
756         @Override
757         public boolean isEmailAddressRegistered (final Contact contact) {
758                 return ((this.emailAddressList instanceof List) && (this.emailAddressList.contains(contact.getContactEmailAddress())));
759         }
760
761         @Override
762         public boolean isRequiredChangePersonalDataSet () {
763                 return ((this.getGender() != null) &&
764                                 (this.getFirstName() != null) &&
765                                 (this.getFamilyName() != null) &&
766                                 (this.getStreet() != null) &&
767                                 (this.getHouseNumber() != null) &&
768                                 (this.getZipCode() != null) &&
769                                 (this.getCity() != null));
770         }
771
772         @Override
773         public boolean isRequiredPersonalDataSet () {
774                 return ((this.getGender() != null) &&
775                                 (this.getFirstName() != null) &&
776                                 (this.getFamilyName() != null) &&
777                                 (this.getStreet() != null) &&
778                                 (this.getHouseNumber() != null) &&
779                                 (this.getZipCode() != null) &&
780                                 (this.getCity() != null) &&
781                                 (this.getEmailAddress() != null) &&
782                                 (this.getEmailAddressRepeat() != null));
783         }
784
785         @Override
786         public boolean isSameEmailAddressEntered () {
787                 return (Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat()));
788         }
789
790         @Override
791         public Contact lookupContactById (final Long contactId) throws ContactNotFoundException {
792                 // Init variable
793                 Contact localContact = null;
794
795                 // Clear this bean
796                 this.clear();
797
798                 // Try to lookup it in visible user list
799                 for (final Iterator<Contact> iterator = this.contactList.iterator(); iterator.hasNext();) {
800                         // Get next user
801                         Contact next = iterator.next();
802
803                         // Is the user id found?
804                         if (Objects.equals(next.getContactId(), contactId)) {
805                                 // Copy to other variable
806                                 localContact = next;
807                                 break;
808                         }
809                 }
810
811                 // Is it still null?
812                 if (null == localContact) {
813                         // Not visible for the current user
814                         throw new ContactNotFoundException(contactId);
815                 }
816
817                 // Copy all data to this bean
818                 this.copyContact(localContact);
819
820                 // Return it
821                 return localContact;
822         }
823
824         @Override
825         public void updateContactDataFromController (final Contact contact) {
826                 // Is the instance valid?
827                 if (null == contact) {
828                         // Throw NPE
829                         throw new NullPointerException("contact is null"); //NOI18N
830                 } else if (contact.getContactId() == null) {
831                         // Throw NPE
832                         throw new NullPointerException("contact.contactId is null"); //NOI18N
833                 } else if (contact.getContactId() < 1) {
834                         // Not valid id number
835                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
836                 }
837
838                 // Set all
839                 this.copyContact(contact);
840         }
841
842         /**
843          * Adds email address to bean's internal list.
844          * <p>
845          * @param contact Contact instance
846          */
847         private void addUserNameEmailAddress (final Contact contact) {
848                 // Make sure the entry is not added yet
849                 if (this.emailAddressList.contains(contact.getContactEmailAddress())) {
850                         // Already added
851                         throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", contact.getContactEmailAddress())); //NOI18N
852                 }
853
854                 // Add email addres
855                 this.emailAddressList.add(contact.getContactEmailAddress());
856         }
857
858         /**
859          * Clears this bean
860          */
861         private void clear () {
862                 // Clear all data
863                 // - personal data
864                 this.setGender(Gender.UNKNOWN);
865                 this.setFirstName(null);
866                 this.setFamilyName(null);
867                 this.setStreet(null);
868                 this.setHouseNumber(null);
869                 this.setZipCode(null);
870                 this.setCity(null);
871                 this.setCountry(null);
872
873                 // - contact data
874                 this.setEmailAddress(null);
875                 this.setEmailAddressRepeat(null);
876                 this.setPhoneAreaCode(null);
877                 this.setCellphoneCarrier(null);
878                 this.setFaxAreaCode(null);
879
880                 // - other data
881                 this.setBirthday(null);
882                 this.setComment(null);
883         }
884
885         /**
886          * Copies given contact into the controller
887          * <p>
888          * @param contact Contact instance
889          */
890         private void copyContact (final Contact contact) {
891                 // Is the instance valid?
892                 if (null == contact) {
893                         // Throw NPE
894                         throw new NullPointerException("contact is null"); //NOI18N
895                 } else if (contact.getContactId() == null) {
896                         // Throw NPE
897                         throw new NullPointerException("contact.contactId is null"); //NOI18N
898                 } else if (contact.getContactId() < 1) {
899                         // Not valid id number
900                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
901                 }
902
903                 // Copy all fields:
904                 // - base data
905                 this.setGender(contact.getContactGender());
906                 this.setFirstName(contact.getContactFirstName());
907                 this.setFamilyName(contact.getContactFamilyName());
908                 this.setStreet(contact.getContactStreet());
909                 this.setHouseNumber(contact.getContactHouseNumber());
910                 this.setZipCode(contact.getContactZipCode());
911                 this.setCity(contact.getContactCity());
912                 this.setCountry(contact.getContactCountry());
913
914                 // Get cellphone, phone and fax instance
915                 DialableCellphoneNumber cellphone = contact.getContactCellphoneNumber();
916                 DialableFaxNumber fax = contact.getContactFaxNumber();
917                 DialableLandLineNumber phone = contact.getContactLandLineNumber();
918
919                 // - contact data
920                 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneAreaCode() > 0)) {
921                         this.setPhoneCountry(phone.getPhoneCountry());
922                         this.setPhoneAreaCode(phone.getPhoneAreaCode());
923                         this.setPhoneNumber(phone.getPhoneNumber());
924                 }
925                 if ((cellphone instanceof DialableCellphoneNumber) && (cellphone.getCellphoneProvider() instanceof MobileProvider)) {
926                         this.setCellphoneCarrier(cellphone.getCellphoneProvider());
927                         this.setCellphoneNumber(cellphone.getPhoneNumber());
928                 }
929                 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneAreaCode() > 0)) {
930                         this.setFaxCountry(fax.getPhoneCountry());
931                         this.setFaxAreaCode(fax.getPhoneAreaCode());
932                         this.setFaxNumber(fax.getPhoneNumber());
933                 }
934                 this.setEmailAddress(contact.getContactEmailAddress());
935
936                 // -- other data
937                 this.setBirthday(contact.getContactBirthday());
938                 this.setComment(contact.getContactComment());
939         }
940
941         /**
942          * Removes given contact from all lists
943          * <p>
944          * @param contact Contact instance to remove
945          */
946         private void removeContact (final Contact contact) {
947                 // Is the instance valid?
948                 if (null == contact) {
949                         // Throw NPE
950                         throw new NullPointerException("contact is null"); //NOI18N
951                 } else if (contact.getContactId() == null) {
952                         // Throw NPE
953                         throw new NullPointerException("contact.contactId is null"); //NOI18N
954                 } else if (contact.getContactId() < 1) {
955                         // Not valid id number
956                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
957                 }
958
959                 // Remove from general list
960                 if (!this.contactList.remove(contact)) {
961                         // Did not remove contact
962                         throw new IllegalStateException(MessageFormat.format("contact {0} was not removed.", contact.getContactId())); //NOI18N
963                 }
964
965                 // Remove from other lists
966                 this.emailAddressList.remove(contact.getContactEmailAddress());
967         }
968
969 }