2 * Copyright (C) 2016 Roland Haeder
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.HashMap;
23 import java.util.Iterator;
24 import java.util.LinkedList;
25 import java.util.List;
27 import java.util.Objects;
28 import javax.annotation.PostConstruct;
29 import javax.enterprise.context.SessionScoped;
30 import javax.enterprise.event.Observes;
31 import javax.faces.view.facelets.FaceletException;
32 import javax.inject.Inject;
33 import javax.inject.Named;
34 import javax.naming.Context;
35 import javax.naming.InitialContext;
36 import javax.naming.NamingException;
37 import org.mxchange.jcontacts.contact.Contact;
38 import org.mxchange.jcontacts.contact.ContactSessionBeanRemote;
39 import org.mxchange.jcontacts.contact.UserContact;
40 import org.mxchange.jcontacts.contact.gender.Gender;
41 import org.mxchange.jcontacts.contact.utils.ContactUtils;
42 import org.mxchange.jcontacts.events.contact.add.AdminAddedContactEvent;
43 import org.mxchange.jcontacts.events.contact.update.AdminUpdatedContactEvent;
44 import org.mxchange.jcontacts.exceptions.ContactNotFoundException;
45 import org.mxchange.jcountry.data.Country;
46 import org.mxchange.jphone.phonenumbers.cellphone.CellphoneNumber;
47 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
48 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
49 import org.mxchange.jphone.phonenumbers.fax.FaxNumber;
50 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
51 import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
52 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
53 import org.mxchange.jphone.phonenumbers.phone.AdminPhoneSessionBeanRemote;
54 import org.mxchange.jusercore.events.confirmation.UserConfirmedAccountEvent;
55 import org.mxchange.jusercore.events.login.UserLoggedInEvent;
56 import org.mxchange.jusercore.events.registration.UserRegisteredEvent;
57 import org.mxchange.jusercore.events.user.add.AdminAddedUserEvent;
58 import org.mxchange.jusercore.events.user.linked.AdminLinkedUserEvent;
59 import org.mxchange.jusercore.exceptions.UserPasswordMismatchException;
60 import org.mxchange.jusercore.model.user.User;
61 import org.mxchange.pizzaapplication.beans.BasePizzaController;
62 import org.mxchange.pizzaapplication.beans.login.PizzaUserLoginWebSessionController;
63 import org.mxchange.pizzaapplication.beans.phone.PizzaAdminPhoneWebRequestController;
64 import org.mxchange.pizzaapplication.beans.user.PizzaUserWebSessionController;
67 * A general contact bean (controller)
69 * @author Roland Haeder<roland@mxchange.org>
71 @Named ("contactController")
73 public class PizzaContactWebSessionBean extends BasePizzaController implements PizzaContactWebSessionController {
78 private static final long serialVersionUID = 542_145_347_916L;
81 * Remote EJB for phone number (administrative)
83 private AdminPhoneSessionBeanRemote adminPhoneBean;
86 * Administrative phone controller
89 private PizzaAdminPhoneWebRequestController adminPhoneController;
94 private Date birthday;
97 * Cellphone number's carrier
99 private MobileProvider cellphoneCarrier;
104 private Long cellphoneNumber;
107 * All cell phone numbers
109 private final List<DialableCellphoneNumber> cellphoneNumbers;
119 private String comment;
122 * Remote contact bean
124 private final ContactSessionBeanRemote contactBean;
129 private final List<Contact> contactList;
132 * "Cache" for contact lists, mostly only one is assigned. So this cache
133 * shouldn't grow beyond control.
135 private final Map<Long, List<Contact>> contacts;
140 private Country country;
145 private String emailAddress;
150 private final List<String> emailAddressList;
153 * Email address repeated
155 private String emailAddressRepeat;
160 private String familyName;
163 * Fax number's area code
165 private Integer faxAreaCode;
168 * Country instance for fax number
170 private Country faxCountry;
175 private Long faxNumber;
180 private String firstName;
185 private Gender gender;
190 private Short houseNumber;
193 * House number extension
195 private String houseNumberExtension;
198 * Whether a cellphone entry has been unlinked
200 private boolean isCellphoneUnlinked;
203 * Whether a fax entry has been unlinked
205 private boolean isFaxUnlinked;
208 * Whether a land-line number has been unlinked
210 private boolean isLandLineUnlinked;
213 * Phone number area code
215 private Integer phoneAreaCode;
218 * Country instance for phone number
220 private Country phoneCountry;
225 private Long phoneNumber;
228 * A list of all selectable contacts
230 private List<Contact> selectableContacts;
235 private String street;
240 private String title;
243 * Regular user controller
246 private PizzaUserWebSessionController userController;
249 * Login bean (controller)
252 private PizzaUserLoginWebSessionController userLoginController;
257 private Integer zipCode;
260 * Default constructor
262 public PizzaContactWebSessionBean () {
265 // Get initial context
266 Context context = new InitialContext();
269 this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/pizzaservice-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
271 // Try to lookup the beans
272 this.adminPhoneBean = (AdminPhoneSessionBeanRemote) context.lookup("java:global/pizzaservice-ejb/adminphone!org.mxchange.jphone.phonenumbers.phone.AdminPhoneSessionBeanRemote"); //NOI18N
273 } catch (final NamingException e) {
275 throw new FaceletException(e);
279 this.cellphoneNumbers = new LinkedList<>();
280 this.contacts = new HashMap<>(10);
281 this.contactList = new LinkedList<>();
282 this.emailAddressList = new LinkedList<>();
286 public void afterAdminAddedContact (@Observes final AdminAddedContactEvent event) {
288 /* NOISY-DEBUG: */ System.out.println(MessageFormat.format("contactController.afterAdminAddedContact(): event={0} - CALLED!", event)); //NOI18N
290 // The event must be valid
293 throw new NullPointerException("event is null"); //NOI18N
294 } else if (event.getAddedContact() == null) {
296 throw new NullPointerException("event.addedContact is null"); //NOI18N
297 } else if (event.getAddedContact().getContactId() == null) {
299 throw new NullPointerException("event.addedContact.contactId is null"); //NOI18N
300 } else if (event.getAddedContact().getContactId() < 1) {
302 throw new IllegalArgumentException(MessageFormat.format("event.addedContact.contactId={0} is not valid", event.getAddedContact().getContactId())); //NOI18N //NOI18N
309 this.uniqueAddContact(event.getAddedContact());
311 // Add to selectable contacts
312 this.selectableContacts.add(event.getAddedContact());
316 public void afterAdminAddedUserEvent (@Observes final AdminAddedUserEvent event) {
317 // event should not be null
320 throw new NullPointerException("event is null"); //NOI18N
321 } else if (event.getAddedUser() == null) {
323 throw new NullPointerException("event.addedUser is null"); //NOI18N
324 } else if (event.getAddedUser().getUserId() == null) {
326 throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
327 } else if (event.getAddedUser().getUserId() < 1) {
329 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
337 public void afterAdminLinkedUser (@Observes final AdminLinkedUserEvent event) {
338 // event should not be null
341 throw new NullPointerException("event is null"); //NOI18N
342 } else if (event.getLinkedUser() == null) {
344 throw new NullPointerException("event.linkedUser is null"); //NOI18N
345 } else if (event.getLinkedUser().getUserContact() == null) {
347 throw new NullPointerException("event.linkedUser.userContact is null"); //NOI18N
348 } else if (event.getLinkedUser().getUserContact().getContactId() == null) {
350 throw new NullPointerException("event.linkedUser.userContact.contactId is null"); //NOI18N
351 } else if (event.getLinkedUser().getUserContact().getContactId() < 1) {
353 throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getLinkedUser(), event.getLinkedUser().getUserContact().getContactId())); //NOI18N
356 // Remove contact from list available contacts list
357 this.selectableContacts.remove(event.getLinkedUser().getUserContact());
364 public void afterAdminUpdatedContactDataEvent (@Observes final AdminUpdatedContactEvent event) {
365 // event should not be null
368 throw new NullPointerException("event is null"); //NOI18N
369 } else if (event.getUpdatedContact() == null) {
371 throw new NullPointerException("event.updatedContact is null"); //NOI18N
372 } else if (event.getUpdatedContact().getContactId() == null) {
374 throw new NullPointerException("event.updatedContact.contactId is null"); //NOI18N
375 } else if (event.getUpdatedContact().getContactId() < 1) {
377 throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
380 // Add contact instance only once
381 this.uniqueAddContact(event.getUpdatedContact());
383 // Add email address to list
384 this.emailAddressList.add(event.getUpdatedContact().getContactEmailAddress());
388 public void afterRegistrationEvent (@Observes final UserRegisteredEvent event) {
389 // event should not be null
392 throw new NullPointerException("event is null"); //NOI18N
393 } else if (event.getRegisteredUser() == null) {
395 throw new NullPointerException("event.registeredUser is null"); //NOI18N
396 } else if (event.getRegisteredUser().getUserId() == null) {
398 throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
399 } else if (event.getRegisteredUser().getUserId() < 1) {
401 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
405 Contact registeredContact = event.getRegisteredUser().getUserContact();
407 // Copy all data from registered->user
408 this.copyContact(registeredContact);
410 // Add contact instance only once
411 this.uniqueAddContact(registeredContact);
413 // Add user name and email address
414 this.addUserNameEmailAddress(registeredContact);
421 public void afterUserConfirmedAccount (@Observes final UserConfirmedAccountEvent event) {
422 // event should not be null
425 throw new NullPointerException("event is null"); //NOI18N
426 } else if (event.getConfirmedUser() == null) {
428 throw new NullPointerException("event.confirmedUser is null"); //NOI18N
429 } else if (event.getConfirmedUser().getUserId() == null) {
431 throw new NullPointerException("event.confirmedUser.userId is null"); //NOI18N
432 } else if (event.getConfirmedUser().getUserId() < 1) {
434 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getConfirmedUser(), event.getConfirmedUser().getUserId())); //NOI18N
437 // Add contact instance only once
438 this.uniqueAddContact(event.getConfirmedUser().getUserContact());
442 public void afterUserLogin (@Observes final UserLoggedInEvent event) {
443 // event should not be null
446 throw new NullPointerException("event is null"); //NOI18N
447 } else if (event.getLoggedInUser() == null) {
449 throw new NullPointerException("event.loggedInUser is null"); //NOI18N
450 } else if (event.getLoggedInUser().getUserId() == null) {
452 throw new NullPointerException("event.loggedInUser.userId is null"); //NOI18N
453 } else if (event.getLoggedInUser().getUserId() < 1) {
455 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
458 // Copy all data to this bean
459 this.copyContact(event.getLoggedInUser().getUserContact());
463 public List<Contact> allCellphoneContacts () {
465 Long phoneId = this.adminPhoneController.getCellPhone().getPhoneId();
468 if (this.contacts.containsKey(phoneId)) {
469 // Return cached version
470 return this.contacts.get(phoneId);
473 List<Contact> list = new LinkedList<>();
475 // "Walk" through all contacts
476 for (final Contact contact : this.contactList) {
477 // Is cellphone instance the same?
478 if (Objects.equals(contact.getContactCellphoneNumber(), this.adminPhoneController.getCellPhone())) {
484 // Store result in cache
485 this.contacts.put(phoneId, list);
487 // Return now-cached list
493 public List<Contact> allContacts () {
495 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ContactController.allContacts: contactList.size()={0} - EXIT!", this.contactList.size()));
497 // Return un-modified list
498 return Collections.unmodifiableList(this.contactList);
502 public Contact createContactInstance () {
504 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createContactInstance: CALLED!", this.getClass().getSimpleName()));
506 // Is all required data set?
507 if (!this.isRequiredPersonalDataSet()) {
508 // No, then abort here
509 throw new FaceletException(new IllegalArgumentException("Not all personal data is set, but createContactInstance() is called.")); //NOI18N
512 // Generate phone number
513 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
514 DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
515 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
517 // Create new contact
518 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
519 contact.setContactStreet(this.getStreet());
520 contact.setContactHouseNumber(this.getHouseNumber());
521 contact.setContactHouseNumberExtension(this.getHouseNumberExtension());
522 contact.setContactZipCode(this.getZipCode());
523 contact.setContactCity(this.getCity());
524 contact.setContactCountry(this.getCountry());
525 contact.setContactEmailAddress(this.getEmailAddress());
526 contact.setContactBirthday(this.getBirthday());
527 contact.setContactComment(this.getComment());
530 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createContactInstance: this.emailAddress={1}", this.getClass().getSimpleName(), this.getEmailAddress()));
532 // Don't set null or wrong references
533 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getPhoneAreaCode() != null) && (this.getPhoneNumber() != null) && (this.getPhoneAreaCode() > 0) && (this.getPhoneNumber() > 0)) {
534 // Now the number must be given
535 if (phone.getPhoneAreaCode() == null) {
537 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
538 } else if (phone.getPhoneAreaCode() < 1) {
540 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
541 } else if (phone.getPhoneNumber() == null) {
543 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
544 } else if (phone.getPhoneNumber() < 1) {
546 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
550 contact.setContactLandLineNumber(phone);
553 // Don't set null or wrong references
554 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
555 // Now the number must be given
556 if (fax.getPhoneAreaCode() == null) {
558 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
559 } else if (fax.getPhoneAreaCode() < 1) {
561 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
562 } else if (fax.getPhoneNumber() == null) {
564 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
565 } else if (fax.getPhoneNumber() < 1) {
567 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
571 contact.setContactFaxNumber(fax);
574 // Is the provider set?
575 if ((cellphone instanceof DialableCellphoneNumber) && (this.getCellphoneCarrier() instanceof MobileProvider) && (this.getCellphoneNumber() != null) && (this.getCellphoneNumber() > 0)) {
576 // Is the number set?
577 if (cellphone.getPhoneNumber() == null) {
579 throw new NullPointerException("cellphone.phoneNumber is null"); //NOI18N
580 } else if (cellphone.getPhoneNumber() < 1) {
582 throw new IllegalArgumentException("cellphone.phoneNumber is zero or below."); //NOI18N
585 // Set cellphone number
586 contact.setContactCellphoneNumber(cellphone);
590 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createContactInstance: contact={1} - EXIT!", this.getClass().getSimpleName(), contact));
597 public String doChangePersonalContactData () {
598 // This method shall only be called if the user is logged-in
599 if (!this.userLoginController.isUserLoggedIn()) {
601 throw new IllegalStateException("User is not logged-in"); //NOI18N
602 } else if (!this.isRequiredChangePersonalDataSet()) {
603 // Not all required fields are set
604 throw new FaceletException("Not all required fields are set."); //NOI18N
605 } else if (!this.userLoginController.ifCurrentPasswordMatches()) {
606 // Password not matching
607 this.showFacesMessage("form_login_change_personal:currentPassword", new UserPasswordMismatchException(this.userLoginController.getLoggedInUser())); //NOI18N
611 // Get contact instance
612 Contact contact = this.userLoginController.getLoggedInUser().getUserContact();
614 // It should be there, so run some tests on it
615 assert (contact instanceof Contact) : "Instance userLoginController.loggedInUser.userContact is null"; //NOI18N
616 assert (contact.getContactId() instanceof Long) : "Instance userLoginController.userContact.contactId is null"; //NOI18N
617 assert (contact.getContactId() > 0) : MessageFormat.format("Instance userLoginController.userContact.contactId={0} is invalid", contact.getContactId()); //NOI18N
620 contact.setContactGender(this.getGender());
621 contact.setContactFirstName(this.getFirstName());
622 contact.setContactFamilyName(this.getFamilyName());
623 contact.setContactStreet(this.getStreet());
624 contact.setContactHouseNumber(this.getHouseNumber());
625 contact.setContactHouseNumberExtension(this.getHouseNumberExtension());
626 contact.setContactZipCode(this.getZipCode());
627 contact.setContactCity(this.getCity());
628 contact.setContactCountry(this.getCountry());
630 // Update contact's cellphone number
631 this.isCellphoneUnlinked = ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
633 // Update contact's land-line number
634 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
636 // Update contact's fax number
637 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
639 // Send it to the EJB
640 this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
643 return "contact_data_saved"; //NOI18N
647 @SuppressWarnings ("ReturnOfDateField")
648 public Date getBirthday () {
649 return this.birthday;
653 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
654 public void setBirthday (final Date birthday) {
655 this.birthday = birthday;
659 public MobileProvider getCellphoneCarrier () {
660 return this.cellphoneCarrier;
664 public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
665 this.cellphoneCarrier = cellphoneCarrier;
669 public Long getCellphoneNumber () {
670 return this.cellphoneNumber;
674 public void setCellphoneNumber (Long cellphoneNumber) {
675 this.cellphoneNumber = cellphoneNumber;
679 public String getCity () {
684 public void setCity (final String city) {
689 public String getComment () {
694 public void setComment (final String comment) {
695 this.comment = comment;
699 public String getControllerType () {
700 return "general"; //NOI18N
705 public void setControllerType (final String controllerType) {
706 throw new UnsupportedOperationException("Setting controller type is not supported."); //NOI18N
710 public Country getCountry () {
715 public void setCountry (final Country country) {
716 this.country = country;
720 public String getEmailAddress () {
721 return this.emailAddress;
725 public void setEmailAddress (final String emailAddress) {
726 this.emailAddress = emailAddress;
730 public String getEmailAddressRepeat () {
731 return this.emailAddressRepeat;
735 public void setEmailAddressRepeat (final String emailAddressRepeat) {
736 this.emailAddressRepeat = emailAddressRepeat;
740 public String getFamilyName () {
741 return this.familyName;
745 public void setFamilyName (final String familyName) {
746 this.familyName = familyName;
750 public Integer getFaxAreaCode () {
751 return this.faxAreaCode;
755 public void setFaxAreaCode (final Integer faxAreaCode) {
756 this.faxAreaCode = faxAreaCode;
760 public Country getFaxCountry () {
761 return this.faxCountry;
765 public void setFaxCountry (final Country faxCountry) {
766 this.faxCountry = faxCountry;
770 public Long getFaxNumber () {
771 return this.faxNumber;
775 public void setFaxNumber (final Long faxNumber) {
776 this.faxNumber = faxNumber;
780 public String getFirstName () {
781 return this.firstName;
785 public void setFirstName (final String firstName) {
786 this.firstName = firstName;
790 public Gender getGender () {
795 public void setGender (final Gender gender) {
796 this.gender = gender;
800 public Short getHouseNumber () {
801 return this.houseNumber;
805 public void setHouseNumber (final Short houseNumber) {
806 this.houseNumber = houseNumber;
810 public String getHouseNumberExtension () {
811 return this.houseNumberExtension;
815 public void setHouseNumberExtension (final String houseNumberExtension) {
816 this.houseNumberExtension = houseNumberExtension;
820 public Integer getPhoneAreaCode () {
821 return this.phoneAreaCode;
825 public void setPhoneAreaCode (final Integer phoneAreaCode) {
826 this.phoneAreaCode = phoneAreaCode;
830 public Country getPhoneCountry () {
831 return this.phoneCountry;
835 public void setPhoneCountry (final Country phoneCountry) {
836 this.phoneCountry = phoneCountry;
840 public Long getPhoneNumber () {
841 return this.phoneNumber;
845 public void setPhoneNumber (final Long phoneNumber) {
846 this.phoneNumber = phoneNumber;
850 public String getStreet () {
855 public void setStreet (final String street) {
856 this.street = street;
860 public String getTitle () {
865 public void setTitle (final String title) {
870 public Integer getZipCode () {
875 public void setZipCode (final Integer zipCode) {
876 this.zipCode = zipCode;
880 public boolean hasContacts () {
881 return (!this.contactList.isEmpty());
885 * Post-initialization of this class
888 public void init () {
889 // Get full email address list for reducing EJB calls
890 this.emailAddressList.addAll(this.contactBean.getEmailAddressList());
892 // Get full contact list
893 this.contactList.addAll(this.contactBean.getAllContacts());
896 List<User> allUsers = this.userController.allUsers();
899 List<Contact> allContacts = this.contactBean.getAllContacts();
902 Iterator<Contact> iterator = allContacts.iterator();
905 while (iterator.hasNext()) {
907 Contact next = iterator.next();
910 Iterator<User> userIterator = allUsers.iterator();
912 // Loop through all users
913 while (userIterator.hasNext()) {
915 User nextUser = userIterator.next();
918 if (Objects.equals(next, nextUser.getUserContact())) {
927 this.selectableContacts = allContacts;
930 this.cellphoneNumbers.addAll(this.adminPhoneBean.allCellphoneNumbers());
934 public boolean isEmailAddressRegistered (final Contact contact) {
936 if (null == contact) {
938 throw new NullPointerException("contact is null"); //NOI18N
939 } else if (contact.getContactEmailAddress() == null) {
941 throw new NullPointerException("contact.contactEmailAddress is null"); //NOI18N
942 } else if (contact.getContactEmailAddress().isEmpty()) {
944 throw new IllegalArgumentException("contact.contactEmailAddress is empty."); //NOI18N
948 return ((this.emailAddressList instanceof List) && (this.emailAddressList.contains(contact.getContactEmailAddress())));
952 public boolean isRequiredChangePersonalDataSet () {
953 return ((this.getGender() != null) &&
954 (this.getFirstName() != null) &&
955 (this.getFamilyName() != null) &&
956 (this.getStreet() != null) &&
957 (this.getHouseNumber() != null) &&
958 (this.getZipCode() != null) &&
959 (this.getCity() != null));
963 public boolean isRequiredPersonalDataSet () {
964 return ((this.getGender() != null) &&
965 (this.getFirstName() != null) &&
966 (this.getFamilyName() != null) &&
967 (this.getStreet() != null) &&
968 (this.getHouseNumber() != null) &&
969 (this.getZipCode() != null) &&
970 (this.getCity() != null) &&
971 (this.getEmailAddress() != null) &&
972 (this.getEmailAddressRepeat() != null));
976 public boolean isSameEmailAddressEntered () {
977 return (Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat()));
981 public Contact lookupContactById (final Long contactId) throws ContactNotFoundException {
983 Contact localContact = null;
988 // Try to lookup it in visible user list
989 for (final Iterator<Contact> iterator = this.contactList.iterator(); iterator.hasNext();) {
991 Contact next = iterator.next();
993 // Is the user id found?
994 if (Objects.equals(next.getContactId(), contactId)) {
995 // Copy to other variable
1001 // Is it still null?
1002 if (null == localContact) {
1003 // Not visible for the current user
1004 throw new ContactNotFoundException(contactId);
1007 // Copy all data to this bean
1008 this.copyContact(localContact);
1011 return localContact;
1015 public List<Contact> selectableContacts () {
1016 return Collections.unmodifiableList(this.selectableContacts);
1020 public void updateContactDataFromController (final Contact contact) {
1021 // Is the instance valid?
1022 if (null == contact) {
1024 throw new NullPointerException("contact is null"); //NOI18N
1025 } else if (contact.getContactId() == null) {
1027 throw new NullPointerException("contact.contactId is null"); //NOI18N
1028 } else if (contact.getContactId() < 1) {
1029 // Not valid id number
1030 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1034 this.copyContact(contact);
1038 * Adds email address to bean's internal list.
1040 * @param contact Contact instance
1042 private void addUserNameEmailAddress (final Contact contact) {
1043 // Make sure the entry is not added yet
1044 if (this.emailAddressList.contains(contact.getContactEmailAddress())) {
1046 throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", contact.getContactEmailAddress())); //NOI18N
1050 this.emailAddressList.add(contact.getContactEmailAddress());
1056 private void clear () {
1059 this.setGender(null);
1060 this.setTitle(null);
1061 this.setFirstName(null);
1062 this.setFamilyName(null);
1063 this.setStreet(null);
1064 this.setHouseNumber(null);
1065 this.setHouseNumberExtension(null);
1066 this.setZipCode(null);
1068 this.setCountry(null);
1071 this.setEmailAddress(null);
1072 this.setEmailAddressRepeat(null);
1073 this.setPhoneAreaCode(null);
1074 this.setPhoneCountry(null);
1075 this.setPhoneNumber(null);
1076 this.setCellphoneCarrier(null);
1077 this.setCellphoneNumber(null);
1078 this.setFaxAreaCode(null);
1079 this.setFaxCountry(null);
1080 this.setFaxNumber(null);
1083 this.setBirthday(null);
1084 this.setComment(null);
1088 * Copies given contact into the controller
1090 * @param contact Contact instance
1092 private void copyContact (final Contact contact) {
1093 // Is the instance valid?
1094 if (null == contact) {
1096 throw new NullPointerException("contact is null"); //NOI18N
1097 } else if (contact.getContactId() == null) {
1099 throw new NullPointerException("contact.contactId is null"); //NOI18N
1100 } else if (contact.getContactId() < 1) {
1101 // Not valid id number
1102 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1107 this.setGender(contact.getContactGender());
1108 this.setTitle(contact.getContactTitle());
1109 this.setFirstName(contact.getContactFirstName());
1110 this.setFamilyName(contact.getContactFamilyName());
1111 this.setStreet(contact.getContactStreet());
1112 this.setHouseNumber(contact.getContactHouseNumber());
1113 this.setHouseNumberExtension(contact.getContactHouseNumberExtension());
1114 this.setZipCode(contact.getContactZipCode());
1115 this.setCity(contact.getContactCity());
1116 this.setCountry(contact.getContactCountry());
1117 this.setEmailAddress(contact.getContactEmailAddress());
1118 this.setBirthday(contact.getContactBirthday());
1119 this.setComment(contact.getContactComment());
1121 // Get cellphone, phone and fax instance
1122 DialableCellphoneNumber cellphone = contact.getContactCellphoneNumber();
1123 DialableFaxNumber fax = contact.getContactFaxNumber();
1124 DialableLandLineNumber phone = contact.getContactLandLineNumber();
1127 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneAreaCode() > 0)) {
1128 this.setPhoneCountry(phone.getPhoneCountry());
1129 this.setPhoneAreaCode(phone.getPhoneAreaCode());
1130 this.setPhoneNumber(phone.getPhoneNumber());
1133 if ((cellphone instanceof DialableCellphoneNumber) && (cellphone.getCellphoneProvider() instanceof MobileProvider)) {
1134 this.setCellphoneCarrier(cellphone.getCellphoneProvider());
1135 this.setCellphoneNumber(cellphone.getPhoneNumber());
1138 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneAreaCode() > 0)) {
1139 this.setFaxCountry(fax.getPhoneCountry());
1140 this.setFaxAreaCode(fax.getPhoneAreaCode());
1141 this.setFaxNumber(fax.getPhoneNumber());
1146 * Removes given contact from all lists
1148 * @param contact Contact instance to remove
1150 private void removeContact (final Contact contact) {
1151 // Is the instance valid?
1152 if (null == contact) {
1154 throw new NullPointerException("contact is null"); //NOI18N
1155 } else if (contact.getContactId() == null) {
1157 throw new NullPointerException("contact.contactId is null"); //NOI18N
1158 } else if (contact.getContactId() < 1) {
1159 // Not valid id number
1160 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1163 // Remove from general list
1164 if (!this.contactList.remove(contact)) {
1165 // Did not remove contact
1166 throw new IllegalStateException(MessageFormat.format("contact {0} was not removed.", contact.getContactId())); //NOI18N
1169 // Remove from other lists
1170 this.emailAddressList.remove(contact.getContactEmailAddress());
1174 * Adds unique instance to contact list. First any existing instance is
1175 * being removed, then the new instance is added.
1177 * @param contact Contact instance to add uniquely
1179 private void uniqueAddContact (final Contact contact) {
1180 // Is the instance valid?
1181 if (null == contact) {
1183 throw new NullPointerException("contact is null"); //NOI18N
1184 } else if (contact.getContactId() == null) {
1186 throw new NullPointerException("contact.contactId is null"); //NOI18N
1187 } else if (contact.getContactId() < 1) {
1188 // Not valid id number
1189 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1192 // Get iterator from list
1193 Iterator<Contact> iterator = this.contactList.iterator();
1195 // "Walk" through all entries
1196 while (iterator.hasNext()) {
1198 Contact next = iterator.next();
1200 // Is id number the same?
1201 if (Objects.equals(contact.getContactId(), next.getContactId())) {
1202 // Found entry, so remove it and abort
1203 this.removeContact(next);
1208 // Add contact to list
1209 this.contactList.add(contact);