]> git.mxchange.org Git - addressbook-war.git/blob - src/java/org/mxchange/addressbook/beans/contact/AddressbookContactWebSessionBean.java
No, putting these methods into admin (request-scoped) controller is not good as no...
[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 addEmailAddress (final String contactEmailAddress) {
223                 // Add it
224                 this.emailAddressList.add(contactEmailAddress);
225         }
226
227         @Override
228         public void afterAdminAddedUserEvent (@Observes final AdminAddedUserEvent event) {
229                 // Trace message
230                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterAdminAddedUserEvent: event={0} - CALLED!", event)); //NOI18N
231
232                 // event should not be null
233                 if (null == event) {
234                         // Throw NPE
235                         throw new NullPointerException("event is null"); //NOI18N
236                 } else if (event.getAddedUser() == null) {
237                         // Throw NPE again
238                         throw new NullPointerException("event.addedUser is null"); //NOI18N
239                 } else if (event.getAddedUser().getUserId() == null) {
240                         // userId is null
241                         throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
242                 } else if (event.getAddedUser().getUserId() < 1) {
243                         // Not avalid id
244                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
245                 }
246
247                 // Clear all data
248                 this.clear();
249
250                 // Trace message
251                 //* NOISY-DEBUG: */ System.out.println("UserWebBean:afterAdminAddedUserEvent: EXIT!"); //NOI18N
252         }
253
254         @Override
255         public void afterAdminUpdatedContactDataEvent (@Observes final AdminUpdatedContactEvent event) {
256                 // Trace message
257                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ContactWebBean:afterAdminUpdatedContactDataEvent: event={0} - CALLED!", event)); //NOI18N
258
259                 // event should not be null
260                 if (null == event) {
261                         // Throw NPE
262                         throw new NullPointerException("event is null"); //NOI18N
263                 } else if (event.getUpdatedContact() == null) {
264                         // Throw NPE again
265                         throw new NullPointerException("event.user is null"); //NOI18N
266                 } else if (event.getUpdatedContact().getContactId() == null) {
267                         // userId is null
268                         throw new NullPointerException("event.user.userId is null"); //NOI18N
269                 } else if (event.getUpdatedContact().getContactId() < 1) {
270                         // Not avalid id
271                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
272                 }
273
274                 // Get iterator from list
275                 Iterator<Contact> iterator = this.contactList.iterator();
276
277                 // "Walk" through all entries
278                 while (iterator.hasNext()) {
279                         // Get next element
280                         Contact next = iterator.next();
281
282                         // Is id number the same?
283                         if (Objects.equals(event.getUpdatedContact().getContactId(), next.getContactId())) {
284                                 // Found entry, so remove it and abort
285                                 this.contactList.remove(next);
286
287                                 // Remove also email from list
288                                 this.emailAddressList.remove(next.getContactEmailAddress());
289                                 break;
290                         }
291                 }
292
293                 // Add contact to list
294                 this.contactList.add(event.getUpdatedContact());
295
296                 // Add email address to list
297                 this.emailAddressList.add(event.getUpdatedContact().getContactEmailAddress());
298         }
299
300         @Override
301         public void afterRegistrationEvent (final @Observes UserRegisteredEvent event) {
302                 // Trace message
303                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ContactWebBean:afterRegistration: event={0} - CALLED!", event)); //NOI18N
304
305                 // event should not be null
306                 if (null == event) {
307                         // Throw NPE
308                         throw new NullPointerException("event is null"); //NOI18N
309                 } else if (event.getRegisteredUser() == null) {
310                         // Throw NPE again
311                         throw new NullPointerException("event.user is null"); //NOI18N
312                 } else if (event.getRegisteredUser().getUserId() == null) {
313                         // userId is null
314                         throw new NullPointerException("event.user.userId is null"); //NOI18N
315                 } else if (event.getRegisteredUser().getUserId() < 1) {
316                         // Not avalid id
317                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
318                 }
319
320                 // Get user instance
321                 Contact registeredContact = event.getRegisteredUser().getUserContact();
322
323                 // Debug message
324                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ContactWebBean:afterRegistration: registeredContact={0}", registeredContact)); //NOI18N
325
326                 // Copy all data from registered->user
327                 this.copyContact(registeredContact);
328
329                 // Add user name and email address
330                 this.addUserNameEmailAddress(registeredContact);
331
332                 // Clear all data
333                 this.clear();
334
335                 // Trace message
336                 //* NOISY-DEBUG: */ System.out.println("ContactWebBean:afterRegistration: EXIT!"); //NOI18N
337         }
338
339         @Override
340         public void afterUserLogin (final @Observes UserLoggedInEvent event) {
341                 // Trace message
342                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ContactWebBean:afterUserLogin: event={0} - CALLED!", event)); //NOI18N
343
344                 // event should not be null
345                 if (null == event) {
346                         // Throw NPE
347                         throw new NullPointerException("event is null"); //NOI18N
348                 } else if (event.getLoggedInUser() == null) {
349                         // Throw NPE again
350                         throw new NullPointerException("event.user is null"); //NOI18N
351                 } else if (event.getLoggedInUser().getUserId() == null) {
352                         // userId is null
353                         throw new NullPointerException("event.user.userId is null"); //NOI18N
354                 } else if (event.getLoggedInUser().getUserId() < 1) {
355                         // Not avalid id
356                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
357                 }
358
359                 // Copy all data to this bean
360                 this.copyContact(event.getLoggedInUser().getUserContact());
361
362                 // Trace message
363                 //* NOISY-DEBUG: */ System.out.println("ContactWebBean:afterUserLogin - EXIT!"); //NOI18N
364         }
365
366         @Override
367         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
368         public List<Contact> allContacts () {
369                 return this.contactList;
370         }
371
372         @Override
373         public Contact createContactInstance () {
374                 // User message
375                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createContactInstance: CALLED!", this.getClass().getSimpleName()));
376
377                 // Is all required data set?
378                 if (!this.isRequiredPersonalDataSet()) {
379                         // No, then abort here
380                         throw new FaceletException(new IllegalArgumentException("Not all personal data is set, but createContactInstance() is called.")); //NOI18N
381                 }
382
383                 // Required personal data must be set
384                 assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N
385
386                 // Create new contact instance
387                 Contact localContact = new UserContact();
388
389                 // Generate phone number
390                 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
391                 DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
392                 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
393
394                 // Create new contact
395                 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
396                 contact.setContactStreet(this.getStreet());
397                 contact.setContactHouseNumber(this.getHouseNumber());
398                 contact.setContactZipCode(this.getZipCode());
399                 contact.setContactCity(this.getCity());
400                 contact.setContactCountry(this.getCountry());
401                 contact.setContactEmailAddress(this.getEmailAddress());
402                 contact.setContactBirthday(this.getBirthday());
403                 contact.setContactComment(this.getComment());
404
405                 // Debug message
406                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createContactInstance: this.emailAddress={1}", this.getClass().getSimpleName(), this.getEmailAddress()));
407                 // Don't set null or wrong references
408                 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getPhoneAreaCode() != null) && (this.getPhoneNumber() != null) && (this.getPhoneAreaCode() > 0) && (this.getPhoneNumber() > 0)) {
409                         // Now the number must be given
410                         if (phone.getPhoneAreaCode() == null) {
411                                 // Is null
412                                 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
413                         } else if (phone.getPhoneAreaCode() < 1) {
414                                 // Abort here
415                                 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
416                         } else if (phone.getPhoneNumber() == null) {
417                                 // Is null
418                                 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
419                         } else if (phone.getPhoneNumber() < 1) {
420                                 // Abort here
421                                 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
422                         }
423
424                         // Set phone number
425                         contact.setContactLandLineNumber(phone);
426                 }
427
428                 // Don't set null or wrong references
429                 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
430                         // Now the number must be given
431                         if (fax.getPhoneAreaCode() == null) {
432                                 // Is null
433                                 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
434                         } else if (fax.getPhoneAreaCode() < 1) {
435                                 // Abort here
436                                 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
437                         } else if (fax.getPhoneNumber() == null) {
438                                 // Is null
439                                 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
440                         } else if (fax.getPhoneNumber() < 1) {
441                                 // Abort here
442                                 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
443                         }
444
445                         // Set fax number
446                         contact.setContactFaxNumber(fax);
447                 }
448
449                 // Is the provider set?
450                 if ((cellphone instanceof DialableCellphoneNumber) && (this.getCellphoneCarrier() instanceof MobileProvider) && (this.getCellphoneNumber() != null) && (this.getCellphoneNumber() > 0)) {
451                         // Is the number set?
452                         if (cellphone.getPhoneNumber() == null) {
453                                 // Is null
454                                 throw new NullPointerException("cellphone.phoneNumber is null"); //NOI18N
455                         } else if (cellphone.getPhoneNumber() < 1) {
456                                 // Abort here
457                                 throw new IllegalArgumentException("cellphone.phoneNumber is zero or below."); //NOI18N
458                         }
459
460                         // Set cellphone number
461                         contact.setContactCellphoneNumber(cellphone);
462                 }
463
464                 // Trace message
465                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createContactInstance: contact={1} - EXIT!", this.getClass().getSimpleName(), contact));
466                 // Created timestamp and ownContact
467                 contact.setContactOwnContact(Boolean.TRUE);
468
469                 // Trace message
470                 //this.getLogger().logTrace(MessageFormat.format("createContactInstance: localContact={0} - EXIT!", localContact));
471                 // Return it
472                 return localContact;
473         }
474
475         @Override
476         public String doChangePersonalContactData () {
477                 // This method shall only be called if the user is logged-in
478                 if (!this.loginController.isUserLoggedIn()) {
479                         // Not logged-in
480                         throw new IllegalStateException("User is not logged-in"); //NOI18N
481                 } else if (!this.isRequiredChangePersonalDataSet()) {
482                         // Not all required fields are set
483                         throw new FaceletException("Not all required fields are set."); //NOI18N
484                 } else if (!this.loginController.ifCurrentPasswordMatches()) {
485                         // Password not matching
486                         throw new FaceletException(new UserPasswordMismatchException(this.loginController.getLoggedInUser()));
487                 }
488
489                 // Get contact instance
490                 Contact contact = this.loginController.getLoggedInUser().getUserContact();
491
492                 // It should be there, so run some tests on it
493                 assert (contact instanceof Contact) : "Instance loginController.loggedInUser.userContact is null"; //NOI18N
494                 assert (contact.getContactId() instanceof Long) : "Instance loginController.userContact.contactId is null"; //NOI18N
495                 assert (contact.getContactId() > 0) : MessageFormat.format("Instance loginController.userContact.contactId={0} is invalid", contact.getContactId()); //NOI18N
496
497                 // Update all fields
498                 contact.setContactGender(this.getGender());
499                 contact.setContactFirstName(this.getFirstName());
500                 contact.setContactFamilyName(this.getFamilyName());
501                 contact.setContactStreet(this.getStreet());
502                 contact.setContactHouseNumber(this.getHouseNumber());
503                 contact.setContactZipCode(this.getZipCode());
504                 contact.setContactCity(this.getCity());
505                 contact.setContactCountry(this.getCountry());
506
507                 // Update contact's cellphone number
508                 this.isCellphoneUnlinked = ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
509
510                 // Update contact's land-line number
511                 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
512
513                 // Update contact's fax number
514                 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
515
516                 // Send it to the EJB
517                 this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
518
519                 // All fine
520                 return "contact_data_saved"; //NOI18N
521         }
522
523         @Override
524         @SuppressWarnings ("ReturnOfDateField")
525         public Date getBirthday () {
526                 return this.birthday;
527         }
528
529         @Override
530         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
531         public void setBirthday (final Date birthday) {
532                 this.birthday = birthday;
533         }
534
535         @Override
536         public MobileProvider getCellphoneCarrier () {
537                 return this.cellphoneCarrier;
538         }
539
540         @Override
541         public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
542                 this.cellphoneCarrier = cellphoneCarrier;
543         }
544
545         @Override
546         public Long getCellphoneNumber () {
547                 return this.cellphoneNumber;
548         }
549
550         @Override
551         public void setCellphoneNumber (Long cellphoneNumber) {
552                 this.cellphoneNumber = cellphoneNumber;
553         }
554
555         @Override
556         public String getCity () {
557                 return this.city;
558         }
559
560         @Override
561         public void setCity (final String city) {
562                 this.city = city;
563         }
564
565         @Override
566         public String getComment () {
567                 return this.comment;
568         }
569
570         @Override
571         public void setComment (final String comment) {
572                 this.comment = comment;
573         }
574
575         @Override
576         public Country getCountry () {
577                 return this.country;
578         }
579
580         @Override
581         public void setCountry (final Country country) {
582                 this.country = country;
583         }
584
585         @Override
586         public String getEmailAddress () {
587                 return this.emailAddress;
588         }
589
590         @Override
591         public void setEmailAddress (final String emailAddress) {
592                 this.emailAddress = emailAddress;
593         }
594
595         @Override
596         public String getEmailAddressRepeat () {
597                 return this.emailAddressRepeat;
598         }
599
600         @Override
601         public void setEmailAddressRepeat (final String emailAddressRepeat) {
602                 this.emailAddressRepeat = emailAddressRepeat;
603         }
604
605         @Override
606         public String getFamilyName () {
607                 return this.familyName;
608         }
609
610         @Override
611         public void setFamilyName (final String familyName) {
612                 this.familyName = familyName;
613         }
614
615         @Override
616         public Integer getFaxAreaCode () {
617                 return this.faxAreaCode;
618         }
619
620         @Override
621         public void setFaxAreaCode (final Integer faxAreaCode) {
622                 this.faxAreaCode = faxAreaCode;
623         }
624
625         @Override
626         public Country getFaxCountry () {
627                 return this.faxCountry;
628         }
629
630         @Override
631         public void setFaxCountry (final Country faxCountry) {
632                 this.faxCountry = faxCountry;
633         }
634
635         @Override
636         public Long getFaxNumber () {
637                 return this.faxNumber;
638         }
639
640         @Override
641         public void setFaxNumber (final Long faxNumber) {
642                 this.faxNumber = faxNumber;
643         }
644
645         @Override
646         public String getFirstName () {
647                 return this.firstName;
648         }
649
650         @Override
651         public void setFirstName (final String firstName) {
652                 this.firstName = firstName;
653         }
654
655         @Override
656         public Gender getGender () {
657                 return this.gender;
658         }
659
660         @Override
661         public void setGender (final Gender gender) {
662                 this.gender = gender;
663         }
664
665         @Override
666         public Short getHouseNumber () {
667                 return this.houseNumber;
668         }
669
670         @Override
671         public void setHouseNumber (final Short houseNumber) {
672                 this.houseNumber = houseNumber;
673         }
674
675         @Override
676         public Integer getPhoneAreaCode () {
677                 return this.phoneAreaCode;
678         }
679
680         @Override
681         public void setPhoneAreaCode (final Integer phoneAreaCode) {
682                 this.phoneAreaCode = phoneAreaCode;
683         }
684
685         @Override
686         public Country getPhoneCountry () {
687                 return this.phoneCountry;
688         }
689
690         @Override
691         public void setPhoneCountry (final Country phoneCountry) {
692                 this.phoneCountry = phoneCountry;
693         }
694
695         @Override
696         public Long getPhoneNumber () {
697                 return this.phoneNumber;
698         }
699
700         @Override
701         public void setPhoneNumber (final Long phoneNumber) {
702                 this.phoneNumber = phoneNumber;
703         }
704
705         @Override
706         public String getStreet () {
707                 return this.street;
708         }
709
710         @Override
711         public void setStreet (final String street) {
712                 this.street = street;
713         }
714
715         @Override
716         public Integer getZipCode () {
717                 return this.zipCode;
718         }
719
720         @Override
721         public void setZipCode (final Integer zipCode) {
722                 this.zipCode = zipCode;
723         }
724
725         @Override
726         public boolean hasContacts () {
727                 return (!this.allContacts().isEmpty());
728         }
729
730         /**
731          * Post-initialization of this class
732          */
733         @PostConstruct
734         public void init () {
735                 // Get full email address list for reducing EJB calls
736                 this.emailAddressList = this.contactBean.getEmailAddressList();
737
738                 // Get full contact list
739                 this.contactList = this.contactBean.getAllContacts();
740         }
741
742         @Override
743         public boolean isEmailAddressRegistered (final Contact contact) {
744                 return ((this.emailAddressList instanceof List) && (this.emailAddressList.contains(contact.getContactEmailAddress())));
745         }
746
747         @Override
748         public boolean isRequiredChangePersonalDataSet () {
749                 return ((this.getGender() != null) &&
750                                 (this.getFirstName() != null) &&
751                                 (this.getFamilyName() != null) &&
752                                 (this.getStreet() != null) &&
753                                 (this.getHouseNumber() != null) &&
754                                 (this.getZipCode() != null) &&
755                                 (this.getCity() != null));
756         }
757
758         @Override
759         public boolean isRequiredPersonalDataSet () {
760                 return ((this.getGender() != null) &&
761                                 (this.getFirstName() != null) &&
762                                 (this.getFamilyName() != null) &&
763                                 (this.getStreet() != null) &&
764                                 (this.getHouseNumber() != null) &&
765                                 (this.getZipCode() != null) &&
766                                 (this.getCity() != null) &&
767                                 (this.getEmailAddress() != null) &&
768                                 (this.getEmailAddressRepeat() != null));
769         }
770
771         @Override
772         public boolean isSameEmailAddressEntered () {
773                 return (Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat()));
774         }
775
776         @Override
777         public Contact lookupContactById (final Long contactId) throws ContactNotFoundException {
778                 // Init variable
779                 Contact localContact = null;
780
781                 // Clear this bean
782                 this.clear();
783
784                 // Try to lookup it in visible user list
785                 for (final Iterator<Contact> iterator = this.contactList.iterator(); iterator.hasNext();) {
786                         // Get next user
787                         Contact next = iterator.next();
788
789                         // Is the user id found?
790                         if (Objects.equals(next.getContactId(), contactId)) {
791                                 // Copy to other variable
792                                 localContact = next;
793                                 break;
794                         }
795                 }
796
797                 // Is it still null?
798                 if (null == localContact) {
799                         // Not visible for the current user
800                         throw new ContactNotFoundException(contactId);
801                 }
802
803                 // Copy all data to this bean
804                 this.copyContact(localContact);
805
806                 // Return it
807                 return localContact;
808         }
809
810         @Override
811         public void updateContactDataFromController (final Contact userContact) {
812                 // Is the instance valid?
813                 if (null == userContact) {
814                         // Throw NPE
815                         throw new NullPointerException("userContact is null"); //NOI18N
816                 } else if (userContact.getContactId() == null) {
817                         // Throw NPE
818                         throw new NullPointerException("userContact.contactId is null"); //NOI18N
819                 } else if (userContact.getContactId() < 1) {
820                         // Not valid id number
821                         throw new IllegalArgumentException(MessageFormat.format("userContact.contactId={0} is not valid.", userContact.getContactId())); //NOI18N
822                 }
823
824                 // Set all
825                 this.copyContact(userContact);
826         }
827
828         /**
829          * Adds email address to bean's internal list.
830          * <p>
831          * @param contact Contact instance
832          */
833         private void addUserNameEmailAddress (final Contact contact) {
834                 // Make sure the entry is not added yet
835                 if (this.emailAddressList.contains(contact.getContactEmailAddress())) {
836                         // Already added
837                         throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", contact.getContactEmailAddress())); //NOI18N
838                 }
839
840                 // Add email addres
841                 this.emailAddressList.add(contact.getContactEmailAddress());
842         }
843
844         /**
845          * Clears this bean
846          */
847         private void clear () {
848                 // Clear all data
849                 // - personal data
850                 this.setGender(Gender.UNKNOWN);
851                 this.setFirstName(null);
852                 this.setFamilyName(null);
853                 this.setStreet(null);
854                 this.setHouseNumber(null);
855                 this.setZipCode(null);
856                 this.setCity(null);
857                 this.setCountry(null);
858
859                 // - contact data
860                 this.setEmailAddress(null);
861                 this.setEmailAddressRepeat(null);
862                 this.setPhoneAreaCode(null);
863                 this.setCellphoneCarrier(null);
864                 this.setFaxAreaCode(null);
865
866                 // - other data
867                 this.setBirthday(null);
868                 this.setComment(null);
869         }
870
871         /**
872          * Copies given contact into the controller
873          * <p>
874          * @param contact Contact instance
875          */
876         private void copyContact (final Contact contact) {
877                 // Copy all fields:
878                 // - base data
879                 this.setGender(contact.getContactGender());
880                 this.setFirstName(contact.getContactFirstName());
881                 this.setFamilyName(contact.getContactFamilyName());
882                 this.setStreet(contact.getContactStreet());
883                 this.setHouseNumber(contact.getContactHouseNumber());
884                 this.setZipCode(contact.getContactZipCode());
885                 this.setCity(contact.getContactCity());
886                 this.setCountry(contact.getContactCountry());
887
888                 // Get cellphone, phone and fax instance
889                 DialableCellphoneNumber cellphone = contact.getContactCellphoneNumber();
890                 DialableFaxNumber fax = contact.getContactFaxNumber();
891                 DialableLandLineNumber phone = contact.getContactLandLineNumber();
892
893                 // - contact data
894                 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneAreaCode() > 0)) {
895                         this.setPhoneCountry(phone.getPhoneCountry());
896                         this.setPhoneAreaCode(phone.getPhoneAreaCode());
897                         this.setPhoneNumber(phone.getPhoneNumber());
898                 }
899                 if ((cellphone instanceof DialableCellphoneNumber) && (cellphone.getCellphoneProvider() instanceof MobileProvider)) {
900                         this.setCellphoneCarrier(cellphone.getCellphoneProvider());
901                         this.setCellphoneNumber(cellphone.getPhoneNumber());
902                 }
903                 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneAreaCode() > 0)) {
904                         this.setFaxCountry(fax.getPhoneCountry());
905                         this.setFaxAreaCode(fax.getPhoneAreaCode());
906                         this.setFaxNumber(fax.getPhoneNumber());
907                 }
908                 this.setEmailAddress(contact.getContactEmailAddress());
909
910                 // -- other data
911                 this.setBirthday(contact.getContactBirthday());
912                 this.setComment(contact.getContactComment());
913         }
914
915 }