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