]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/contact/PizzaContactWebSessionBean.java
Please cherry-pick:
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / contact / PizzaContactWebSessionBean.java
1 /*
2  * Copyright (C) 2016 Roland Häder
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.LinkedList;
24 import java.util.List;
25 import java.util.Objects;
26 import javax.annotation.PostConstruct;
27 import javax.enterprise.context.SessionScoped;
28 import javax.enterprise.event.Observes;
29 import javax.faces.view.facelets.FaceletException;
30 import javax.inject.Inject;
31 import javax.inject.Named;
32 import javax.naming.Context;
33 import javax.naming.InitialContext;
34 import javax.naming.NamingException;
35 import org.mxchange.jcontacts.contact.Contact;
36 import org.mxchange.jcontacts.contact.ContactSessionBeanRemote;
37 import org.mxchange.jcontacts.contact.UserContact;
38 import org.mxchange.jcontacts.contact.gender.Gender;
39 import org.mxchange.jcontacts.contact.utils.ContactUtils;
40 import org.mxchange.jcontacts.events.contact.add.ObservableAdminAddedContactEvent;
41 import org.mxchange.jcontacts.events.contact.deleted.ObservableAdminDeletedContactEvent;
42 import org.mxchange.jcontacts.events.contact.update.ObservableAdminUpdatedContactEvent;
43 import org.mxchange.jcontacts.events.mobile.unlinked.ObservableAdminUnlinkedMobileNumberEvent;
44 import org.mxchange.jcontacts.exceptions.ContactNotFoundException;
45 import org.mxchange.jcountry.data.Country;
46 import org.mxchange.jphone.events.mobile.remove.AdminRemoveMobileNumberFromListEvent;
47 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
48 import org.mxchange.jphone.phonenumbers.fax.FaxNumber;
49 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
50 import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
51 import org.mxchange.jphone.phonenumbers.mobile.DialableMobileNumber;
52 import org.mxchange.jphone.phonenumbers.mobile.MobileNumber;
53 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
54 import org.mxchange.jusercore.events.confirmation.ObservableUserConfirmedAccountEvent;
55 import org.mxchange.jusercore.events.login.ObservableUserLoggedInEvent;
56 import org.mxchange.jusercore.events.registration.ObservableUserRegisteredEvent;
57 import org.mxchange.jusercore.events.user.add.ObservableAdminAddedUserEvent;
58 import org.mxchange.jusercore.events.user.linked.ObservableAdminLinkedUserEvent;
59 import org.mxchange.jusercore.model.user.User;
60 import org.mxchange.pizzaapplication.beans.BasePizzaController;
61 import org.mxchange.pizzaapplication.beans.login.PizzaUserLoginWebSessionController;
62 import org.mxchange.pizzaapplication.beans.user.PizzaUserWebSessionController;
63
64 /**
65  * A general contact bean (controller)
66  * <p>
67  * @author Roland Häder<roland@mxchange.org>
68  */
69 @Named ("contactController")
70 @SessionScoped
71 public class PizzaContactWebSessionBean extends BasePizzaController implements PizzaContactWebSessionController {
72
73         /**
74          * Serial number
75          */
76         private static final long serialVersionUID = 542_145_347_916L;
77
78         /**
79          * Birth day
80          */
81         private Date birthday;
82
83         /**
84          * City
85          */
86         private String city;
87
88         /**
89          * Optional comments
90          */
91         private String comment;
92
93         /**
94          * Remote contact bean
95          */
96         private final ContactSessionBeanRemote contactBean;
97
98         /**
99          * Contact list
100          */
101         private final List<Contact> contacts;
102
103         /**
104          * Country instance
105          */
106         private Country country;
107
108         /**
109          * Email address
110          */
111         private String emailAddress;
112
113         /**
114          * Email address repeated
115          */
116         private String emailAddressRepeat;
117
118         /**
119          * Email address list
120          */
121         private final List<String> emailAddresses;
122
123         /**
124          * Family name
125          */
126         private String familyName;
127
128         /**
129          * Fax number's area code
130          */
131         private Integer faxAreaCode;
132
133         /**
134          * Country instance for fax number
135          */
136         private Country faxCountry;
137
138         /**
139          * Fax number
140          */
141         private Long faxNumber;
142
143         /**
144          * First name
145          */
146         private String firstName;
147
148         /**
149          * Gender instance
150          */
151         private Gender gender;
152
153         /**
154          * House number
155          */
156         private Short houseNumber;
157
158         /**
159          * House number extension
160          */
161         private String houseNumberExtension;
162
163         /**
164          * Whether a fax entry has been unlinked
165          */
166         private boolean isFaxUnlinked;
167
168         /**
169          * Whether a land-line number has been unlinked
170          */
171         private boolean isLandLineUnlinked;
172
173         /**
174          * Whether a mobile entry has been unlinked
175          */
176         private boolean isMobileUnlinked;
177
178         /**
179          * Land-line number area code
180          */
181         private Integer landLineAreaCode;
182
183         /**
184          * Country instance for land-line number
185          */
186         private Country landLineCountry;
187
188         /**
189          * Land-line number
190          */
191         private Long landLineNumber;
192
193         /**
194          * Mobile number
195          */
196         private Long mobileNumber;
197
198         /**
199          * Mobile provider
200          */
201         private MobileProvider mobileProvider;
202
203         /**
204          * A list of all selectable contacts
205          */
206         private List<Contact> selectableContacts;
207
208         /**
209          * Street
210          */
211         private String street;
212
213         /**
214          * Title
215          */
216         private String title;
217
218         /**
219          * Regular user controller
220          */
221         @Inject
222         private PizzaUserWebSessionController userController;
223
224         /**
225          * Login controller (bean)
226          */
227         @Inject
228         private PizzaUserLoginWebSessionController userLoginController;
229
230         /**
231          * ZIP code
232          */
233         private Integer zipCode;
234
235         /**
236          * Default constructor
237          */
238         public PizzaContactWebSessionBean () {
239                 // Try it
240                 try {
241                         // Get initial context
242                         Context context = new InitialContext();
243
244                         // Try to lookup
245                         this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/jjobs-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
246                 } catch (final NamingException e) {
247                         // Throw again
248                         throw new FaceletException(e);
249                 }
250
251                 // Init lists/maps
252                 this.contacts = new LinkedList<>();
253                 this.emailAddresses = new LinkedList<>();
254         }
255
256         /**
257          * Observes events being fired when an administrator has added a new
258          * contact.
259          * <p>
260          * @param event Event being fired
261          */
262         public void afterAdminAddedContactEvent (@Observes final ObservableAdminAddedContactEvent event) {
263                 // The event must be valid
264                 if (null == event) {
265                         // Throw NPE
266                         throw new NullPointerException("event is null"); //NOI18N
267                 } else if (event.getAddedContact() == null) {
268                         // Throw again ...
269                         throw new NullPointerException("event.addedContact is null"); //NOI18N
270                 } else if (event.getAddedContact().getContactId() == null) {
271                         // ... and again
272                         throw new NullPointerException("event.addedContact.contactId is null"); //NOI18N
273                 } else if (event.getAddedContact().getContactId() < 1) {
274                         // Not valid
275                         throw new IllegalArgumentException(MessageFormat.format("event.addedContact.contactId={0} is not valid", event.getAddedContact().getContactId())); //NOI18N
276                 }
277
278                 // Clear this bean
279                 this.clear();
280
281                 // Call other method
282                 this.uniqueAddContact(event.getAddedContact());
283
284                 // Add to selectable contacts
285                 this.selectableContacts.add(event.getAddedContact());
286         }
287
288         /**
289          * Event observer for newly added users by adminstrator
290          * <p>
291          * @param event Event being fired
292          */
293         public void afterAdminAddedUserEvent (@Observes final ObservableAdminAddedUserEvent event) {
294                 // event should not be null
295                 if (null == event) {
296                         // Throw NPE
297                         throw new NullPointerException("event is null"); //NOI18N
298                 } else if (event.getAddedUser() == null) {
299                         // Throw NPE again
300                         throw new NullPointerException("event.addedUser is null"); //NOI18N
301                 } else if (event.getAddedUser().getUserId() == null) {
302                         // userId is null
303                         throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
304                 } else if (event.getAddedUser().getUserId() < 1) {
305                         // Not avalid id
306                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
307                 }
308
309                 // Clear all data
310                 this.clear();
311         }
312
313         /**
314          * Event observer for deleted contact instance by administrator
315          * <p>
316          * @param event Event being fired
317          */
318         public void afterAdminDeletedContactEvent (@Observes final ObservableAdminDeletedContactEvent event) {
319                 // event should not be null
320                 if (null == event) {
321                         // Throw NPE
322                         throw new NullPointerException("event is null"); //NOI18N
323                 } else if (event.getDeletedContact() == null) {
324                         // Throw NPE again
325                         throw new NullPointerException("event.deletedContact is null"); //NOI18N
326                 } else if (event.getDeletedContact().getContactId() == null) {
327                         // userId is null
328                         throw new NullPointerException("event.deletedContact.contactId is null"); //NOI18N
329                 } else if (event.getDeletedContact().getContactId() < 1) {
330                         // Not avalid id
331                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getDeletedContact(), event.getDeletedContact().getContactId())); //NOI18N
332                 }
333
334                 // Remove from both lists
335                 this.contacts.remove(event.getDeletedContact());
336                 this.selectableContacts.remove(event.getDeletedContact());
337
338                 // Clear all data
339                 this.clear();
340         }
341
342         /**
343          * Observes events being fired when an administrator has linked a new user
344          * with existing contact data.
345          * <p>
346          * @param event Event being fired
347          */
348         public void afterAdminLinkedUserEvent (@Observes final ObservableAdminLinkedUserEvent event) {
349                 // event should not be null
350                 if (null == event) {
351                         // Throw NPE
352                         throw new NullPointerException("event is null"); //NOI18N
353                 } else if (event.getLinkedUser() == null) {
354                         // Throw NPE again
355                         throw new NullPointerException("event.linkedUser is null"); //NOI18N
356                 } else if (event.getLinkedUser().getUserContact() == null) {
357                         // Throw NPE again
358                         throw new NullPointerException("event.linkedUser.userContact is null"); //NOI18N
359                 } else if (event.getLinkedUser().getUserContact().getContactId() == null) {
360                         // userId is null
361                         throw new NullPointerException("event.linkedUser.userContact.contactId is null"); //NOI18N
362                 } else if (event.getLinkedUser().getUserContact().getContactId() < 1) {
363                         // Not avalid id
364                         throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getLinkedUser(), event.getLinkedUser().getUserContact().getContactId())); //NOI18N
365                 }
366
367                 // Remove contact from list available contacts list
368                 this.selectableContacts.remove(event.getLinkedUser().getUserContact());
369
370                 // Clear all data
371                 this.clear();
372         }
373
374         /**
375          * Event observer for updated contact data by administrators
376          * <p>
377          * @param event Updated contact data event
378          */
379         public void afterAdminUpdatedContactDataEvent (@Observes final ObservableAdminUpdatedContactEvent event) {
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.getUpdatedContact() == null) {
385                         // Throw NPE again
386                         throw new NullPointerException("event.updatedContact is null"); //NOI18N
387                 } else if (event.getUpdatedContact().getContactId() == null) {
388                         // userId is null
389                         throw new NullPointerException("event.updatedContact.contactId is null"); //NOI18N
390                 } else if (event.getUpdatedContact().getContactId() < 1) {
391                         // Not avalid id
392                         throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
393                 }
394
395                 // Add contact instance only once
396                 this.uniqueAddContact(event.getUpdatedContact());
397
398                 // Add email address to list
399                 this.emailAddresses.add(event.getUpdatedContact().getContactEmailAddress());
400         }
401
402         /**
403          * Event observer for new user registrations
404          * <p>
405          * @param event User registration event
406          */
407         public void afterUserRegistrationEvent (@Observes final ObservableUserRegisteredEvent event) {
408                 // event should not be null
409                 if (null == event) {
410                         // Throw NPE
411                         throw new NullPointerException("event is null"); //NOI18N
412                 } else if (event.getRegisteredUser() == null) {
413                         // Throw NPE again
414                         throw new NullPointerException("event.registeredUser is null"); //NOI18N
415                 } else if (event.getRegisteredUser().getUserId() == null) {
416                         // userId is null
417                         throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
418                 } else if (event.getRegisteredUser().getUserId() < 1) {
419                         // Not avalid id
420                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
421                 }
422
423                 // Get user instance
424                 Contact registeredContact = event.getRegisteredUser().getUserContact();
425
426                 // Copy all data from registered->user
427                 this.copyContact(registeredContact);
428
429                 // Add user name and email address
430                 this.addUserNameEmailAddress(registeredContact);
431
432                 // Clear all data
433                 this.clear();
434         }
435
436         /**
437          * Event observer for unlinked mobile contact by administrators
438          * <p>
439          * @param event Unlinked mobile contact event
440          */
441         public void afterAdminUnlinkedMobileContactDataEvent (@Observes final ObservableAdminUnlinkedMobileNumberEvent event) {
442                 // event should not be null
443                 if (null == event) {
444                         // Throw NPE
445                         throw new NullPointerException("event is null"); //NOI18N
446                 } else if (event.getUnlinkedMobileNumber() == null) {
447                         // Throw NPE again
448                         throw new NullPointerException("event.unlinkedMobileNumber is null"); //NOI18N
449                 } else if (event.getUnlinkedMobileNumber().getPhoneId() == null) {
450                         // userId is null
451                         throw new NullPointerException("event.unlinkedMobileNumber.contactId is null"); //NOI18N
452                 } else if (event.getUnlinkedMobileNumber().getPhoneId() < 1) {
453                         // Not avalid id
454                         throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUnlinkedMobileNumber(), event.getUnlinkedMobileNumber().getPhoneId())); //NOI18N
455                 }
456
457                 // Remove it from list
458                 this.removeFromContacts(event.getUnlinkedMobileNumber());
459
460                 // Clear all data
461                 this.clear();
462         }
463
464         /**
465          * Event observer when user confirmed account.
466          * <p>
467          * @param event Event being fired
468          */
469         public void afterUserConfirmedAccount (@Observes final ObservableUserConfirmedAccountEvent event) {
470                 // event should not be null
471                 if (null == event) {
472                         // Throw NPE
473                         throw new NullPointerException("event is null"); //NOI18N
474                 } else if (event.getConfirmedUser() == null) {
475                         // Throw NPE again
476                         throw new NullPointerException("event.confirmedUser is null"); //NOI18N
477                 } else if (event.getConfirmedUser().getUserId() == null) {
478                         // userId is null
479                         throw new NullPointerException("event.confirmedUser.userId is null"); //NOI18N
480                 } else if (event.getConfirmedUser().getUserId() < 1) {
481                         // Not avalid id
482                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getConfirmedUser(), event.getConfirmedUser().getUserId())); //NOI18N
483                 }
484
485                 // Add contact instance only once
486                 this.uniqueAddContact(event.getConfirmedUser().getUserContact());
487         }
488
489         /**
490          * Event observer for logged-in user
491          * <p>
492          * @param event Event instance
493          */
494         public void afterUserLoginEvent (@Observes final ObservableUserLoggedInEvent event) {
495                 // event should not be null
496                 if (null == event) {
497                         // Throw NPE
498                         throw new NullPointerException("event is null"); //NOI18N
499                 } else if (event.getLoggedInUser() == null) {
500                         // Throw NPE again
501                         throw new NullPointerException("event.loggedInUser is null"); //NOI18N
502                 } else if (event.getLoggedInUser().getUserId() == null) {
503                         // userId is null
504                         throw new NullPointerException("event.loggedInUser.userId is null"); //NOI18N
505                 } else if (event.getLoggedInUser().getUserId() < 1) {
506                         // Not avalid id
507                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
508                 }
509
510                 // Copy all data to this bean
511                 this.copyContact(event.getLoggedInUser().getUserContact());
512         }
513
514         @Override
515         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
516         public List<Contact> allContacts () {
517                 // Return un-modified list
518                 return this.contacts;
519         }
520
521         @Override
522         public Contact createContactInstance () {
523                 // Is all required data set?
524                 if (!this.isRequiredPersonalDataSet()) {
525                         // No, then abort here
526                         throw new FaceletException(new IllegalArgumentException("Not all personal data is set, but createContactInstance() is called.")); //NOI18N
527                 }
528
529                 // Generate phone number
530                 DialableLandLineNumber phone = new LandLineNumber(this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber());
531                 DialableMobileNumber mobile = new MobileNumber(this.getMobileProvider(), this.getMobileNumber());
532                 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
533
534                 // Create new contact
535                 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
536                 contact.setContactStreet(this.getStreet());
537                 contact.setContactHouseNumber(this.getHouseNumber());
538                 contact.setContactHouseNumberExtension(this.getHouseNumberExtension());
539                 contact.setContactZipCode(this.getZipCode());
540                 contact.setContactCity(this.getCity());
541                 contact.setContactCountry(this.getCountry());
542                 contact.setContactEmailAddress(this.getEmailAddress());
543                 contact.setContactBirthday(this.getBirthday());
544                 contact.setContactComment(this.getComment());
545
546                 // Don't set null or wrong references
547                 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getLandLineAreaCode() != null) && (this.getLandLineNumber() != null) && (this.getLandLineAreaCode() > 0) && (this.getLandLineNumber() > 0)) {
548                         // Now the number must be given
549                         if (phone.getPhoneAreaCode() == null) {
550                                 // Is null
551                                 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
552                         } else if (phone.getPhoneAreaCode() < 1) {
553                                 // Abort here
554                                 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
555                         } else if (phone.getPhoneNumber() == null) {
556                                 // Is null
557                                 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
558                         } else if (phone.getPhoneNumber() < 1) {
559                                 // Abort here
560                                 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
561                         }
562
563                         // Set phone number
564                         contact.setContactLandLineNumber(phone);
565                 }
566
567                 // Don't set null or wrong references
568                 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
569                         // Now the number must be given
570                         if (fax.getPhoneAreaCode() == null) {
571                                 // Is null
572                                 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
573                         } else if (fax.getPhoneAreaCode() < 1) {
574                                 // Abort here
575                                 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
576                         } else if (fax.getPhoneNumber() == null) {
577                                 // Is null
578                                 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
579                         } else if (fax.getPhoneNumber() < 1) {
580                                 // Abort here
581                                 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
582                         }
583
584                         // Set fax number
585                         contact.setContactFaxNumber(fax);
586                 }
587
588                 // Is the provider set?
589                 if ((mobile instanceof DialableMobileNumber) && (this.getMobileProvider() instanceof MobileProvider) && (this.getMobileNumber() != null) && (this.getMobileNumber() > 0)) {
590                         // Is the number set?
591                         if (mobile.getPhoneNumber() == null) {
592                                 // Is null
593                                 throw new NullPointerException("mobileNumber.phoneNumber is null"); //NOI18N
594                         } else if (mobile.getPhoneNumber() < 1) {
595                                 // Abort here
596                                 throw new IllegalArgumentException("mobileNumber.phoneNumber is zero or below."); //NOI18N
597                         }
598
599                         // Set mobile number
600                         contact.setContactMobileNumber(mobile);
601                 }
602
603                 // Return it
604                 return contact;
605         }
606
607         @Override
608         public String doChangePersonalContactData () {
609                 // This method shall only be called if the user is logged-in
610                 if (!this.userLoginController.isUserLoggedIn()) {
611                         // Not logged-in
612                         throw new IllegalStateException("User is not logged-in"); //NOI18N
613                 } else if (!this.isRequiredChangePersonalDataSet()) {
614                         // Not all required fields are set
615                         throw new FaceletException("Not all required fields are set."); //NOI18N
616                 } else if (!this.userLoginController.ifCurrentPasswordMatches()) {
617                         // Password not matching
618                         this.showFacesMessage("form_login_change_personal:currentPassword", "ERROR_USER_CURRENT_PASSWORD_MISMATCHING"); //NOI18N
619                         return ""; //NOI18N
620                 }
621
622                 // Get contact instance
623                 Contact contact = this.userLoginController.getLoggedInUser().getUserContact();
624
625                 // It should be there, so run some tests on it
626                 assert (contact instanceof Contact) : "Instance userLoginController.loggedInUser.userContact is null"; //NOI18N
627                 assert (contact.getContactId() instanceof Long) : "Instance userLoginController.userContact.contactId is null"; //NOI18N
628                 assert (contact.getContactId() > 0) : MessageFormat.format("Instance userLoginController.userContact.contactId={0} is invalid", contact.getContactId()); //NOI18N
629
630                 // Update all fields
631                 contact.setContactGender(this.getGender());
632                 contact.setContactFirstName(this.getFirstName());
633                 contact.setContactFamilyName(this.getFamilyName());
634                 contact.setContactStreet(this.getStreet());
635                 contact.setContactHouseNumber(this.getHouseNumber());
636                 contact.setContactHouseNumberExtension(this.getHouseNumberExtension());
637                 contact.setContactZipCode(this.getZipCode());
638                 contact.setContactCity(this.getCity());
639                 contact.setContactCountry(this.getCountry());
640
641                 // Update contact's mobile number
642                 this.isMobileUnlinked = ContactUtils.updateMobileNumber(contact, this.getMobileProvider(), this.getMobileNumber());
643
644                 // Update contact's land-line number
645                 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber());
646
647                 // Update contact's fax number
648                 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
649
650                 // Send it to the EJB
651                 this.contactBean.updateContactData(contact, this.isMobileUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
652
653                 // All fine
654                 return "user_contact_data_saved"; //NOI18N
655         }
656
657         @Override
658         @SuppressWarnings ("ReturnOfDateField")
659         public Date getBirthday () {
660                 return this.birthday;
661         }
662
663         @Override
664         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
665         public void setBirthday (final Date birthday) {
666                 this.birthday = birthday;
667         }
668
669         @Override
670         public String getCity () {
671                 return this.city;
672         }
673
674         @Override
675         public void setCity (final String city) {
676                 this.city = city;
677         }
678
679         @Override
680         public String getComment () {
681                 return this.comment;
682         }
683
684         @Override
685         public void setComment (final String comment) {
686                 this.comment = comment;
687         }
688
689         @Override
690         public String getControllerType () {
691                 return "general"; //NOI18N
692         }
693
694         @Override
695         @Deprecated
696         public void setControllerType (final String controllerType) {
697                 throw new UnsupportedOperationException("Setting controller type is not supported."); //NOI18N
698         }
699
700         @Override
701         public Country getCountry () {
702                 return this.country;
703         }
704
705         @Override
706         public void setCountry (final Country country) {
707                 this.country = country;
708         }
709
710         @Override
711         public String getEmailAddress () {
712                 return this.emailAddress;
713         }
714
715         @Override
716         public void setEmailAddress (final String emailAddress) {
717                 this.emailAddress = emailAddress;
718         }
719
720         @Override
721         public String getEmailAddressRepeat () {
722                 return this.emailAddressRepeat;
723         }
724
725         @Override
726         public void setEmailAddressRepeat (final String emailAddressRepeat) {
727                 this.emailAddressRepeat = emailAddressRepeat;
728         }
729
730         @Override
731         public String getFamilyName () {
732                 return this.familyName;
733         }
734
735         @Override
736         public void setFamilyName (final String familyName) {
737                 this.familyName = familyName;
738         }
739
740         @Override
741         public Integer getFaxAreaCode () {
742                 return this.faxAreaCode;
743         }
744
745         @Override
746         public void setFaxAreaCode (final Integer faxAreaCode) {
747                 this.faxAreaCode = faxAreaCode;
748         }
749
750         @Override
751         public Country getFaxCountry () {
752                 return this.faxCountry;
753         }
754
755         @Override
756         public void setFaxCountry (final Country faxCountry) {
757                 this.faxCountry = faxCountry;
758         }
759
760         @Override
761         public Long getFaxNumber () {
762                 return this.faxNumber;
763         }
764
765         @Override
766         public void setFaxNumber (final Long faxNumber) {
767                 this.faxNumber = faxNumber;
768         }
769
770         @Override
771         public String getFirstName () {
772                 return this.firstName;
773         }
774
775         @Override
776         public void setFirstName (final String firstName) {
777                 this.firstName = firstName;
778         }
779
780         @Override
781         public Gender getGender () {
782                 return this.gender;
783         }
784
785         @Override
786         public void setGender (final Gender gender) {
787                 this.gender = gender;
788         }
789
790         @Override
791         public Short getHouseNumber () {
792                 return this.houseNumber;
793         }
794
795         @Override
796         public void setHouseNumber (final Short houseNumber) {
797                 this.houseNumber = houseNumber;
798         }
799
800         @Override
801         public String getHouseNumberExtension () {
802                 return this.houseNumberExtension;
803         }
804
805         @Override
806         public void setHouseNumberExtension (final String houseNumberExtension) {
807                 this.houseNumberExtension = houseNumberExtension;
808         }
809
810         @Override
811         public Integer getLandLineAreaCode () {
812                 return this.landLineAreaCode;
813         }
814
815         @Override
816         public void setLandLineAreaCode (final Integer landLineAreaCode) {
817                 this.landLineAreaCode = landLineAreaCode;
818         }
819
820         @Override
821         public Country getLandLineCountry () {
822                 return this.landLineCountry;
823         }
824
825         @Override
826         public void setLandLineCountry (final Country landLineCountry) {
827                 this.landLineCountry = landLineCountry;
828         }
829
830         @Override
831         public Long getLandLineNumber () {
832                 return this.landLineNumber;
833         }
834
835         @Override
836         public void setLandLineNumber (final Long landLineNumber) {
837                 this.landLineNumber = landLineNumber;
838         }
839
840         @Override
841         public Long getMobileNumber () {
842                 return this.mobileNumber;
843         }
844
845         @Override
846         public void setMobileNumber (Long mobileNumber) {
847                 this.mobileNumber = mobileNumber;
848         }
849
850         @Override
851         public MobileProvider getMobileProvider () {
852                 return this.mobileProvider;
853         }
854
855         @Override
856         public void setMobileProvider (final MobileProvider mobileProvider) {
857                 this.mobileProvider = mobileProvider;
858         }
859
860         @Override
861         public String getStreet () {
862                 return this.street;
863         }
864
865         @Override
866         public void setStreet (final String street) {
867                 this.street = street;
868         }
869
870         @Override
871         public String getTitle () {
872                 return this.title;
873         }
874
875         @Override
876         public void setTitle (final String title) {
877                 this.title = title;
878         }
879
880         @Override
881         public Integer getZipCode () {
882                 return this.zipCode;
883         }
884
885         @Override
886         public void setZipCode (final Integer zipCode) {
887                 this.zipCode = zipCode;
888         }
889
890         /**
891          * Post-initialization of this class
892          */
893         @PostConstruct
894         public void init () {
895                 // Get full email address list for reducing EJB calls
896                 this.emailAddresses.addAll(this.contactBean.getEmailAddressList());
897
898                 // Get all contacts
899                 List<Contact> allContacts = this.contactBean.getAllContacts();
900
901                 // Get full contact list
902                 this.contacts.addAll(allContacts);
903
904                 // Get all users
905                 List<User> allUsers = this.userController.allUsers();
906
907                 // Get iterator
908                 Iterator<Contact> iterator = allContacts.iterator();
909
910                 // Loop through it
911                 while (iterator.hasNext()) {
912                         // Get next element
913                         Contact next = iterator.next();
914
915                         // Get iterator
916                         Iterator<User> userIterator = allUsers.iterator();
917
918                         // Loop through all users
919                         while (userIterator.hasNext()) {
920                                 // Get user instance
921                                 User nextUser = userIterator.next();
922
923                                 // Is contact same?
924                                 if (Objects.equals(next, nextUser.getUserContact())) {
925                                         // Found same
926                                         iterator.remove();
927                                         break;
928                                 }
929                         }
930                 }
931
932                 // Set contact list
933                 this.selectableContacts = allContacts;
934         }
935
936         @Override
937         public boolean isEmailAddressRegistered (final Contact contact) {
938                 // Cherck parameter
939                 if (null == contact) {
940                         // Throw NPE
941                         throw new NullPointerException("contact is null"); //NOI18N
942                 } else if (contact.getContactEmailAddress() == null) {
943                         // Throw again
944                         throw new NullPointerException("contact.contactEmailAddress is null"); //NOI18N
945                 } else if (contact.getContactEmailAddress().isEmpty()) {
946                         // Is empty
947                         throw new IllegalArgumentException("contact.contactEmailAddress is empty."); //NOI18N
948                 }
949
950                 // Determine it
951                 return ((this.emailAddresses instanceof List) && (this.emailAddresses.contains(contact.getContactEmailAddress())));
952         }
953
954         @Override
955         public boolean isRequiredChangePersonalDataSet () {
956                 return ((this.getGender() != null) &&
957                                 (this.getFirstName() != null) &&
958                                 (this.getFamilyName() != null) &&
959                                 (this.getStreet() != null) &&
960                                 (this.getHouseNumber() != null) &&
961                                 (this.getZipCode() != null) &&
962                                 (this.getCity() != null));
963         }
964
965         @Override
966         public boolean isRequiredPersonalDataSet () {
967                 return ((this.getGender() != null) &&
968                                 (this.getFirstName() != null) &&
969                                 (this.getFamilyName() != null) &&
970                                 (this.getStreet() != null) &&
971                                 (this.getHouseNumber() != null) &&
972                                 (this.getZipCode() != null) &&
973                                 (this.getCity() != null) &&
974                                 (this.getEmailAddress() != null) &&
975                                 (this.getEmailAddressRepeat() != null));
976         }
977
978         @Override
979         public boolean isSameEmailAddressEntered () {
980                 return (Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat()));
981         }
982
983         @Override
984         public Contact lookupContactById (final Long contactId) throws ContactNotFoundException {
985                 // Init variable
986                 Contact localContact = null;
987
988                 // Clear this bean
989                 this.clear();
990
991                 // Try to lookup it in visible user list
992                 for (final Iterator<Contact> iterator = this.contacts.iterator(); iterator.hasNext();) {
993                         // Get next user
994                         Contact next = iterator.next();
995
996                         // Is the user id found?
997                         if (Objects.equals(next.getContactId(), contactId)) {
998                                 // Copy to other variable
999                                 localContact = next;
1000                                 break;
1001                         }
1002                 }
1003
1004                 // Is it still null?
1005                 if (null == localContact) {
1006                         // Not visible for the current user
1007                         throw new ContactNotFoundException(contactId);
1008                 }
1009
1010                 // Copy all data to this bean
1011                 this.copyContact(localContact);
1012
1013                 // Return it
1014                 return localContact;
1015         }
1016
1017         /**
1018          * Event observer when a list with unused mobile numbers is being created
1019          * <p>
1020          * @param event Event being fired
1021          */
1022         public void removeMobileNumberFromListEvent (@Observes final AdminRemoveMobileNumberFromListEvent event) {
1023                 // event should not be null
1024                 if (null == event) {
1025                         // Throw NPE
1026                         throw new NullPointerException("event is null"); //NOI18N
1027                 } else if (event.getMobileNumberList() == null) {
1028                         // Throw NPE again
1029                         throw new NullPointerException("event.mobileList is null"); //NOI18N
1030                 } else if (event.getMobileNumberList().isEmpty()) {
1031                         // List is empty, no need to check
1032                         return;
1033                 }
1034
1035                 // Check all entries
1036                 for (final Contact contact : this.contacts) {
1037                         // Is the mobile instance set and in list?
1038                         if ((contact.getContactMobileNumber() instanceof DialableMobileNumber) && (event.getMobileNumberList().contains(contact.getContactMobileNumber()))) {
1039                                 // Found it, so remvoe it from list
1040                                 event.getMobileNumberList().remove(contact.getContactMobileNumber());
1041                         }
1042                 }
1043         }
1044
1045         @Override
1046         public List<Contact> selectableContacts () {
1047                 return Collections.unmodifiableList(this.selectableContacts);
1048         }
1049
1050         @Override
1051         public void updateContactDataFromController (final Contact contact) {
1052                 // Is the instance valid?
1053                 if (null == contact) {
1054                         // Throw NPE
1055                         throw new NullPointerException("contact is null"); //NOI18N
1056                 } else if (contact.getContactId() == null) {
1057                         // Throw NPE
1058                         throw new NullPointerException("contact.contactId is null"); //NOI18N
1059                 } else if (contact.getContactId() < 1) {
1060                         // Not valid id number
1061                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1062                 }
1063
1064                 // Set all
1065                 this.copyContact(contact);
1066         }
1067
1068         /**
1069          * Adds email address to bean's internal list.
1070          * <p>
1071          * @param contact Contact instance
1072          */
1073         private void addUserNameEmailAddress (final Contact contact) {
1074                 // Make sure the entry is not added yet
1075                 if (this.emailAddresses.contains(contact.getContactEmailAddress())) {
1076                         // Already added
1077                         throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", contact.getContactEmailAddress())); //NOI18N
1078                 }
1079
1080                 // Add email addres
1081                 this.emailAddresses.add(contact.getContactEmailAddress());
1082         }
1083
1084         /**
1085          * Clears this bean
1086          */
1087         private void clear () {
1088                 // Clear all data
1089                 // - personal data
1090                 this.setGender(null);
1091                 this.setTitle(null);
1092                 this.setFirstName(null);
1093                 this.setFamilyName(null);
1094                 this.setStreet(null);
1095                 this.setHouseNumber(null);
1096                 this.setHouseNumberExtension(null);
1097                 this.setZipCode(null);
1098                 this.setCity(null);
1099                 this.setCountry(null);
1100
1101                 // - contact data
1102                 this.setEmailAddress(null);
1103                 this.setEmailAddressRepeat(null);
1104                 this.setLandLineAreaCode(null);
1105                 this.setLandLineCountry(null);
1106                 this.setLandLineNumber(null);
1107                 this.setMobileProvider(null);
1108                 this.setMobileNumber(null);
1109                 this.setFaxAreaCode(null);
1110                 this.setFaxCountry(null);
1111                 this.setFaxNumber(null);
1112
1113                 // - other data
1114                 this.setBirthday(null);
1115                 this.setComment(null);
1116         }
1117
1118         /**
1119          * Copies given contact into the controller
1120          * <p>
1121          * @param contact Contact instance
1122          */
1123         private void copyContact (final Contact contact) {
1124                 // Is the instance valid?
1125                 if (null == contact) {
1126                         // Throw NPE
1127                         throw new NullPointerException("contact is null"); //NOI18N
1128                 } else if (contact.getContactId() == null) {
1129                         // Throw NPE
1130                         throw new NullPointerException("contact.contactId is null"); //NOI18N
1131                 } else if (contact.getContactId() < 1) {
1132                         // Not valid id number
1133                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1134                 }
1135
1136                 // Copy all fields:
1137                 // - base data
1138                 this.setGender(contact.getContactGender());
1139                 this.setTitle(contact.getContactTitle());
1140                 this.setFirstName(contact.getContactFirstName());
1141                 this.setFamilyName(contact.getContactFamilyName());
1142                 this.setStreet(contact.getContactStreet());
1143                 this.setHouseNumber(contact.getContactHouseNumber());
1144                 this.setHouseNumberExtension(contact.getContactHouseNumberExtension());
1145                 this.setZipCode(contact.getContactZipCode());
1146                 this.setCity(contact.getContactCity());
1147                 this.setCountry(contact.getContactCountry());
1148                 this.setEmailAddress(contact.getContactEmailAddress());
1149                 this.setBirthday(contact.getContactBirthday());
1150                 this.setComment(contact.getContactComment());
1151
1152                 // Get mobile, phone and fax instance
1153                 DialableMobileNumber mobile = contact.getContactMobileNumber();
1154                 DialableFaxNumber fax = contact.getContactFaxNumber();
1155                 DialableLandLineNumber phone = contact.getContactLandLineNumber();
1156
1157                 // - contact data
1158                 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneAreaCode() > 0)) {
1159                         this.setLandLineCountry(phone.getPhoneCountry());
1160                         this.setLandLineAreaCode(phone.getPhoneAreaCode());
1161                         this.setLandLineNumber(phone.getPhoneNumber());
1162                 }
1163
1164                 if ((mobile instanceof DialableMobileNumber) && (mobile.getMobileProvider() instanceof MobileProvider)) {
1165                         this.setMobileProvider(mobile.getMobileProvider());
1166                         this.setMobileNumber(mobile.getPhoneNumber());
1167                 }
1168
1169                 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneAreaCode() > 0)) {
1170                         this.setFaxCountry(fax.getPhoneCountry());
1171                         this.setFaxAreaCode(fax.getPhoneAreaCode());
1172                         this.setFaxNumber(fax.getPhoneNumber());
1173                 }
1174         }
1175
1176         /**
1177          * Removes given contact from all lists
1178          * <p>
1179          * @param contact Contact instance to remove
1180          */
1181         private void removeContact (final Contact contact) {
1182                 // Is the instance valid?
1183                 if (null == contact) {
1184                         // Throw NPE
1185                         throw new NullPointerException("contact is null"); //NOI18N
1186                 } else if (contact.getContactId() == null) {
1187                         // Throw NPE
1188                         throw new NullPointerException("contact.contactId is null"); //NOI18N
1189                 } else if (contact.getContactId() < 1) {
1190                         // Not valid id number
1191                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1192                 }
1193
1194                 // Remove from general list
1195                 if (!this.contacts.remove(contact)) {
1196                         // Did not remove contact
1197                         throw new IllegalStateException(MessageFormat.format("contact {0} was not removed.", contact.getContactId())); //NOI18N
1198                 }
1199
1200                 // Remove from other lists
1201                 this.emailAddresses.remove(contact.getContactEmailAddress());
1202         }
1203
1204         /**
1205          * Removes given fax number from all contacts
1206          * <p>
1207          * @param faxNumber Fax number to remove
1208          */
1209         private void removeFromContacts (final DialableFaxNumber faxNumber) {
1210                 // Is it valid?
1211                 if (null == faxNumber) {
1212                         // Throw NPE
1213                         throw new NullPointerException("faxNumber is null"); //NOI18N
1214                 } else if (faxNumber.getPhoneId() == null) {
1215                         // Throw NPE again
1216                         throw new NullPointerException("faxNumber.phoneId is null"); //NOI18N
1217                 } else if (faxNumber.getPhoneId() < 1) {
1218                         // Throw NPE again
1219                         throw new NullPointerException(MessageFormat.format("faxNumber.phoneId={0} is not valid", faxNumber.getPhoneId())); //NOI18N
1220                 }
1221
1222                 // Loop through all contacts
1223                 for (final Contact contact : this.contacts) {
1224                         // Is the number set?
1225                         if (Objects.equals(contact.getContactFaxNumber(), faxNumber)) {
1226                                 // Found it
1227                                 contact.setContactFaxNumber(null);
1228                         }
1229                 }
1230         }
1231
1232         /**
1233          * Removes given land-line number from all contacts
1234          * <p>
1235          * @param landLineNumber Land-line number to remove
1236          */
1237         private void removeFromContacts (final DialableLandLineNumber landLineNumber) {
1238                 // Is it valid?
1239                 if (null == landLineNumber) {
1240                         // Throw NPE
1241                         throw new NullPointerException("landLineNumber is null"); //NOI18N
1242                 } else if (landLineNumber.getPhoneId() == null) {
1243                         // Throw NPE again
1244                         throw new NullPointerException("landLineNumber.phoneId is null"); //NOI18N
1245                 } else if (landLineNumber.getPhoneId() < 1) {
1246                         // Throw NPE again
1247                         throw new NullPointerException(MessageFormat.format("landLineNumber.phoneId={0} is not valid", landLineNumber.getPhoneId())); //NOI18N
1248                 }
1249
1250                 // Loop through all contacts
1251                 for (final Contact contact : this.contacts) {
1252                         // Is the number set?
1253                         if (Objects.equals(contact.getContactLandLineNumber(), landLineNumber)) {
1254                                 // Found it
1255                                 contact.setContactLandLineNumber(null);
1256                         }
1257                 }
1258         }
1259
1260         /**
1261          * Removes given mobile number from all contacts
1262          * <p>
1263          * @param mobileNumber Mobile number to remove
1264          */
1265         private void removeFromContacts (final DialableMobileNumber mobileNumber) {
1266                 // Is it valid?
1267                 if (null == mobileNumber) {
1268                         // Throw NPE
1269                         throw new NullPointerException("mobileNumber is null"); //NOI18N
1270                 } else if (mobileNumber.getPhoneId() == null) {
1271                         // Throw NPE again
1272                         throw new NullPointerException("mobileNumber.phoneId is null"); //NOI18N
1273                 } else if (mobileNumber.getPhoneId() < 1) {
1274                         // Throw NPE again
1275                         throw new NullPointerException(MessageFormat.format("mobileNumber.phoneId={0} is not valid", mobileNumber.getPhoneId())); //NOI18N
1276                 }
1277
1278                 // Loop through all contacts
1279                 for (final Contact contact : this.contacts) {
1280                         // Is the number set?
1281                         if (Objects.equals(contact.getContactMobileNumber(), mobileNumber)) {
1282                                 // Unset it
1283                                 contact.setContactMobileNumber(null);
1284                         }
1285                 }
1286         }
1287
1288         /**
1289          * Adds unique instance to contact list. First any existing instance is
1290          * being removed, then the new instance is added.
1291          * <p>
1292          * @param contact Contact instance to add uniquely
1293          */
1294         private void uniqueAddContact (final Contact contact) {
1295                 // Is the instance valid?
1296                 if (null == contact) {
1297                         // Throw NPE
1298                         throw new NullPointerException("contact is null"); //NOI18N
1299                 } else if (contact.getContactId() == null) {
1300                         // Throw NPE
1301                         throw new NullPointerException("contact.contactId is null"); //NOI18N
1302                 } else if (contact.getContactId() < 1) {
1303                         // Not valid id number
1304                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1305                 }
1306
1307                 // Get iterator from list
1308                 Iterator<Contact> iterator = this.contacts.iterator();
1309
1310                 // "Walk" through all entries
1311                 while (iterator.hasNext()) {
1312                         // Get next element
1313                         Contact next = iterator.next();
1314
1315                         // Is id number the same?
1316                         if (Objects.equals(contact.getContactId(), next.getContactId())) {
1317                                 // Found entry, so remove it and abort
1318                                 this.removeContact(next);
1319                                 break;
1320                         }
1321                 }
1322
1323                 // Add contact to list
1324                 this.contacts.add(contact);
1325         }
1326
1327 }