From 2b1a256249782e382c88091df34da12e5a193d34 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Mon, 15 Aug 2016 12:45:01 +0200 Subject: [PATCH] Please cherry-pick: - splitted contact remote interface into general and administrative purposes MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../JobsAdminContactWebRequestBean.java | 125 +++++++++++------- .../JobsAdminContactWebRequestController.java | 42 ++++-- 2 files changed, 111 insertions(+), 56 deletions(-) diff --git a/src/java/org/mxchange/jjobs/beans/contact/JobsAdminContactWebRequestBean.java b/src/java/org/mxchange/jjobs/beans/contact/JobsAdminContactWebRequestBean.java index 4c0036ea..189f9cea 100644 --- a/src/java/org/mxchange/jjobs/beans/contact/JobsAdminContactWebRequestBean.java +++ b/src/java/org/mxchange/jjobs/beans/contact/JobsAdminContactWebRequestBean.java @@ -22,14 +22,15 @@ import java.util.Iterator; import javax.annotation.PostConstruct; import javax.enterprise.context.RequestScoped; import javax.enterprise.event.Event; +import javax.enterprise.event.Observes; import javax.enterprise.inject.Any; -import javax.faces.context.FacesContext; import javax.faces.view.facelets.FaceletException; import javax.inject.Inject; import javax.inject.Named; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; +import org.mxchange.jcontacts.contact.AdminContactSessionBeanRemote; import org.mxchange.jcontacts.contact.Contact; import org.mxchange.jcontacts.contact.ContactSessionBeanRemote; import org.mxchange.jcontacts.contact.UserContact; @@ -37,6 +38,8 @@ 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.deleted.AdminContactDeletedEvent; +import org.mxchange.jcontacts.events.contact.deleted.AdminDeletedContactEvent; import org.mxchange.jcontacts.events.contact.update.AdminContactUpdatedEvent; import org.mxchange.jcontacts.events.contact.update.AdminUpdatedContactEvent; import org.mxchange.jcontacts.exceptions.ContactAlreadyAddedException; @@ -44,13 +47,11 @@ import org.mxchange.jcountry.data.Country; 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 controller (bean) @@ -73,6 +74,11 @@ public class JobsAdminContactWebRequestBean extends BaseJobsController implement @Any private Event addedContactEvent; + /** + * Administrative contact EJB + */ + private AdminContactSessionBeanRemote adminContactBean; + /** * Admin helper instance */ @@ -110,7 +116,7 @@ public class JobsAdminContactWebRequestBean extends BaseJobsController implement private String comment; /** - * Remote contact bean + * General contact EJB */ private final ContactSessionBeanRemote contactBean; @@ -130,6 +136,13 @@ public class JobsAdminContactWebRequestBean extends BaseJobsController implement */ private Country country; + /** + * Event being fired when contact data has been deleted + */ + @Inject + @Any + private Event deletedContactEvent; + /** * Email address */ @@ -246,8 +259,11 @@ public class JobsAdminContactWebRequestBean extends BaseJobsController implement // 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 + // Try to lookup (general) + this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/jjobs-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N + + // Try to lookup (administative) + this.adminContactBean = (AdminContactSessionBeanRemote) context.lookup("java:global/jjobs-ejb/adminContact!org.mxchange.jcontacts.contact.AdminContactSessionBeanRemote"); //NOI18N } catch (final NamingException e) { // Throw again throw new FaceletException(e); @@ -289,7 +305,7 @@ public class JobsAdminContactWebRequestBean extends BaseJobsController implement // Try to call EJB try { // Call EJB - updatedContact = this.contactBean.addContact(contact); + updatedContact = this.adminContactBean.addContact(contact); } catch (final ContactAlreadyAddedException ex) { // Throw again throw new FaceletException(ex); @@ -305,6 +321,27 @@ public class JobsAdminContactWebRequestBean extends BaseJobsController implement return "admin_list_contact"; //NOI18N } + @Override + public void afterAdminAddedUserEvent (@Observes final AdminAddedUserEvent event) { + // 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(); + } + @Override public void copyContactToController (final Contact contact) { // The contact instance must be valid @@ -361,40 +398,38 @@ public class JobsAdminContactWebRequestBean extends BaseJobsController implement @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 + // Update all data in contact + this.updateContactData(contact); + + // Return it + return contact; + } + + @Override + public String deleteContactData (final Contact contact) { + // The instance must be valid if (null == contact) { // Throw NPE - throw new NullPointerException("beanHelper.contact is null"); //NOI18N + throw new NullPointerException("contact is null"); //NOI18N } else if (contact.getContactId() == null) { - // Throw NPE again - throw new NullPointerException("beanHelper.contact.contactId is null"); //NOI18N //NOI18N + // Throw it again + throw new NullPointerException("contact.contactId is null"); //NOI18N } else if (contact.getContactId() < 1) { - // Invalid id - throw new IllegalStateException(MessageFormat.format("beanHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N + // Invalid id number + throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", 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); + // Call EJB + this.adminContactBean.deleteContactData(contact); // Fire event - this.updatedContactEvent.fire(new AdminContactUpdatedEvent(updatedContact)); - - // Clear bean - this.clear(); + this.deletedContactEvent.fire(new AdminContactDeletedEvent(contact)); - // Return it - return contact; + // All fine + return "admin_list_contact"; //NOI18N } @Override @@ -542,6 +577,17 @@ public class JobsAdminContactWebRequestBean extends BaseJobsController implement this.contactId = contactId; } + @Override + public String getControllerType () { + return "admin"; //NOI18N + } + + @Override + @Deprecated + public void setControllerType (final String controllerType) { + throw new UnsupportedOperationException("Setting controller type is not supported."); //NOI18N + } + @Override public Country getCountry () { return this.country; @@ -729,18 +775,6 @@ public class JobsAdminContactWebRequestBean extends BaseJobsController implement public void init () { } - @Override - public boolean isGenderRequired () { - // Get context parameter - String contextParameter = FacesContext.getCurrentInstance().getExternalContext().getInitParameter("is_admin_gender_enabled"); //NOI18N - - // Is it set? - boolean isRequired = ((contextParameter instanceof String) && (contextParameter.toLowerCase().equals("true"))); //NOI18N - - // Return value - return isRequired; - } - /** * Clears this bean */ @@ -815,12 +849,6 @@ public class JobsAdminContactWebRequestBean extends BaseJobsController implement if (null == contact) { // Throw NPE 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) { - // Invalid id - throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is invalid", contact.getContactId())); //NOI18N } // Update all fields @@ -834,6 +862,7 @@ public class JobsAdminContactWebRequestBean extends BaseJobsController implement contact.setContactZipCode(this.getZipCode()); contact.setContactCity(this.getCity()); contact.setContactCountry(this.getCountry()); + contact.setContactEmailAddress(this.getEmailAddress()); // Update contact's cellphone number this.isCellphoneUnlinked = ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber()); diff --git a/src/java/org/mxchange/jjobs/beans/contact/JobsAdminContactWebRequestController.java b/src/java/org/mxchange/jjobs/beans/contact/JobsAdminContactWebRequestController.java index e4c9484a..ba180026 100644 --- a/src/java/org/mxchange/jjobs/beans/contact/JobsAdminContactWebRequestController.java +++ b/src/java/org/mxchange/jjobs/beans/contact/JobsAdminContactWebRequestController.java @@ -24,6 +24,7 @@ import org.mxchange.jcountry.data.Country; import org.mxchange.jphone.phonenumbers.DialableNumber; import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber; import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider; +import org.mxchange.jusercore.events.user.add.AdminAddedUserEvent; /** * An administrative interface for user beans @@ -40,6 +41,22 @@ public interface JobsAdminContactWebRequestController extends Serializable { */ String addContact (); + /** + * Deletes contact data + *

+ * @param contact Contact instance to be deleted + *

+ * @return Redirect outcome + */ + String deleteContactData (final Contact contact); + + /** + * Event observer for newly added users by adminstrator + *

+ * @param event Event being fired + */ + void afterAdminAddedUserEvent (final AdminAddedUserEvent event); + /** * Creates an instance from contact data *

@@ -412,6 +429,23 @@ public interface JobsAdminContactWebRequestController extends Serializable { */ void setZipCode (final Integer zipCode); + /** + * Getter for controller type + *

+ * @return controller type + */ + String getControllerType (); + + /** + * Setter for controller type + *

+ * @param controllerType Controller type + *

+ * @deprecated Don't use this method + */ + @Deprecated + void setControllerType (final String controllerType); + /** * Returns a text respresentation of given phone number or null if not set. *

@@ -431,12 +465,4 @@ public interface JobsAdminContactWebRequestController extends Serializable { */ String generateCellphoneNumber (final DialableCellphoneNumber cellphoneNumber); - /** - * Checks/returns whether the gender/salutation is required for this - * controller. - *

- * @return Whether gender is required - */ - boolean isGenderRequired (); - } -- 2.39.5