2 * Copyright (C) 2016 Roland Häder
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.
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.
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/>.
17 package org.mxchange.pizzaapplication.beans.contact;
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;
65 * A general contact bean (controller)
67 * @author Roland Häder<roland@mxchange.org>
69 @Named ("contactController")
71 public class PizzaContactWebSessionBean extends BasePizzaController implements PizzaContactWebSessionController {
76 private static final long serialVersionUID = 542_145_347_916L;
81 private Date birthday;
91 private String comment;
96 private final ContactSessionBeanRemote contactBean;
101 private final List<Contact> contacts;
106 private Country country;
111 private String emailAddress;
114 * Email address repeated
116 private String emailAddressRepeat;
121 private final List<String> emailAddresses;
126 private String familyName;
129 * Fax number's area code
131 private Integer faxAreaCode;
134 * Country instance for fax number
136 private Country faxCountry;
141 private Long faxNumber;
146 private String firstName;
151 private Gender gender;
156 private Short houseNumber;
159 * House number extension
161 private String houseNumberExtension;
164 * Whether a fax entry has been unlinked
166 private boolean isFaxUnlinked;
169 * Whether a land-line number has been unlinked
171 private boolean isLandLineUnlinked;
174 * Whether a mobile entry has been unlinked
176 private boolean isMobileUnlinked;
179 * Land-line number area code
181 private Integer landLineAreaCode;
184 * Country instance for land-line number
186 private Country landLineCountry;
191 private Long landLineNumber;
196 private Long mobileNumber;
201 private MobileProvider mobileProvider;
204 * A list of all selectable contacts
206 private List<Contact> selectableContacts;
211 private String street;
216 private String title;
219 * Regular user controller
222 private PizzaUserWebSessionController userController;
225 * Login controller (bean)
228 private PizzaUserLoginWebSessionController userLoginController;
233 private Integer zipCode;
236 * Default constructor
238 public PizzaContactWebSessionBean () {
241 // Get initial context
242 Context context = new InitialContext();
245 this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/jjobs-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
246 } catch (final NamingException e) {
248 throw new FaceletException(e);
252 this.contacts = new LinkedList<>();
253 this.emailAddresses = new LinkedList<>();
257 * Observes events being fired when an administrator has added a new
260 * @param event Event being fired
262 public void afterAdminAddedContactEvent (@Observes final ObservableAdminAddedContactEvent event) {
263 // The event must be valid
266 throw new NullPointerException("event is null"); //NOI18N
267 } else if (event.getAddedContact() == null) {
269 throw new NullPointerException("event.addedContact is null"); //NOI18N
270 } else if (event.getAddedContact().getContactId() == null) {
272 throw new NullPointerException("event.addedContact.contactId is null"); //NOI18N
273 } else if (event.getAddedContact().getContactId() < 1) {
275 throw new IllegalArgumentException(MessageFormat.format("event.addedContact.contactId={0} is not valid", event.getAddedContact().getContactId())); //NOI18N
282 this.uniqueAddContact(event.getAddedContact());
284 // Add to selectable contacts
285 this.selectableContacts.add(event.getAddedContact());
289 * Event observer for newly added users by adminstrator
291 * @param event Event being fired
293 public void afterAdminAddedUserEvent (@Observes final ObservableAdminAddedUserEvent event) {
294 // event should not be null
297 throw new NullPointerException("event is null"); //NOI18N
298 } else if (event.getAddedUser() == null) {
300 throw new NullPointerException("event.addedUser is null"); //NOI18N
301 } else if (event.getAddedUser().getUserId() == null) {
303 throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
304 } else if (event.getAddedUser().getUserId() < 1) {
306 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
314 * Event observer for deleted contact instance by administrator
316 * @param event Event being fired
318 public void afterAdminDeletedContactEvent (@Observes final ObservableAdminDeletedContactEvent event) {
319 // event should not be null
322 throw new NullPointerException("event is null"); //NOI18N
323 } else if (event.getDeletedContact() == null) {
325 throw new NullPointerException("event.deletedContact is null"); //NOI18N
326 } else if (event.getDeletedContact().getContactId() == null) {
328 throw new NullPointerException("event.deletedContact.contactId is null"); //NOI18N
329 } else if (event.getDeletedContact().getContactId() < 1) {
331 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getDeletedContact(), event.getDeletedContact().getContactId())); //NOI18N
334 // Remove from both lists
335 this.contacts.remove(event.getDeletedContact());
336 this.selectableContacts.remove(event.getDeletedContact());
343 * Observes events being fired when an administrator has linked a new user
344 * with existing contact data.
346 * @param event Event being fired
348 public void afterAdminLinkedUserEvent (@Observes final ObservableAdminLinkedUserEvent event) {
349 // event should not be null
352 throw new NullPointerException("event is null"); //NOI18N
353 } else if (event.getLinkedUser() == null) {
355 throw new NullPointerException("event.linkedUser is null"); //NOI18N
356 } else if (event.getLinkedUser().getUserContact() == null) {
358 throw new NullPointerException("event.linkedUser.userContact is null"); //NOI18N
359 } else if (event.getLinkedUser().getUserContact().getContactId() == null) {
361 throw new NullPointerException("event.linkedUser.userContact.contactId is null"); //NOI18N
362 } else if (event.getLinkedUser().getUserContact().getContactId() < 1) {
364 throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getLinkedUser(), event.getLinkedUser().getUserContact().getContactId())); //NOI18N
367 // Remove contact from list available contacts list
368 this.selectableContacts.remove(event.getLinkedUser().getUserContact());
375 * Event observer for updated contact data by administrators
377 * @param event Updated contact data event
379 public void afterAdminUpdatedContactDataEvent (@Observes final ObservableAdminUpdatedContactEvent event) {
380 // event should not be null
383 throw new NullPointerException("event is null"); //NOI18N
384 } else if (event.getUpdatedContact() == null) {
386 throw new NullPointerException("event.updatedContact is null"); //NOI18N
387 } else if (event.getUpdatedContact().getContactId() == null) {
389 throw new NullPointerException("event.updatedContact.contactId is null"); //NOI18N
390 } else if (event.getUpdatedContact().getContactId() < 1) {
392 throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
395 // Add contact instance only once
396 this.uniqueAddContact(event.getUpdatedContact());
398 // Add email address to list
399 this.emailAddresses.add(event.getUpdatedContact().getContactEmailAddress());
403 * Event observer for new user registrations
405 * @param event User registration event
407 public void afterUserRegistrationEvent (@Observes final ObservableUserRegisteredEvent event) {
408 // event should not be null
411 throw new NullPointerException("event is null"); //NOI18N
412 } else if (event.getRegisteredUser() == null) {
414 throw new NullPointerException("event.registeredUser is null"); //NOI18N
415 } else if (event.getRegisteredUser().getUserId() == null) {
417 throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
418 } else if (event.getRegisteredUser().getUserId() < 1) {
420 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
424 Contact registeredContact = event.getRegisteredUser().getUserContact();
426 // Copy all data from registered->user
427 this.copyContact(registeredContact);
429 // Add user name and email address
430 this.addUserNameEmailAddress(registeredContact);
437 * Event observer for unlinked mobile contact by administrators
439 * @param event Unlinked mobile contact event
441 public void afterAdminUnlinkedMobileContactDataEvent (@Observes final ObservableAdminUnlinkedMobileNumberEvent event) {
442 // event should not be null
445 throw new NullPointerException("event is null"); //NOI18N
446 } else if (event.getUnlinkedMobileNumber() == null) {
448 throw new NullPointerException("event.unlinkedMobileNumber is null"); //NOI18N
449 } else if (event.getUnlinkedMobileNumber().getPhoneId() == null) {
451 throw new NullPointerException("event.unlinkedMobileNumber.contactId is null"); //NOI18N
452 } else if (event.getUnlinkedMobileNumber().getPhoneId() < 1) {
454 throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUnlinkedMobileNumber(), event.getUnlinkedMobileNumber().getPhoneId())); //NOI18N
457 // Remove it from list
458 this.removeFromContacts(event.getUnlinkedMobileNumber());
465 * Event observer when user confirmed account.
467 * @param event Event being fired
469 public void afterUserConfirmedAccount (@Observes final ObservableUserConfirmedAccountEvent event) {
470 // event should not be null
473 throw new NullPointerException("event is null"); //NOI18N
474 } else if (event.getConfirmedUser() == null) {
476 throw new NullPointerException("event.confirmedUser is null"); //NOI18N
477 } else if (event.getConfirmedUser().getUserId() == null) {
479 throw new NullPointerException("event.confirmedUser.userId is null"); //NOI18N
480 } else if (event.getConfirmedUser().getUserId() < 1) {
482 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getConfirmedUser(), event.getConfirmedUser().getUserId())); //NOI18N
485 // Add contact instance only once
486 this.uniqueAddContact(event.getConfirmedUser().getUserContact());
490 * Event observer for logged-in user
492 * @param event Event instance
494 public void afterUserLoginEvent (@Observes final ObservableUserLoggedInEvent event) {
495 // event should not be null
498 throw new NullPointerException("event is null"); //NOI18N
499 } else if (event.getLoggedInUser() == null) {
501 throw new NullPointerException("event.loggedInUser is null"); //NOI18N
502 } else if (event.getLoggedInUser().getUserId() == null) {
504 throw new NullPointerException("event.loggedInUser.userId is null"); //NOI18N
505 } else if (event.getLoggedInUser().getUserId() < 1) {
507 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
510 // Copy all data to this bean
511 this.copyContact(event.getLoggedInUser().getUserContact());
515 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
516 public List<Contact> allContacts () {
517 // Return un-modified list
518 return this.contacts;
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
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());
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());
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) {
551 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
552 } else if (phone.getPhoneAreaCode() < 1) {
554 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
555 } else if (phone.getPhoneNumber() == null) {
557 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
558 } else if (phone.getPhoneNumber() < 1) {
560 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
564 contact.setContactLandLineNumber(phone);
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) {
572 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
573 } else if (fax.getPhoneAreaCode() < 1) {
575 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
576 } else if (fax.getPhoneNumber() == null) {
578 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
579 } else if (fax.getPhoneNumber() < 1) {
581 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
585 contact.setContactFaxNumber(fax);
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) {
593 throw new NullPointerException("mobileNumber.phoneNumber is null"); //NOI18N
594 } else if (mobile.getPhoneNumber() < 1) {
596 throw new IllegalArgumentException("mobileNumber.phoneNumber is zero or below."); //NOI18N
600 contact.setContactMobileNumber(mobile);
608 public String doChangePersonalContactData () {
609 // This method shall only be called if the user is logged-in
610 if (!this.userLoginController.isUserLoggedIn()) {
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
622 // Get contact instance
623 Contact contact = this.userLoginController.getLoggedInUser().getUserContact();
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
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());
641 // Update contact's mobile number
642 this.isMobileUnlinked = ContactUtils.updateMobileNumber(contact, this.getMobileProvider(), this.getMobileNumber());
644 // Update contact's land-line number
645 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber());
647 // Update contact's fax number
648 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
650 // Send it to the EJB
651 this.contactBean.updateContactData(contact, this.isMobileUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
654 return "user_contact_data_saved"; //NOI18N
658 @SuppressWarnings ("ReturnOfDateField")
659 public Date getBirthday () {
660 return this.birthday;
664 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
665 public void setBirthday (final Date birthday) {
666 this.birthday = birthday;
670 public String getCity () {
675 public void setCity (final String city) {
680 public String getComment () {
685 public void setComment (final String comment) {
686 this.comment = comment;
690 public String getControllerType () {
691 return "general"; //NOI18N
696 public void setControllerType (final String controllerType) {
697 throw new UnsupportedOperationException("Setting controller type is not supported."); //NOI18N
701 public Country getCountry () {
706 public void setCountry (final Country country) {
707 this.country = country;
711 public String getEmailAddress () {
712 return this.emailAddress;
716 public void setEmailAddress (final String emailAddress) {
717 this.emailAddress = emailAddress;
721 public String getEmailAddressRepeat () {
722 return this.emailAddressRepeat;
726 public void setEmailAddressRepeat (final String emailAddressRepeat) {
727 this.emailAddressRepeat = emailAddressRepeat;
731 public String getFamilyName () {
732 return this.familyName;
736 public void setFamilyName (final String familyName) {
737 this.familyName = familyName;
741 public Integer getFaxAreaCode () {
742 return this.faxAreaCode;
746 public void setFaxAreaCode (final Integer faxAreaCode) {
747 this.faxAreaCode = faxAreaCode;
751 public Country getFaxCountry () {
752 return this.faxCountry;
756 public void setFaxCountry (final Country faxCountry) {
757 this.faxCountry = faxCountry;
761 public Long getFaxNumber () {
762 return this.faxNumber;
766 public void setFaxNumber (final Long faxNumber) {
767 this.faxNumber = faxNumber;
771 public String getFirstName () {
772 return this.firstName;
776 public void setFirstName (final String firstName) {
777 this.firstName = firstName;
781 public Gender getGender () {
786 public void setGender (final Gender gender) {
787 this.gender = gender;
791 public Short getHouseNumber () {
792 return this.houseNumber;
796 public void setHouseNumber (final Short houseNumber) {
797 this.houseNumber = houseNumber;
801 public String getHouseNumberExtension () {
802 return this.houseNumberExtension;
806 public void setHouseNumberExtension (final String houseNumberExtension) {
807 this.houseNumberExtension = houseNumberExtension;
811 public Integer getLandLineAreaCode () {
812 return this.landLineAreaCode;
816 public void setLandLineAreaCode (final Integer landLineAreaCode) {
817 this.landLineAreaCode = landLineAreaCode;
821 public Country getLandLineCountry () {
822 return this.landLineCountry;
826 public void setLandLineCountry (final Country landLineCountry) {
827 this.landLineCountry = landLineCountry;
831 public Long getLandLineNumber () {
832 return this.landLineNumber;
836 public void setLandLineNumber (final Long landLineNumber) {
837 this.landLineNumber = landLineNumber;
841 public Long getMobileNumber () {
842 return this.mobileNumber;
846 public void setMobileNumber (Long mobileNumber) {
847 this.mobileNumber = mobileNumber;
851 public MobileProvider getMobileProvider () {
852 return this.mobileProvider;
856 public void setMobileProvider (final MobileProvider mobileProvider) {
857 this.mobileProvider = mobileProvider;
861 public String getStreet () {
866 public void setStreet (final String street) {
867 this.street = street;
871 public String getTitle () {
876 public void setTitle (final String title) {
881 public Integer getZipCode () {
886 public void setZipCode (final Integer zipCode) {
887 this.zipCode = zipCode;
891 * Post-initialization of this class
894 public void init () {
895 // Get full email address list for reducing EJB calls
896 this.emailAddresses.addAll(this.contactBean.getEmailAddressList());
899 List<Contact> allContacts = this.contactBean.getAllContacts();
901 // Get full contact list
902 this.contacts.addAll(allContacts);
905 List<User> allUsers = this.userController.allUsers();
908 Iterator<Contact> iterator = allContacts.iterator();
911 while (iterator.hasNext()) {
913 Contact next = iterator.next();
916 Iterator<User> userIterator = allUsers.iterator();
918 // Loop through all users
919 while (userIterator.hasNext()) {
921 User nextUser = userIterator.next();
924 if (Objects.equals(next, nextUser.getUserContact())) {
933 this.selectableContacts = allContacts;
937 public boolean isEmailAddressRegistered (final Contact contact) {
939 if (null == contact) {
941 throw new NullPointerException("contact is null"); //NOI18N
942 } else if (contact.getContactEmailAddress() == null) {
944 throw new NullPointerException("contact.contactEmailAddress is null"); //NOI18N
945 } else if (contact.getContactEmailAddress().isEmpty()) {
947 throw new IllegalArgumentException("contact.contactEmailAddress is empty."); //NOI18N
951 return ((this.emailAddresses instanceof List) && (this.emailAddresses.contains(contact.getContactEmailAddress())));
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));
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));
979 public boolean isSameEmailAddressEntered () {
980 return (Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat()));
984 public Contact lookupContactById (final Long contactId) throws ContactNotFoundException {
986 Contact localContact = null;
991 // Try to lookup it in visible user list
992 for (final Iterator<Contact> iterator = this.contacts.iterator(); iterator.hasNext();) {
994 Contact next = iterator.next();
996 // Is the user id found?
997 if (Objects.equals(next.getContactId(), contactId)) {
998 // Copy to other variable
1004 // Is it still null?
1005 if (null == localContact) {
1006 // Not visible for the current user
1007 throw new ContactNotFoundException(contactId);
1010 // Copy all data to this bean
1011 this.copyContact(localContact);
1014 return localContact;
1018 * Event observer when a list with unused mobile numbers is being created
1020 * @param event Event being fired
1022 public void removeMobileNumberFromListEvent (@Observes final AdminRemoveMobileNumberFromListEvent event) {
1023 // event should not be null
1024 if (null == event) {
1026 throw new NullPointerException("event is null"); //NOI18N
1027 } else if (event.getMobileNumberList() == null) {
1029 throw new NullPointerException("event.mobileList is null"); //NOI18N
1030 } else if (event.getMobileNumberList().isEmpty()) {
1031 // List is empty, no need to check
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());
1046 public List<Contact> selectableContacts () {
1047 return Collections.unmodifiableList(this.selectableContacts);
1051 public void updateContactDataFromController (final Contact contact) {
1052 // Is the instance valid?
1053 if (null == contact) {
1055 throw new NullPointerException("contact is null"); //NOI18N
1056 } else if (contact.getContactId() == null) {
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
1065 this.copyContact(contact);
1069 * Adds email address to bean's internal list.
1071 * @param contact Contact instance
1073 private void addUserNameEmailAddress (final Contact contact) {
1074 // Make sure the entry is not added yet
1075 if (this.emailAddresses.contains(contact.getContactEmailAddress())) {
1077 throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", contact.getContactEmailAddress())); //NOI18N
1081 this.emailAddresses.add(contact.getContactEmailAddress());
1087 private void clear () {
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);
1099 this.setCountry(null);
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);
1114 this.setBirthday(null);
1115 this.setComment(null);
1119 * Copies given contact into the controller
1121 * @param contact Contact instance
1123 private void copyContact (final Contact contact) {
1124 // Is the instance valid?
1125 if (null == contact) {
1127 throw new NullPointerException("contact is null"); //NOI18N
1128 } else if (contact.getContactId() == null) {
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
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());
1152 // Get mobile, phone and fax instance
1153 DialableMobileNumber mobile = contact.getContactMobileNumber();
1154 DialableFaxNumber fax = contact.getContactFaxNumber();
1155 DialableLandLineNumber phone = contact.getContactLandLineNumber();
1158 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneAreaCode() > 0)) {
1159 this.setLandLineCountry(phone.getPhoneCountry());
1160 this.setLandLineAreaCode(phone.getPhoneAreaCode());
1161 this.setLandLineNumber(phone.getPhoneNumber());
1164 if ((mobile instanceof DialableMobileNumber) && (mobile.getMobileProvider() instanceof MobileProvider)) {
1165 this.setMobileProvider(mobile.getMobileProvider());
1166 this.setMobileNumber(mobile.getPhoneNumber());
1169 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneAreaCode() > 0)) {
1170 this.setFaxCountry(fax.getPhoneCountry());
1171 this.setFaxAreaCode(fax.getPhoneAreaCode());
1172 this.setFaxNumber(fax.getPhoneNumber());
1177 * Removes given contact from all lists
1179 * @param contact Contact instance to remove
1181 private void removeContact (final Contact contact) {
1182 // Is the instance valid?
1183 if (null == contact) {
1185 throw new NullPointerException("contact is null"); //NOI18N
1186 } else if (contact.getContactId() == null) {
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
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
1200 // Remove from other lists
1201 this.emailAddresses.remove(contact.getContactEmailAddress());
1205 * Removes given fax number from all contacts
1207 * @param faxNumber Fax number to remove
1209 private void removeFromContacts (final DialableFaxNumber faxNumber) {
1211 if (null == faxNumber) {
1213 throw new NullPointerException("faxNumber is null"); //NOI18N
1214 } else if (faxNumber.getPhoneId() == null) {
1216 throw new NullPointerException("faxNumber.phoneId is null"); //NOI18N
1217 } else if (faxNumber.getPhoneId() < 1) {
1219 throw new NullPointerException(MessageFormat.format("faxNumber.phoneId={0} is not valid", faxNumber.getPhoneId())); //NOI18N
1222 // Loop through all contacts
1223 for (final Contact contact : this.contacts) {
1224 // Is the number set?
1225 if (Objects.equals(contact.getContactFaxNumber(), faxNumber)) {
1227 contact.setContactFaxNumber(null);
1233 * Removes given land-line number from all contacts
1235 * @param landLineNumber Land-line number to remove
1237 private void removeFromContacts (final DialableLandLineNumber landLineNumber) {
1239 if (null == landLineNumber) {
1241 throw new NullPointerException("landLineNumber is null"); //NOI18N
1242 } else if (landLineNumber.getPhoneId() == null) {
1244 throw new NullPointerException("landLineNumber.phoneId is null"); //NOI18N
1245 } else if (landLineNumber.getPhoneId() < 1) {
1247 throw new NullPointerException(MessageFormat.format("landLineNumber.phoneId={0} is not valid", landLineNumber.getPhoneId())); //NOI18N
1250 // Loop through all contacts
1251 for (final Contact contact : this.contacts) {
1252 // Is the number set?
1253 if (Objects.equals(contact.getContactLandLineNumber(), landLineNumber)) {
1255 contact.setContactLandLineNumber(null);
1261 * Removes given mobile number from all contacts
1263 * @param mobileNumber Mobile number to remove
1265 private void removeFromContacts (final DialableMobileNumber mobileNumber) {
1267 if (null == mobileNumber) {
1269 throw new NullPointerException("mobileNumber is null"); //NOI18N
1270 } else if (mobileNumber.getPhoneId() == null) {
1272 throw new NullPointerException("mobileNumber.phoneId is null"); //NOI18N
1273 } else if (mobileNumber.getPhoneId() < 1) {
1275 throw new NullPointerException(MessageFormat.format("mobileNumber.phoneId={0} is not valid", mobileNumber.getPhoneId())); //NOI18N
1278 // Loop through all contacts
1279 for (final Contact contact : this.contacts) {
1280 // Is the number set?
1281 if (Objects.equals(contact.getContactMobileNumber(), mobileNumber)) {
1283 contact.setContactMobileNumber(null);
1289 * Adds unique instance to contact list. First any existing instance is
1290 * being removed, then the new instance is added.
1292 * @param contact Contact instance to add uniquely
1294 private void uniqueAddContact (final Contact contact) {
1295 // Is the instance valid?
1296 if (null == contact) {
1298 throw new NullPointerException("contact is null"); //NOI18N
1299 } else if (contact.getContactId() == null) {
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
1307 // Get iterator from list
1308 Iterator<Contact> iterator = this.contacts.iterator();
1310 // "Walk" through all entries
1311 while (iterator.hasNext()) {
1313 Contact next = iterator.next();
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);
1323 // Add contact to list
1324 this.contacts.add(contact);