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