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