X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2Fjava%2Forg%2Fmxchange%2Fjjobs%2Fbeans%2Fcontact%2FJobsAdminContactWebRequestBean.java;h=ea6ad9664b990d3593b53259cfb0edd2c33814a4;hb=a231e9a11ec7bde003846d8143033229aca2e6a4;hp=2362342448ad25887aedaaf83475d5eecc25489c;hpb=81ea43c6c468a8484b611c3ab38c76162b7363cf;p=jjobs-war.git diff --git a/src/java/org/mxchange/jjobs/beans/contact/JobsAdminContactWebRequestBean.java b/src/java/org/mxchange/jjobs/beans/contact/JobsAdminContactWebRequestBean.java index 23623424..ea6ad966 100644 --- a/src/java/org/mxchange/jjobs/beans/contact/JobsAdminContactWebRequestBean.java +++ b/src/java/org/mxchange/jjobs/beans/contact/JobsAdminContactWebRequestBean.java @@ -18,7 +18,7 @@ package org.mxchange.jjobs.beans.contact; import java.text.MessageFormat; import java.util.Date; -import java.util.List; +import java.util.Iterator; import javax.annotation.PostConstruct; import javax.enterprise.context.RequestScoped; import javax.enterprise.event.Event; @@ -31,15 +31,25 @@ import javax.naming.InitialContext; 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.jjobs.beans.helper.JobsAdminWebRequestController; +import org.mxchange.jjobs.beans.BaseJobsController; +import org.mxchange.jjobs.beans.helper.JobsWebRequestController; +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.jusercore.events.user.add.AdminAddedUserEvent; /** * An administrative user bean (controller) @@ -48,7 +58,7 @@ import org.mxchange.jusercore.events.user.add.AdminAddedUserEvent; */ @Named ("adminContactController") @RequestScoped -public class JobsAdminContactWebRequestBean implements JobsAdminContactWebRequestController { +public class JobsAdminContactWebRequestBean extends BaseJobsController implements JobsAdminContactWebRequestController { /** * Serial number @@ -56,17 +66,17 @@ public class JobsAdminContactWebRequestBean implements JobsAdminContactWebReques private static final long serialVersionUID = 542_145_347_916L; /** - * An event fired when the administrator has added a new user + * An event fired when the administrator has added a new contact */ @Inject @Any - private Event addedUserEvent; + private Event addedContactEvent; /** * Admin helper instance */ @Inject - private JobsAdminWebRequestController adminHelper; + private JobsWebRequestController beanHelper; /** * Birth day @@ -103,6 +113,12 @@ public class JobsAdminContactWebRequestBean implements JobsAdminContactWebReques */ private final ContactSessionBeanRemote contactBean; + /** + * General contact controller + */ + @Inject + private JobsContactWebSessionController contactController; + /** * Contact id */ @@ -198,6 +214,11 @@ public class JobsAdminContactWebRequestBean implements JobsAdminContactWebReques */ private String street; + /** + * Title + */ + private String title; + /** * An event fired when the administrator has updated contact data */ @@ -214,9 +235,6 @@ public class JobsAdminContactWebRequestBean implements JobsAdminContactWebReques * Default constructor */ public JobsAdminContactWebRequestBean () { - // Set gender to UNKNOWN - this.gender = Gender.UNKNOWN; - // Try it try { // Get initial context @@ -232,6 +250,24 @@ public class JobsAdminContactWebRequestBean implements JobsAdminContactWebReques @Override public String addContact () { + // 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 + } + // Create new contact instance Contact contact = this.createContactInstance(); @@ -263,11 +299,6 @@ public class JobsAdminContactWebRequestBean implements JobsAdminContactWebReques return "admin_list_contact"; //NOI18N } - @Override - public List allContacts () { - return this.contactController.allContacts(); - } - @Override public void copyContactToController (final Contact contact) { // The contact instance must be valid @@ -284,6 +315,7 @@ public class JobsAdminContactWebRequestBean implements JobsAdminContactWebReques // Set all fields: contact this.setContactId(contact.getContactId()); + this.setTitle(contact.getContactTitle()); this.setBirthday(contact.getContactBirthday()); this.setCity(contact.getContactCity()); this.setComment(contact.getContactComment()); @@ -293,42 +325,87 @@ public class JobsAdminContactWebRequestBean implements JobsAdminContactWebReques this.setFirstName(contact.getContactFirstName()); this.setGender(contact.getContactGender()); this.setHouseNumber(contact.getContactHouseNumber()); + this.setHouseNumberExtension(contact.getContactHouseNumberExtension()); this.setStreet(contact.getContactStreet()); this.setZipCode(contact.getContactZipCode()); // ... cellphone data - this.setCellphoneId(contact.getContactCellphoneNumber().getPhoneId()); - this.setCellphoneCarrier(contact.getContactCellphoneNumber().getCellphoneProvider()); - this.setCellphoneNumber(contact.getContactCellphoneNumber().getPhoneNumber()); + if (contact.getContactCellphoneNumber() instanceof DialableCellphoneNumber) { + this.setCellphoneId(contact.getContactCellphoneNumber().getPhoneId()); + this.setCellphoneCarrier(contact.getContactCellphoneNumber().getCellphoneProvider()); + this.setCellphoneNumber(contact.getContactCellphoneNumber().getPhoneNumber()); + } // ... fax data - this.setFaxId(contact.getContactFaxNumber().getPhoneId()); - this.setFaxAreaCode(contact.getContactFaxNumber().getPhoneAreaCode()); - this.setFaxCountry(contact.getContactFaxNumber().getPhoneCountry()); - this.setFaxNumber(contact.getContactFaxNumber().getPhoneNumber()); + if (contact.getContactFaxNumber() instanceof DialableFaxNumber) { + this.setFaxId(contact.getContactFaxNumber().getPhoneId()); + this.setFaxAreaCode(contact.getContactFaxNumber().getPhoneAreaCode()); + this.setFaxCountry(contact.getContactFaxNumber().getPhoneCountry()); + this.setFaxNumber(contact.getContactFaxNumber().getPhoneNumber()); + } // .. land-line data - this.setLandLineId(contact.getContactLandLineNumber().getPhoneId()); - this.setPhoneAreaCode(contact.getContactLandLineNumber().getPhoneAreaCode()); - this.setPhoneCountry(contact.getContactLandLineNumber().getPhoneCountry()); - this.setPhoneNumber(contact.getContactLandLineNumber().getPhoneNumber()); + if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) { + this.setLandLineId(contact.getContactLandLineNumber().getPhoneId()); + this.setPhoneAreaCode(contact.getContactLandLineNumber().getPhoneAreaCode()); + this.setPhoneCountry(contact.getContactLandLineNumber().getPhoneCountry()); + this.setPhoneNumber(contact.getContactLandLineNumber().getPhoneNumber()); + } + } + + @Override + public Contact createContactInstance () { + // 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()); + + // 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 it + return contact; } @Override public String editContactData () { // Get contact instance - Contact contact = this.adminHelper.getContact(); + Contact contact = this.beanHelper.getContact(); // Check if contact instance is in helper and valid if (null == contact) { // Throw NPE - throw new NullPointerException("adminHelper.contact is null"); //NOI18N + throw new NullPointerException("beanHelper.contact is null"); //NOI18N } else if (contact.getContactId() == null) { // Throw NPE again - throw new NullPointerException("adminHelper.contact.contactId is null"); //NOI18N //NOI18N + throw new NullPointerException("beanHelper.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 + throw new IllegalStateException(MessageFormat.format("beanHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N } // Update all data in contact @@ -347,6 +424,46 @@ public class JobsAdminContactWebRequestBean implements JobsAdminContactWebReques 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 () { @@ -569,6 +686,16 @@ public class JobsAdminContactWebRequestBean implements JobsAdminContactWebReques 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; @@ -586,6 +713,70 @@ public class JobsAdminContactWebRequestBean implements JobsAdminContactWebReques public void init () { } + /** + * Clears this bean + */ + private void clear () { + // Clear all data + // - personal data + this.setGender(null); + this.setTitle(null); + this.setFirstName(null); + this.setFamilyName(null); + this.setStreet(null); + this.setHouseNumber(null); + this.setHouseNumberExtension(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 + *

+ * @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 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. *

@@ -606,6 +797,7 @@ public class JobsAdminContactWebRequestBean implements JobsAdminContactWebReques // Update all fields contact.setContactGender(this.getGender()); + contact.setContactTitle(this.getTitle()); contact.setContactFirstName(this.getFirstName()); contact.setContactFamilyName(this.getFamilyName()); contact.setContactStreet(this.getStreet());