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.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.jjobs.beans.BaseJobsController;
44 import org.mxchange.jjobs.beans.login.JobsUserLoginWebSessionController;
45 import org.mxchange.jjobs.beans.user.JobsUserWebSessionController;
46 import org.mxchange.jphone.phonenumbers.cellphone.CellphoneNumber;
47 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
48 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
49 import org.mxchange.jphone.phonenumbers.fax.FaxNumber;
50 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
51 import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
52 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
53 import org.mxchange.jusercore.events.confirmation.UserConfirmedAccountEvent;
54 import org.mxchange.jusercore.events.login.UserLoggedInEvent;
55 import org.mxchange.jusercore.events.registration.UserRegisteredEvent;
56 import org.mxchange.jusercore.events.user.add.AdminAddedUserEvent;
57 import org.mxchange.jusercore.events.user.linked.AdminLinkedUserEvent;
58 import org.mxchange.jusercore.exceptions.UserPasswordMismatchException;
59 import org.mxchange.jusercore.model.user.User;
62 * A general contact bean (controller)
64 * @author Roland Haeder<roland@mxchange.org>
66 @Named ("contactController")
68 public class JobsContactWebSessionBean extends BaseJobsController implements JobsContactWebSessionController {
73 private static final long serialVersionUID = 542_145_347_916L;
78 private Date birthday;
81 * Cellphone number's carrier
83 private MobileProvider cellphoneCarrier;
88 private Long cellphoneNumber;
98 private String comment;
101 * Remote contact bean
103 private final ContactSessionBeanRemote contactBean;
108 private List<Contact> contactList;
113 private Country country;
118 private String emailAddress;
123 private List<String> emailAddressList;
126 * Email address repeated
128 private String emailAddressRepeat;
133 private String familyName;
136 * Fax number's area code
138 private Integer faxAreaCode;
141 * Country instance for fax number
143 private Country faxCountry;
148 private Long faxNumber;
153 private String firstName;
158 private Gender gender;
163 private Short houseNumber;
166 * House number extension
168 private String houseNumberExtension;
171 * Whether a cellphone entry has been unlinked
173 private boolean isCellphoneUnlinked;
176 * Whether a fax entry has been unlinked
178 private boolean isFaxUnlinked;
181 * Whether a land-line number has been unlinked
183 private boolean isLandLineUnlinked;
186 * Phone number area code
188 private Integer phoneAreaCode;
191 * Country instance for phone number
193 private Country phoneCountry;
198 private Long phoneNumber;
201 * A list of all selectable contacts
203 private List<Contact> selectableContacts;
208 private String street;
211 * Regular user controller
214 private JobsUserWebSessionController userController;
217 * Login bean (controller)
220 private JobsUserLoginWebSessionController userLoginController;
225 private Integer zipCode;
228 * Default constructor
230 public JobsContactWebSessionBean () {
233 // Get initial context
234 Context context = new InitialContext();
237 this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/jratecalc-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
238 } catch (final NamingException e) {
240 throw new FaceletException(e);
245 public void afterAdminAddedContact (@Observes final AdminAddedContactEvent event) {
246 // The event must be valid
249 throw new NullPointerException("event is null"); //NOI18N
250 } else if (event.getAddedContact() == null) {
252 throw new NullPointerException("event.addedContact is null"); //NOI18N
253 } else if (event.getAddedContact().getContactId() == null) {
255 throw new NullPointerException("event.addedContact.contactId is null"); //NOI18N
256 } else if (event.getAddedContact().getContactId() < 1) {
258 throw new IllegalArgumentException(MessageFormat.format("event.addedContact.contactId={0} is not valid", event.getAddedContact().getContactId())); //NOI18N //NOI18N
265 this.uniqueAddContact(event.getAddedContact());
267 // Add to selectable contacts
268 this.selectableContacts.add(event.getAddedContact());
272 public void afterAdminAddedUserEvent (@Observes final AdminAddedUserEvent event) {
273 // event should not be null
276 throw new NullPointerException("event is null"); //NOI18N
277 } else if (event.getAddedUser() == null) {
279 throw new NullPointerException("event.addedUser is null"); //NOI18N
280 } else if (event.getAddedUser().getUserId() == null) {
282 throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
283 } else if (event.getAddedUser().getUserId() < 1) {
285 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
293 public void afterAdminLinkedUser (@Observes final AdminLinkedUserEvent event) {
294 // event should not be null
297 throw new NullPointerException("event is null"); //NOI18N
298 } else if (event.getLinkedUser() == null) {
300 throw new NullPointerException("event.linkedUser is null"); //NOI18N
301 } else if (event.getLinkedUser().getUserContact() == null) {
303 throw new NullPointerException("event.linkedUser.userContact is null"); //NOI18N
304 } else if (event.getLinkedUser().getUserContact().getContactId() == null) {
306 throw new NullPointerException("event.linkedUser.userContact.contactId is null"); //NOI18N
307 } else if (event.getLinkedUser().getUserContact().getContactId() < 1) {
309 throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getLinkedUser(), event.getLinkedUser().getUserContact().getContactId())); //NOI18N
312 // Remove contact from list available contacts list
313 this.selectableContacts.remove(event.getLinkedUser().getUserContact());
320 public void afterAdminUpdatedContactDataEvent (@Observes final AdminUpdatedContactEvent event) {
321 // event should not be null
324 throw new NullPointerException("event is null"); //NOI18N
325 } else if (event.getUpdatedContact() == null) {
327 throw new NullPointerException("event.updatedContact is null"); //NOI18N
328 } else if (event.getUpdatedContact().getContactId() == null) {
330 throw new NullPointerException("event.updatedContact.contactId is null"); //NOI18N
331 } else if (event.getUpdatedContact().getContactId() < 1) {
333 throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
336 // Add contact instance only once
337 this.uniqueAddContact(event.getUpdatedContact());
339 // Add email address to list
340 this.emailAddressList.add(event.getUpdatedContact().getContactEmailAddress());
344 public void afterRegistrationEvent (@Observes final UserRegisteredEvent event) {
345 // event should not be null
348 throw new NullPointerException("event is null"); //NOI18N
349 } else if (event.getRegisteredUser() == null) {
351 throw new NullPointerException("event.registeredUser is null"); //NOI18N
352 } else if (event.getRegisteredUser().getUserId() == null) {
354 throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
355 } else if (event.getRegisteredUser().getUserId() < 1) {
357 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
361 Contact registeredContact = event.getRegisteredUser().getUserContact();
363 // Copy all data from registered->user
364 this.copyContact(registeredContact);
366 // Add contact instance only once
367 this.uniqueAddContact(registeredContact);
369 // Add user name and email address
370 this.addUserNameEmailAddress(registeredContact);
377 public void afterUserConfirmedAccount (@Observes final UserConfirmedAccountEvent event) {
378 // event should not be null
381 throw new NullPointerException("event is null"); //NOI18N
382 } else if (event.getConfirmedUser() == null) {
384 throw new NullPointerException("event.confirmedUser is null"); //NOI18N
385 } else if (event.getConfirmedUser().getUserId() == null) {
387 throw new NullPointerException("event.confirmedUser.userId is null"); //NOI18N
388 } else if (event.getConfirmedUser().getUserId() < 1) {
390 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getConfirmedUser(), event.getConfirmedUser().getUserId())); //NOI18N
393 // Add contact instance only once
394 this.uniqueAddContact(event.getConfirmedUser().getUserContact());
398 public void afterUserLogin (@Observes final UserLoggedInEvent event) {
399 // event should not be null
402 throw new NullPointerException("event is null"); //NOI18N
403 } else if (event.getLoggedInUser() == null) {
405 throw new NullPointerException("event.loggedInUser is null"); //NOI18N
406 } else if (event.getLoggedInUser().getUserId() == null) {
408 throw new NullPointerException("event.loggedInUser.userId is null"); //NOI18N
409 } else if (event.getLoggedInUser().getUserId() < 1) {
411 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
414 // Copy all data to this bean
415 this.copyContact(event.getLoggedInUser().getUserContact());
419 public List<Contact> allContacts () {
421 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ContactController.allContacts: contactList.size()={0} - EXIT!", this.contactList.size()));
423 // Return un-modified list
424 return Collections.unmodifiableList(this.contactList);
428 public Contact createContactInstance () {
430 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createContactInstance: CALLED!", this.getClass().getSimpleName()));
432 // Is all required data set?
433 if (!this.isRequiredPersonalDataSet()) {
434 // No, then abort here
435 throw new FaceletException(new IllegalArgumentException("Not all personal data is set, but createContactInstance() is called.")); //NOI18N
438 // Generate phone number
439 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
440 DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
441 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
443 // Create new contact
444 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
445 contact.setContactStreet(this.getStreet());
446 contact.setContactHouseNumber(this.getHouseNumber());
447 contact.setContactHouseNumberExtension(this.getHouseNumberExtension());
448 contact.setContactZipCode(this.getZipCode());
449 contact.setContactCity(this.getCity());
450 contact.setContactCountry(this.getCountry());
451 contact.setContactEmailAddress(this.getEmailAddress());
452 contact.setContactBirthday(this.getBirthday());
453 contact.setContactComment(this.getComment());
456 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createContactInstance: this.emailAddress={1}", this.getClass().getSimpleName(), this.getEmailAddress()));
458 // Don't set null or wrong references
459 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getPhoneAreaCode() != null) && (this.getPhoneNumber() != null) && (this.getPhoneAreaCode() > 0) && (this.getPhoneNumber() > 0)) {
460 // Now the number must be given
461 if (phone.getPhoneAreaCode() == null) {
463 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
464 } else if (phone.getPhoneAreaCode() < 1) {
466 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
467 } else if (phone.getPhoneNumber() == null) {
469 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
470 } else if (phone.getPhoneNumber() < 1) {
472 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
476 contact.setContactLandLineNumber(phone);
479 // Don't set null or wrong references
480 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
481 // Now the number must be given
482 if (fax.getPhoneAreaCode() == null) {
484 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
485 } else if (fax.getPhoneAreaCode() < 1) {
487 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
488 } else if (fax.getPhoneNumber() == null) {
490 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
491 } else if (fax.getPhoneNumber() < 1) {
493 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
497 contact.setContactFaxNumber(fax);
500 // Is the provider set?
501 if ((cellphone instanceof DialableCellphoneNumber) && (this.getCellphoneCarrier() instanceof MobileProvider) && (this.getCellphoneNumber() != null) && (this.getCellphoneNumber() > 0)) {
502 // Is the number set?
503 if (cellphone.getPhoneNumber() == null) {
505 throw new NullPointerException("cellphone.phoneNumber is null"); //NOI18N
506 } else if (cellphone.getPhoneNumber() < 1) {
508 throw new IllegalArgumentException("cellphone.phoneNumber is zero or below."); //NOI18N
511 // Set cellphone number
512 contact.setContactCellphoneNumber(cellphone);
516 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createContactInstance: contact={1} - EXIT!", this.getClass().getSimpleName(), contact));
523 public String doChangePersonalContactData () {
524 // This method shall only be called if the user is logged-in
525 if (!this.userLoginController.isUserLoggedIn()) {
527 throw new IllegalStateException("User is not logged-in"); //NOI18N
528 } else if (!this.isRequiredChangePersonalDataSet()) {
529 // Not all required fields are set
530 throw new FaceletException("Not all required fields are set."); //NOI18N
531 } else if (!this.userLoginController.ifCurrentPasswordMatches()) {
532 // Password not matching
533 this.showFacesMessage("form_login_change_personal:currentPassword", new UserPasswordMismatchException(this.userLoginController.getLoggedInUser())); //NOI18N
537 // Get contact instance
538 Contact contact = this.userLoginController.getLoggedInUser().getUserContact();
540 // It should be there, so run some tests on it
541 assert (contact instanceof Contact) : "Instance userLoginController.loggedInUser.userContact is null"; //NOI18N
542 assert (contact.getContactId() instanceof Long) : "Instance userLoginController.userContact.contactId is null"; //NOI18N
543 assert (contact.getContactId() > 0) : MessageFormat.format("Instance userLoginController.userContact.contactId={0} is invalid", contact.getContactId()); //NOI18N
546 contact.setContactGender(this.getGender());
547 contact.setContactFirstName(this.getFirstName());
548 contact.setContactFamilyName(this.getFamilyName());
549 contact.setContactStreet(this.getStreet());
550 contact.setContactHouseNumber(this.getHouseNumber());
551 contact.setContactHouseNumberExtension(this.getHouseNumberExtension());
552 contact.setContactZipCode(this.getZipCode());
553 contact.setContactCity(this.getCity());
554 contact.setContactCountry(this.getCountry());
556 // Update contact's cellphone number
557 this.isCellphoneUnlinked = ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
559 // Update contact's land-line number
560 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
562 // Update contact's fax number
563 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
565 // Send it to the EJB
566 this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
569 return "contact_data_saved"; //NOI18N
573 @SuppressWarnings ("ReturnOfDateField")
574 public Date getBirthday () {
575 return this.birthday;
579 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
580 public void setBirthday (final Date birthday) {
581 this.birthday = birthday;
585 public MobileProvider getCellphoneCarrier () {
586 return this.cellphoneCarrier;
590 public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
591 this.cellphoneCarrier = cellphoneCarrier;
595 public Long getCellphoneNumber () {
596 return this.cellphoneNumber;
600 public void setCellphoneNumber (Long cellphoneNumber) {
601 this.cellphoneNumber = cellphoneNumber;
605 public String getCity () {
610 public void setCity (final String city) {
615 public String getComment () {
620 public void setComment (final String comment) {
621 this.comment = comment;
625 public String getControllerType () {
626 return "general"; //NOI18N
631 public void setControllerType (final String controllerType) {
632 throw new UnsupportedOperationException("Setting controller type is not supported."); //NOI18N
636 public Country getCountry () {
641 public void setCountry (final Country country) {
642 this.country = country;
646 public String getEmailAddress () {
647 return this.emailAddress;
651 public void setEmailAddress (final String emailAddress) {
652 this.emailAddress = emailAddress;
656 public String getEmailAddressRepeat () {
657 return this.emailAddressRepeat;
661 public void setEmailAddressRepeat (final String emailAddressRepeat) {
662 this.emailAddressRepeat = emailAddressRepeat;
666 public String getFamilyName () {
667 return this.familyName;
671 public void setFamilyName (final String familyName) {
672 this.familyName = familyName;
676 public Integer getFaxAreaCode () {
677 return this.faxAreaCode;
681 public void setFaxAreaCode (final Integer faxAreaCode) {
682 this.faxAreaCode = faxAreaCode;
686 public Country getFaxCountry () {
687 return this.faxCountry;
691 public void setFaxCountry (final Country faxCountry) {
692 this.faxCountry = faxCountry;
696 public Long getFaxNumber () {
697 return this.faxNumber;
701 public void setFaxNumber (final Long faxNumber) {
702 this.faxNumber = faxNumber;
706 public String getFirstName () {
707 return this.firstName;
711 public void setFirstName (final String firstName) {
712 this.firstName = firstName;
716 public Gender getGender () {
721 public void setGender (final Gender gender) {
722 this.gender = gender;
726 public Short getHouseNumber () {
727 return this.houseNumber;
731 public void setHouseNumber (final Short houseNumber) {
732 this.houseNumber = houseNumber;
736 public String getHouseNumberExtension () {
737 return this.houseNumberExtension;
741 public void setHouseNumberExtension (final String houseNumberExtension) {
742 this.houseNumberExtension = houseNumberExtension;
746 public Integer getPhoneAreaCode () {
747 return this.phoneAreaCode;
751 public void setPhoneAreaCode (final Integer phoneAreaCode) {
752 this.phoneAreaCode = phoneAreaCode;
756 public Country getPhoneCountry () {
757 return this.phoneCountry;
761 public void setPhoneCountry (final Country phoneCountry) {
762 this.phoneCountry = phoneCountry;
766 public Long getPhoneNumber () {
767 return this.phoneNumber;
771 public void setPhoneNumber (final Long phoneNumber) {
772 this.phoneNumber = phoneNumber;
776 public String getStreet () {
781 public void setStreet (final String street) {
782 this.street = street;
786 public Integer getZipCode () {
791 public void setZipCode (final Integer zipCode) {
792 this.zipCode = zipCode;
796 public boolean hasContacts () {
797 return (!this.contactList.isEmpty());
801 * Post-initialization of this class
804 public void init () {
805 // Get full email address list for reducing EJB calls
806 this.emailAddressList = this.contactBean.getEmailAddressList();
808 // Get full contact list
809 this.contactList = this.contactBean.getAllContacts();
812 List<User> allUsers = this.userController.allUsers();
815 List<Contact> allContacts = this.contactBean.getAllContacts();
818 Iterator<Contact> iterator = allContacts.iterator();
821 while (iterator.hasNext()) {
823 Contact next = iterator.next();
826 Iterator<User> userIterator = allUsers.iterator();
828 // Loop through all users
829 while (userIterator.hasNext()) {
831 User nextUser = userIterator.next();
834 if (Objects.equals(next, nextUser.getUserContact())) {
843 this.selectableContacts = allContacts;
847 public boolean isEmailAddressRegistered (final Contact contact) {
849 if (null == contact) {
851 throw new NullPointerException("contact is null"); //NOI18N
852 } else if (contact.getContactEmailAddress() == null) {
854 throw new NullPointerException("contact.contactEmailAddress is null"); //NOI18N
855 } else if (contact.getContactEmailAddress().isEmpty()) {
857 throw new IllegalArgumentException("contact.contactEmailAddress is empty."); //NOI18N
861 return ((this.emailAddressList instanceof List) && (this.emailAddressList.contains(contact.getContactEmailAddress())));
865 public boolean isRequiredChangePersonalDataSet () {
866 return ((this.getGender() != null) &&
867 (this.getFirstName() != null) &&
868 (this.getFamilyName() != null) &&
869 (this.getStreet() != null) &&
870 (this.getHouseNumber() != null) &&
871 (this.getZipCode() != null) &&
872 (this.getCity() != null));
876 public boolean isRequiredPersonalDataSet () {
877 return ((this.getGender() != null) &&
878 (this.getFirstName() != null) &&
879 (this.getFamilyName() != null) &&
880 (this.getStreet() != null) &&
881 (this.getHouseNumber() != null) &&
882 (this.getZipCode() != null) &&
883 (this.getCity() != null) &&
884 (this.getEmailAddress() != null) &&
885 (this.getEmailAddressRepeat() != null));
889 public boolean isSameEmailAddressEntered () {
890 return (Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat()));
894 public Contact lookupContactById (final Long contactId) throws ContactNotFoundException {
896 Contact localContact = null;
901 // Try to lookup it in visible user list
902 for (final Iterator<Contact> iterator = this.contactList.iterator(); iterator.hasNext();) {
904 Contact next = iterator.next();
906 // Is the user id found?
907 if (Objects.equals(next.getContactId(), contactId)) {
908 // Copy to other variable
915 if (null == localContact) {
916 // Not visible for the current user
917 throw new ContactNotFoundException(contactId);
920 // Copy all data to this bean
921 this.copyContact(localContact);
928 public List<Contact> selectableContacts () {
929 return Collections.unmodifiableList(this.selectableContacts);
933 public void updateContactDataFromController (final Contact contact) {
934 // Is the instance valid?
935 if (null == contact) {
937 throw new NullPointerException("contact is null"); //NOI18N
938 } else if (contact.getContactId() == null) {
940 throw new NullPointerException("contact.contactId is null"); //NOI18N
941 } else if (contact.getContactId() < 1) {
942 // Not valid id number
943 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
947 this.copyContact(contact);
951 * Adds email address to bean's internal list.
953 * @param contact Contact instance
955 private void addUserNameEmailAddress (final Contact contact) {
956 // Make sure the entry is not added yet
957 if (this.emailAddressList.contains(contact.getContactEmailAddress())) {
959 throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", contact.getContactEmailAddress())); //NOI18N
963 this.emailAddressList.add(contact.getContactEmailAddress());
969 private void clear () {
972 this.setGender(null);
973 // TODO: Unfinished this.setTitle(null);
974 this.setFirstName(null);
975 this.setFamilyName(null);
976 this.setStreet(null);
977 this.setHouseNumber(null);
978 this.setHouseNumberExtension(null);
979 this.setZipCode(null);
981 this.setCountry(null);
984 this.setEmailAddress(null);
985 this.setEmailAddressRepeat(null);
986 this.setPhoneAreaCode(null);
987 this.setPhoneCountry(null);
988 this.setPhoneNumber(null);
989 this.setCellphoneCarrier(null);
990 this.setCellphoneNumber(null);
991 this.setFaxAreaCode(null);
992 this.setFaxCountry(null);
993 this.setFaxNumber(null);
996 this.setBirthday(null);
997 this.setComment(null);
1001 * Copies given contact into the controller
1003 * @param contact Contact instance
1005 private void copyContact (final Contact contact) {
1006 // Is the instance valid?
1007 if (null == contact) {
1009 throw new NullPointerException("contact is null"); //NOI18N
1010 } else if (contact.getContactId() == null) {
1012 throw new NullPointerException("contact.contactId is null"); //NOI18N
1013 } else if (contact.getContactId() < 1) {
1014 // Not valid id number
1015 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1020 this.setGender(contact.getContactGender());
1021 this.setFirstName(contact.getContactFirstName());
1022 this.setFamilyName(contact.getContactFamilyName());
1023 this.setStreet(contact.getContactStreet());
1024 this.setHouseNumber(contact.getContactHouseNumber());
1025 this.setHouseNumberExtension(contact.getContactHouseNumberExtension());
1026 this.setZipCode(contact.getContactZipCode());
1027 this.setCity(contact.getContactCity());
1028 this.setCountry(contact.getContactCountry());
1029 this.setEmailAddress(contact.getContactEmailAddress());
1030 this.setBirthday(contact.getContactBirthday());
1031 this.setComment(contact.getContactComment());
1033 // Get cellphone, phone and fax instance
1034 DialableCellphoneNumber cellphone = contact.getContactCellphoneNumber();
1035 DialableFaxNumber fax = contact.getContactFaxNumber();
1036 DialableLandLineNumber phone = contact.getContactLandLineNumber();
1039 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneAreaCode() > 0)) {
1040 this.setPhoneCountry(phone.getPhoneCountry());
1041 this.setPhoneAreaCode(phone.getPhoneAreaCode());
1042 this.setPhoneNumber(phone.getPhoneNumber());
1045 if ((cellphone instanceof DialableCellphoneNumber) && (cellphone.getCellphoneProvider() instanceof MobileProvider)) {
1046 this.setCellphoneCarrier(cellphone.getCellphoneProvider());
1047 this.setCellphoneNumber(cellphone.getPhoneNumber());
1050 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneAreaCode() > 0)) {
1051 this.setFaxCountry(fax.getPhoneCountry());
1052 this.setFaxAreaCode(fax.getPhoneAreaCode());
1053 this.setFaxNumber(fax.getPhoneNumber());
1058 * Removes given contact from all lists
1060 * @param contact Contact instance to remove
1062 private void removeContact (final Contact contact) {
1063 // Is the instance valid?
1064 if (null == contact) {
1066 throw new NullPointerException("contact is null"); //NOI18N
1067 } else if (contact.getContactId() == null) {
1069 throw new NullPointerException("contact.contactId is null"); //NOI18N
1070 } else if (contact.getContactId() < 1) {
1071 // Not valid id number
1072 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1075 // Remove from general list
1076 if (!this.contactList.remove(contact)) {
1077 // Did not remove contact
1078 throw new IllegalStateException(MessageFormat.format("contact {0} was not removed.", contact.getContactId())); //NOI18N
1081 // Remove from other lists
1082 this.emailAddressList.remove(contact.getContactEmailAddress());
1086 * Adds unique instance to contact list. First any existing instance is
1087 * being removed, then the new instance is added.
1089 * @param contact Contact instance to add uniquely
1091 private void uniqueAddContact (final Contact contact) {
1092 // Is the instance valid?
1093 if (null == contact) {
1095 throw new NullPointerException("contact is null"); //NOI18N
1096 } else if (contact.getContactId() == null) {
1098 throw new NullPointerException("contact.contactId is null"); //NOI18N
1099 } else if (contact.getContactId() < 1) {
1100 // Not valid id number
1101 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1104 // Get iterator from list
1105 Iterator<Contact> iterator = this.contactList.iterator();
1107 // "Walk" through all entries
1108 while (iterator.hasNext()) {
1110 Contact next = iterator.next();
1112 // Is id number the same?
1113 if (Objects.equals(contact.getContactId(), next.getContactId())) {
1114 // Found entry, so remove it and abort
1115 this.removeContact(next);
1120 // Add contact to list
1121 this.contactList.add(contact);