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