]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/contact/PizzaContactWebSessionBean.java
Opps, wrong import
[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.Collections;
21 import java.util.Date;
22 import java.util.Iterator;
23 import java.util.List;
24 import java.util.Objects;
25 import javax.annotation.PostConstruct;
26 import javax.enterprise.context.SessionScoped;
27 import javax.enterprise.event.Observes;
28 import javax.faces.view.facelets.FaceletException;
29 import javax.inject.Inject;
30 import javax.inject.Named;
31 import javax.naming.Context;
32 import javax.naming.InitialContext;
33 import javax.naming.NamingException;
34 import org.mxchange.jcontacts.contact.Contact;
35 import org.mxchange.jcontacts.contact.ContactSessionBeanRemote;
36 import org.mxchange.jcontacts.contact.UserContact;
37 import org.mxchange.jcontacts.contact.gender.Gender;
38 import org.mxchange.jcontacts.contact.utils.ContactUtils;
39 import org.mxchange.jcontacts.events.contact.add.AdminAddedContactEvent;
40 import org.mxchange.jcontacts.events.contact.update.AdminUpdatedContactEvent;
41 import org.mxchange.jcontacts.exceptions.ContactNotFoundException;
42 import org.mxchange.jcountry.data.Country;
43 import org.mxchange.jcustomercore.events.customer.added.AdminAddedCustomerEvent;
44 import org.mxchange.jphone.phonenumbers.cellphone.CellphoneNumber;
45 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
46 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
47 import org.mxchange.jphone.phonenumbers.fax.FaxNumber;
48 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
49 import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
50 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
51 import org.mxchange.jusercore.events.login.UserLoggedInEvent;
52 import org.mxchange.jusercore.events.registration.UserRegisteredEvent;
53 import org.mxchange.jusercore.events.user.add.AdminAddedUserEvent;
54 import org.mxchange.jusercore.exceptions.UserPasswordMismatchException;
55 import org.mxchange.pizzaapplication.beans.login.PizzaUserLoginWebSessionController;
56
57 /**
58  * A general contact bean (controller)
59  * <p>
60  * @author Roland Haeder<roland@mxchange.org>
61  */
62 @Named ("contactController")
63 @SessionScoped
64 public class PizzaContactWebSessionBean implements PizzaContactWebSessionController {
65
66         /**
67          * Serial number
68          */
69         private static final long serialVersionUID = 542_145_347_916L;
70
71         /**
72          * Birth day
73          */
74         private Date birthday;
75
76         /**
77          * Cellphone number's carrier
78          */
79         private MobileProvider cellphoneCarrier;
80
81         /**
82          * Cellphone number
83          */
84         private Long cellphoneNumber;
85
86         /**
87          * City
88          */
89         private String city;
90
91         /**
92          * Optional comments
93          */
94         private String comment;
95
96         /**
97          * Remote contact bean
98          */
99         private final ContactSessionBeanRemote contactBean;
100
101         /**
102          * Contact list
103          */
104         private List<Contact> contactList;
105
106         /**
107          * Country instance
108          */
109         private Country country;
110
111         /**
112          * Email address
113          */
114         private String emailAddress;
115
116         /**
117          * Email address list
118          */
119         private List<String> emailAddressList;
120
121         /**
122          * Email address repeated
123          */
124         private String emailAddressRepeat;
125
126         /**
127          * Family name
128          */
129         private String familyName;
130
131         /**
132          * Fax number's area code
133          */
134         private Integer faxAreaCode;
135
136         /**
137          * Country instance for fax number
138          */
139         private Country faxCountry;
140
141         /**
142          * Fax number
143          */
144         private Long faxNumber;
145
146         /**
147          * First name
148          */
149         private String firstName;
150
151         /**
152          * Gender instance
153          */
154         private Gender gender;
155
156         /**
157          * House number
158          */
159         private Short houseNumber;
160
161         /**
162          * Whether a cellphone entry has been unlinked
163          */
164         private boolean isCellphoneUnlinked;
165
166         /**
167          * Whether a fax entry has been unlinked
168          */
169         private boolean isFaxUnlinked;
170
171         /**
172          * Whether a land-line number has been unlinked
173          */
174         private boolean isLandLineUnlinked;
175
176         /**
177          * Login bean (controller)
178          */
179         @Inject
180         private PizzaUserLoginWebSessionController userLoginController;
181
182         /**
183          * Phone number area code
184          */
185         private Integer phoneAreaCode;
186
187         /**
188          * Country instance for phone number
189          */
190         private Country phoneCountry;
191
192         /**
193          * Phone number
194          */
195         private Long phoneNumber;
196
197         /**
198          * Street
199          */
200         private String street;
201
202         /**
203          * ZIP code
204          */
205         private Integer zipCode;
206
207         /**
208          * Default constructor
209          */
210         public PizzaContactWebSessionBean () {
211                 // Try it
212                 try {
213                         // Get initial context
214                         Context context = new InitialContext();
215
216                         // Try to lookup
217                         this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/PizzaService-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
218                 } catch (final NamingException e) {
219                         // Throw again
220                         throw new FaceletException(e);
221                 }
222         }
223
224         @Override
225         public void afterAdminAddedContact (@Observes final AdminAddedContactEvent event) {
226                 // The event must be valid
227                 if (null == event) {
228                         // Throw NPE
229                         throw new NullPointerException("event is null"); //NOI18N
230                 } else if (event.getAddedContact() == null) {
231                         // Throw again ...
232                         throw new NullPointerException("event.addedContact is null"); //NOI18N
233                 } else if (event.getAddedContact().getContactId() == null) {
234                         // ... and again
235                         throw new NullPointerException("event.addedContact.customerId is null"); //NOI18N
236                 } else if (event.getAddedContact().getContactId() < 1) {
237                         // Not valid
238                         throw new IllegalArgumentException(MessageFormat.format("event.addedContact.customerId={0} is not valid", event.getAddedContact().getContactId())); //NOI18N //NOI18N
239                 }
240
241                 // Clear this bean
242                 this.clear();
243
244                 // Call other method
245                 this.contactList.add(event.getAddedContact());
246         }
247
248         @Override
249         public void afterAdminAddedCustomer (@Observes final AdminAddedCustomerEvent event) {
250                 // The event must be valid
251                 if (null == event) {
252                         // Throw NPE
253                         throw new NullPointerException("event is null"); //NOI18N
254                 } else if (event.getAddedCustomer() == null) {
255                         // Throw again ...
256                         throw new NullPointerException("event.addedCustomer is null"); //NOI18N
257                 } else if (event.getAddedCustomer().getCustomerId() == null) {
258                         // ... and again
259                         throw new NullPointerException("event.addedCustomer.customerId is null"); //NOI18N
260                 } else if (event.getAddedCustomer().getCustomerId() < 1) {
261                         // Not valid
262                         throw new IllegalArgumentException(MessageFormat.format("event.addedCustomer.customerId={0} is not valid", event.getAddedCustomer().getCustomerId())); //NOI18N //NOI18N
263                 }
264
265                 // Clear this bean
266                 this.clear();
267
268                 // Call other method
269                 this.contactList.add(event.getAddedCustomer().getCustomerContact());
270         }
271
272         @Override
273         public void afterAdminAddedUserEvent (@Observes final AdminAddedUserEvent event) {
274                 // Trace message
275                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterAdminAddedUserEvent: 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.getAddedUser() == null) {
282                         // Throw NPE again
283                         throw new NullPointerException("event.addedUser is null"); //NOI18N
284                 } else if (event.getAddedUser().getUserId() == null) {
285                         // userId is null
286                         throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
287                 } else if (event.getAddedUser().getUserId() < 1) {
288                         // Not avalid id
289                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
290                 }
291
292                 // Clear all data
293                 this.clear();
294
295                 // Trace message
296                 //* NOISY-DEBUG: */ System.out.println("UserWebBean:afterAdminAddedUserEvent: EXIT!"); //NOI18N
297         }
298
299         @Override
300         public void afterAdminUpdatedContactDataEvent (@Observes final AdminUpdatedContactEvent event) {
301                 // Trace message
302                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ContactWebBean:afterAdminUpdatedContactDataEvent: event={0} - CALLED!", event)); //NOI18N
303
304                 // event should not be null
305                 if (null == event) {
306                         // Throw NPE
307                         throw new NullPointerException("event is null"); //NOI18N
308                 } else if (event.getUpdatedContact() == null) {
309                         // Throw NPE again
310                         throw new NullPointerException("event.updatedUser is null"); //NOI18N
311                 } else if (event.getUpdatedContact().getContactId() == null) {
312                         // userId is null
313                         throw new NullPointerException("event.updatedUser.userId is null"); //NOI18N
314                 } else if (event.getUpdatedContact().getContactId() < 1) {
315                         // Not avalid id
316                         throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
317                 }
318
319                 // Get iterator from list
320                 Iterator<Contact> iterator = this.contactList.iterator();
321
322                 // "Walk" through all entries
323                 while (iterator.hasNext()) {
324                         // Get next element
325                         Contact next = iterator.next();
326
327                         // Is id number the same?
328                         if (Objects.equals(event.getUpdatedContact().getContactId(), next.getContactId())) {
329                                 // Found entry, so remove it and abort
330                                 this.contactList.remove(next);
331
332                                 // Remove also email from list
333                                 this.emailAddressList.remove(next.getContactEmailAddress());
334                                 break;
335                         }
336                 }
337
338                 // Add contact to list
339                 this.contactList.add(event.getUpdatedContact());
340
341                 // Add email address to list
342                 this.emailAddressList.add(event.getUpdatedContact().getContactEmailAddress());
343         }
344
345         @Override
346         public void afterRegistrationEvent (@Observes final UserRegisteredEvent event) {
347                 // Trace message
348                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ContactWebBean:afterRegistration: event={0} - CALLED!", event)); //NOI18N
349
350                 // event should not be null
351                 if (null == event) {
352                         // Throw NPE
353                         throw new NullPointerException("event is null"); //NOI18N
354                 } else if (event.getRegisteredUser() == null) {
355                         // Throw NPE again
356                         throw new NullPointerException("event.user is null"); //NOI18N
357                 } else if (event.getRegisteredUser().getUserId() == null) {
358                         // userId is null
359                         throw new NullPointerException("event.user.userId is null"); //NOI18N
360                 } else if (event.getRegisteredUser().getUserId() < 1) {
361                         // Not avalid id
362                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
363                 }
364
365                 // Get user instance
366                 Contact registeredContact = event.getRegisteredUser().getUserContact();
367
368                 // Debug message
369                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ContactWebBean:afterRegistration: registeredContact={0}", registeredContact)); //NOI18N
370                 // Copy all data from registered->user
371                 this.copyContact(registeredContact);
372
373                 // Add user name and email address
374                 this.addUserNameEmailAddress(registeredContact);
375
376                 // Clear all data
377                 this.clear();
378
379                 // Trace message
380                 //* NOISY-DEBUG: */ System.out.println("ContactWebBean:afterRegistration: EXIT!"); //NOI18N
381         }
382
383         @Override
384         public void afterUserLogin (@Observes final UserLoggedInEvent event) {
385                 // Trace message
386                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ContactWebBean:afterUserLogin: event={0} - CALLED!", event)); //NOI18N
387
388                 // event should not be null
389                 if (null == event) {
390                         // Throw NPE
391                         throw new NullPointerException("event is null"); //NOI18N
392                 } else if (event.getLoggedInUser() == null) {
393                         // Throw NPE again
394                         throw new NullPointerException("event.user is null"); //NOI18N
395                 } else if (event.getLoggedInUser().getUserId() == null) {
396                         // userId is null
397                         throw new NullPointerException("event.user.userId is null"); //NOI18N
398                 } else if (event.getLoggedInUser().getUserId() < 1) {
399                         // Not avalid id
400                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
401                 }
402
403                 // Copy all data to this bean
404                 this.copyContact(event.getLoggedInUser().getUserContact());
405
406                 // Trace message
407                 //* NOISY-DEBUG: */ System.out.println("ContactWebBean:afterUserLogin - EXIT!"); //NOI18N
408         }
409
410         @Override
411         public List<Contact> allContacts () {
412                 // Debug message
413                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ContactController.allContacts: contactList.size()={0} - EXIT!", this.contactList.size()));
414
415                 // Return un-modified list
416                 return Collections.unmodifiableList(this.contactList);
417         }
418
419         @Override
420         public Contact createContactInstance () {
421                 // User message
422                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createContactInstance: CALLED!", this.getClass().getSimpleName()));
423
424                 // Is all required data set?
425                 if (!this.isRequiredPersonalDataSet()) {
426                         // No, then abort here
427                         throw new FaceletException(new IllegalArgumentException("Not all personal data is set, but createContactInstance() is called.")); //NOI18N
428                 }
429
430                 // Required personal data must be set
431                 assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N
432
433                 // Generate phone number
434                 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
435                 DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
436                 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
437
438                 // Create new contact
439                 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
440                 contact.setContactStreet(this.getStreet());
441                 contact.setContactHouseNumber(this.getHouseNumber());
442                 contact.setContactZipCode(this.getZipCode());
443                 contact.setContactCity(this.getCity());
444                 contact.setContactCountry(this.getCountry());
445                 contact.setContactEmailAddress(this.getEmailAddress());
446                 contact.setContactBirthday(this.getBirthday());
447                 contact.setContactComment(this.getComment());
448
449                 // Debug message
450                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createContactInstance: this.emailAddress={1}", this.getClass().getSimpleName(), this.getEmailAddress()));
451                 // Don't set null or wrong references
452                 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getPhoneAreaCode() != null) && (this.getPhoneNumber() != null) && (this.getPhoneAreaCode() > 0) && (this.getPhoneNumber() > 0)) {
453                         // Now the number must be given
454                         if (phone.getPhoneAreaCode() == null) {
455                                 // Is null
456                                 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
457                         } else if (phone.getPhoneAreaCode() < 1) {
458                                 // Abort here
459                                 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
460                         } else if (phone.getPhoneNumber() == null) {
461                                 // Is null
462                                 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
463                         } else if (phone.getPhoneNumber() < 1) {
464                                 // Abort here
465                                 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
466                         }
467
468                         // Set phone number
469                         contact.setContactLandLineNumber(phone);
470                 }
471
472                 // Don't set null or wrong references
473                 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
474                         // Now the number must be given
475                         if (fax.getPhoneAreaCode() == null) {
476                                 // Is null
477                                 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
478                         } else if (fax.getPhoneAreaCode() < 1) {
479                                 // Abort here
480                                 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
481                         } else if (fax.getPhoneNumber() == null) {
482                                 // Is null
483                                 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
484                         } else if (fax.getPhoneNumber() < 1) {
485                                 // Abort here
486                                 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
487                         }
488
489                         // Set fax number
490                         contact.setContactFaxNumber(fax);
491                 }
492
493                 // Is the provider set?
494                 if ((cellphone instanceof DialableCellphoneNumber) && (this.getCellphoneCarrier() instanceof MobileProvider) && (this.getCellphoneNumber() != null) && (this.getCellphoneNumber() > 0)) {
495                         // Is the number set?
496                         if (cellphone.getPhoneNumber() == null) {
497                                 // Is null
498                                 throw new NullPointerException("cellphone.phoneNumber is null"); //NOI18N
499                         } else if (cellphone.getPhoneNumber() < 1) {
500                                 // Abort here
501                                 throw new IllegalArgumentException("cellphone.phoneNumber is zero or below."); //NOI18N
502                         }
503
504                         // Set cellphone number
505                         contact.setContactCellphoneNumber(cellphone);
506                 }
507
508                 // Trace message
509                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createContactInstance: contact={1} - EXIT!", this.getClass().getSimpleName(), contact));
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.contactList.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                 // Cherck parameter
784                 if (null == contact) {
785                         // Throw NPE
786                         throw new NullPointerException("contact is null"); //NOI18N
787                 } else if (contact.getContactEmailAddress() == null) {
788                         // Throw again
789                         throw new NullPointerException("contact.contactEmailAddress is null"); //NOI18N
790                 } else if (contact.getContactEmailAddress().isEmpty()) {
791                         // Is empty
792                         throw new IllegalArgumentException("contact.contactEmailAddress is empty."); //NOI18N
793                 }
794
795                 // Determine it
796                 return ((this.emailAddressList instanceof List) && (this.emailAddressList.contains(contact.getContactEmailAddress())));
797         }
798
799         @Override
800         public boolean isRequiredChangePersonalDataSet () {
801                 return ((this.getGender() != null) &&
802                                 (this.getFirstName() != null) &&
803                                 (this.getFamilyName() != null) &&
804                                 (this.getStreet() != null) &&
805                                 (this.getHouseNumber() != null) &&
806                                 (this.getZipCode() != null) &&
807                                 (this.getCity() != null));
808         }
809
810         @Override
811         public boolean isRequiredPersonalDataSet () {
812                 return ((this.getGender() != null) &&
813                                 (this.getFirstName() != null) &&
814                                 (this.getFamilyName() != null) &&
815                                 (this.getStreet() != null) &&
816                                 (this.getHouseNumber() != null) &&
817                                 (this.getZipCode() != null) &&
818                                 (this.getCity() != null) &&
819                                 (this.getEmailAddress() != null) &&
820                                 (this.getEmailAddressRepeat() != null));
821         }
822
823         @Override
824         public boolean isSameEmailAddressEntered () {
825                 return (Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat()));
826         }
827
828         @Override
829         public Contact lookupContactById (final Long contactId) throws ContactNotFoundException {
830                 // Init variable
831                 Contact localContact = null;
832
833                 // Clear this bean
834                 this.clear();
835
836                 // Try to lookup it in visible user list
837                 for (final Iterator<Contact> iterator = this.contactList.iterator(); iterator.hasNext();) {
838                         // Get next user
839                         Contact next = iterator.next();
840
841                         // Is the user id found?
842                         if (Objects.equals(next.getContactId(), contactId)) {
843                                 // Copy to other variable
844                                 localContact = next;
845                                 break;
846                         }
847                 }
848
849                 // Is it still null?
850                 if (null == localContact) {
851                         // Not visible for the current user
852                         throw new ContactNotFoundException(contactId);
853                 }
854
855                 // Copy all data to this bean
856                 this.copyContact(localContact);
857
858                 // Return it
859                 return localContact;
860         }
861
862         @Override
863         public void updateContactDataFromController (final Contact contact) {
864                 // Is the instance valid?
865                 if (null == contact) {
866                         // Throw NPE
867                         throw new NullPointerException("contact is null"); //NOI18N
868                 } else if (contact.getContactId() == null) {
869                         // Throw NPE
870                         throw new NullPointerException("contact.contactId is null"); //NOI18N
871                 } else if (contact.getContactId() < 1) {
872                         // Not valid id number
873                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
874                 }
875
876                 // Set all
877                 this.copyContact(contact);
878         }
879
880         /**
881          * Adds email address to bean's internal list.
882          * <p>
883          * @param contact Contact instance
884          */
885         private void addUserNameEmailAddress (final Contact contact) {
886                 // Make sure the entry is not added yet
887                 if (this.emailAddressList.contains(contact.getContactEmailAddress())) {
888                         // Already added
889                         throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", contact.getContactEmailAddress())); //NOI18N
890                 }
891
892                 // Add email addres
893                 this.emailAddressList.add(contact.getContactEmailAddress());
894         }
895
896         /**
897          * Clears this bean
898          */
899         private void clear () {
900                 // Clear all data
901                 // - personal data
902                 this.setGender(Gender.UNKNOWN);
903                 this.setFirstName(null);
904                 this.setFamilyName(null);
905                 this.setStreet(null);
906                 this.setHouseNumber(null);
907                 this.setZipCode(null);
908                 this.setCity(null);
909                 this.setCountry(null);
910
911                 // - contact data
912                 this.setEmailAddress(null);
913                 this.setEmailAddressRepeat(null);
914                 this.setPhoneCountry(null);
915                 this.setPhoneAreaCode(null);
916                 this.setPhoneCountry(null);
917                 this.setPhoneNumber(null);
918                 this.setCellphoneCarrier(null);
919                 this.setCellphoneNumber(null);
920                 this.setFaxAreaCode(null);
921                 this.setFaxCountry(null);
922                 this.setFaxNumber(null);
923
924                 // - other data
925                 this.setBirthday(null);
926                 this.setComment(null);
927         }
928
929         /**
930          * Copies given contact into the controller
931          * <p>
932          * @param contact Contact instance
933          */
934         private void copyContact (final Contact contact) {
935                 // Is the instance valid?
936                 if (null == contact) {
937                         // Throw NPE
938                         throw new NullPointerException("contact is null"); //NOI18N
939                 } else if (contact.getContactId() == null) {
940                         // Throw NPE
941                         throw new NullPointerException("contact.contactId is null"); //NOI18N
942                 } else if (contact.getContactId() < 1) {
943                         // Not valid id number
944                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
945                 }
946
947                 // Copy all fields:
948                 // - base data
949                 this.setGender(contact.getContactGender());
950                 this.setFirstName(contact.getContactFirstName());
951                 this.setFamilyName(contact.getContactFamilyName());
952                 this.setStreet(contact.getContactStreet());
953                 this.setHouseNumber(contact.getContactHouseNumber());
954                 this.setZipCode(contact.getContactZipCode());
955                 this.setCity(contact.getContactCity());
956                 this.setCountry(contact.getContactCountry());
957                 this.setEmailAddress(contact.getContactEmailAddress());
958                 this.setBirthday(contact.getContactBirthday());
959                 this.setComment(contact.getContactComment());
960
961                 // Get cellphone, phone and fax instance
962                 DialableCellphoneNumber cellphone = contact.getContactCellphoneNumber();
963                 DialableFaxNumber fax = contact.getContactFaxNumber();
964                 DialableLandLineNumber phone = contact.getContactLandLineNumber();
965
966                 // - contact data
967                 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneAreaCode() > 0)) {
968                         this.setPhoneCountry(phone.getPhoneCountry());
969                         this.setPhoneAreaCode(phone.getPhoneAreaCode());
970                         this.setPhoneNumber(phone.getPhoneNumber());
971                 }
972
973                 if ((cellphone instanceof DialableCellphoneNumber) && (cellphone.getCellphoneProvider() instanceof MobileProvider)) {
974                         this.setCellphoneCarrier(cellphone.getCellphoneProvider());
975                         this.setCellphoneNumber(cellphone.getPhoneNumber());
976                 }
977
978                 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneAreaCode() > 0)) {
979                         this.setFaxCountry(fax.getPhoneCountry());
980                         this.setFaxAreaCode(fax.getPhoneAreaCode());
981                         this.setFaxNumber(fax.getPhoneNumber());
982                 }
983         }
984
985         /**
986          * Removes given contact from all lists
987          * <p>
988          * @param contact Contact instance to remove
989          */
990         private void removeContact (final Contact contact) {
991                 // Is the instance valid?
992                 if (null == contact) {
993                         // Throw NPE
994                         throw new NullPointerException("contact is null"); //NOI18N
995                 } else if (contact.getContactId() == null) {
996                         // Throw NPE
997                         throw new NullPointerException("contact.contactId is null"); //NOI18N
998                 } else if (contact.getContactId() < 1) {
999                         // Not valid id number
1000                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1001                 }
1002
1003                 // Remove from general list
1004                 if (!this.contactList.remove(contact)) {
1005                         // Did not remove contact
1006                         throw new IllegalStateException(MessageFormat.format("contact {0} was not removed.", contact.getContactId())); //NOI18N
1007                 }
1008
1009                 // Remove from other lists
1010                 this.emailAddressList.remove(contact.getContactEmailAddress());
1011         }
1012
1013 }