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;
213 private String title;
216 * Regular user controller
219 private PizzaUserWebSessionController userController;
222 * Login bean (controller)
225 private PizzaUserLoginWebSessionController userLoginController;
230 private Integer zipCode;
233 * Default constructor
235 public PizzaContactWebSessionBean () {
238 // Get initial context
239 Context context = new InitialContext();
242 this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/pizzaservice-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
243 } catch (final NamingException e) {
245 throw new FaceletException(e);
250 public void afterAdminAddedContact (@Observes final AdminAddedContactEvent event) {
252 /* NOISY-DEBUG: */ System.out.println(MessageFormat.format("contactController.afterAdminAddedContact(): event={0} - CALLED!", event)); //NOI18N
254 // The event must be valid
257 throw new NullPointerException("event is null"); //NOI18N
258 } else if (event.getAddedContact() == null) {
260 throw new NullPointerException("event.addedContact is null"); //NOI18N
261 } else if (event.getAddedContact().getContactId() == null) {
263 throw new NullPointerException("event.addedContact.contactId is null"); //NOI18N
264 } else if (event.getAddedContact().getContactId() < 1) {
266 throw new IllegalArgumentException(MessageFormat.format("event.addedContact.contactId={0} is not valid", event.getAddedContact().getContactId())); //NOI18N //NOI18N
273 this.uniqueAddContact(event.getAddedContact());
275 // Add to selectable contacts
276 this.selectableContacts.add(event.getAddedContact());
280 public void afterAdminAddedUserEvent (@Observes final AdminAddedUserEvent event) {
281 // event should not be null
284 throw new NullPointerException("event is null"); //NOI18N
285 } else if (event.getAddedUser() == null) {
287 throw new NullPointerException("event.addedUser is null"); //NOI18N
288 } else if (event.getAddedUser().getUserId() == null) {
290 throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
291 } else if (event.getAddedUser().getUserId() < 1) {
293 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
301 public void afterAdminLinkedUser (@Observes final AdminLinkedUserEvent event) {
302 // event should not be null
305 throw new NullPointerException("event is null"); //NOI18N
306 } else if (event.getLinkedUser() == null) {
308 throw new NullPointerException("event.linkedUser is null"); //NOI18N
309 } else if (event.getLinkedUser().getUserContact() == null) {
311 throw new NullPointerException("event.linkedUser.userContact is null"); //NOI18N
312 } else if (event.getLinkedUser().getUserContact().getContactId() == null) {
314 throw new NullPointerException("event.linkedUser.userContact.contactId is null"); //NOI18N
315 } else if (event.getLinkedUser().getUserContact().getContactId() < 1) {
317 throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getLinkedUser(), event.getLinkedUser().getUserContact().getContactId())); //NOI18N
320 // Remove contact from list available contacts list
321 this.selectableContacts.remove(event.getLinkedUser().getUserContact());
328 public void afterAdminUpdatedContactDataEvent (@Observes final AdminUpdatedContactEvent event) {
329 // event should not be null
332 throw new NullPointerException("event is null"); //NOI18N
333 } else if (event.getUpdatedContact() == null) {
335 throw new NullPointerException("event.updatedContact is null"); //NOI18N
336 } else if (event.getUpdatedContact().getContactId() == null) {
338 throw new NullPointerException("event.updatedContact.contactId is null"); //NOI18N
339 } else if (event.getUpdatedContact().getContactId() < 1) {
341 throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
344 // Add contact instance only once
345 this.uniqueAddContact(event.getUpdatedContact());
347 // Add email address to list
348 this.emailAddressList.add(event.getUpdatedContact().getContactEmailAddress());
352 public void afterRegistrationEvent (@Observes final UserRegisteredEvent event) {
353 // event should not be null
356 throw new NullPointerException("event is null"); //NOI18N
357 } else if (event.getRegisteredUser() == null) {
359 throw new NullPointerException("event.registeredUser is null"); //NOI18N
360 } else if (event.getRegisteredUser().getUserId() == null) {
362 throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
363 } else if (event.getRegisteredUser().getUserId() < 1) {
365 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
369 Contact registeredContact = event.getRegisteredUser().getUserContact();
371 // Copy all data from registered->user
372 this.copyContact(registeredContact);
374 // Add contact instance only once
375 this.uniqueAddContact(registeredContact);
377 // Add user name and email address
378 this.addUserNameEmailAddress(registeredContact);
385 public void afterUserConfirmedAccount (@Observes final UserConfirmedAccountEvent event) {
386 // event should not be null
389 throw new NullPointerException("event is null"); //NOI18N
390 } else if (event.getConfirmedUser() == null) {
392 throw new NullPointerException("event.confirmedUser is null"); //NOI18N
393 } else if (event.getConfirmedUser().getUserId() == null) {
395 throw new NullPointerException("event.confirmedUser.userId is null"); //NOI18N
396 } else if (event.getConfirmedUser().getUserId() < 1) {
398 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getConfirmedUser(), event.getConfirmedUser().getUserId())); //NOI18N
401 // Add contact instance only once
402 this.uniqueAddContact(event.getConfirmedUser().getUserContact());
406 public void afterUserLogin (@Observes final UserLoggedInEvent event) {
407 // event should not be null
410 throw new NullPointerException("event is null"); //NOI18N
411 } else if (event.getLoggedInUser() == null) {
413 throw new NullPointerException("event.loggedInUser is null"); //NOI18N
414 } else if (event.getLoggedInUser().getUserId() == null) {
416 throw new NullPointerException("event.loggedInUser.userId is null"); //NOI18N
417 } else if (event.getLoggedInUser().getUserId() < 1) {
419 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
422 // Copy all data to this bean
423 this.copyContact(event.getLoggedInUser().getUserContact());
427 public List<Contact> allContacts () {
429 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ContactController.allContacts: contactList.size()={0} - EXIT!", this.contactList.size()));
431 // Return un-modified list
432 return Collections.unmodifiableList(this.contactList);
436 public Contact createContactInstance () {
438 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createContactInstance: CALLED!", this.getClass().getSimpleName()));
440 // Is all required data set?
441 if (!this.isRequiredPersonalDataSet()) {
442 // No, then abort here
443 throw new FaceletException(new IllegalArgumentException("Not all personal data is set, but createContactInstance() is called.")); //NOI18N
446 // Generate phone number
447 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
448 DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
449 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
451 // Create new contact
452 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
453 contact.setContactStreet(this.getStreet());
454 contact.setContactHouseNumber(this.getHouseNumber());
455 contact.setContactHouseNumberExtension(this.getHouseNumberExtension());
456 contact.setContactZipCode(this.getZipCode());
457 contact.setContactCity(this.getCity());
458 contact.setContactCountry(this.getCountry());
459 contact.setContactEmailAddress(this.getEmailAddress());
460 contact.setContactBirthday(this.getBirthday());
461 contact.setContactComment(this.getComment());
464 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createContactInstance: this.emailAddress={1}", this.getClass().getSimpleName(), this.getEmailAddress()));
466 // Don't set null or wrong references
467 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getPhoneAreaCode() != null) && (this.getPhoneNumber() != null) && (this.getPhoneAreaCode() > 0) && (this.getPhoneNumber() > 0)) {
468 // Now the number must be given
469 if (phone.getPhoneAreaCode() == null) {
471 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
472 } else if (phone.getPhoneAreaCode() < 1) {
474 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
475 } else if (phone.getPhoneNumber() == null) {
477 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
478 } else if (phone.getPhoneNumber() < 1) {
480 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
484 contact.setContactLandLineNumber(phone);
487 // Don't set null or wrong references
488 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
489 // Now the number must be given
490 if (fax.getPhoneAreaCode() == null) {
492 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
493 } else if (fax.getPhoneAreaCode() < 1) {
495 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
496 } else if (fax.getPhoneNumber() == null) {
498 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
499 } else if (fax.getPhoneNumber() < 1) {
501 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
505 contact.setContactFaxNumber(fax);
508 // Is the provider set?
509 if ((cellphone instanceof DialableCellphoneNumber) && (this.getCellphoneCarrier() instanceof MobileProvider) && (this.getCellphoneNumber() != null) && (this.getCellphoneNumber() > 0)) {
510 // Is the number set?
511 if (cellphone.getPhoneNumber() == null) {
513 throw new NullPointerException("cellphone.phoneNumber is null"); //NOI18N
514 } else if (cellphone.getPhoneNumber() < 1) {
516 throw new IllegalArgumentException("cellphone.phoneNumber is zero or below."); //NOI18N
519 // Set cellphone number
520 contact.setContactCellphoneNumber(cellphone);
524 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createContactInstance: contact={1} - EXIT!", this.getClass().getSimpleName(), contact));
531 public String doChangePersonalContactData () {
532 // This method shall only be called if the user is logged-in
533 if (!this.userLoginController.isUserLoggedIn()) {
535 throw new IllegalStateException("User is not logged-in"); //NOI18N
536 } else if (!this.isRequiredChangePersonalDataSet()) {
537 // Not all required fields are set
538 throw new FaceletException("Not all required fields are set."); //NOI18N
539 } else if (!this.userLoginController.ifCurrentPasswordMatches()) {
540 // Password not matching
541 this.showFacesMessage("form_login_change_personal:currentPassword", new UserPasswordMismatchException(this.userLoginController.getLoggedInUser())); //NOI18N
545 // Get contact instance
546 Contact contact = this.userLoginController.getLoggedInUser().getUserContact();
548 // It should be there, so run some tests on it
549 assert (contact instanceof Contact) : "Instance userLoginController.loggedInUser.userContact is null"; //NOI18N
550 assert (contact.getContactId() instanceof Long) : "Instance userLoginController.userContact.contactId is null"; //NOI18N
551 assert (contact.getContactId() > 0) : MessageFormat.format("Instance userLoginController.userContact.contactId={0} is invalid", contact.getContactId()); //NOI18N
554 contact.setContactGender(this.getGender());
555 contact.setContactFirstName(this.getFirstName());
556 contact.setContactFamilyName(this.getFamilyName());
557 contact.setContactStreet(this.getStreet());
558 contact.setContactHouseNumber(this.getHouseNumber());
559 contact.setContactHouseNumberExtension(this.getHouseNumberExtension());
560 contact.setContactZipCode(this.getZipCode());
561 contact.setContactCity(this.getCity());
562 contact.setContactCountry(this.getCountry());
564 // Update contact's cellphone number
565 this.isCellphoneUnlinked = ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
567 // Update contact's land-line number
568 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
570 // Update contact's fax number
571 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
573 // Send it to the EJB
574 this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
577 return "contact_data_saved"; //NOI18N
581 @SuppressWarnings ("ReturnOfDateField")
582 public Date getBirthday () {
583 return this.birthday;
587 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
588 public void setBirthday (final Date birthday) {
589 this.birthday = birthday;
593 public MobileProvider getCellphoneCarrier () {
594 return this.cellphoneCarrier;
598 public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
599 this.cellphoneCarrier = cellphoneCarrier;
603 public Long getCellphoneNumber () {
604 return this.cellphoneNumber;
608 public void setCellphoneNumber (Long cellphoneNumber) {
609 this.cellphoneNumber = cellphoneNumber;
613 public String getCity () {
618 public void setCity (final String city) {
623 public String getComment () {
628 public void setComment (final String comment) {
629 this.comment = comment;
633 public String getControllerType () {
634 return "general"; //NOI18N
639 public void setControllerType (final String controllerType) {
640 throw new UnsupportedOperationException("Setting controller type is not supported."); //NOI18N
644 public Country getCountry () {
649 public void setCountry (final Country country) {
650 this.country = country;
654 public String getEmailAddress () {
655 return this.emailAddress;
659 public void setEmailAddress (final String emailAddress) {
660 this.emailAddress = emailAddress;
664 public String getEmailAddressRepeat () {
665 return this.emailAddressRepeat;
669 public void setEmailAddressRepeat (final String emailAddressRepeat) {
670 this.emailAddressRepeat = emailAddressRepeat;
674 public String getFamilyName () {
675 return this.familyName;
679 public void setFamilyName (final String familyName) {
680 this.familyName = familyName;
684 public Integer getFaxAreaCode () {
685 return this.faxAreaCode;
689 public void setFaxAreaCode (final Integer faxAreaCode) {
690 this.faxAreaCode = faxAreaCode;
694 public Country getFaxCountry () {
695 return this.faxCountry;
699 public void setFaxCountry (final Country faxCountry) {
700 this.faxCountry = faxCountry;
704 public Long getFaxNumber () {
705 return this.faxNumber;
709 public void setFaxNumber (final Long faxNumber) {
710 this.faxNumber = faxNumber;
714 public String getFirstName () {
715 return this.firstName;
719 public void setFirstName (final String firstName) {
720 this.firstName = firstName;
724 public Gender getGender () {
729 public void setGender (final Gender gender) {
730 this.gender = gender;
734 public Short getHouseNumber () {
735 return this.houseNumber;
739 public void setHouseNumber (final Short houseNumber) {
740 this.houseNumber = houseNumber;
744 public String getHouseNumberExtension () {
745 return this.houseNumberExtension;
749 public void setHouseNumberExtension (final String houseNumberExtension) {
750 this.houseNumberExtension = houseNumberExtension;
754 public Integer getPhoneAreaCode () {
755 return this.phoneAreaCode;
759 public void setPhoneAreaCode (final Integer phoneAreaCode) {
760 this.phoneAreaCode = phoneAreaCode;
764 public Country getPhoneCountry () {
765 return this.phoneCountry;
769 public void setPhoneCountry (final Country phoneCountry) {
770 this.phoneCountry = phoneCountry;
774 public Long getPhoneNumber () {
775 return this.phoneNumber;
779 public void setPhoneNumber (final Long phoneNumber) {
780 this.phoneNumber = phoneNumber;
784 public String getStreet () {
789 public void setStreet (final String street) {
790 this.street = street;
794 public String getTitle () {
799 public void setTitle (final String title) {
804 public Integer getZipCode () {
809 public void setZipCode (final Integer zipCode) {
810 this.zipCode = zipCode;
814 public boolean hasContacts () {
815 return (!this.contactList.isEmpty());
819 * Post-initialization of this class
822 public void init () {
823 // Get full email address list for reducing EJB calls
824 this.emailAddressList = this.contactBean.getEmailAddressList();
826 // Get full contact list
827 this.contactList = this.contactBean.getAllContacts();
830 List<User> allUsers = this.userController.allUsers();
833 List<Contact> allContacts = this.contactBean.getAllContacts();
836 Iterator<Contact> iterator = allContacts.iterator();
839 while (iterator.hasNext()) {
841 Contact next = iterator.next();
844 Iterator<User> userIterator = allUsers.iterator();
846 // Loop through all users
847 while (userIterator.hasNext()) {
849 User nextUser = userIterator.next();
852 if (Objects.equals(next, nextUser.getUserContact())) {
861 this.selectableContacts = allContacts;
865 public boolean isEmailAddressRegistered (final Contact contact) {
867 if (null == contact) {
869 throw new NullPointerException("contact is null"); //NOI18N
870 } else if (contact.getContactEmailAddress() == null) {
872 throw new NullPointerException("contact.contactEmailAddress is null"); //NOI18N
873 } else if (contact.getContactEmailAddress().isEmpty()) {
875 throw new IllegalArgumentException("contact.contactEmailAddress is empty."); //NOI18N
879 return ((this.emailAddressList instanceof List) && (this.emailAddressList.contains(contact.getContactEmailAddress())));
883 public boolean isRequiredChangePersonalDataSet () {
884 return ((this.getGender() != null) &&
885 (this.getFirstName() != null) &&
886 (this.getFamilyName() != null) &&
887 (this.getStreet() != null) &&
888 (this.getHouseNumber() != null) &&
889 (this.getZipCode() != null) &&
890 (this.getCity() != null));
894 public boolean isRequiredPersonalDataSet () {
895 return ((this.getGender() != null) &&
896 (this.getFirstName() != null) &&
897 (this.getFamilyName() != null) &&
898 (this.getStreet() != null) &&
899 (this.getHouseNumber() != null) &&
900 (this.getZipCode() != null) &&
901 (this.getCity() != null) &&
902 (this.getEmailAddress() != null) &&
903 (this.getEmailAddressRepeat() != null));
907 public boolean isSameEmailAddressEntered () {
908 return (Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat()));
912 public Contact lookupContactById (final Long contactId) throws ContactNotFoundException {
914 Contact localContact = null;
919 // Try to lookup it in visible user list
920 for (final Iterator<Contact> iterator = this.contactList.iterator(); iterator.hasNext();) {
922 Contact next = iterator.next();
924 // Is the user id found?
925 if (Objects.equals(next.getContactId(), contactId)) {
926 // Copy to other variable
933 if (null == localContact) {
934 // Not visible for the current user
935 throw new ContactNotFoundException(contactId);
938 // Copy all data to this bean
939 this.copyContact(localContact);
946 public List<Contact> selectableContacts () {
947 return Collections.unmodifiableList(this.selectableContacts);
951 public void updateContactDataFromController (final Contact contact) {
952 // Is the instance valid?
953 if (null == contact) {
955 throw new NullPointerException("contact is null"); //NOI18N
956 } else if (contact.getContactId() == null) {
958 throw new NullPointerException("contact.contactId is null"); //NOI18N
959 } else if (contact.getContactId() < 1) {
960 // Not valid id number
961 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
965 this.copyContact(contact);
969 * Adds email address to bean's internal list.
971 * @param contact Contact instance
973 private void addUserNameEmailAddress (final Contact contact) {
974 // Make sure the entry is not added yet
975 if (this.emailAddressList.contains(contact.getContactEmailAddress())) {
977 throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", contact.getContactEmailAddress())); //NOI18N
981 this.emailAddressList.add(contact.getContactEmailAddress());
987 private void clear () {
990 this.setGender(null);
992 this.setFirstName(null);
993 this.setFamilyName(null);
994 this.setStreet(null);
995 this.setHouseNumber(null);
996 this.setHouseNumberExtension(null);
997 this.setZipCode(null);
999 this.setCountry(null);
1002 this.setEmailAddress(null);
1003 this.setEmailAddressRepeat(null);
1004 this.setPhoneAreaCode(null);
1005 this.setPhoneCountry(null);
1006 this.setPhoneNumber(null);
1007 this.setCellphoneCarrier(null);
1008 this.setCellphoneNumber(null);
1009 this.setFaxAreaCode(null);
1010 this.setFaxCountry(null);
1011 this.setFaxNumber(null);
1014 this.setBirthday(null);
1015 this.setComment(null);
1019 * Copies given contact into the controller
1021 * @param contact Contact instance
1023 private void copyContact (final Contact contact) {
1024 // Is the instance valid?
1025 if (null == contact) {
1027 throw new NullPointerException("contact is null"); //NOI18N
1028 } else if (contact.getContactId() == null) {
1030 throw new NullPointerException("contact.contactId is null"); //NOI18N
1031 } else if (contact.getContactId() < 1) {
1032 // Not valid id number
1033 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1038 this.setGender(contact.getContactGender());
1039 this.setTitle(contact.getContactTitle());
1040 this.setFirstName(contact.getContactFirstName());
1041 this.setFamilyName(contact.getContactFamilyName());
1042 this.setStreet(contact.getContactStreet());
1043 this.setHouseNumber(contact.getContactHouseNumber());
1044 this.setHouseNumberExtension(contact.getContactHouseNumberExtension());
1045 this.setZipCode(contact.getContactZipCode());
1046 this.setCity(contact.getContactCity());
1047 this.setCountry(contact.getContactCountry());
1048 this.setEmailAddress(contact.getContactEmailAddress());
1049 this.setBirthday(contact.getContactBirthday());
1050 this.setComment(contact.getContactComment());
1052 // Get cellphone, phone and fax instance
1053 DialableCellphoneNumber cellphone = contact.getContactCellphoneNumber();
1054 DialableFaxNumber fax = contact.getContactFaxNumber();
1055 DialableLandLineNumber phone = contact.getContactLandLineNumber();
1058 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneAreaCode() > 0)) {
1059 this.setPhoneCountry(phone.getPhoneCountry());
1060 this.setPhoneAreaCode(phone.getPhoneAreaCode());
1061 this.setPhoneNumber(phone.getPhoneNumber());
1064 if ((cellphone instanceof DialableCellphoneNumber) && (cellphone.getCellphoneProvider() instanceof MobileProvider)) {
1065 this.setCellphoneCarrier(cellphone.getCellphoneProvider());
1066 this.setCellphoneNumber(cellphone.getPhoneNumber());
1069 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneAreaCode() > 0)) {
1070 this.setFaxCountry(fax.getPhoneCountry());
1071 this.setFaxAreaCode(fax.getPhoneAreaCode());
1072 this.setFaxNumber(fax.getPhoneNumber());
1077 * Removes given contact from all lists
1079 * @param contact Contact instance to remove
1081 private void removeContact (final Contact contact) {
1082 // Is the instance valid?
1083 if (null == contact) {
1085 throw new NullPointerException("contact is null"); //NOI18N
1086 } else if (contact.getContactId() == null) {
1088 throw new NullPointerException("contact.contactId is null"); //NOI18N
1089 } else if (contact.getContactId() < 1) {
1090 // Not valid id number
1091 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1094 // Remove from general list
1095 if (!this.contactList.remove(contact)) {
1096 // Did not remove contact
1097 throw new IllegalStateException(MessageFormat.format("contact {0} was not removed.", contact.getContactId())); //NOI18N
1100 // Remove from other lists
1101 this.emailAddressList.remove(contact.getContactEmailAddress());
1105 * Adds unique instance to contact list. First any existing instance is
1106 * being removed, then the new instance is added.
1108 * @param contact Contact instance to add uniquely
1110 private void uniqueAddContact (final Contact contact) {
1111 // Is the instance valid?
1112 if (null == contact) {
1114 throw new NullPointerException("contact is null"); //NOI18N
1115 } else if (contact.getContactId() == null) {
1117 throw new NullPointerException("contact.contactId is null"); //NOI18N
1118 } else if (contact.getContactId() < 1) {
1119 // Not valid id number
1120 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1123 // Get iterator from list
1124 Iterator<Contact> iterator = this.contactList.iterator();
1126 // "Walk" through all entries
1127 while (iterator.hasNext()) {
1129 Contact next = iterator.next();
1131 // Is id number the same?
1132 if (Objects.equals(contact.getContactId(), next.getContactId())) {
1133 // Found entry, so remove it and abort
1134 this.removeContact(next);
1139 // Add contact to list
1140 this.contactList.add(contact);