package org.mxchange.addressbook.beans.contact;
import java.text.MessageFormat;
+import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.mxchange.jcontacts.contact.ContactSessionBeanRemote;
import org.mxchange.jcontacts.contact.UserContact;
import org.mxchange.jcontacts.contact.gender.Gender;
+import org.mxchange.jcontacts.contact.utils.ContactUtils;
+import org.mxchange.jcontacts.events.contact.add.AdminAddedContactEvent;
+import org.mxchange.jcontacts.events.contact.update.AdminUpdatedContactEvent;
import org.mxchange.jcontacts.exceptions.ContactNotFoundException;
import org.mxchange.jcountry.data.Country;
import org.mxchange.jphone.phonenumbers.cellphone.CellphoneNumber;
import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
import org.mxchange.jusercore.events.login.UserLoggedInEvent;
import org.mxchange.jusercore.events.registration.UserRegisteredEvent;
+import org.mxchange.jusercore.events.user.add.AdminAddedUserEvent;
import org.mxchange.jusercore.exceptions.UserPasswordMismatchException;
/**
*/
private Short houseNumber;
+ /**
+ * Whether a cellphone entry has been unlinked
+ */
+ private boolean isCellphoneUnlinked;
+
+ /**
+ * Whether a fax entry has been unlinked
+ */
+ private boolean isFaxUnlinked;
+
+ /**
+ * Whether a land-line number has been unlinked
+ */
+ private boolean isLandLineUnlinked;
+
/**
* Login bean (controller)
*/
* Default constructor
*/
public AddressbookContactWebSessionBean () {
- // Set gender to UNKNOWN
- this.gender = Gender.UNKNOWN;
-
// Try it
try {
// Get initial context
Context context = new InitialContext();
// Try to lookup
- this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/jratecalc-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
+ this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/addressbook-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
} catch (final NamingException e) {
// Throw again
throw new FaceletException(e);
}
@Override
- public void addEmailAddress (final String contactEmailAddress) {
- // Add it
- this.emailAddressList.add(contactEmailAddress);
+ public void afterAdminAddedContact (@Observes final AdminAddedContactEvent event) {
+ // The event must be valid
+ if (null == event) {
+ // Throw NPE
+ throw new NullPointerException("event is null"); //NOI18N
+ } else if (event.getAddedContact() == null) {
+ // Throw again ...
+ throw new NullPointerException("event.addedContact is null"); //NOI18N
+ } else if (event.getAddedContact().getContactId() == null) {
+ // ... and again
+ throw new NullPointerException("event.addedContact.customerId is null"); //NOI18N
+ } else if (event.getAddedContact().getContactId() < 1) {
+ // Not valid
+ throw new IllegalArgumentException(MessageFormat.format("event.addedContact.customerId={0} is not valid", event.getAddedContact().getContactId())); //NOI18N //NOI18N
+ }
+
+ // Clear this bean
+ this.clear();
+
+ // Call other method
+ this.contactList.add(event.getAddedContact());
+ }
+
+ @Override
+ public void afterAdminAddedUserEvent (@Observes final AdminAddedUserEvent event) {
+ // Trace message
+ //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterAdminAddedUserEvent: event={0} - CALLED!", event)); //NOI18N
+
+ // event should not be null
+ if (null == event) {
+ // Throw NPE
+ throw new NullPointerException("event is null"); //NOI18N
+ } else if (event.getAddedUser() == null) {
+ // Throw NPE again
+ throw new NullPointerException("event.addedUser is null"); //NOI18N
+ } else if (event.getAddedUser().getUserId() == null) {
+ // userId is null
+ throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
+ } else if (event.getAddedUser().getUserId() < 1) {
+ // Not avalid id
+ throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
+ }
+
+ // Clear all data
+ this.clear();
+
+ // Trace message
+ //* NOISY-DEBUG: */ System.out.println("UserWebBean:afterAdminAddedUserEvent: EXIT!"); //NOI18N
+ }
+
+ @Override
+ public void afterAdminUpdatedContactDataEvent (@Observes final AdminUpdatedContactEvent event) {
+ // Trace message
+ //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ContactWebBean:afterAdminUpdatedContactDataEvent: event={0} - CALLED!", event)); //NOI18N
+
+ // event should not be null
+ if (null == event) {
+ // Throw NPE
+ throw new NullPointerException("event is null"); //NOI18N
+ } else if (event.getUpdatedContact() == null) {
+ // Throw NPE again
+ throw new NullPointerException("event.user is null"); //NOI18N
+ } else if (event.getUpdatedContact().getContactId() == null) {
+ // userId is null
+ throw new NullPointerException("event.user.userId is null"); //NOI18N
+ } else if (event.getUpdatedContact().getContactId() < 1) {
+ // Not avalid id
+ throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
+ }
+
+ // Get iterator from list
+ Iterator<Contact> iterator = this.contactList.iterator();
+
+ // "Walk" through all entries
+ while (iterator.hasNext()) {
+ // Get next element
+ Contact next = iterator.next();
+
+ // Is id number the same?
+ if (Objects.equals(event.getUpdatedContact().getContactId(), next.getContactId())) {
+ // Found entry, so remove it and abort
+ this.contactList.remove(next);
+
+ // Remove also email from list
+ this.emailAddressList.remove(next.getContactEmailAddress());
+ break;
+ }
+ }
+
+ // Add contact to list
+ this.contactList.add(event.getUpdatedContact());
+
+ // Add email address to list
+ this.emailAddressList.add(event.getUpdatedContact().getContactEmailAddress());
}
@Override
public void afterRegistrationEvent (final @Observes UserRegisteredEvent event) {
// Trace message
- System.out.println(MessageFormat.format("ContactWebBean:afterRegistration: event={0} - CALLED!", event)); //NOI18N
+ //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ContactWebBean:afterRegistration: event={0} - CALLED!", event)); //NOI18N
// event should not be null
if (null == event) {
Contact registeredContact = event.getRegisteredUser().getUserContact();
// Debug message
- System.out.println(MessageFormat.format("ContactWebBean:afterRegistration: registeredContact={0}", registeredContact)); //NOI18N
+ //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ContactWebBean:afterRegistration: registeredContact={0}", registeredContact)); //NOI18N
// Copy all data from registered->user
this.copyContact(registeredContact);
this.clear();
// Trace message
- System.out.println("ContactWebBean:afterRegistration: EXIT!"); //NOI18N
+ //* NOISY-DEBUG: */ System.out.println("ContactWebBean:afterRegistration: EXIT!"); //NOI18N
}
@Override
public void afterUserLogin (final @Observes UserLoggedInEvent event) {
// Trace message
- System.out.println(MessageFormat.format("ContactWebBean:afterUserLogin: event={0} - CALLED!", event)); //NOI18N
+ //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ContactWebBean:afterUserLogin: event={0} - CALLED!", event)); //NOI18N
// event should not be null
if (null == event) {
this.copyContact(event.getLoggedInUser().getUserContact());
// Trace message
- System.out.println("ContactWebBean:afterUserLogin - EXIT!"); //NOI18N
+ //* NOISY-DEBUG: */ System.out.println("ContactWebBean:afterUserLogin - EXIT!"); //NOI18N
}
- /**
- * Clears this bean
- */
@Override
- public void clear () {
- // Clear all data
- // - personal data
- this.setGender(Gender.UNKNOWN);
- this.setFirstName(null);
- this.setFamilyName(null);
- this.setStreet(null);
- this.setHouseNumber(null);
- this.setZipCode(null);
- this.setCity(null);
- this.setCountry(null);
-
- // - contact data
- this.setEmailAddress(null);
- this.setEmailAddressRepeat(null);
- this.setPhoneAreaCode(null);
- this.setCellphoneCarrier(null);
- this.setFaxAreaCode(null);
+ public List<Contact> allContacts () {
+ // Debug message
+ /* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ContactController.allContacts: contactList.size()={0} - EXIT!", this.contactList.size()));
- // - other data
- this.setBirthday(null);
- this.setComment(null);
+ // Return un-modified list
+ return Collections.unmodifiableList(this.contactList);
}
@Override
public Contact createContactInstance () {
// User message
- //this.getLogger().logTrace("createContactInstance: CALLED!");
+ //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createContactInstance: CALLED!", this.getClass().getSimpleName()));
+
+ // Is all required data set?
+ if (!this.isRequiredPersonalDataSet()) {
+ // No, then abort here
+ throw new FaceletException(new IllegalArgumentException("Not all personal data is set, but createContactInstance() is called.")); //NOI18N
+ }
// Required personal data must be set
assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N
contact.setContactCity(this.getCity());
contact.setContactCountry(this.getCountry());
contact.setContactEmailAddress(this.getEmailAddress());
+ contact.setContactBirthday(this.getBirthday());
+ contact.setContactComment(this.getComment());
+ // Debug message
+ //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createContactInstance: this.emailAddress={1}", this.getClass().getSimpleName(), this.getEmailAddress()));
// Don't set null or wrong references
if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getPhoneAreaCode() != null) && (this.getPhoneNumber() != null) && (this.getPhoneAreaCode() > 0) && (this.getPhoneNumber() > 0)) {
// Now the number must be given
contact.setContactCellphoneNumber(cellphone);
}
- contact.setContactBirthday(this.getBirthday());
- contact.setContactComment(this.getComment());
-
- // Created timestamp and ownContact
- contact.setContactOwnContact(Boolean.TRUE);
-
// Trace message
- //this.getLogger().logTrace(MessageFormat.format("createContactInstance: localContact={0} - EXIT!", localContact));
+ //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createContactInstance: contact={1} - EXIT!", this.getClass().getSimpleName(), contact));
+
// Return it
return localContact;
}
contact.setContactCity(this.getCity());
contact.setContactCountry(this.getCountry());
- // Is there a phone number?
- if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
- // Debug message
- System.out.println(MessageFormat.format("ContactWebBean:doChangePersonalData: phoneId={0}", contact.getContactLandLineNumber().getPhoneId())); //NOI18N
-
- // Yes, then update as well
- contact.getContactLandLineNumber().setPhoneAreaCode(this.getPhoneAreaCode());
- contact.getContactLandLineNumber().setPhoneNumber(this.getPhoneNumber());
- }
-
- // Is there a fax number?
- if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
- // Debug message
- System.out.println(MessageFormat.format("ContactWebBean:doChangePersonalData: faxId={0}", contact.getContactFaxNumber().getPhoneId())); //NOI18N
-
- // Yes, then update as well
- contact.getContactFaxNumber().setPhoneAreaCode(this.getFaxAreaCode());
- contact.getContactFaxNumber().setPhoneNumber(this.getFaxNumber());
- }
+ // Update contact's cellphone number
+ this.isCellphoneUnlinked = ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
- // Is there a cellphone number?
- if (contact.getContactCellphoneNumber() instanceof DialableCellphoneNumber) {
- // Debug message
- System.out.println(MessageFormat.format("ContactWebBean:doChangePersonalData: cellPhoneId={0}", contact.getContactCellphoneNumber().getPhoneId())); //NOI18N
+ // Update contact's land-line number
+ this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
- // Yes, then update as well
- contact.getContactCellphoneNumber().setCellphoneProvider(this.getCellphoneCarrier());
- contact.getContactCellphoneNumber().setPhoneNumber(this.getCellphoneNumber());
- }
+ // Update contact's fax number
+ this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
// Send it to the EJB
- this.contactBean.updateContactPersonalData(contact);
+ this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
// All fine
return "contact_data_saved"; //NOI18N
}
@Override
+ @SuppressWarnings ("ReturnOfDateField")
public Date getBirthday () {
return this.birthday;
}
@Override
+ @SuppressWarnings ("AssignmentToDateFieldFromParameter")
public void setBirthday (final Date birthday) {
this.birthday = birthday;
}
this.zipCode = zipCode;
}
+ @Override
+ public boolean hasContacts () {
+ return (!this.contactList.isEmpty());
+ }
+
/**
* Post-initialization of this class
*/
@Override
public boolean isEmailAddressRegistered (final Contact contact) {
+ // Cherck parameter
+ if (null == contact) {
+ // Throw NPE
+ throw new NullPointerException("contact is null"); //NOI18N
+ } else if (contact.getContactEmailAddress() == null) {
+ // Throw again
+ throw new NullPointerException("contact.contactEmailAddress is null"); //NOI18N
+ } else if (contact.getContactEmailAddress().isEmpty()) {
+ // Is empty
+ throw new IllegalArgumentException("contact.contactEmailAddress is empty."); //NOI18N
+ }
+
+ // Determine it
return ((this.emailAddressList instanceof List) && (this.emailAddressList.contains(contact.getContactEmailAddress())));
}
}
@Override
- public void updateContactDataFromController (final Contact userContact) {
+ public void updateContactDataFromController (final Contact contact) {
// Is the instance valid?
- if (null == userContact) {
+ if (null == contact) {
// Throw NPE
- throw new NullPointerException("userContact is null"); //NOI18N
- } else if (userContact.getContactId() == null) {
+ throw new NullPointerException("contact is null"); //NOI18N
+ } else if (contact.getContactId() == null) {
// Throw NPE
- throw new NullPointerException("userContact.contactId is null"); //NOI18N
- } else if (userContact.getContactId() < 1) {
+ throw new NullPointerException("contact.contactId is null"); //NOI18N
+ } else if (contact.getContactId() < 1) {
// Not valid id number
- throw new IllegalArgumentException(MessageFormat.format("userContact.contactId={0} is not valid.", userContact.getContactId())); //NOI18N
+ throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
}
// Set all
- this.copyContact(userContact);
+ this.copyContact(contact);
}
/**
this.emailAddressList.add(contact.getContactEmailAddress());
}
+ /**
+ * Clears this bean
+ */
+ private void clear () {
+ // Clear all data
+ // - personal data
+ this.setGender(Gender.UNKNOWN);
+ this.setFirstName(null);
+ this.setFamilyName(null);
+ this.setStreet(null);
+ this.setHouseNumber(null);
+ this.setZipCode(null);
+ this.setCity(null);
+ this.setCountry(null);
+
+ // - contact data
+ this.setEmailAddress(null);
+ this.setEmailAddressRepeat(null);
+ this.setPhoneAreaCode(null);
+ this.setCellphoneCarrier(null);
+ this.setFaxAreaCode(null);
+
+ // - other data
+ this.setBirthday(null);
+ this.setComment(null);
+ }
+
/**
* Copies given contact into the controller
* <p>
* @param contact Contact instance
*/
private void copyContact (final Contact contact) {
+ // Is the instance valid?
+ if (null == contact) {
+ // Throw NPE
+ throw new NullPointerException("contact is null"); //NOI18N
+ } else if (contact.getContactId() == null) {
+ // Throw NPE
+ throw new NullPointerException("contact.contactId is null"); //NOI18N
+ } else if (contact.getContactId() < 1) {
+ // Not valid id number
+ throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
+ }
+
// Copy all fields:
// - base data
this.setGender(contact.getContactGender());
this.setZipCode(contact.getContactZipCode());
this.setCity(contact.getContactCity());
this.setCountry(contact.getContactCountry());
+ this.setEmailAddress(contact.getContactEmailAddress());
+ this.setBirthday(contact.getContactBirthday());
+ this.setComment(contact.getContactComment());
// Get cellphone, phone and fax instance
DialableCellphoneNumber cellphone = contact.getContactCellphoneNumber();
this.setPhoneAreaCode(phone.getPhoneAreaCode());
this.setPhoneNumber(phone.getPhoneNumber());
}
+
if ((cellphone instanceof DialableCellphoneNumber) && (cellphone.getCellphoneProvider() instanceof MobileProvider)) {
this.setCellphoneCarrier(cellphone.getCellphoneProvider());
this.setCellphoneNumber(cellphone.getPhoneNumber());
}
+
if ((fax instanceof DialableFaxNumber) && (fax.getPhoneAreaCode() > 0)) {
this.setFaxCountry(fax.getPhoneCountry());
this.setFaxAreaCode(fax.getPhoneAreaCode());
this.setFaxNumber(fax.getPhoneNumber());
}
- this.setEmailAddress(contact.getContactEmailAddress());
+ }
- // -- other data
- this.setBirthday(contact.getContactBirthday());
- this.setComment(contact.getContactComment());
+ /**
+ * Removes given contact from all lists
+ * <p>
+ * @param contact Contact instance to remove
+ */
+ private void removeContact (final Contact contact) {
+ // Is the instance valid?
+ if (null == contact) {
+ // Throw NPE
+ throw new NullPointerException("contact is null"); //NOI18N
+ } else if (contact.getContactId() == null) {
+ // Throw NPE
+ throw new NullPointerException("contact.contactId is null"); //NOI18N
+ } else if (contact.getContactId() < 1) {
+ // Not valid id number
+ throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
+ }
+
+ // Remove from general list
+ if (!this.contactList.remove(contact)) {
+ // Did not remove contact
+ throw new IllegalStateException(MessageFormat.format("contact {0} was not removed.", contact.getContactId())); //NOI18N
+ }
+
+ // Remove from other lists
+ this.emailAddressList.remove(contact.getContactEmailAddress());
}
}