2 * Copyright (C) 2016, 2017 Roland Häder
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License as
6 * published by the Free Software Foundation, either version 3 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Affero General Public License for more details.
14 * You should have received a copy of the GNU Affero General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package org.mxchange.pizzaapplication.beans.contact;
19 import java.text.MessageFormat;
20 import java.util.Collections;
21 import java.util.Date;
22 import java.util.Iterator;
23 import java.util.LinkedList;
24 import java.util.List;
25 import java.util.Objects;
26 import javax.annotation.PostConstruct;
27 import javax.enterprise.context.SessionScoped;
28 import javax.enterprise.event.Observes;
29 import javax.faces.view.facelets.FaceletException;
30 import javax.inject.Inject;
31 import javax.inject.Named;
32 import javax.naming.Context;
33 import javax.naming.InitialContext;
34 import javax.naming.NamingException;
35 import org.mxchange.jcontacts.contact.Contact;
36 import org.mxchange.jcontacts.contact.ContactSessionBeanRemote;
37 import org.mxchange.jcontacts.contact.ContactUtils;
38 import org.mxchange.jcontacts.contact.UserContact;
39 import org.mxchange.jcontacts.contact.title.PersonalTitle;
40 import org.mxchange.jcontacts.events.contact.add.ObservableAdminAddedContactEvent;
41 import org.mxchange.jcontacts.events.contact.update.ObservableAdminUpdatedContactEvent;
42 import org.mxchange.jcontacts.exceptions.ContactNotFoundException;
43 import org.mxchange.jcountry.data.Country;
44 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
45 import org.mxchange.jphone.phonenumbers.fax.FaxNumber;
46 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
47 import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
48 import org.mxchange.jphone.phonenumbers.mobile.DialableMobileNumber;
49 import org.mxchange.jphone.phonenumbers.mobile.MobileNumber;
50 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
51 import org.mxchange.jusercore.events.confirmation.ObservableUserConfirmedAccountEvent;
52 import org.mxchange.jusercore.events.login.ObservableUserLoggedInEvent;
53 import org.mxchange.jusercore.events.registration.ObservableUserRegisteredEvent;
54 import org.mxchange.jusercore.events.user.add.ObservableAdminAddedUserEvent;
55 import org.mxchange.jusercore.events.user.linked.ObservableAdminLinkedUserEvent;
56 import org.mxchange.jusercore.exceptions.UserPasswordMismatchException;
57 import org.mxchange.jusercore.model.user.User;
58 import org.mxchange.pizzaapplication.beans.BasePizzaController;
59 import org.mxchange.pizzaapplication.beans.login.user.PizzaUserLoginWebSessionController;
60 import org.mxchange.pizzaapplication.beans.user.PizzaUserWebSessionController;
63 * A general contact bean (controller)
65 * @author Roland Häder<roland@mxchange.org>
67 @Named ("contactController")
69 public class PizzaContactWebSessionBean extends BasePizzaController implements PizzaContactWebSessionController {
74 private static final long serialVersionUID = 542_145_347_916L;
77 * Academic academicTitle
79 private String academicTitle;
84 private Date birthday;
94 private String comment;
99 private ContactSessionBeanRemote contactBean;
104 private final List<Contact> contactList;
109 private Country country;
114 private String emailAddress;
119 private final List<String> emailAddressList;
122 * Email address repeated
124 private String emailAddressRepeat;
129 private String familyName;
132 * Fax number's area code
134 private Integer faxAreaCode;
137 * Country instance for fax number
139 private Country faxCountry;
144 private Long faxNumber;
149 private String firstName;
154 private Short houseNumber;
157 * House number extension
159 private String houseNumberExtension;
162 * Whether a fax entry has been unlinked
164 private boolean isFaxUnlinked;
167 * Whether a land-line number has been unlinked
169 private boolean isLandLineUnlinked;
172 * Whether a mobile entry has been unlinked
174 private boolean isMobileUnlinked;
177 * Phone number area code
179 private Integer landLineAreaCode;
182 * Country instance for phone number
184 private Country landLineCountry;
189 private Long landLineNumber;
194 private Long mobileNumber;
197 * Mobile number's carrier
199 private MobileProvider mobileProvider;
202 * Personal academicTitle
204 private PersonalTitle personalTitle;
207 * A list of all selectable contacts
209 private List<Contact> selectableContacts;
214 private String street;
217 * Regular user controller
220 private PizzaUserWebSessionController userController;
223 * Login bean (controller)
226 private PizzaUserLoginWebSessionController userLoginController;
231 private Integer zipCode;
234 * Default constructor
236 public PizzaContactWebSessionBean () {
237 // Call super constructor
241 this.contactList = new LinkedList<>();
242 this.emailAddressList = new LinkedList<>();
246 * Observes events being fired when an administrator has added a new
249 * @param event Event being fired
251 public void afterAdminAddedContactEvent (@Observes final ObservableAdminAddedContactEvent event) {
252 // The event must be valid
255 throw new NullPointerException("event is null"); //NOI18N
256 } else if (event.getAddedContact() == null) {
258 throw new NullPointerException("event.addedContact is null"); //NOI18N
259 } else if (event.getAddedContact().getContactId() == null) {
261 throw new NullPointerException("event.addedContact.contactId is null"); //NOI18N
262 } else if (event.getAddedContact().getContactId() < 1) {
264 throw new IllegalArgumentException(MessageFormat.format("event.addedContact.contactId={0} is not valid", event.getAddedContact().getContactId())); //NOI18N //NOI18N
271 this.uniqueAddContact(event.getAddedContact());
273 // Add to selectable contacts
274 this.selectableContacts.add(event.getAddedContact());
278 * Event observer for newly added users by administrator
280 * @param event Event being fired
282 public void afterAdminAddedUserEvent (@Observes final ObservableAdminAddedUserEvent event) {
283 // event should not be null
286 throw new NullPointerException("event is null"); //NOI18N
287 } else if (event.getAddedUser() == null) {
289 throw new NullPointerException("event.addedUser is null"); //NOI18N
290 } else if (event.getAddedUser().getUserId() == null) {
292 throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
293 } else if (event.getAddedUser().getUserId() < 1) {
295 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
303 * Observes events being fired when an administrator has linked a new user
304 * with existing contact data.
306 * @param event Event being fired
308 public void afterAdminLinkedUserEvent (@Observes final ObservableAdminLinkedUserEvent event) {
309 // event should not be null
312 throw new NullPointerException("event is null"); //NOI18N
313 } else if (event.getLinkedUser() == null) {
315 throw new NullPointerException("event.linkedUser is null"); //NOI18N
316 } else if (event.getLinkedUser().getUserContact() == null) {
318 throw new NullPointerException("event.linkedUser.userContact is null"); //NOI18N
319 } else if (event.getLinkedUser().getUserContact().getContactId() == null) {
321 throw new NullPointerException("event.linkedUser.userContact.contactId is null"); //NOI18N
322 } else if (event.getLinkedUser().getUserContact().getContactId() < 1) {
324 throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getLinkedUser(), event.getLinkedUser().getUserContact().getContactId())); //NOI18N
327 // Remove contact from list available contacts list
328 this.selectableContacts.remove(event.getLinkedUser().getUserContact());
335 * Event observer for updated contact data by administrators
337 * @param event Updated contact data event
339 public void afterAdminUpdatedContactDataEvent (@Observes final ObservableAdminUpdatedContactEvent event) {
340 // event should not be null
343 throw new NullPointerException("event is null"); //NOI18N
344 } else if (event.getUpdatedContact() == null) {
346 throw new NullPointerException("event.updatedContact is null"); //NOI18N
347 } else if (event.getUpdatedContact().getContactId() == null) {
349 throw new NullPointerException("event.updatedContact.contactId is null"); //NOI18N
350 } else if (event.getUpdatedContact().getContactId() < 1) {
352 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
355 // Add contact instance only once
356 this.uniqueAddContact(event.getUpdatedContact());
358 // Add email address to list
359 this.emailAddressList.add(event.getUpdatedContact().getContactEmailAddress());
363 * Event observer when user confirmed account.
365 * @param event Event being fired
367 public void afterUserConfirmedAccount (@Observes final ObservableUserConfirmedAccountEvent event) {
368 // event should not be null
371 throw new NullPointerException("event is null"); //NOI18N
372 } else if (event.getConfirmedUser() == null) {
374 throw new NullPointerException("event.confirmedUser is null"); //NOI18N
375 } else if (event.getConfirmedUser().getUserId() == null) {
377 throw new NullPointerException("event.confirmedUser.userId is null"); //NOI18N
378 } else if (event.getConfirmedUser().getUserId() < 1) {
380 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getConfirmedUser(), event.getConfirmedUser().getUserId())); //NOI18N
383 // Add contact instance only once
384 this.uniqueAddContact(event.getConfirmedUser().getUserContact());
388 * Event observer for logged-in user
390 * @param event Event instance
392 public void afterUserLoginEvent (@Observes final ObservableUserLoggedInEvent event) {
393 // event should not be null
396 throw new NullPointerException("event is null"); //NOI18N
397 } else if (event.getLoggedInUser() == null) {
399 throw new NullPointerException("event.loggedInUser is null"); //NOI18N
400 } else if (event.getLoggedInUser().getUserId() == null) {
402 throw new NullPointerException("event.loggedInUser.userId is null"); //NOI18N
403 } else if (event.getLoggedInUser().getUserId() < 1) {
405 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
408 // Copy all data to this bean
409 this.copyContact(event.getLoggedInUser().getUserContact());
413 * Event observer for new user registrations
415 * @param event User registration event
417 public void afterUserRegistrationEvent (@Observes final ObservableUserRegisteredEvent event) {
418 // event should not be null
421 throw new NullPointerException("event is null"); //NOI18N
422 } else if (event.getRegisteredUser() == null) {
424 throw new NullPointerException("event.registeredUser is null"); //NOI18N
425 } else if (event.getRegisteredUser().getUserId() == null) {
427 throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
428 } else if (event.getRegisteredUser().getUserId() < 1) {
430 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
434 Contact registeredContact = event.getRegisteredUser().getUserContact();
436 // Copy all data from registered->user
437 this.copyContact(registeredContact);
439 // Add contact instance only once
440 this.uniqueAddContact(registeredContact);
442 // Add user name and email address
443 this.addUserNameEmailAddress(registeredContact);
450 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
451 public List<Contact> allContacts () {
452 // Return un-modified list
453 return this.contactList;
457 public Contact createContactInstance () {
458 // Is all required data set?
459 if (!this.isRequiredPersonalDataSet()) {
460 // No, then abort here
461 throw new FaceletException(new IllegalArgumentException("Not all personal data is set, but createContactInstance() is called.")); //NOI18N
464 // Required personal data must be set
465 assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N
467 // Generate phone number
468 DialableLandLineNumber phone = new LandLineNumber(this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber());
469 DialableMobileNumber mobile = new MobileNumber(this.getMobileProvider(), this.getMobileNumber());
470 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
472 // Create new contact
473 Contact contact = new UserContact(this.getPersonalTitle(), this.getFirstName(), this.getFamilyName());
474 contact.setContactStreet(this.getStreet());
475 contact.setContactHouseNumber(this.getHouseNumber());
476 contact.setContactHouseNumberExtension(this.getHouseNumberExtension());
477 contact.setContactZipCode(this.getZipCode());
478 contact.setContactCity(this.getCity());
479 contact.setContactCountry(this.getCountry());
480 contact.setContactEmailAddress(this.getEmailAddress());
481 contact.setContactBirthday(this.getBirthday());
482 contact.setContactComment(this.getComment());
484 // Don't set null or wrong references
485 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getLandLineAreaCode() != null) && (this.getLandLineNumber() != null) && (this.getLandLineAreaCode() > 0) && (this.getLandLineNumber() > 0)) {
486 // Now the number must be given
487 if (phone.getPhoneAreaCode() == null) {
489 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
490 } else if (phone.getPhoneAreaCode() < 1) {
492 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
493 } else if (phone.getPhoneNumber() == null) {
495 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
496 } else if (phone.getPhoneNumber() < 1) {
498 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
502 contact.setContactLandLineNumber(phone);
505 // Don't set null or wrong references
506 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
507 // Now the number must be given
508 if (fax.getPhoneAreaCode() == null) {
510 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
511 } else if (fax.getPhoneAreaCode() < 1) {
513 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
514 } else if (fax.getPhoneNumber() == null) {
516 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
517 } else if (fax.getPhoneNumber() < 1) {
519 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
523 contact.setContactFaxNumber(fax);
526 // Is the provider set?
527 if ((mobile instanceof DialableMobileNumber) && (this.getMobileProvider() instanceof MobileProvider) && (this.getMobileNumber() != null) && (this.getMobileNumber() > 0)) {
528 // Is the number set?
529 if (mobile.getPhoneNumber() == null) {
531 throw new NullPointerException("mobile.phoneNumber is null"); //NOI18N
532 } else if (mobile.getPhoneNumber() < 1) {
534 throw new IllegalArgumentException("mobile.phoneNumber is zero or below."); //NOI18N
538 contact.setContactMobileNumber(mobile);
546 public String doChangePersonalContactData () {
547 // This method shall only be called if the user is logged-in
548 if (!this.userLoginController.isUserLoggedIn()) {
550 throw new IllegalStateException("User is not logged-in"); //NOI18N
551 } else if (!this.isRequiredChangePersonalDataSet()) {
552 // Not all required fields are set
553 throw new FaceletException("Not all required fields are set."); //NOI18N
554 } else if (!this.userLoginController.ifCurrentPasswordMatches()) {
555 // Password not matching
556 this.showFacesMessage("form_login_change_personal:currentPassword", new UserPasswordMismatchException(this.userLoginController.getLoggedInUser())); //NOI18N
560 // Get contact instance
561 Contact contact = this.userLoginController.getLoggedInUser().getUserContact();
563 // It should be there, so run some tests on it
564 assert (contact instanceof Contact) : "Instance userLoginController.loggedInUser.userContact is null"; //NOI18N
565 assert (contact.getContactId() instanceof Long) : "Instance userLoginController.userContact.contactId is null"; //NOI18N
566 assert (contact.getContactId() > 0) : MessageFormat.format("Instance userLoginController.userContact.contactId={0} is invalid", contact.getContactId()); //NOI18N
569 contact.setContactPersonalTitle(this.getPersonalTitle());
570 contact.setContactFirstName(this.getFirstName());
571 contact.setContactFamilyName(this.getFamilyName());
572 contact.setContactStreet(this.getStreet());
573 contact.setContactHouseNumber(this.getHouseNumber());
574 contact.setContactHouseNumberExtension(this.getHouseNumberExtension());
575 contact.setContactZipCode(this.getZipCode());
576 contact.setContactCity(this.getCity());
577 contact.setContactCountry(this.getCountry());
579 // Update contact's mobile number
580 this.isMobileUnlinked = ContactUtils.updateMobileNumber(contact, this.getMobileProvider(), this.getMobileNumber());
582 // Update contact's land-line number
583 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber());
585 // Update contact's fax number
586 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
588 // Send it to the EJB
589 this.contactBean.updateContactData(contact, this.isMobileUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
592 return "contact_data_saved"; //NOI18N
596 public String getAcademicTitle () {
597 return this.academicTitle;
601 public void setAcademicTitle (final String academicTitle) {
602 this.academicTitle = academicTitle;
606 @SuppressWarnings ("ReturnOfDateField")
607 public Date getBirthday () {
608 return this.birthday;
612 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
613 public void setBirthday (final Date birthday) {
614 this.birthday = birthday;
618 public String getCity () {
623 public void setCity (final String city) {
628 public String getComment () {
633 public void setComment (final String comment) {
634 this.comment = comment;
638 public String getControllerType () {
639 return "general"; //NOI18N
644 public void setControllerType (final String controllerType) {
645 throw new UnsupportedOperationException("Setting controller type is not supported."); //NOI18N
649 public Country getCountry () {
654 public void setCountry (final Country country) {
655 this.country = country;
659 public String getEmailAddress () {
660 return this.emailAddress;
664 public void setEmailAddress (final String emailAddress) {
665 this.emailAddress = emailAddress;
669 public String getEmailAddressRepeat () {
670 return this.emailAddressRepeat;
674 public void setEmailAddressRepeat (final String emailAddressRepeat) {
675 this.emailAddressRepeat = emailAddressRepeat;
679 public String getFamilyName () {
680 return this.familyName;
684 public void setFamilyName (final String familyName) {
685 this.familyName = familyName;
689 public Integer getFaxAreaCode () {
690 return this.faxAreaCode;
694 public void setFaxAreaCode (final Integer faxAreaCode) {
695 this.faxAreaCode = faxAreaCode;
699 public Country getFaxCountry () {
700 return this.faxCountry;
704 public void setFaxCountry (final Country faxCountry) {
705 this.faxCountry = faxCountry;
709 public Long getFaxNumber () {
710 return this.faxNumber;
714 public void setFaxNumber (final Long faxNumber) {
715 this.faxNumber = faxNumber;
719 public String getFirstName () {
720 return this.firstName;
724 public void setFirstName (final String firstName) {
725 this.firstName = firstName;
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 getLandLineAreaCode () {
750 return this.landLineAreaCode;
754 public void setLandLineAreaCode (final Integer landLineAreaCode) {
755 this.landLineAreaCode = landLineAreaCode;
759 public Country getLandLineCountry () {
760 return this.landLineCountry;
764 public void setLandLineCountry (final Country landLineCountry) {
765 this.landLineCountry = landLineCountry;
769 public Long getLandLineNumber () {
770 return this.landLineNumber;
774 public void setLandLineNumber (final Long landLineNumber) {
775 this.landLineNumber = landLineNumber;
779 public Long getMobileNumber () {
780 return this.mobileNumber;
784 public void setMobileNumber (final Long mobileNumber) {
785 this.mobileNumber = mobileNumber;
789 public MobileProvider getMobileProvider () {
790 return this.mobileProvider;
794 public void setMobileProvider (final MobileProvider mobileProvider) {
795 this.mobileProvider = mobileProvider;
799 public PersonalTitle getPersonalTitle () {
800 return this.personalTitle;
804 public void setPersonalTitle (final PersonalTitle personalTitle) {
805 this.personalTitle = personalTitle;
809 public String getStreet () {
814 public void setStreet (final String street) {
815 this.street = street;
819 public Integer getZipCode () {
824 public void setZipCode (final Integer zipCode) {
825 this.zipCode = zipCode;
829 * Post-construction method
832 public void init () {
835 // Get initial context
836 Context context = new InitialContext();
839 this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/pizzaservice-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
840 } catch (final NamingException e) {
842 throw new FaceletException(e);
845 // Get full email address list for reducing EJB calls
846 this.emailAddressList.addAll(this.contactBean.getEmailAddressList());
849 List<Contact> allContacts = this.contactBean.getAllContacts();
851 // Get full contact list
852 this.contactList.addAll(allContacts);
855 List<User> allUsers = this.userController.allUsers();
858 Iterator<Contact> iterator = allContacts.iterator();
861 while (iterator.hasNext()) {
863 Contact next = iterator.next();
866 Iterator<User> userIterator = allUsers.iterator();
868 // Loop through all users
869 while (userIterator.hasNext()) {
871 User nextUser = userIterator.next();
874 if (Objects.equals(next, nextUser.getUserContact())) {
883 this.selectableContacts = allContacts;
887 public boolean isEmailAddressRegistered (final Contact contact) {
889 if (null == contact) {
891 throw new NullPointerException("contact is null"); //NOI18N
892 } else if (contact.getContactEmailAddress() == null) {
894 throw new NullPointerException("contact.contactEmailAddress is null"); //NOI18N
895 } else if (contact.getContactEmailAddress().isEmpty()) {
897 throw new IllegalArgumentException("contact.contactEmailAddress is empty."); //NOI18N
901 return ((this.emailAddressList instanceof List) && (this.emailAddressList.contains(contact.getContactEmailAddress())));
905 public boolean isRequiredChangePersonalDataSet () {
906 return ((this.getPersonalTitle() != null) &&
907 (this.getFirstName() != null) &&
908 (this.getFamilyName() != null) &&
909 (this.getStreet() != null) &&
910 (this.getHouseNumber() != null) &&
911 (this.getZipCode() != null) &&
912 (this.getCity() != null));
916 public boolean isRequiredPersonalDataSet () {
917 return ((this.getPersonalTitle() != null) &&
918 (this.getFirstName() != null) &&
919 (this.getFamilyName() != null) &&
920 (this.getStreet() != null) &&
921 (this.getHouseNumber() != null) &&
922 (this.getZipCode() != null) &&
923 (this.getCity() != null) &&
924 (this.getEmailAddress() != null) &&
925 (this.getEmailAddressRepeat() != null));
929 public boolean isSameEmailAddressEntered () {
930 return (Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat()));
934 public Contact lookupContactById (final Long contactId) throws ContactNotFoundException {
936 Contact localContact = null;
941 // Try to lookup it in visible user list
942 for (final Iterator<Contact> iterator = this.contactList.iterator(); iterator.hasNext();) {
944 Contact next = iterator.next();
946 // Is the user id found?
947 if (Objects.equals(next.getContactId(), contactId)) {
948 // Copy to other variable
955 if (null == localContact) {
956 // Not visible for the current user
957 throw new ContactNotFoundException(contactId);
960 // Copy all data to this bean
961 this.copyContact(localContact);
968 public List<Contact> selectableContacts () {
969 return Collections.unmodifiableList(this.selectableContacts);
973 public void updateContactDataFromController (final Contact contact) {
974 // Is the instance valid?
975 if (null == contact) {
977 throw new NullPointerException("contact is null"); //NOI18N
978 } else if (contact.getContactId() == null) {
980 throw new NullPointerException("contact.contactId is null"); //NOI18N
981 } else if (contact.getContactId() < 1) {
982 // Not valid id number
983 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
987 this.copyContact(contact);
991 * Adds email address to bean's internal list.
993 * @param contact Contact instance
995 private void addUserNameEmailAddress (final Contact contact) {
996 // Make sure the entry is not added yet
997 if (this.emailAddressList.contains(contact.getContactEmailAddress())) {
999 throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", contact.getContactEmailAddress())); //NOI18N
1003 this.emailAddressList.add(contact.getContactEmailAddress());
1009 private void clear () {
1012 this.setPersonalTitle(null);
1013 this.setAcademicTitle(null);
1014 this.setFirstName(null);
1015 this.setFamilyName(null);
1016 this.setStreet(null);
1017 this.setHouseNumber(null);
1018 this.setHouseNumberExtension(null);
1019 this.setZipCode(null);
1021 this.setCountry(null);
1024 this.setEmailAddress(null);
1025 this.setEmailAddressRepeat(null);
1026 this.setLandLineAreaCode(null);
1027 this.setLandLineCountry(null);
1028 this.setLandLineNumber(null);
1029 this.setMobileProvider(null);
1030 this.setMobileNumber(null);
1031 this.setFaxAreaCode(null);
1032 this.setFaxCountry(null);
1033 this.setFaxNumber(null);
1036 this.setBirthday(null);
1037 this.setComment(null);
1041 * Copies given contact into the controller
1043 * @param contact Contact instance
1045 private void copyContact (final Contact contact) {
1046 // Is the instance valid?
1047 if (null == contact) {
1049 throw new NullPointerException("contact is null"); //NOI18N
1050 } else if (contact.getContactId() == null) {
1052 throw new NullPointerException("contact.contactId is null"); //NOI18N
1053 } else if (contact.getContactId() < 1) {
1054 // Not valid id number
1055 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1060 this.setPersonalTitle(contact.getContactPersonalTitle());
1061 this.setAcademicTitle(contact.getContactTitle());
1062 this.setFirstName(contact.getContactFirstName());
1063 this.setFamilyName(contact.getContactFamilyName());
1064 this.setStreet(contact.getContactStreet());
1065 this.setHouseNumber(contact.getContactHouseNumber());
1066 this.setHouseNumberExtension(contact.getContactHouseNumberExtension());
1067 this.setZipCode(contact.getContactZipCode());
1068 this.setCity(contact.getContactCity());
1069 this.setCountry(contact.getContactCountry());
1070 this.setEmailAddress(contact.getContactEmailAddress());
1071 this.setBirthday(contact.getContactBirthday());
1072 this.setComment(contact.getContactComment());
1074 // Get mobile, phone and fax instance
1075 DialableMobileNumber mobile = contact.getContactMobileNumber();
1076 DialableFaxNumber fax = contact.getContactFaxNumber();
1077 DialableLandLineNumber phone = contact.getContactLandLineNumber();
1080 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneAreaCode() > 0)) {
1081 this.setLandLineCountry(phone.getPhoneCountry());
1082 this.setLandLineAreaCode(phone.getPhoneAreaCode());
1083 this.setLandLineNumber(phone.getPhoneNumber());
1086 if ((mobile instanceof DialableMobileNumber) && (mobile.getMobileProvider() instanceof MobileProvider)) {
1087 this.setMobileProvider(mobile.getMobileProvider());
1088 this.setMobileNumber(mobile.getPhoneNumber());
1091 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneAreaCode() > 0)) {
1092 this.setFaxCountry(fax.getPhoneCountry());
1093 this.setFaxAreaCode(fax.getPhoneAreaCode());
1094 this.setFaxNumber(fax.getPhoneNumber());
1099 * Removes given contact from all lists
1101 * @param contact Contact instance to remove
1103 private void removeContact (final Contact contact) {
1104 // Is the instance valid?
1105 if (null == contact) {
1107 throw new NullPointerException("contact is null"); //NOI18N
1108 } else if (contact.getContactId() == null) {
1110 throw new NullPointerException("contact.contactId is null"); //NOI18N
1111 } else if (contact.getContactId() < 1) {
1112 // Not valid id number
1113 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1116 // Remove from general list
1117 if (!this.contactList.remove(contact)) {
1118 // Did not remove contact
1119 throw new IllegalStateException(MessageFormat.format("contact {0} was not removed.", contact.getContactId())); //NOI18N
1122 // Remove from other lists
1123 this.emailAddressList.remove(contact.getContactEmailAddress());
1127 * Adds unique instance to contact list. First any existing instance is
1128 * being removed, then the new instance is added.
1130 * @param contact Contact instance to add uniquely
1132 private void uniqueAddContact (final Contact contact) {
1133 // Is the instance valid?
1134 if (null == contact) {
1136 throw new NullPointerException("contact is null"); //NOI18N
1137 } else if (contact.getContactId() == null) {
1139 throw new NullPointerException("contact.contactId is null"); //NOI18N
1140 } else if (contact.getContactId() < 1) {
1141 // Not valid id number
1142 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1145 // Get iterator from list
1146 Iterator<Contact> iterator = this.contactList.iterator();
1148 // "Walk" through all entries
1149 while (iterator.hasNext()) {
1151 Contact next = iterator.next();
1153 // Is id number the same?
1154 if (Objects.equals(contact.getContactId(), next.getContactId())) {
1155 // Found entry, so remove it and abort
1156 this.removeContact(next);
1161 // Add contact to list
1162 this.contactList.add(contact);