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