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.Iterator;
23 import java.util.List;
24 import java.util.Objects;
25 import javax.annotation.PostConstruct;
26 import javax.enterprise.context.SessionScoped;
27 import javax.enterprise.event.Observes;
28 import javax.faces.view.facelets.FaceletException;
29 import javax.inject.Inject;
30 import javax.inject.Named;
31 import javax.naming.Context;
32 import javax.naming.InitialContext;
33 import javax.naming.NamingException;
34 import org.mxchange.jcontacts.contact.Contact;
35 import org.mxchange.jcontacts.contact.ContactSessionBeanRemote;
36 import org.mxchange.jcontacts.contact.UserContact;
37 import org.mxchange.jcontacts.contact.gender.Gender;
38 import org.mxchange.jcontacts.contact.utils.ContactUtils;
39 import org.mxchange.jcontacts.events.contact.add.AdminAddedContactEvent;
40 import org.mxchange.jcontacts.events.contact.update.AdminUpdatedContactEvent;
41 import org.mxchange.jcontacts.exceptions.ContactNotFoundException;
42 import org.mxchange.jcountry.data.Country;
43 import org.mxchange.jphone.phonenumbers.cellphone.CellphoneNumber;
44 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
45 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
46 import org.mxchange.jphone.phonenumbers.fax.FaxNumber;
47 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
48 import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
49 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
50 import org.mxchange.jusercore.events.confirmation.UserConfirmedAccountEvent;
51 import org.mxchange.jusercore.events.login.UserLoggedInEvent;
52 import org.mxchange.jusercore.events.registration.UserRegisteredEvent;
53 import org.mxchange.jusercore.events.user.add.AdminAddedUserEvent;
54 import org.mxchange.jusercore.events.user.linked.AdminLinkedUserEvent;
55 import org.mxchange.jusercore.exceptions.UserPasswordMismatchException;
56 import org.mxchange.jusercore.model.user.User;
57 import org.mxchange.pizzaapplication.beans.BasePizzaController;
58 import org.mxchange.pizzaapplication.beans.login.PizzaUserLoginWebSessionController;
59 import org.mxchange.pizzaapplication.beans.user.PizzaUserWebSessionController;
62 * A general contact bean (controller)
64 * @author Roland Haeder<roland@mxchange.org>
66 @Named ("contactController")
68 public class PizzaContactWebSessionBean extends BasePizzaController implements PizzaContactWebSessionController {
73 private static final long serialVersionUID = 542_145_347_916L;
78 private Date birthday;
81 * Cellphone number's carrier
83 private MobileProvider cellphoneCarrier;
88 private Long cellphoneNumber;
98 private String comment;
101 * Remote contact bean
103 private final ContactSessionBeanRemote contactBean;
108 private List<Contact> contactList;
113 private Country country;
118 private String emailAddress;
123 private List<String> emailAddressList;
126 * Email address repeated
128 private String emailAddressRepeat;
133 private String familyName;
136 * Fax number's area code
138 private Integer faxAreaCode;
141 * Country instance for fax number
143 private Country faxCountry;
148 private Long faxNumber;
153 private String firstName;
158 private Gender gender;
163 private Short houseNumber;
166 * House number extension
168 private String houseNumberExtension;
171 * Whether a cellphone entry has been unlinked
173 private boolean isCellphoneUnlinked;
176 * Whether a fax entry has been unlinked
178 private boolean isFaxUnlinked;
181 * Whether a land-line number has been unlinked
183 private boolean isLandLineUnlinked;
186 * Phone number area code
188 private Integer phoneAreaCode;
191 * Country instance for phone number
193 private Country phoneCountry;
198 private Long phoneNumber;
201 * A list of all selectable contacts
203 private List<Contact> selectableContacts;
208 private String street;
211 * Regular user controller
214 private PizzaUserWebSessionController userController;
217 * Login bean (controller)
220 private PizzaUserLoginWebSessionController userLoginController;
225 private Integer zipCode;
228 * Default constructor
230 public PizzaContactWebSessionBean () {
233 // Get initial context
234 Context context = new InitialContext();
237 this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/pizzaservice-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
238 } catch (final NamingException e) {
240 throw new FaceletException(e);
245 public void afterAdminAddedContact (@Observes final AdminAddedContactEvent event) {
247 /* NOISY-DEBUG: */ System.out.println(MessageFormat.format("contactController.afterAdminAddedContact(): event={0} - CALLED!", event)); //NOI18N
249 // The event must be valid
252 throw new NullPointerException("event is null"); //NOI18N
253 } else if (event.getAddedContact() == null) {
255 throw new NullPointerException("event.addedContact is null"); //NOI18N
256 } else if (event.getAddedContact().getContactId() == null) {
258 throw new NullPointerException("event.addedContact.contactId is null"); //NOI18N
259 } else if (event.getAddedContact().getContactId() < 1) {
261 throw new IllegalArgumentException(MessageFormat.format("event.addedContact.contactId={0} is not valid", event.getAddedContact().getContactId())); //NOI18N //NOI18N
268 this.uniqueAddContact(event.getAddedContact());
270 // Add to selectable contacts
271 this.selectableContacts.add(event.getAddedContact());
275 public void afterAdminAddedUserEvent (@Observes final AdminAddedUserEvent event) {
276 // event should not be null
279 throw new NullPointerException("event is null"); //NOI18N
280 } else if (event.getAddedUser() == null) {
282 throw new NullPointerException("event.addedUser is null"); //NOI18N
283 } else if (event.getAddedUser().getUserId() == null) {
285 throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
286 } else if (event.getAddedUser().getUserId() < 1) {
288 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
296 public void afterAdminLinkedUser (@Observes final AdminLinkedUserEvent event) {
297 // event should not be null
300 throw new NullPointerException("event is null"); //NOI18N
301 } else if (event.getLinkedUser() == null) {
303 throw new NullPointerException("event.linkedUser is null"); //NOI18N
304 } else if (event.getLinkedUser().getUserContact() == null) {
306 throw new NullPointerException("event.linkedUser.userContact is null"); //NOI18N
307 } else if (event.getLinkedUser().getUserContact().getContactId() == null) {
309 throw new NullPointerException("event.linkedUser.userContact.contactId is null"); //NOI18N
310 } else if (event.getLinkedUser().getUserContact().getContactId() < 1) {
312 throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getLinkedUser(), event.getLinkedUser().getUserContact().getContactId())); //NOI18N
315 // Remove contact from list available contacts list
316 this.selectableContacts.remove(event.getLinkedUser().getUserContact());
323 public void afterAdminUpdatedContactDataEvent (@Observes final AdminUpdatedContactEvent event) {
324 // event should not be null
327 throw new NullPointerException("event is null"); //NOI18N
328 } else if (event.getUpdatedContact() == null) {
330 throw new NullPointerException("event.updatedContact is null"); //NOI18N
331 } else if (event.getUpdatedContact().getContactId() == null) {
333 throw new NullPointerException("event.updatedContact.contactId is null"); //NOI18N
334 } else if (event.getUpdatedContact().getContactId() < 1) {
336 throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
339 // Add contact instance only once
340 this.uniqueAddContact(event.getUpdatedContact());
342 // Add email address to list
343 this.emailAddressList.add(event.getUpdatedContact().getContactEmailAddress());
347 public void afterRegistrationEvent (@Observes final UserRegisteredEvent event) {
348 // event should not be null
351 throw new NullPointerException("event is null"); //NOI18N
352 } else if (event.getRegisteredUser() == null) {
354 throw new NullPointerException("event.registeredUser is null"); //NOI18N
355 } else if (event.getRegisteredUser().getUserId() == null) {
357 throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
358 } else if (event.getRegisteredUser().getUserId() < 1) {
360 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
364 Contact registeredContact = event.getRegisteredUser().getUserContact();
366 // Copy all data from registered->user
367 this.copyContact(registeredContact);
369 // Add contact instance only once
370 this.uniqueAddContact(registeredContact);
372 // Add user name and email address
373 this.addUserNameEmailAddress(registeredContact);
380 public void afterUserConfirmedAccount (@Observes final UserConfirmedAccountEvent event) {
381 // event should not be null
384 throw new NullPointerException("event is null"); //NOI18N
385 } else if (event.getConfirmedUser() == null) {
387 throw new NullPointerException("event.confirmedUser is null"); //NOI18N
388 } else if (event.getConfirmedUser().getUserId() == null) {
390 throw new NullPointerException("event.confirmedUser.userId is null"); //NOI18N
391 } else if (event.getConfirmedUser().getUserId() < 1) {
393 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getConfirmedUser(), event.getConfirmedUser().getUserId())); //NOI18N
396 // Add contact instance only once
397 this.uniqueAddContact(event.getConfirmedUser().getUserContact());
401 public void afterUserLogin (@Observes final UserLoggedInEvent event) {
402 // event should not be null
405 throw new NullPointerException("event is null"); //NOI18N
406 } else if (event.getLoggedInUser() == null) {
408 throw new NullPointerException("event.loggedInUser is null"); //NOI18N
409 } else if (event.getLoggedInUser().getUserId() == null) {
411 throw new NullPointerException("event.loggedInUser.userId is null"); //NOI18N
412 } else if (event.getLoggedInUser().getUserId() < 1) {
414 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
417 // Copy all data to this bean
418 this.copyContact(event.getLoggedInUser().getUserContact());
422 public List<Contact> allContacts () {
424 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ContactController.allContacts: contactList.size()={0} - EXIT!", this.contactList.size()));
426 // Return un-modified list
427 return Collections.unmodifiableList(this.contactList);
431 public Contact createContactInstance () {
433 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createContactInstance: CALLED!", this.getClass().getSimpleName()));
435 // Is all required data set?
436 if (!this.isRequiredPersonalDataSet()) {
437 // No, then abort here
438 throw new FaceletException(new IllegalArgumentException("Not all personal data is set, but createContactInstance() is called.")); //NOI18N
441 // Generate phone number
442 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
443 DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
444 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
446 // Create new contact
447 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
448 contact.setContactStreet(this.getStreet());
449 contact.setContactHouseNumber(this.getHouseNumber());
450 contact.setContactHouseNumberExtension(this.getHouseNumberExtension());
451 contact.setContactZipCode(this.getZipCode());
452 contact.setContactCity(this.getCity());
453 contact.setContactCountry(this.getCountry());
454 contact.setContactEmailAddress(this.getEmailAddress());
455 contact.setContactBirthday(this.getBirthday());
456 contact.setContactComment(this.getComment());
459 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createContactInstance: this.emailAddress={1}", this.getClass().getSimpleName(), this.getEmailAddress()));
461 // Don't set null or wrong references
462 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getPhoneAreaCode() != null) && (this.getPhoneNumber() != null) && (this.getPhoneAreaCode() > 0) && (this.getPhoneNumber() > 0)) {
463 // Now the number must be given
464 if (phone.getPhoneAreaCode() == null) {
466 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
467 } else if (phone.getPhoneAreaCode() < 1) {
469 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
470 } else if (phone.getPhoneNumber() == null) {
472 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
473 } else if (phone.getPhoneNumber() < 1) {
475 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
479 contact.setContactLandLineNumber(phone);
482 // Don't set null or wrong references
483 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
484 // Now the number must be given
485 if (fax.getPhoneAreaCode() == null) {
487 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
488 } else if (fax.getPhoneAreaCode() < 1) {
490 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
491 } else if (fax.getPhoneNumber() == null) {
493 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
494 } else if (fax.getPhoneNumber() < 1) {
496 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
500 contact.setContactFaxNumber(fax);
503 // Is the provider set?
504 if ((cellphone instanceof DialableCellphoneNumber) && (this.getCellphoneCarrier() instanceof MobileProvider) && (this.getCellphoneNumber() != null) && (this.getCellphoneNumber() > 0)) {
505 // Is the number set?
506 if (cellphone.getPhoneNumber() == null) {
508 throw new NullPointerException("cellphone.phoneNumber is null"); //NOI18N
509 } else if (cellphone.getPhoneNumber() < 1) {
511 throw new IllegalArgumentException("cellphone.phoneNumber is zero or below."); //NOI18N
514 // Set cellphone number
515 contact.setContactCellphoneNumber(cellphone);
519 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createContactInstance: contact={1} - EXIT!", this.getClass().getSimpleName(), contact));
526 public String doChangePersonalContactData () {
527 // This method shall only be called if the user is logged-in
528 if (!this.userLoginController.isUserLoggedIn()) {
530 throw new IllegalStateException("User is not logged-in"); //NOI18N
531 } else if (!this.isRequiredChangePersonalDataSet()) {
532 // Not all required fields are set
533 throw new FaceletException("Not all required fields are set."); //NOI18N
534 } else if (!this.userLoginController.ifCurrentPasswordMatches()) {
535 // Password not matching
536 this.showFacesMessage("form_login_change_personal:currentPassword", new UserPasswordMismatchException(this.userLoginController.getLoggedInUser())); //NOI18N
540 // Get contact instance
541 Contact contact = this.userLoginController.getLoggedInUser().getUserContact();
543 // It should be there, so run some tests on it
544 assert (contact instanceof Contact) : "Instance userLoginController.loggedInUser.userContact is null"; //NOI18N
545 assert (contact.getContactId() instanceof Long) : "Instance userLoginController.userContact.contactId is null"; //NOI18N
546 assert (contact.getContactId() > 0) : MessageFormat.format("Instance userLoginController.userContact.contactId={0} is invalid", contact.getContactId()); //NOI18N
549 contact.setContactGender(this.getGender());
550 contact.setContactFirstName(this.getFirstName());
551 contact.setContactFamilyName(this.getFamilyName());
552 contact.setContactStreet(this.getStreet());
553 contact.setContactHouseNumber(this.getHouseNumber());
554 contact.setContactHouseNumberExtension(this.getHouseNumberExtension());
555 contact.setContactZipCode(this.getZipCode());
556 contact.setContactCity(this.getCity());
557 contact.setContactCountry(this.getCountry());
559 // Update contact's cellphone number
560 this.isCellphoneUnlinked = ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
562 // Update contact's land-line number
563 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
565 // Update contact's fax number
566 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
568 // Send it to the EJB
569 this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
572 return "contact_data_saved"; //NOI18N
576 @SuppressWarnings ("ReturnOfDateField")
577 public Date getBirthday () {
578 return this.birthday;
582 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
583 public void setBirthday (final Date birthday) {
584 this.birthday = birthday;
588 public MobileProvider getCellphoneCarrier () {
589 return this.cellphoneCarrier;
593 public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
594 this.cellphoneCarrier = cellphoneCarrier;
598 public Long getCellphoneNumber () {
599 return this.cellphoneNumber;
603 public void setCellphoneNumber (Long cellphoneNumber) {
604 this.cellphoneNumber = cellphoneNumber;
608 public String getCity () {
613 public void setCity (final String city) {
618 public String getComment () {
623 public void setComment (final String comment) {
624 this.comment = comment;
628 public String getControllerType () {
629 return "general"; //NOI18N
634 public void setControllerType (final String controllerType) {
635 throw new UnsupportedOperationException("Setting controller type is not supported."); //NOI18N
639 public Country getCountry () {
644 public void setCountry (final Country country) {
645 this.country = country;
649 public String getEmailAddress () {
650 return this.emailAddress;
654 public void setEmailAddress (final String emailAddress) {
655 this.emailAddress = emailAddress;
659 public String getEmailAddressRepeat () {
660 return this.emailAddressRepeat;
664 public void setEmailAddressRepeat (final String emailAddressRepeat) {
665 this.emailAddressRepeat = emailAddressRepeat;
669 public String getFamilyName () {
670 return this.familyName;
674 public void setFamilyName (final String familyName) {
675 this.familyName = familyName;
679 public Integer getFaxAreaCode () {
680 return this.faxAreaCode;
684 public void setFaxAreaCode (final Integer faxAreaCode) {
685 this.faxAreaCode = faxAreaCode;
689 public Country getFaxCountry () {
690 return this.faxCountry;
694 public void setFaxCountry (final Country faxCountry) {
695 this.faxCountry = faxCountry;
699 public Long getFaxNumber () {
700 return this.faxNumber;
704 public void setFaxNumber (final Long faxNumber) {
705 this.faxNumber = faxNumber;
709 public String getFirstName () {
710 return this.firstName;
714 public void setFirstName (final String firstName) {
715 this.firstName = firstName;
719 public Gender getGender () {
724 public void setGender (final Gender gender) {
725 this.gender = gender;
729 public Short getHouseNumber () {
730 return this.houseNumber;
734 public void setHouseNumber (final Short houseNumber) {
735 this.houseNumber = houseNumber;
739 public String getHouseNumberExtension () {
740 return this.houseNumberExtension;
744 public void setHouseNumberExtension (final String houseNumberExtension) {
745 this.houseNumberExtension = houseNumberExtension;
749 public Integer getPhoneAreaCode () {
750 return this.phoneAreaCode;
754 public void setPhoneAreaCode (final Integer phoneAreaCode) {
755 this.phoneAreaCode = phoneAreaCode;
759 public Country getPhoneCountry () {
760 return this.phoneCountry;
764 public void setPhoneCountry (final Country phoneCountry) {
765 this.phoneCountry = phoneCountry;
769 public Long getPhoneNumber () {
770 return this.phoneNumber;
774 public void setPhoneNumber (final Long phoneNumber) {
775 this.phoneNumber = phoneNumber;
779 public String getStreet () {
784 public void setStreet (final String street) {
785 this.street = street;
789 public Integer getZipCode () {
794 public void setZipCode (final Integer zipCode) {
795 this.zipCode = zipCode;
799 public boolean hasContacts () {
800 return (!this.contactList.isEmpty());
804 * Post-initialization of this class
807 public void init () {
808 // Get full email address list for reducing EJB calls
809 this.emailAddressList = this.contactBean.getEmailAddressList();
811 // Get full contact list
812 this.contactList = this.contactBean.getAllContacts();
815 List<User> allUsers = this.userController.allUsers();
818 List<Contact> allContacts = this.contactBean.getAllContacts();
821 Iterator<Contact> iterator = allContacts.iterator();
824 while (iterator.hasNext()) {
826 Contact next = iterator.next();
829 Iterator<User> userIterator = allUsers.iterator();
831 // Loop through all users
832 while (userIterator.hasNext()) {
834 User nextUser = userIterator.next();
837 if (Objects.equals(next, nextUser.getUserContact())) {
846 this.selectableContacts = allContacts;
850 public boolean isEmailAddressRegistered (final Contact contact) {
852 if (null == contact) {
854 throw new NullPointerException("contact is null"); //NOI18N
855 } else if (contact.getContactEmailAddress() == null) {
857 throw new NullPointerException("contact.contactEmailAddress is null"); //NOI18N
858 } else if (contact.getContactEmailAddress().isEmpty()) {
860 throw new IllegalArgumentException("contact.contactEmailAddress is empty."); //NOI18N
864 return ((this.emailAddressList instanceof List) && (this.emailAddressList.contains(contact.getContactEmailAddress())));
868 public boolean isRequiredChangePersonalDataSet () {
869 return ((this.getGender() != null) &&
870 (this.getFirstName() != null) &&
871 (this.getFamilyName() != null) &&
872 (this.getStreet() != null) &&
873 (this.getHouseNumber() != null) &&
874 (this.getZipCode() != null) &&
875 (this.getCity() != null));
879 public boolean isRequiredPersonalDataSet () {
880 return ((this.getGender() != null) &&
881 (this.getFirstName() != null) &&
882 (this.getFamilyName() != null) &&
883 (this.getStreet() != null) &&
884 (this.getHouseNumber() != null) &&
885 (this.getZipCode() != null) &&
886 (this.getCity() != null) &&
887 (this.getEmailAddress() != null) &&
888 (this.getEmailAddressRepeat() != null));
892 public boolean isSameEmailAddressEntered () {
893 return (Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat()));
897 public Contact lookupContactById (final Long contactId) throws ContactNotFoundException {
899 Contact localContact = null;
904 // Try to lookup it in visible user list
905 for (final Iterator<Contact> iterator = this.contactList.iterator(); iterator.hasNext();) {
907 Contact next = iterator.next();
909 // Is the user id found?
910 if (Objects.equals(next.getContactId(), contactId)) {
911 // Copy to other variable
918 if (null == localContact) {
919 // Not visible for the current user
920 throw new ContactNotFoundException(contactId);
923 // Copy all data to this bean
924 this.copyContact(localContact);
931 public List<Contact> selectableContacts () {
932 return Collections.unmodifiableList(this.selectableContacts);
936 public void updateContactDataFromController (final Contact contact) {
937 // Is the instance valid?
938 if (null == contact) {
940 throw new NullPointerException("contact is null"); //NOI18N
941 } else if (contact.getContactId() == null) {
943 throw new NullPointerException("contact.contactId is null"); //NOI18N
944 } else if (contact.getContactId() < 1) {
945 // Not valid id number
946 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
950 this.copyContact(contact);
954 * Adds email address to bean's internal list.
956 * @param contact Contact instance
958 private void addUserNameEmailAddress (final Contact contact) {
959 // Make sure the entry is not added yet
960 if (this.emailAddressList.contains(contact.getContactEmailAddress())) {
962 throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", contact.getContactEmailAddress())); //NOI18N
966 this.emailAddressList.add(contact.getContactEmailAddress());
972 private void clear () {
975 this.setGender(null);
976 // TODO: Unfinished this.setTitle(null);
977 this.setFirstName(null);
978 this.setFamilyName(null);
979 this.setStreet(null);
980 this.setHouseNumber(null);
981 this.setHouseNumberExtension(null);
982 this.setZipCode(null);
984 this.setCountry(null);
987 this.setEmailAddress(null);
988 this.setEmailAddressRepeat(null);
989 this.setPhoneAreaCode(null);
990 this.setPhoneCountry(null);
991 this.setPhoneNumber(null);
992 this.setCellphoneCarrier(null);
993 this.setCellphoneNumber(null);
994 this.setFaxAreaCode(null);
995 this.setFaxCountry(null);
996 this.setFaxNumber(null);
999 this.setBirthday(null);
1000 this.setComment(null);
1004 * Copies given contact into the controller
1006 * @param contact Contact instance
1008 private void copyContact (final Contact contact) {
1009 // Is the instance valid?
1010 if (null == contact) {
1012 throw new NullPointerException("contact is null"); //NOI18N
1013 } else if (contact.getContactId() == null) {
1015 throw new NullPointerException("contact.contactId is null"); //NOI18N
1016 } else if (contact.getContactId() < 1) {
1017 // Not valid id number
1018 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1023 this.setGender(contact.getContactGender());
1024 this.setFirstName(contact.getContactFirstName());
1025 this.setFamilyName(contact.getContactFamilyName());
1026 this.setStreet(contact.getContactStreet());
1027 this.setHouseNumber(contact.getContactHouseNumber());
1028 this.setHouseNumberExtension(contact.getContactHouseNumberExtension());
1029 this.setZipCode(contact.getContactZipCode());
1030 this.setCity(contact.getContactCity());
1031 this.setCountry(contact.getContactCountry());
1032 this.setEmailAddress(contact.getContactEmailAddress());
1033 this.setBirthday(contact.getContactBirthday());
1034 this.setComment(contact.getContactComment());
1036 // Get cellphone, phone and fax instance
1037 DialableCellphoneNumber cellphone = contact.getContactCellphoneNumber();
1038 DialableFaxNumber fax = contact.getContactFaxNumber();
1039 DialableLandLineNumber phone = contact.getContactLandLineNumber();
1042 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneAreaCode() > 0)) {
1043 this.setPhoneCountry(phone.getPhoneCountry());
1044 this.setPhoneAreaCode(phone.getPhoneAreaCode());
1045 this.setPhoneNumber(phone.getPhoneNumber());
1048 if ((cellphone instanceof DialableCellphoneNumber) && (cellphone.getCellphoneProvider() instanceof MobileProvider)) {
1049 this.setCellphoneCarrier(cellphone.getCellphoneProvider());
1050 this.setCellphoneNumber(cellphone.getPhoneNumber());
1053 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneAreaCode() > 0)) {
1054 this.setFaxCountry(fax.getPhoneCountry());
1055 this.setFaxAreaCode(fax.getPhoneAreaCode());
1056 this.setFaxNumber(fax.getPhoneNumber());
1061 * Removes given contact from all lists
1063 * @param contact Contact instance to remove
1065 private void removeContact (final Contact contact) {
1066 // Is the instance valid?
1067 if (null == contact) {
1069 throw new NullPointerException("contact is null"); //NOI18N
1070 } else if (contact.getContactId() == null) {
1072 throw new NullPointerException("contact.contactId is null"); //NOI18N
1073 } else if (contact.getContactId() < 1) {
1074 // Not valid id number
1075 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1078 // Remove from general list
1079 if (!this.contactList.remove(contact)) {
1080 // Did not remove contact
1081 throw new IllegalStateException(MessageFormat.format("contact {0} was not removed.", contact.getContactId())); //NOI18N
1084 // Remove from other lists
1085 this.emailAddressList.remove(contact.getContactEmailAddress());
1089 * Adds unique instance to contact list. First any existing instance is
1090 * being removed, then the new instance is added.
1092 * @param contact Contact instance to add uniquely
1094 private void uniqueAddContact (final Contact contact) {
1095 // Is the instance valid?
1096 if (null == contact) {
1098 throw new NullPointerException("contact is null"); //NOI18N
1099 } else if (contact.getContactId() == null) {
1101 throw new NullPointerException("contact.contactId is null"); //NOI18N
1102 } else if (contact.getContactId() < 1) {
1103 // Not valid id number
1104 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1107 // Get iterator from list
1108 Iterator<Contact> iterator = this.contactList.iterator();
1110 // "Walk" through all entries
1111 while (iterator.hasNext()) {
1113 Contact next = iterator.next();
1115 // Is id number the same?
1116 if (Objects.equals(contact.getContactId(), next.getContactId())) {
1117 // Found entry, so remove it and abort
1118 this.removeContact(next);
1123 // Add contact to list
1124 this.contactList.add(contact);