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