]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/contact/JobsContactWebSessionBean.java
Continued a bit:
[jjobs-war.git] / src / java / org / mxchange / jjobs / beans / contact / JobsContactWebSessionBean.java
1 /*
2  * Copyright (C) 2016 Roland Haeder
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Affero General Public License as
6  * published by the Free Software Foundation, either version 3 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU Affero General Public License for more details.
13  *
14  * You should have received a copy of the GNU Affero General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.jjobs.beans.contact;
18
19 import java.text.MessageFormat;
20 import java.util.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.jcontacts.contact.Contact;
34 import org.mxchange.jcontacts.contact.ContactSessionBeanRemote;
35 import org.mxchange.jcontacts.contact.UserContact;
36 import org.mxchange.jcontacts.contact.gender.Gender;
37 import org.mxchange.jcontacts.contact.utils.ContactUtils;
38 import org.mxchange.jcontacts.events.contact.update.AdminUpdatedContactEvent;
39 import org.mxchange.jcontacts.exceptions.ContactNotFoundException;
40 import org.mxchange.jcountry.data.Country;
41 import org.mxchange.jjobs.beans.login.JobsUserLoginWebSessionController;
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 JobsContactWebSessionBean implements JobsContactWebSessionController {
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          * Phone number area code
175          */
176         private Integer phoneAreaCode;
177
178         /**
179          * Country instance for phone number
180          */
181         private Country phoneCountry;
182
183         /**
184          * Phone number
185          */
186         private Long phoneNumber;
187
188         /**
189          * Street
190          */
191         private String street;
192
193         /**
194          * Login bean (controller)
195          */
196         @Inject
197         private JobsUserLoginWebSessionController userLoginController;
198
199         /**
200          * ZIP code
201          */
202         private Integer zipCode;
203
204         /**
205          * Default constructor
206          */
207         public JobsContactWebSessionBean () {
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.updatedUser is null"); //NOI18N
284                 } else if (event.getUpdatedContact().getContactId() == null) {
285                         // userId is null
286                         throw new NullPointerException("event.updatedUser.userId is null"); //NOI18N
287                 } else if (event.getUpdatedContact().getContactId() < 1) {
288                         // Not avalid id
289                         throw new IllegalArgumentException(MessageFormat.format("contactId of contact={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 (@Observes final 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                 // Copy all data from registered->user
344                 this.copyContact(registeredContact);
345
346                 // Add user name and email address
347                 this.addUserNameEmailAddress(registeredContact);
348
349                 // Clear all data
350                 this.clear();
351
352                 // Trace message
353                 //* NOISY-DEBUG: */ System.out.println("ContactWebBean:afterRegistration: EXIT!"); //NOI18N
354         }
355
356         @Override
357         public void afterUserLogin (@Observes final UserLoggedInEvent event) {
358                 // Trace message
359                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ContactWebBean:afterUserLogin: event={0} - CALLED!", event)); //NOI18N
360
361                 // event should not be null
362                 if (null == event) {
363                         // Throw NPE
364                         throw new NullPointerException("event is null"); //NOI18N
365                 } else if (event.getLoggedInUser() == null) {
366                         // Throw NPE again
367                         throw new NullPointerException("event.user is null"); //NOI18N
368                 } else if (event.getLoggedInUser().getUserId() == null) {
369                         // userId is null
370                         throw new NullPointerException("event.user.userId is null"); //NOI18N
371                 } else if (event.getLoggedInUser().getUserId() < 1) {
372                         // Not avalid id
373                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
374                 }
375
376                 // Copy all data to this bean
377                 this.copyContact(event.getLoggedInUser().getUserContact());
378
379                 // Trace message
380                 //* NOISY-DEBUG: */ System.out.println("ContactWebBean:afterUserLogin - EXIT!"); //NOI18N
381         }
382
383         @Override
384         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
385         public List<Contact> allContacts () {
386                 // Debug message
387                 /* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ContactController.allContacts: contactList.size()={0} - EXIT!", this.contactList.size()));
388
389                 // Return un-modified list
390                 return this.contactList;
391         }
392
393         @Override
394         public Contact createContactInstance () {
395                 // User message
396                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createContactInstance: CALLED!", this.getClass().getSimpleName()));
397
398                 // Is all required data set?
399                 if (!this.isRequiredPersonalDataSet()) {
400                         // No, then abort here
401                         throw new FaceletException(new IllegalArgumentException("Not all personal data is set, but createContactInstance() is called.")); //NOI18N
402                 }
403
404                 // Required personal data must be set
405                 assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N
406
407                 // Create new contact instance
408                 Contact localContact = new UserContact();
409
410                 // Generate phone number
411                 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
412                 DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
413                 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
414
415                 // Create new contact
416                 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
417                 contact.setContactStreet(this.getStreet());
418                 contact.setContactHouseNumber(this.getHouseNumber());
419                 contact.setContactZipCode(this.getZipCode());
420                 contact.setContactCity(this.getCity());
421                 contact.setContactCountry(this.getCountry());
422                 contact.setContactEmailAddress(this.getEmailAddress());
423                 contact.setContactBirthday(this.getBirthday());
424                 contact.setContactComment(this.getComment());
425
426                 // Debug message
427                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createContactInstance: this.emailAddress={1}", this.getClass().getSimpleName(), this.getEmailAddress()));
428                 // Don't set null or wrong references
429                 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getPhoneAreaCode() != null) && (this.getPhoneNumber() != null) && (this.getPhoneAreaCode() > 0) && (this.getPhoneNumber() > 0)) {
430                         // Now the number must be given
431                         if (phone.getPhoneAreaCode() == null) {
432                                 // Is null
433                                 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
434                         } else if (phone.getPhoneAreaCode() < 1) {
435                                 // Abort here
436                                 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
437                         } else if (phone.getPhoneNumber() == null) {
438                                 // Is null
439                                 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
440                         } else if (phone.getPhoneNumber() < 1) {
441                                 // Abort here
442                                 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
443                         }
444
445                         // Set phone number
446                         contact.setContactLandLineNumber(phone);
447                 }
448
449                 // Don't set null or wrong references
450                 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
451                         // Now the number must be given
452                         if (fax.getPhoneAreaCode() == null) {
453                                 // Is null
454                                 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
455                         } else if (fax.getPhoneAreaCode() < 1) {
456                                 // Abort here
457                                 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
458                         } else if (fax.getPhoneNumber() == null) {
459                                 // Is null
460                                 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
461                         } else if (fax.getPhoneNumber() < 1) {
462                                 // Abort here
463                                 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
464                         }
465
466                         // Set fax number
467                         contact.setContactFaxNumber(fax);
468                 }
469
470                 // Is the provider set?
471                 if ((cellphone instanceof DialableCellphoneNumber) && (this.getCellphoneCarrier() instanceof MobileProvider) && (this.getCellphoneNumber() != null) && (this.getCellphoneNumber() > 0)) {
472                         // Is the number set?
473                         if (cellphone.getPhoneNumber() == null) {
474                                 // Is null
475                                 throw new NullPointerException("cellphone.phoneNumber is null"); //NOI18N
476                         } else if (cellphone.getPhoneNumber() < 1) {
477                                 // Abort here
478                                 throw new IllegalArgumentException("cellphone.phoneNumber is zero or below."); //NOI18N
479                         }
480
481                         // Set cellphone number
482                         contact.setContactCellphoneNumber(cellphone);
483                 }
484
485                 // Trace message
486                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createContactInstance: contact={1} - EXIT!", this.getClass().getSimpleName(), contact));
487
488                 // Return it
489                 return localContact;
490         }
491
492         @Override
493         public String doChangePersonalContactData () {
494                 // This method shall only be called if the user is logged-in
495                 if (!this.userLoginController.isUserLoggedIn()) {
496                         // Not logged-in
497                         throw new IllegalStateException("User is not logged-in"); //NOI18N
498                 } else if (!this.isRequiredChangePersonalDataSet()) {
499                         // Not all required fields are set
500                         throw new FaceletException("Not all required fields are set."); //NOI18N
501                 } else if (!this.userLoginController.ifCurrentPasswordMatches()) {
502                         // Password not matching
503                         throw new FaceletException(new UserPasswordMismatchException(this.userLoginController.getLoggedInUser()));
504                 }
505
506                 // Get contact instance
507                 Contact contact = this.userLoginController.getLoggedInUser().getUserContact();
508
509                 // It should be there, so run some tests on it
510                 assert (contact instanceof Contact) : "Instance userLoginController.loggedInUser.userContact is null"; //NOI18N
511                 assert (contact.getContactId() instanceof Long) : "Instance userLoginController.userContact.contactId is null"; //NOI18N
512                 assert (contact.getContactId() > 0) : MessageFormat.format("Instance userLoginController.userContact.contactId={0} is invalid", contact.getContactId()); //NOI18N
513
514                 // Update all fields
515                 contact.setContactGender(this.getGender());
516                 contact.setContactFirstName(this.getFirstName());
517                 contact.setContactFamilyName(this.getFamilyName());
518                 contact.setContactStreet(this.getStreet());
519                 contact.setContactHouseNumber(this.getHouseNumber());
520                 contact.setContactZipCode(this.getZipCode());
521                 contact.setContactCity(this.getCity());
522                 contact.setContactCountry(this.getCountry());
523
524                 // Update contact's cellphone number
525                 this.isCellphoneUnlinked = ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
526
527                 // Update contact's land-line number
528                 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
529
530                 // Update contact's fax number
531                 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
532
533                 // Send it to the EJB
534                 this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
535
536                 // All fine
537                 return "contact_data_saved"; //NOI18N
538         }
539
540         @Override
541         @SuppressWarnings ("ReturnOfDateField")
542         public Date getBirthday () {
543                 return this.birthday;
544         }
545
546         @Override
547         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
548         public void setBirthday (final Date birthday) {
549                 this.birthday = birthday;
550         }
551
552         @Override
553         public MobileProvider getCellphoneCarrier () {
554                 return this.cellphoneCarrier;
555         }
556
557         @Override
558         public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
559                 this.cellphoneCarrier = cellphoneCarrier;
560         }
561
562         @Override
563         public Long getCellphoneNumber () {
564                 return this.cellphoneNumber;
565         }
566
567         @Override
568         public void setCellphoneNumber (Long cellphoneNumber) {
569                 this.cellphoneNumber = cellphoneNumber;
570         }
571
572         @Override
573         public String getCity () {
574                 return this.city;
575         }
576
577         @Override
578         public void setCity (final String city) {
579                 this.city = city;
580         }
581
582         @Override
583         public String getComment () {
584                 return this.comment;
585         }
586
587         @Override
588         public void setComment (final String comment) {
589                 this.comment = comment;
590         }
591
592         @Override
593         public Country getCountry () {
594                 return this.country;
595         }
596
597         @Override
598         public void setCountry (final Country country) {
599                 this.country = country;
600         }
601
602         @Override
603         public String getEmailAddress () {
604                 return this.emailAddress;
605         }
606
607         @Override
608         public void setEmailAddress (final String emailAddress) {
609                 this.emailAddress = emailAddress;
610         }
611
612         @Override
613         public String getEmailAddressRepeat () {
614                 return this.emailAddressRepeat;
615         }
616
617         @Override
618         public void setEmailAddressRepeat (final String emailAddressRepeat) {
619                 this.emailAddressRepeat = emailAddressRepeat;
620         }
621
622         @Override
623         public String getFamilyName () {
624                 return this.familyName;
625         }
626
627         @Override
628         public void setFamilyName (final String familyName) {
629                 this.familyName = familyName;
630         }
631
632         @Override
633         public Integer getFaxAreaCode () {
634                 return this.faxAreaCode;
635         }
636
637         @Override
638         public void setFaxAreaCode (final Integer faxAreaCode) {
639                 this.faxAreaCode = faxAreaCode;
640         }
641
642         @Override
643         public Country getFaxCountry () {
644                 return this.faxCountry;
645         }
646
647         @Override
648         public void setFaxCountry (final Country faxCountry) {
649                 this.faxCountry = faxCountry;
650         }
651
652         @Override
653         public Long getFaxNumber () {
654                 return this.faxNumber;
655         }
656
657         @Override
658         public void setFaxNumber (final Long faxNumber) {
659                 this.faxNumber = faxNumber;
660         }
661
662         @Override
663         public String getFirstName () {
664                 return this.firstName;
665         }
666
667         @Override
668         public void setFirstName (final String firstName) {
669                 this.firstName = firstName;
670         }
671
672         @Override
673         public Gender getGender () {
674                 return this.gender;
675         }
676
677         @Override
678         public void setGender (final Gender gender) {
679                 this.gender = gender;
680         }
681
682         @Override
683         public Short getHouseNumber () {
684                 return this.houseNumber;
685         }
686
687         @Override
688         public void setHouseNumber (final Short houseNumber) {
689                 this.houseNumber = houseNumber;
690         }
691
692         @Override
693         public Integer getPhoneAreaCode () {
694                 return this.phoneAreaCode;
695         }
696
697         @Override
698         public void setPhoneAreaCode (final Integer phoneAreaCode) {
699                 this.phoneAreaCode = phoneAreaCode;
700         }
701
702         @Override
703         public Country getPhoneCountry () {
704                 return this.phoneCountry;
705         }
706
707         @Override
708         public void setPhoneCountry (final Country phoneCountry) {
709                 this.phoneCountry = phoneCountry;
710         }
711
712         @Override
713         public Long getPhoneNumber () {
714                 return this.phoneNumber;
715         }
716
717         @Override
718         public void setPhoneNumber (final Long phoneNumber) {
719                 this.phoneNumber = phoneNumber;
720         }
721
722         @Override
723         public String getStreet () {
724                 return this.street;
725         }
726
727         @Override
728         public void setStreet (final String street) {
729                 this.street = street;
730         }
731
732         @Override
733         public Integer getZipCode () {
734                 return this.zipCode;
735         }
736
737         @Override
738         public void setZipCode (final Integer zipCode) {
739                 this.zipCode = zipCode;
740         }
741
742         @Override
743         public boolean hasContacts () {
744                 return (!this.allContacts().isEmpty());
745         }
746
747         /**
748          * Post-initialization of this class
749          */
750         @PostConstruct
751         public void init () {
752                 // Get full email address list for reducing EJB calls
753                 this.emailAddressList = this.contactBean.getEmailAddressList();
754
755                 // Get full contact list
756                 this.contactList = this.contactBean.getAllContacts();
757         }
758
759         @Override
760         public boolean isEmailAddressRegistered (final Contact contact) {
761                 return ((this.emailAddressList instanceof List) && (this.emailAddressList.contains(contact.getContactEmailAddress())));
762         }
763
764         @Override
765         public boolean isRequiredChangePersonalDataSet () {
766                 return ((this.getGender() != null) &&
767                                 (this.getFirstName() != null) &&
768                                 (this.getFamilyName() != null) &&
769                                 (this.getStreet() != null) &&
770                                 (this.getHouseNumber() != null) &&
771                                 (this.getZipCode() != null) &&
772                                 (this.getCity() != null));
773         }
774
775         @Override
776         public boolean isRequiredPersonalDataSet () {
777                 return ((this.getGender() != null) &&
778                                 (this.getFirstName() != null) &&
779                                 (this.getFamilyName() != null) &&
780                                 (this.getStreet() != null) &&
781                                 (this.getHouseNumber() != null) &&
782                                 (this.getZipCode() != null) &&
783                                 (this.getCity() != null) &&
784                                 (this.getEmailAddress() != null) &&
785                                 (this.getEmailAddressRepeat() != null));
786         }
787
788         @Override
789         public boolean isSameEmailAddressEntered () {
790                 return (Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat()));
791         }
792
793         @Override
794         public Contact lookupContactById (final Long contactId) throws ContactNotFoundException {
795                 // Init variable
796                 Contact localContact = null;
797
798                 // Clear this bean
799                 this.clear();
800
801                 // Try to lookup it in visible user list
802                 for (final Iterator<Contact> iterator = this.contactList.iterator(); iterator.hasNext();) {
803                         // Get next user
804                         Contact next = iterator.next();
805
806                         // Is the user id found?
807                         if (Objects.equals(next.getContactId(), contactId)) {
808                                 // Copy to other variable
809                                 localContact = next;
810                                 break;
811                         }
812                 }
813
814                 // Is it still null?
815                 if (null == localContact) {
816                         // Not visible for the current user
817                         throw new ContactNotFoundException(contactId);
818                 }
819
820                 // Copy all data to this bean
821                 this.copyContact(localContact);
822
823                 // Return it
824                 return localContact;
825         }
826
827         @Override
828         public void updateContactDataFromController (final Contact contact) {
829                 // Is the instance valid?
830                 if (null == contact) {
831                         // Throw NPE
832                         throw new NullPointerException("contact is null"); //NOI18N
833                 } else if (contact.getContactId() == null) {
834                         // Throw NPE
835                         throw new NullPointerException("contact.contactId is null"); //NOI18N
836                 } else if (contact.getContactId() < 1) {
837                         // Not valid id number
838                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
839                 }
840
841                 // Set all
842                 this.copyContact(contact);
843         }
844
845         /**
846          * Adds email address to bean's internal list.
847          * <p>
848          * @param contact Contact instance
849          */
850         private void addUserNameEmailAddress (final Contact contact) {
851                 // Make sure the entry is not added yet
852                 if (this.emailAddressList.contains(contact.getContactEmailAddress())) {
853                         // Already added
854                         throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", contact.getContactEmailAddress())); //NOI18N
855                 }
856
857                 // Add email addres
858                 this.emailAddressList.add(contact.getContactEmailAddress());
859         }
860
861         /**
862          * Clears this bean
863          */
864         private void clear () {
865                 // Clear all data
866                 // - personal data
867                 this.setGender(Gender.UNKNOWN);
868                 this.setFirstName(null);
869                 this.setFamilyName(null);
870                 this.setStreet(null);
871                 this.setHouseNumber(null);
872                 this.setZipCode(null);
873                 this.setCity(null);
874                 this.setCountry(null);
875
876                 // - contact data
877                 this.setEmailAddress(null);
878                 this.setEmailAddressRepeat(null);
879                 this.setPhoneAreaCode(null);
880                 this.setCellphoneCarrier(null);
881                 this.setFaxAreaCode(null);
882
883                 // - other data
884                 this.setBirthday(null);
885                 this.setComment(null);
886         }
887
888         /**
889          * Copies given contact into the controller
890          * <p>
891          * @param contact Contact instance
892          */
893         private void copyContact (final Contact contact) {
894                 // Is the instance valid?
895                 if (null == contact) {
896                         // Throw NPE
897                         throw new NullPointerException("contact is null"); //NOI18N
898                 } else if (contact.getContactId() == null) {
899                         // Throw NPE
900                         throw new NullPointerException("contact.contactId is null"); //NOI18N
901                 } else if (contact.getContactId() < 1) {
902                         // Not valid id number
903                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
904                 }
905
906                 // Copy all fields:
907                 // - base data
908                 this.setGender(contact.getContactGender());
909                 this.setFirstName(contact.getContactFirstName());
910                 this.setFamilyName(contact.getContactFamilyName());
911                 this.setStreet(contact.getContactStreet());
912                 this.setHouseNumber(contact.getContactHouseNumber());
913                 this.setZipCode(contact.getContactZipCode());
914                 this.setCity(contact.getContactCity());
915                 this.setCountry(contact.getContactCountry());
916                 this.setEmailAddress(contact.getContactEmailAddress());
917                 this.setBirthday(contact.getContactBirthday());
918                 this.setComment(contact.getContactComment());
919
920                 // Get cellphone, phone and fax instance
921                 DialableCellphoneNumber cellphone = contact.getContactCellphoneNumber();
922                 DialableFaxNumber fax = contact.getContactFaxNumber();
923                 DialableLandLineNumber phone = contact.getContactLandLineNumber();
924
925                 // - contact data
926                 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneAreaCode() > 0)) {
927                         this.setPhoneCountry(phone.getPhoneCountry());
928                         this.setPhoneAreaCode(phone.getPhoneAreaCode());
929                         this.setPhoneNumber(phone.getPhoneNumber());
930                 }
931
932                 if ((cellphone instanceof DialableCellphoneNumber) && (cellphone.getCellphoneProvider() instanceof MobileProvider)) {
933                         this.setCellphoneCarrier(cellphone.getCellphoneProvider());
934                         this.setCellphoneNumber(cellphone.getPhoneNumber());
935                 }
936
937                 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneAreaCode() > 0)) {
938                         this.setFaxCountry(fax.getPhoneCountry());
939                         this.setFaxAreaCode(fax.getPhoneAreaCode());
940                         this.setFaxNumber(fax.getPhoneNumber());
941                 }
942         }
943
944         /**
945          * Removes given contact from all lists
946          * <p>
947          * @param contact Contact instance to remove
948          */
949         private void removeContact (final Contact contact) {
950                 // Is the instance valid?
951                 if (null == contact) {
952                         // Throw NPE
953                         throw new NullPointerException("contact is null"); //NOI18N
954                 } else if (contact.getContactId() == null) {
955                         // Throw NPE
956                         throw new NullPointerException("contact.contactId is null"); //NOI18N
957                 } else if (contact.getContactId() < 1) {
958                         // Not valid id number
959                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
960                 }
961
962                 // Remove from general list
963                 if (!this.contactList.remove(contact)) {
964                         // Did not remove contact
965                         throw new IllegalStateException(MessageFormat.format("contact {0} was not removed.", contact.getContactId())); //NOI18N
966                 }
967
968                 // Remove from other lists
969                 this.emailAddressList.remove(contact.getContactEmailAddress());
970         }
971
972 }