]> 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                 // 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 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
511                 // Return it
512                 return contact;
513         }
514
515         @Override
516         public String doChangePersonalContactData () {
517                 // This method shall only be called if the user is logged-in
518                 if (!this.userLoginController.isUserLoggedIn()) {
519                         // Not logged-in
520                         throw new IllegalStateException("User is not logged-in"); //NOI18N
521                 } else if (!this.isRequiredChangePersonalDataSet()) {
522                         // Not all required fields are set
523                         throw new FaceletException("Not all required fields are set."); //NOI18N
524                 } else if (!this.userLoginController.ifCurrentPasswordMatches()) {
525                         // Password not matching
526                         throw new FaceletException(new UserPasswordMismatchException(this.userLoginController.getLoggedInUser()));
527                 }
528
529                 // Get contact instance
530                 Contact contact = this.userLoginController.getLoggedInUser().getUserContact();
531
532                 // It should be there, so run some tests on it
533                 assert (contact instanceof Contact) : "Instance userLoginController.loggedInUser.userContact is null"; //NOI18N
534                 assert (contact.getContactId() instanceof Long) : "Instance userLoginController.userContact.contactId is null"; //NOI18N
535                 assert (contact.getContactId() > 0) : MessageFormat.format("Instance userLoginController.userContact.contactId={0} is invalid", contact.getContactId()); //NOI18N
536
537                 // Update all fields
538                 contact.setContactGender(this.getGender());
539                 contact.setContactFirstName(this.getFirstName());
540                 contact.setContactFamilyName(this.getFamilyName());
541                 contact.setContactStreet(this.getStreet());
542                 contact.setContactHouseNumber(this.getHouseNumber());
543                 contact.setContactZipCode(this.getZipCode());
544                 contact.setContactCity(this.getCity());
545                 contact.setContactCountry(this.getCountry());
546
547                 // Update contact's cellphone number
548                 this.isCellphoneUnlinked = ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
549
550                 // Update contact's land-line number
551                 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
552
553                 // Update contact's fax number
554                 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
555
556                 // Send it to the EJB
557                 this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
558
559                 // All fine
560                 return "contact_data_saved"; //NOI18N
561         }
562
563         @Override
564         @SuppressWarnings ("ReturnOfDateField")
565         public Date getBirthday () {
566                 return this.birthday;
567         }
568
569         @Override
570         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
571         public void setBirthday (final Date birthday) {
572                 this.birthday = birthday;
573         }
574
575         @Override
576         public MobileProvider getCellphoneCarrier () {
577                 return this.cellphoneCarrier;
578         }
579
580         @Override
581         public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
582                 this.cellphoneCarrier = cellphoneCarrier;
583         }
584
585         @Override
586         public Long getCellphoneNumber () {
587                 return this.cellphoneNumber;
588         }
589
590         @Override
591         public void setCellphoneNumber (Long cellphoneNumber) {
592                 this.cellphoneNumber = cellphoneNumber;
593         }
594
595         @Override
596         public String getCity () {
597                 return this.city;
598         }
599
600         @Override
601         public void setCity (final String city) {
602                 this.city = city;
603         }
604
605         @Override
606         public String getComment () {
607                 return this.comment;
608         }
609
610         @Override
611         public void setComment (final String comment) {
612                 this.comment = comment;
613         }
614
615         @Override
616         public Country getCountry () {
617                 return this.country;
618         }
619
620         @Override
621         public void setCountry (final Country country) {
622                 this.country = country;
623         }
624
625         @Override
626         public String getEmailAddress () {
627                 return this.emailAddress;
628         }
629
630         @Override
631         public void setEmailAddress (final String emailAddress) {
632                 this.emailAddress = emailAddress;
633         }
634
635         @Override
636         public String getEmailAddressRepeat () {
637                 return this.emailAddressRepeat;
638         }
639
640         @Override
641         public void setEmailAddressRepeat (final String emailAddressRepeat) {
642                 this.emailAddressRepeat = emailAddressRepeat;
643         }
644
645         @Override
646         public String getFamilyName () {
647                 return this.familyName;
648         }
649
650         @Override
651         public void setFamilyName (final String familyName) {
652                 this.familyName = familyName;
653         }
654
655         @Override
656         public Integer getFaxAreaCode () {
657                 return this.faxAreaCode;
658         }
659
660         @Override
661         public void setFaxAreaCode (final Integer faxAreaCode) {
662                 this.faxAreaCode = faxAreaCode;
663         }
664
665         @Override
666         public Country getFaxCountry () {
667                 return this.faxCountry;
668         }
669
670         @Override
671         public void setFaxCountry (final Country faxCountry) {
672                 this.faxCountry = faxCountry;
673         }
674
675         @Override
676         public Long getFaxNumber () {
677                 return this.faxNumber;
678         }
679
680         @Override
681         public void setFaxNumber (final Long faxNumber) {
682                 this.faxNumber = faxNumber;
683         }
684
685         @Override
686         public String getFirstName () {
687                 return this.firstName;
688         }
689
690         @Override
691         public void setFirstName (final String firstName) {
692                 this.firstName = firstName;
693         }
694
695         @Override
696         public Gender getGender () {
697                 return this.gender;
698         }
699
700         @Override
701         public void setGender (final Gender gender) {
702                 this.gender = gender;
703         }
704
705         @Override
706         public Short getHouseNumber () {
707                 return this.houseNumber;
708         }
709
710         @Override
711         public void setHouseNumber (final Short houseNumber) {
712                 this.houseNumber = houseNumber;
713         }
714
715         @Override
716         public Integer getPhoneAreaCode () {
717                 return this.phoneAreaCode;
718         }
719
720         @Override
721         public void setPhoneAreaCode (final Integer phoneAreaCode) {
722                 this.phoneAreaCode = phoneAreaCode;
723         }
724
725         @Override
726         public Country getPhoneCountry () {
727                 return this.phoneCountry;
728         }
729
730         @Override
731         public void setPhoneCountry (final Country phoneCountry) {
732                 this.phoneCountry = phoneCountry;
733         }
734
735         @Override
736         public Long getPhoneNumber () {
737                 return this.phoneNumber;
738         }
739
740         @Override
741         public void setPhoneNumber (final Long phoneNumber) {
742                 this.phoneNumber = phoneNumber;
743         }
744
745         @Override
746         public String getStreet () {
747                 return this.street;
748         }
749
750         @Override
751         public void setStreet (final String street) {
752                 this.street = street;
753         }
754
755         @Override
756         public Integer getZipCode () {
757                 return this.zipCode;
758         }
759
760         @Override
761         public void setZipCode (final Integer zipCode) {
762                 this.zipCode = zipCode;
763         }
764
765         @Override
766         public boolean hasContacts () {
767                 return (!this.allContacts().isEmpty());
768         }
769
770         /**
771          * Post-initialization of this class
772          */
773         @PostConstruct
774         public void init () {
775                 // Get full email address list for reducing EJB calls
776                 this.emailAddressList = this.contactBean.getEmailAddressList();
777
778                 // Get full contact list
779                 this.contactList = this.contactBean.getAllContacts();
780         }
781
782         @Override
783         public boolean isEmailAddressRegistered (final Contact contact) {
784                 return ((this.emailAddressList instanceof List) && (this.emailAddressList.contains(contact.getContactEmailAddress())));
785         }
786
787         @Override
788         public boolean isRequiredChangePersonalDataSet () {
789                 return ((this.getGender() != null) &&
790                                 (this.getFirstName() != null) &&
791                                 (this.getFamilyName() != null) &&
792                                 (this.getStreet() != null) &&
793                                 (this.getHouseNumber() != null) &&
794                                 (this.getZipCode() != null) &&
795                                 (this.getCity() != null));
796         }
797
798         @Override
799         public boolean isRequiredPersonalDataSet () {
800                 return ((this.getGender() != null) &&
801                                 (this.getFirstName() != null) &&
802                                 (this.getFamilyName() != null) &&
803                                 (this.getStreet() != null) &&
804                                 (this.getHouseNumber() != null) &&
805                                 (this.getZipCode() != null) &&
806                                 (this.getCity() != null) &&
807                                 (this.getEmailAddress() != null) &&
808                                 (this.getEmailAddressRepeat() != null));
809         }
810
811         @Override
812         public boolean isSameEmailAddressEntered () {
813                 return (Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat()));
814         }
815
816         @Override
817         public Contact lookupContactById (final Long contactId) throws ContactNotFoundException {
818                 // Init variable
819                 Contact localContact = null;
820
821                 // Clear this bean
822                 this.clear();
823
824                 // Try to lookup it in visible user list
825                 for (final Iterator<Contact> iterator = this.contactList.iterator(); iterator.hasNext();) {
826                         // Get next user
827                         Contact next = iterator.next();
828
829                         // Is the user id found?
830                         if (Objects.equals(next.getContactId(), contactId)) {
831                                 // Copy to other variable
832                                 localContact = next;
833                                 break;
834                         }
835                 }
836
837                 // Is it still null?
838                 if (null == localContact) {
839                         // Not visible for the current user
840                         throw new ContactNotFoundException(contactId);
841                 }
842
843                 // Copy all data to this bean
844                 this.copyContact(localContact);
845
846                 // Return it
847                 return localContact;
848         }
849
850         @Override
851         public void updateContactDataFromController (final Contact contact) {
852                 // Is the instance valid?
853                 if (null == contact) {
854                         // Throw NPE
855                         throw new NullPointerException("contact is null"); //NOI18N
856                 } else if (contact.getContactId() == null) {
857                         // Throw NPE
858                         throw new NullPointerException("contact.contactId is null"); //NOI18N
859                 } else if (contact.getContactId() < 1) {
860                         // Not valid id number
861                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
862                 }
863
864                 // Set all
865                 this.copyContact(contact);
866         }
867
868         /**
869          * Adds email address to bean's internal list.
870          * <p>
871          * @param contact Contact instance
872          */
873         private void addUserNameEmailAddress (final Contact contact) {
874                 // Make sure the entry is not added yet
875                 if (this.emailAddressList.contains(contact.getContactEmailAddress())) {
876                         // Already added
877                         throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", contact.getContactEmailAddress())); //NOI18N
878                 }
879
880                 // Add email addres
881                 this.emailAddressList.add(contact.getContactEmailAddress());
882         }
883
884         /**
885          * Clears this bean
886          */
887         private void clear () {
888                 // Clear all data
889                 // - personal data
890                 this.setGender(Gender.UNKNOWN);
891                 this.setFirstName(null);
892                 this.setFamilyName(null);
893                 this.setStreet(null);
894                 this.setHouseNumber(null);
895                 this.setZipCode(null);
896                 this.setCity(null);
897                 this.setCountry(null);
898
899                 // - contact data
900                 this.setEmailAddress(null);
901                 this.setEmailAddressRepeat(null);
902                 this.setPhoneCountry(null);
903                 this.setPhoneAreaCode(null);
904                 this.setPhoneNumber(null);
905                 this.setCellphoneCarrier(null);
906                 this.setCellphoneNumber(null);
907                 this.setFaxCountry(null);
908                 this.setFaxAreaCode(null);
909                 this.setFaxNumber(null);
910
911                 // - other data
912                 this.setBirthday(null);
913                 this.setComment(null);
914         }
915
916         /**
917          * Copies given contact into the controller
918          * <p>
919          * @param contact Contact instance
920          */
921         private void copyContact (final Contact contact) {
922                 // Is the instance valid?
923                 if (null == contact) {
924                         // Throw NPE
925                         throw new NullPointerException("contact is null"); //NOI18N
926                 } else if (contact.getContactId() == null) {
927                         // Throw NPE
928                         throw new NullPointerException("contact.contactId is null"); //NOI18N
929                 } else if (contact.getContactId() < 1) {
930                         // Not valid id number
931                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
932                 }
933
934                 // Copy all fields:
935                 // - base data
936                 this.setGender(contact.getContactGender());
937                 this.setFirstName(contact.getContactFirstName());
938                 this.setFamilyName(contact.getContactFamilyName());
939                 this.setStreet(contact.getContactStreet());
940                 this.setHouseNumber(contact.getContactHouseNumber());
941                 this.setZipCode(contact.getContactZipCode());
942                 this.setCity(contact.getContactCity());
943                 this.setCountry(contact.getContactCountry());
944                 this.setEmailAddress(contact.getContactEmailAddress());
945                 this.setBirthday(contact.getContactBirthday());
946                 this.setComment(contact.getContactComment());
947
948                 // Get cellphone, phone and fax instance
949                 DialableCellphoneNumber cellphone = contact.getContactCellphoneNumber();
950                 DialableFaxNumber fax = contact.getContactFaxNumber();
951                 DialableLandLineNumber phone = contact.getContactLandLineNumber();
952
953                 // - contact data
954                 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneAreaCode() > 0)) {
955                         this.setPhoneCountry(phone.getPhoneCountry());
956                         this.setPhoneAreaCode(phone.getPhoneAreaCode());
957                         this.setPhoneNumber(phone.getPhoneNumber());
958                 }
959
960                 if ((cellphone instanceof DialableCellphoneNumber) && (cellphone.getCellphoneProvider() instanceof MobileProvider)) {
961                         this.setCellphoneCarrier(cellphone.getCellphoneProvider());
962                         this.setCellphoneNumber(cellphone.getPhoneNumber());
963                 }
964
965                 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneAreaCode() > 0)) {
966                         this.setFaxCountry(fax.getPhoneCountry());
967                         this.setFaxAreaCode(fax.getPhoneAreaCode());
968                         this.setFaxNumber(fax.getPhoneNumber());
969                 }
970         }
971
972         /**
973          * Removes given contact from all lists
974          * <p>
975          * @param contact Contact instance to remove
976          */
977         private void removeContact (final Contact contact) {
978                 // Is the instance valid?
979                 if (null == contact) {
980                         // Throw NPE
981                         throw new NullPointerException("contact is null"); //NOI18N
982                 } else if (contact.getContactId() == null) {
983                         // Throw NPE
984                         throw new NullPointerException("contact.contactId is null"); //NOI18N
985                 } else if (contact.getContactId() < 1) {
986                         // Not valid id number
987                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
988                 }
989
990                 // Remove from general list
991                 if (!this.contactList.remove(contact)) {
992                         // Did not remove contact
993                         throw new IllegalStateException(MessageFormat.format("contact {0} was not removed.", contact.getContactId())); //NOI18N
994                 }
995
996                 // Remove from other lists
997                 this.emailAddressList.remove(contact.getContactEmailAddress());
998         }
999
1000 }