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