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.jjobs.beans.contact;
19 import java.text.MessageFormat;
20 import java.util.Collections;
21 import java.util.Date;
22 import java.util.Iterator;
23 import java.util.LinkedList;
24 import java.util.List;
25 import java.util.Objects;
26 import javax.annotation.PostConstruct;
27 import javax.enterprise.context.SessionScoped;
28 import javax.enterprise.event.Observes;
29 import javax.faces.view.facelets.FaceletException;
30 import javax.inject.Inject;
31 import javax.inject.Named;
32 import javax.naming.Context;
33 import javax.naming.InitialContext;
34 import javax.naming.NamingException;
35 import org.mxchange.jcontacts.contact.Contact;
36 import org.mxchange.jcontacts.contact.ContactSessionBeanRemote;
37 import org.mxchange.jcontacts.contact.UserContact;
38 import org.mxchange.jcontacts.contact.gender.Gender;
39 import org.mxchange.jcontacts.contact.utils.ContactUtils;
40 import org.mxchange.jcontacts.events.contact.add.AdminAddedContactEvent;
41 import org.mxchange.jcontacts.events.contact.update.AdminUpdatedContactEvent;
42 import org.mxchange.jcontacts.exceptions.ContactNotFoundException;
43 import org.mxchange.jcountry.data.Country;
44 import org.mxchange.jjobs.beans.BaseJobsController;
45 import org.mxchange.jjobs.beans.login.JobsUserLoginWebSessionController;
46 import org.mxchange.jjobs.beans.user.JobsUserWebSessionController;
47 import org.mxchange.jphone.phonenumbers.cellphone.CellphoneNumber;
48 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
49 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
50 import org.mxchange.jphone.phonenumbers.fax.FaxNumber;
51 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
52 import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
53 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
54 import org.mxchange.jusercore.events.confirmation.UserConfirmedAccountEvent;
55 import org.mxchange.jusercore.events.login.UserLoggedInEvent;
56 import org.mxchange.jusercore.events.registration.UserRegisteredEvent;
57 import org.mxchange.jusercore.events.user.add.AdminAddedUserEvent;
58 import org.mxchange.jusercore.events.user.linked.AdminLinkedUserEvent;
59 import org.mxchange.jusercore.exceptions.UserPasswordMismatchException;
60 import org.mxchange.jusercore.model.user.User;
63 * A general contact controller (bean)
65 * @author Roland Haeder<roland@mxchange.org>
67 @Named ("contactController")
69 public class JobsContactWebSessionBean extends BaseJobsController implements JobsContactWebSessionController {
74 private static final long serialVersionUID = 542_145_347_916L;
79 private Date birthday;
82 * Cellphone number's carrier
84 private MobileProvider cellphoneCarrier;
89 private Long cellphoneNumber;
99 private String comment;
102 * Remote contact bean
104 private final ContactSessionBeanRemote contactBean;
109 private final List<Contact> contactList;
114 private Country country;
119 private String emailAddress;
124 private final List<String> emailAddressList;
127 * Email address repeated
129 private String emailAddressRepeat;
134 private String familyName;
137 * Fax number's area code
139 private Integer faxAreaCode;
142 * Country instance for fax number
144 private Country faxCountry;
149 private Long faxNumber;
154 private String firstName;
159 private Gender gender;
164 private Short houseNumber;
167 * House number extension
169 private String houseNumberExtension;
172 * Whether a cellphone entry has been unlinked
174 private boolean isCellphoneUnlinked;
177 * Whether a fax entry has been unlinked
179 private boolean isFaxUnlinked;
182 * Whether a land-line number has been unlinked
184 private boolean isLandLineUnlinked;
187 * Phone number area code
189 private Integer phoneAreaCode;
192 * Country instance for phone number
194 private Country phoneCountry;
199 private Long phoneNumber;
202 * A list of all selectable contacts
204 private List<Contact> selectableContacts;
209 private String street;
214 private String title;
217 * Regular user controller
220 private JobsUserWebSessionController userController;
223 * Login controller (bean)
226 private JobsUserLoginWebSessionController userLoginController;
231 private Integer zipCode;
234 * Default constructor
236 public JobsContactWebSessionBean () {
239 // Get initial context
240 Context context = new InitialContext();
243 this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/jratecalc-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
244 } catch (final NamingException e) {
246 throw new FaceletException(e);
250 this.contactList = new LinkedList<>();
251 this.emailAddressList = new LinkedList<>();
255 public void afterAdminAddedContact (@Observes final AdminAddedContactEvent event) {
256 // The event must be valid
259 throw new NullPointerException("event is null"); //NOI18N
260 } else if (event.getAddedContact() == null) {
262 throw new NullPointerException("event.addedContact is null"); //NOI18N
263 } else if (event.getAddedContact().getContactId() == null) {
265 throw new NullPointerException("event.addedContact.contactId is null"); //NOI18N
266 } else if (event.getAddedContact().getContactId() < 1) {
268 throw new IllegalArgumentException(MessageFormat.format("event.addedContact.contactId={0} is not valid", event.getAddedContact().getContactId())); //NOI18N //NOI18N
275 this.uniqueAddContact(event.getAddedContact());
277 // Add to selectable contacts
278 this.selectableContacts.add(event.getAddedContact());
282 public void afterAdminAddedUserEvent (@Observes final AdminAddedUserEvent 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 public void afterAdminLinkedUser (@Observes final AdminLinkedUserEvent event) {
304 // event should not be null
307 throw new NullPointerException("event is null"); //NOI18N
308 } else if (event.getLinkedUser() == null) {
310 throw new NullPointerException("event.linkedUser is null"); //NOI18N
311 } else if (event.getLinkedUser().getUserContact() == null) {
313 throw new NullPointerException("event.linkedUser.userContact is null"); //NOI18N
314 } else if (event.getLinkedUser().getUserContact().getContactId() == null) {
316 throw new NullPointerException("event.linkedUser.userContact.contactId is null"); //NOI18N
317 } else if (event.getLinkedUser().getUserContact().getContactId() < 1) {
319 throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getLinkedUser(), event.getLinkedUser().getUserContact().getContactId())); //NOI18N
322 // Remove contact from list available contacts list
323 this.selectableContacts.remove(event.getLinkedUser().getUserContact());
330 public void afterAdminUpdatedContactDataEvent (@Observes final AdminUpdatedContactEvent event) {
331 // event should not be null
334 throw new NullPointerException("event is null"); //NOI18N
335 } else if (event.getUpdatedContact() == null) {
337 throw new NullPointerException("event.updatedContact is null"); //NOI18N
338 } else if (event.getUpdatedContact().getContactId() == null) {
340 throw new NullPointerException("event.updatedContact.contactId is null"); //NOI18N
341 } else if (event.getUpdatedContact().getContactId() < 1) {
343 throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
346 // Add contact instance only once
347 this.uniqueAddContact(event.getUpdatedContact());
349 // Add email address to list
350 this.emailAddressList.add(event.getUpdatedContact().getContactEmailAddress());
354 public void afterRegistrationEvent (@Observes final UserRegisteredEvent event) {
355 // event should not be null
358 throw new NullPointerException("event is null"); //NOI18N
359 } else if (event.getRegisteredUser() == null) {
361 throw new NullPointerException("event.registeredUser is null"); //NOI18N
362 } else if (event.getRegisteredUser().getUserId() == null) {
364 throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
365 } else if (event.getRegisteredUser().getUserId() < 1) {
367 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
371 Contact registeredContact = event.getRegisteredUser().getUserContact();
373 // Copy all data from registered->user
374 this.copyContact(registeredContact);
376 // Add contact instance only once
377 this.uniqueAddContact(registeredContact);
379 // Add user name and email address
380 this.addUserNameEmailAddress(registeredContact);
387 public void afterUserConfirmedAccount (@Observes final UserConfirmedAccountEvent event) {
388 // event should not be null
391 throw new NullPointerException("event is null"); //NOI18N
392 } else if (event.getConfirmedUser() == null) {
394 throw new NullPointerException("event.confirmedUser is null"); //NOI18N
395 } else if (event.getConfirmedUser().getUserId() == null) {
397 throw new NullPointerException("event.confirmedUser.userId is null"); //NOI18N
398 } else if (event.getConfirmedUser().getUserId() < 1) {
400 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getConfirmedUser(), event.getConfirmedUser().getUserId())); //NOI18N
403 // Add contact instance only once
404 this.uniqueAddContact(event.getConfirmedUser().getUserContact());
408 public void afterUserLogin (@Observes final UserLoggedInEvent event) {
409 // event should not be null
412 throw new NullPointerException("event is null"); //NOI18N
413 } else if (event.getLoggedInUser() == null) {
415 throw new NullPointerException("event.loggedInUser is null"); //NOI18N
416 } else if (event.getLoggedInUser().getUserId() == null) {
418 throw new NullPointerException("event.loggedInUser.userId is null"); //NOI18N
419 } else if (event.getLoggedInUser().getUserId() < 1) {
421 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
424 // Copy all data to this bean
425 this.copyContact(event.getLoggedInUser().getUserContact());
429 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
430 public List<Contact> allContacts () {
432 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ContactController.allContacts: contactList.size()={0} - EXIT!", this.contactList.size()));
434 // Return un-modified list
435 return this.contactList;
439 public Contact createContactInstance () {
441 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createContactInstance: CALLED!", this.getClass().getSimpleName()));
443 // Is all required data set?
444 if (!this.isRequiredPersonalDataSet()) {
445 // No, then abort here
446 throw new FaceletException(new IllegalArgumentException("Not all personal data is set, but createContactInstance() is called.")); //NOI18N
449 // Generate phone number
450 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
451 DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
452 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
454 // Create new contact
455 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
456 contact.setContactStreet(this.getStreet());
457 contact.setContactHouseNumber(this.getHouseNumber());
458 contact.setContactHouseNumberExtension(this.getHouseNumberExtension());
459 contact.setContactZipCode(this.getZipCode());
460 contact.setContactCity(this.getCity());
461 contact.setContactCountry(this.getCountry());
462 contact.setContactEmailAddress(this.getEmailAddress());
463 contact.setContactBirthday(this.getBirthday());
464 contact.setContactComment(this.getComment());
467 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createContactInstance: this.emailAddress={1}", this.getClass().getSimpleName(), this.getEmailAddress()));
468 // Don't set null or wrong references
469 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getPhoneAreaCode() != null) && (this.getPhoneNumber() != null) && (this.getPhoneAreaCode() > 0) && (this.getPhoneNumber() > 0)) {
470 // Now the number must be given
471 if (phone.getPhoneAreaCode() == null) {
473 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
474 } else if (phone.getPhoneAreaCode() < 1) {
476 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
477 } else if (phone.getPhoneNumber() == null) {
479 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
480 } else if (phone.getPhoneNumber() < 1) {
482 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
486 contact.setContactLandLineNumber(phone);
489 // Don't set null or wrong references
490 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
491 // Now the number must be given
492 if (fax.getPhoneAreaCode() == null) {
494 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
495 } else if (fax.getPhoneAreaCode() < 1) {
497 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
498 } else if (fax.getPhoneNumber() == null) {
500 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
501 } else if (fax.getPhoneNumber() < 1) {
503 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
507 contact.setContactFaxNumber(fax);
510 // Is the provider set?
511 if ((cellphone instanceof DialableCellphoneNumber) && (this.getCellphoneCarrier() instanceof MobileProvider) && (this.getCellphoneNumber() != null) && (this.getCellphoneNumber() > 0)) {
512 // Is the number set?
513 if (cellphone.getPhoneNumber() == null) {
515 throw new NullPointerException("cellphone.phoneNumber is null"); //NOI18N
516 } else if (cellphone.getPhoneNumber() < 1) {
518 throw new IllegalArgumentException("cellphone.phoneNumber is zero or below."); //NOI18N
521 // Set cellphone number
522 contact.setContactCellphoneNumber(cellphone);
526 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createContactInstance: contact={1} - EXIT!", this.getClass().getSimpleName(), contact));
532 public String doChangePersonalContactData () {
533 // This method shall only be called if the user is logged-in
534 if (!this.userLoginController.isUserLoggedIn()) {
536 throw new IllegalStateException("User is not logged-in"); //NOI18N
537 } else if (!this.isRequiredChangePersonalDataSet()) {
538 // Not all required fields are set
539 throw new FaceletException("Not all required fields are set."); //NOI18N
540 } else if (!this.userLoginController.ifCurrentPasswordMatches()) {
541 // Password not matching
542 this.showFacesMessage("form_login_change_personal:currentPassword", new UserPasswordMismatchException(this.userLoginController.getLoggedInUser())); //NOI18N
546 // Get contact instance
547 Contact contact = this.userLoginController.getLoggedInUser().getUserContact();
549 // It should be there, so run some tests on it
550 assert (contact instanceof Contact) : "Instance userLoginController.loggedInUser.userContact is null"; //NOI18N
551 assert (contact.getContactId() instanceof Long) : "Instance userLoginController.userContact.contactId is null"; //NOI18N
552 assert (contact.getContactId() > 0) : MessageFormat.format("Instance userLoginController.userContact.contactId={0} is invalid", contact.getContactId()); //NOI18N
555 contact.setContactGender(this.getGender());
556 contact.setContactFirstName(this.getFirstName());
557 contact.setContactFamilyName(this.getFamilyName());
558 contact.setContactStreet(this.getStreet());
559 contact.setContactHouseNumber(this.getHouseNumber());
560 contact.setContactHouseNumberExtension(this.getHouseNumberExtension());
561 contact.setContactZipCode(this.getZipCode());
562 contact.setContactCity(this.getCity());
563 contact.setContactCountry(this.getCountry());
565 // Update contact's cellphone number
566 this.isCellphoneUnlinked = ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
568 // Update contact's land-line number
569 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
571 // Update contact's fax number
572 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
574 // Send it to the EJB
575 this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
578 return "contact_data_saved"; //NOI18N
582 @SuppressWarnings ("ReturnOfDateField")
583 public Date getBirthday () {
584 return this.birthday;
588 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
589 public void setBirthday (final Date birthday) {
590 this.birthday = birthday;
594 public MobileProvider getCellphoneCarrier () {
595 return this.cellphoneCarrier;
599 public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
600 this.cellphoneCarrier = cellphoneCarrier;
604 public Long getCellphoneNumber () {
605 return this.cellphoneNumber;
609 public void setCellphoneNumber (Long cellphoneNumber) {
610 this.cellphoneNumber = cellphoneNumber;
614 public String getCity () {
619 public void setCity (final String city) {
624 public String getComment () {
629 public void setComment (final String comment) {
630 this.comment = comment;
634 public String getControllerType () {
635 return "general"; //NOI18N
640 public void setControllerType (final String controllerType) {
641 throw new UnsupportedOperationException("Setting controller type is not supported."); //NOI18N
645 public Country getCountry () {
650 public void setCountry (final Country country) {
651 this.country = country;
655 public String getEmailAddress () {
656 return this.emailAddress;
660 public void setEmailAddress (final String emailAddress) {
661 this.emailAddress = emailAddress;
665 public String getEmailAddressRepeat () {
666 return this.emailAddressRepeat;
670 public void setEmailAddressRepeat (final String emailAddressRepeat) {
671 this.emailAddressRepeat = emailAddressRepeat;
675 public String getFamilyName () {
676 return this.familyName;
680 public void setFamilyName (final String familyName) {
681 this.familyName = familyName;
685 public Integer getFaxAreaCode () {
686 return this.faxAreaCode;
690 public void setFaxAreaCode (final Integer faxAreaCode) {
691 this.faxAreaCode = faxAreaCode;
695 public Country getFaxCountry () {
696 return this.faxCountry;
700 public void setFaxCountry (final Country faxCountry) {
701 this.faxCountry = faxCountry;
705 public Long getFaxNumber () {
706 return this.faxNumber;
710 public void setFaxNumber (final Long faxNumber) {
711 this.faxNumber = faxNumber;
715 public String getFirstName () {
716 return this.firstName;
720 public void setFirstName (final String firstName) {
721 this.firstName = firstName;
725 public Gender getGender () {
730 public void setGender (final Gender gender) {
731 this.gender = gender;
735 public Short getHouseNumber () {
736 return this.houseNumber;
740 public void setHouseNumber (final Short houseNumber) {
741 this.houseNumber = houseNumber;
745 public String getHouseNumberExtension () {
746 return this.houseNumberExtension;
750 public void setHouseNumberExtension (final String houseNumberExtension) {
751 this.houseNumberExtension = houseNumberExtension;
755 public Integer getPhoneAreaCode () {
756 return this.phoneAreaCode;
760 public void setPhoneAreaCode (final Integer phoneAreaCode) {
761 this.phoneAreaCode = phoneAreaCode;
765 public Country getPhoneCountry () {
766 return this.phoneCountry;
770 public void setPhoneCountry (final Country phoneCountry) {
771 this.phoneCountry = phoneCountry;
775 public Long getPhoneNumber () {
776 return this.phoneNumber;
780 public void setPhoneNumber (final Long phoneNumber) {
781 this.phoneNumber = phoneNumber;
785 public String getStreet () {
790 public void setStreet (final String street) {
791 this.street = street;
795 public String getTitle () {
800 public void setTitle (final String title) {
805 public Integer getZipCode () {
810 public void setZipCode (final Integer zipCode) {
811 this.zipCode = zipCode;
815 * Post-initialization of this class
818 public void init () {
819 // Get full email address list for reducing EJB calls
820 this.emailAddressList.addAll(this.contactBean.getEmailAddressList());
822 // Get full contact list
823 this.contactList.addAll(this.contactBean.getAllContacts());
826 List<User> allUsers = this.userController.allUsers();
829 List<Contact> allContacts = this.contactBean.getAllContacts();
832 Iterator<Contact> iterator = allContacts.iterator();
835 while (iterator.hasNext()) {
837 Contact next = iterator.next();
840 Iterator<User> userIterator = allUsers.iterator();
842 // Loop through all users
843 while (userIterator.hasNext()) {
845 User nextUser = userIterator.next();
848 if (Objects.equals(next, nextUser.getUserContact())) {
857 this.selectableContacts = allContacts;
861 public boolean isEmailAddressRegistered (final Contact contact) {
863 if (null == contact) {
865 throw new NullPointerException("contact is null"); //NOI18N
866 } else if (contact.getContactEmailAddress() == null) {
868 throw new NullPointerException("contact.contactEmailAddress is null"); //NOI18N
869 } else if (contact.getContactEmailAddress().isEmpty()) {
871 throw new IllegalArgumentException("contact.contactEmailAddress is empty."); //NOI18N
875 return ((this.emailAddressList instanceof List) && (this.emailAddressList.contains(contact.getContactEmailAddress())));
879 public boolean isRequiredChangePersonalDataSet () {
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));
890 public boolean isRequiredPersonalDataSet () {
891 return ((this.getGender() != null) &&
892 (this.getFirstName() != null) &&
893 (this.getFamilyName() != null) &&
894 (this.getStreet() != null) &&
895 (this.getHouseNumber() != null) &&
896 (this.getZipCode() != null) &&
897 (this.getCity() != null) &&
898 (this.getEmailAddress() != null) &&
899 (this.getEmailAddressRepeat() != null));
903 public boolean isSameEmailAddressEntered () {
904 return (Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat()));
908 public Contact lookupContactById (final Long contactId) throws ContactNotFoundException {
910 Contact localContact = null;
915 // Try to lookup it in visible user list
916 for (final Iterator<Contact> iterator = this.contactList.iterator(); iterator.hasNext();) {
918 Contact next = iterator.next();
920 // Is the user id found?
921 if (Objects.equals(next.getContactId(), contactId)) {
922 // Copy to other variable
929 if (null == localContact) {
930 // Not visible for the current user
931 throw new ContactNotFoundException(contactId);
934 // Copy all data to this bean
935 this.copyContact(localContact);
942 public List<Contact> selectableContacts () {
943 return Collections.unmodifiableList(this.selectableContacts);
947 public void updateContactDataFromController (final Contact contact) {
948 // Is the instance valid?
949 if (null == contact) {
951 throw new NullPointerException("contact is null"); //NOI18N
952 } else if (contact.getContactId() == null) {
954 throw new NullPointerException("contact.contactId is null"); //NOI18N
955 } else if (contact.getContactId() < 1) {
956 // Not valid id number
957 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
961 this.copyContact(contact);
965 * Adds email address to bean's internal list.
967 * @param contact Contact instance
969 private void addUserNameEmailAddress (final Contact contact) {
970 // Make sure the entry is not added yet
971 if (this.emailAddressList.contains(contact.getContactEmailAddress())) {
973 throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", contact.getContactEmailAddress())); //NOI18N
977 this.emailAddressList.add(contact.getContactEmailAddress());
983 private void clear () {
986 this.setGender(null);
988 this.setFirstName(null);
989 this.setFamilyName(null);
990 this.setStreet(null);
991 this.setHouseNumber(null);
992 this.setHouseNumberExtension(null);
993 this.setZipCode(null);
995 this.setCountry(null);
998 this.setEmailAddress(null);
999 this.setEmailAddressRepeat(null);
1000 this.setPhoneAreaCode(null);
1001 this.setPhoneCountry(null);
1002 this.setPhoneNumber(null);
1003 this.setCellphoneCarrier(null);
1004 this.setCellphoneNumber(null);
1005 this.setFaxAreaCode(null);
1006 this.setFaxCountry(null);
1007 this.setFaxNumber(null);
1010 this.setBirthday(null);
1011 this.setComment(null);
1015 * Copies given contact into the controller
1017 * @param contact Contact instance
1019 private void copyContact (final Contact contact) {
1020 // Is the instance valid?
1021 if (null == contact) {
1023 throw new NullPointerException("contact is null"); //NOI18N
1024 } else if (contact.getContactId() == null) {
1026 throw new NullPointerException("contact.contactId is null"); //NOI18N
1027 } else if (contact.getContactId() < 1) {
1028 // Not valid id number
1029 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1034 this.setGender(contact.getContactGender());
1035 this.setTitle(contact.getContactTitle());
1036 this.setFirstName(contact.getContactFirstName());
1037 this.setFamilyName(contact.getContactFamilyName());
1038 this.setStreet(contact.getContactStreet());
1039 this.setHouseNumber(contact.getContactHouseNumber());
1040 this.setHouseNumberExtension(contact.getContactHouseNumberExtension());
1041 this.setZipCode(contact.getContactZipCode());
1042 this.setCity(contact.getContactCity());
1043 this.setCountry(contact.getContactCountry());
1044 this.setEmailAddress(contact.getContactEmailAddress());
1045 this.setBirthday(contact.getContactBirthday());
1046 this.setComment(contact.getContactComment());
1048 // Get cellphone, phone and fax instance
1049 DialableCellphoneNumber cellphone = contact.getContactCellphoneNumber();
1050 DialableFaxNumber fax = contact.getContactFaxNumber();
1051 DialableLandLineNumber phone = contact.getContactLandLineNumber();
1054 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneAreaCode() > 0)) {
1055 this.setPhoneCountry(phone.getPhoneCountry());
1056 this.setPhoneAreaCode(phone.getPhoneAreaCode());
1057 this.setPhoneNumber(phone.getPhoneNumber());
1060 if ((cellphone instanceof DialableCellphoneNumber) && (cellphone.getCellphoneProvider() instanceof MobileProvider)) {
1061 this.setCellphoneCarrier(cellphone.getCellphoneProvider());
1062 this.setCellphoneNumber(cellphone.getPhoneNumber());
1065 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneAreaCode() > 0)) {
1066 this.setFaxCountry(fax.getPhoneCountry());
1067 this.setFaxAreaCode(fax.getPhoneAreaCode());
1068 this.setFaxNumber(fax.getPhoneNumber());
1073 * Removes given contact from all lists
1075 * @param contact Contact instance to remove
1077 private void removeContact (final Contact contact) {
1078 // Is the instance valid?
1079 if (null == contact) {
1081 throw new NullPointerException("contact is null"); //NOI18N
1082 } else if (contact.getContactId() == null) {
1084 throw new NullPointerException("contact.contactId is null"); //NOI18N
1085 } else if (contact.getContactId() < 1) {
1086 // Not valid id number
1087 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1090 // Remove from general list
1091 if (!this.contactList.remove(contact)) {
1092 // Did not remove contact
1093 throw new IllegalStateException(MessageFormat.format("contact {0} was not removed.", contact.getContactId())); //NOI18N
1096 // Remove from other lists
1097 this.emailAddressList.remove(contact.getContactEmailAddress());
1101 * Adds unique instance to contact list. First any existing instance is
1102 * being removed, then the new instance is added.
1104 * @param contact Contact instance to add uniquely
1106 private void uniqueAddContact (final Contact contact) {
1107 // Is the instance valid?
1108 if (null == contact) {
1110 throw new NullPointerException("contact is null"); //NOI18N
1111 } else if (contact.getContactId() == null) {
1113 throw new NullPointerException("contact.contactId is null"); //NOI18N
1114 } else if (contact.getContactId() < 1) {
1115 // Not valid id number
1116 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1119 // Get iterator from list
1120 Iterator<Contact> iterator = this.contactList.iterator();
1122 // "Walk" through all entries
1123 while (iterator.hasNext()) {
1125 Contact next = iterator.next();
1127 // Is id number the same?
1128 if (Objects.equals(contact.getContactId(), next.getContactId())) {
1129 // Found entry, so remove it and abort
1130 this.removeContact(next);
1135 // Add contact to list
1136 this.contactList.add(contact);