import java.text.MessageFormat;
import java.util.Date;
+import java.util.Iterator;
import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.event.Event;
import javax.naming.NamingException;
import org.mxchange.jcontacts.contact.Contact;
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.add.AdminContactAddedEvent;
import org.mxchange.jcontacts.events.contact.update.AdminContactUpdatedEvent;
import org.mxchange.jcontacts.events.contact.update.AdminUpdatedContactEvent;
+import org.mxchange.jcontacts.exceptions.ContactAlreadyAddedException;
import org.mxchange.jcountry.data.Country;
+import org.mxchange.jphone.phonenumbers.DialableNumber;
+import org.mxchange.jphone.phonenumbers.cellphone.CellphoneNumber;
import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
+import org.mxchange.jphone.phonenumbers.fax.FaxNumber;
import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
+import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
-import org.mxchange.pizzaapplication.beans.helper.PizzaAdminWebRequestController;
+import org.mxchange.pizzaapplication.beans.BasePizzaController;
+import org.mxchange.pizzaapplication.beans.helper.PizzaWebRequestController;
/**
- * A user bean (controller)
+ * Administrative user bean (controller)
* <p>
* @author Roland Haeder<roland@mxchange.org>
*/
@Named ("adminContactController")
@RequestScoped
-public class PizzaAdminContactWebRequestBean implements PizzaAdminContactWebRequestController {
+public class PizzaAdminContactWebRequestBean extends BasePizzaController implements PizzaAdminContactWebRequestController {
/**
* Serial number
* Admin helper instance
*/
@Inject
- private PizzaAdminWebRequestController adminHelper;
+ private PizzaWebRequestController beanHelper;
/**
* Birth day
*/
private final ContactSessionBeanRemote contactBean;
+ /**
+ * General contact controller
+ */
+ @Inject
+ private PizzaContactWebSessionController contactController;
+
/**
* Contact id
*/
*/
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;
+
/**
* Land-line id number
*/
*/
private String street;
+ /**
+ * Title
+ */
+ private String title;
+
/**
* An event fired when the administrator has updated contact data
*/
* Default constructor
*/
public PizzaAdminContactWebRequestBean () {
- // 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/PizzaService-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
+ this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/pizzaservice-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
} catch (final NamingException e) {
// Throw again
throw new FaceletException(e);
}
@Override
- public String changeContactData () {
- // Get contact instance
- Contact contact = this.adminHelper.getContact();
+ public String addContact () {
+ // Create new contact instance
+ Contact contact = this.createContactInstance();
- // Check if contact instance is in helper and valid
- if (null == contact) {
- // Throw NPE
- throw new NullPointerException("adminHelper.contact is null"); //NOI18N
- } else if (contact.getContactId() == null) {
- // Throw NPE again
- throw new NullPointerException("adminHelper.contact.contactId is null"); //NOI18N //NOI18N
- } else if (contact.getContactId() < 1) {
- // Invalid id
- throw new IllegalStateException(MessageFormat.format("adminHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
+ // Default is not same contact
+ if (this.isSameContactFound(contact)) {
+ // Already registered
+ throw new FaceletException(new ContactAlreadyAddedException(contact));
}
- // Update all data in contact
- this.updateContactData(contact);
+ // Init contact
+ Contact updatedContact;
- // Call EJB for updating contact data
- Contact updatedContact = this.contactBean.updateContactPersonalData(contact);
+ // Try to call EJB
+ try {
+ // Call EJB
+ updatedContact = this.contactBean.addContact(contact);
+ } catch (final ContactAlreadyAddedException ex) {
+ // Throw again
+ throw new FaceletException(ex);
+ }
// Fire event
- this.updatedContactEvent.fire(new AdminContactUpdatedEvent(updatedContact));
+ this.addedContactEvent.fire(new AdminContactAddedEvent(updatedContact));
- // Return to contact list (for now)
+ // Clear this bean
+ this.clear();
+
+ // Return outcome
return "admin_list_contact"; //NOI18N
}
@Override
public void copyContactToController (final Contact contact) {
// Log message
- System.out.println(MessageFormat.format("AdminContactController::copyContactToController(): contact={0} - CALLED!", contact)); //NOI18N
+ //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("AdminContactController::copyContactToController(): contact={0} - CALLED!", contact)); //NOI18N
// The contact instance must be valid
if (null == contact) {
// Throw NPE again
throw new NullPointerException("contact is null"); //NOI18N
+ } else if (contact.getContactId() == null) {
+ // Throw NPE again
+ throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
} else if (contact.getContactId() < 1) {
// Not valid
throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
// Set all fields: contact
this.setContactId(contact.getContactId());
+ this.setTitle(contact.getContactTitle());
this.setBirthday(contact.getContactBirthday());
this.setCity(contact.getContactCity());
this.setComment(contact.getContactComment());
}
// Log message
- System.out.println("AdminContactController::copyContactToController(): EXIT!"); //NOI18N
+ //* NOISY-DEBUG: */ System.out.println("AdminContactController::copyContactToController(): EXIT!"); //NOI18N
+ }
+
+ @Override
+ public Contact createContactInstance () {
+ // Are all minimum fields set?
+ if (this.getGender() == null) {
+ // Throw NPE
+ throw new NullPointerException("gender is null"); //NOI18N
+ } else if (this.getFirstName() == null) {
+ // Throw NPE
+ throw new NullPointerException("firstName is null"); //NOI18N
+ } else if (this.getFirstName().isEmpty()) {
+ // Empty string
+ throw new IllegalStateException("firstName is empty"); //NOI18N
+ } else if (this.getFamilyName() == null) {
+ // Throw NPE
+ throw new NullPointerException("familyName is null"); //NOI18N
+ } else if (this.getFamilyName().isEmpty()) {
+ // Empty string
+ throw new IllegalStateException("familyName is empty"); //NOI18N
+ }
+
+ // Generate phone number
+ DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
+ DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
+ DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
+
+ // Create new instance
+ Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
+
+ // Add all others
+ contact.setContactBirthday(this.getBirthday());
+ contact.setContactStreet(this.getStreet());
+ contact.setContactHouseNumber(this.getHouseNumber());
+ contact.setContactZipCode(this.getZipCode());
+ contact.setContactCity(this.getCity());
+ contact.setContactCountry(this.getCountry());
+ contact.setContactEmailAddress(this.getEmailAddress());
+ contact.setContactBirthday(this.getBirthday());
+ contact.setContactComment(this.getComment());
+
+ // Set ownContact
+ contact.setContactOwnContact(Boolean.TRUE);
+
+ // 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
+ if (phone.getPhoneAreaCode() == null) {
+ // Is null
+ throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
+ } else if (phone.getPhoneAreaCode() < 1) {
+ // Abort here
+ throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
+ } else if (phone.getPhoneNumber() == null) {
+ // Is null
+ throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
+ } else if (phone.getPhoneNumber() < 1) {
+ // Abort here
+ throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
+ }
+
+ // Set phone number
+ contact.setContactLandLineNumber(phone);
+ }
+
+ // Don't set null or wrong references
+ if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
+ // Now the number must be given
+ if (fax.getPhoneAreaCode() == null) {
+ // Is null
+ throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
+ } else if (fax.getPhoneAreaCode() < 1) {
+ // Abort here
+ throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
+ } else if (fax.getPhoneNumber() == null) {
+ // Is null
+ throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
+ } else if (fax.getPhoneNumber() < 1) {
+ // Abort here
+ throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
+ }
+
+ // Set fax number
+ contact.setContactFaxNumber(fax);
+ }
+
+ // Is the provider set?
+ if ((cellphone instanceof DialableCellphoneNumber) && (this.getCellphoneCarrier() instanceof MobileProvider) && (this.getCellphoneNumber() != null) && (this.getCellphoneNumber() > 0)) {
+ // Is the number set?
+ if (cellphone.getPhoneNumber() == null) {
+ // Is null
+ throw new NullPointerException("cellphone.phoneNumber is null"); //NOI18N
+ } else if (cellphone.getPhoneNumber() < 1) {
+ // Abort here
+ throw new IllegalArgumentException("cellphone.phoneNumber is zero or below."); //NOI18N
+ }
+
+ // Set cellphone number
+ contact.setContactCellphoneNumber(cellphone);
+ }
+
+ // Return it
+ return contact;
+ }
+
+ @Override
+ public String editContactData () {
+ // Get contact instance
+ Contact contact = this.beanHelper.getContact();
+
+ // Check if contact instance is in helper and valid
+ if (null == contact) {
+ // Throw NPE
+ throw new NullPointerException("beanHelper.contact is null"); //NOI18N
+ } else if (contact.getContactId() == null) {
+ // Throw NPE again
+ throw new NullPointerException("beanHelper.contact.contactId is null"); //NOI18N //NOI18N
+ } else if (contact.getContactId() < 1) {
+ // Invalid id
+ throw new IllegalStateException(MessageFormat.format("beanHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
+ }
+
+ // Update all data in contact
+ this.updateContactData(contact);
+
+ // Call EJB for updating contact data
+ Contact updatedContact = this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
+
+ // Fire event
+ this.updatedContactEvent.fire(new AdminContactUpdatedEvent(updatedContact));
+
+ // Clear bean
+ this.clear();
+
+ // Return to contact list (for now)
+ return "admin_list_contact"; //NOI18N
+ }
+
+ @Override
+ public String generateCellphoneNumber (final DialableCellphoneNumber cellphoneNumber) {
+ // Is it null?
+ if (null == cellphoneNumber) {
+ // Return null
+ return null;
+ }
+
+ // Get all data
+ String number = String.format(
+ "%s%d%d", //NOI18N
+ cellphoneNumber.getCellphoneProvider().getProviderCountry().getCountryExternalDialPrefix(),
+ cellphoneNumber.getCellphoneProvider().getProviderDialPrefix(),
+ cellphoneNumber.getPhoneNumber()
+ );
+
+ // Return it
+ return number;
}
@Override
+ public String generatePhoneNumber (final DialableNumber phoneNumber) {
+ // Is it null?
+ if (null == phoneNumber) {
+ // Return null
+ return null;
+ }
+
+ // Generate it
+ String number = String.format(
+ "%s%d%d", //NOI18N
+ phoneNumber.getPhoneCountry().getCountryExternalDialPrefix(),
+ phoneNumber.getPhoneAreaCode(),
+ phoneNumber.getPhoneNumber()
+ );
+
+ // Return it
+ return number;
+ }
+
+ @Override
+ @SuppressWarnings ("ReturnOfDateField")
public Date getBirthday () {
return this.birthday;
}
@Override
+ @SuppressWarnings ("AssignmentToDateFieldFromParameter")
public void setBirthday (final Date birthday) {
this.birthday = birthday;
}
this.street = street;
}
+ @Override
+ public String getTitle () {
+ return this.title;
+ }
+
+ @Override
+ public void setTitle (final String title) {
+ this.title = title;
+ }
+
@Override
public Integer getZipCode () {
return this.zipCode;
public void init () {
}
+ /**
+ * Clears this bean
+ */
+ private void clear () {
+ // Clear all data
+ // - personal data
+ this.setGender(Gender.UNKNOWN);
+ this.setTitle(null);
+ 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.setPhoneCountry(null);
+ this.setPhoneAreaCode(null);
+ this.setPhoneNumber(null);
+ this.setCellphoneCarrier(null);
+ this.setCellphoneNumber(null);
+ this.setFaxCountry(null);
+ this.setFaxAreaCode(null);
+ this.setFaxNumber(null);
+
+ // - other data
+ this.setBirthday(null);
+ this.setComment(null);
+ }
+
+ /**
+ * Checks whether the given contact is found
+ * <p>
+ * @param contact Contact inastance
+ *
+ * @return Wether contact has been found
+ */
+ private boolean isSameContactFound (final Contact contact) {
+ // Default is not found
+ boolean IsFound = false;
+
+ // Get iterator
+ Iterator<Contact> iterator = this.contactController.allContacts().iterator();
+
+ // Loop through all
+ while (iterator.hasNext()) {
+ // Get next contact
+ Contact next = iterator.next();
+
+ // Is the same?
+ if (ContactUtils.isSameContact(contact, next)) {
+ // Yes, then abort loop
+ IsFound = false;
+ break;
+ }
+ }
+
+ // Return status
+ return IsFound;
+ }
+
/**
* Updates all data in contact instance.
* <p>
// Update all fields
contact.setContactGender(this.getGender());
+ contact.setContactTitle(this.getTitle());
contact.setContactFirstName(this.getFirstName());
contact.setContactFamilyName(this.getFamilyName());
contact.setContactStreet(this.getStreet());
contact.setContactCountry(this.getCountry());
// Update contact's cellphone number
- ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
-
- // Is there a phone number?
- if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
- // Debug message
- System.out.println(MessageFormat.format("updateContactData: phoneId={0}", contact.getContactLandLineNumber().getPhoneId())); //NOI18N
+ this.isCellphoneUnlinked = ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
- // Yes, then update as well
- contact.getContactLandLineNumber().setPhoneAreaCode(this.getPhoneAreaCode());
- contact.getContactLandLineNumber().setPhoneNumber(this.getPhoneNumber());
- }
+ // Update contact's land-line number
+ this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
- // Is there a fax number?
- if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
- // Debug message
- System.out.println(MessageFormat.format("updateContactData: faxId={0}", contact.getContactFaxNumber().getPhoneId())); //NOI18N
-
- // Yes, then update as well
- contact.getContactFaxNumber().setPhoneAreaCode(this.getFaxAreaCode());
- contact.getContactFaxNumber().setPhoneNumber(this.getFaxNumber());
- }
+ // Update contact's fax number
+ this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
}
}