X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2Fjava%2Forg%2Fmxchange%2Fjjobs%2Fbeans%2Fcontact%2FJobsAdminContactWebRequestBean.java;h=6d06039f05b06c205426e8c70e19e3437c083d62;hb=164ba221de68d0ae673f411bd84aae8880f4aaa9;hp=9d7a5ec4d4fd8c2902b9df89931be0657145c843;hpb=d897d352bd9150d3dda7c8b4dea17afc3ecc35ba;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 9d7a5ec4..6d06039f 100644 --- a/src/java/org/mxchange/jjobs/beans/contact/JobsAdminContactWebRequestBean.java +++ b/src/java/org/mxchange/jjobs/beans/contact/JobsAdminContactWebRequestBean.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016, 2017 Roland Häder + * Copyright (C) 2016 - 2022 Free Software Foundation * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as @@ -18,29 +18,32 @@ package org.mxchange.jjobs.beans.contact; import java.text.MessageFormat; import java.util.Date; -import java.util.Iterator; import javax.ejb.EJB; import javax.enterprise.context.RequestScoped; import javax.enterprise.event.Event; import javax.enterprise.event.Observes; import javax.enterprise.inject.Any; -import javax.faces.view.facelets.FaceletException; +import javax.faces.FacesException; import javax.inject.Inject; import javax.inject.Named; import org.mxchange.jcontacts.events.contact.add.AdminAddedContactEvent; import org.mxchange.jcontacts.events.contact.add.ObservableAdminAddedContactEvent; import org.mxchange.jcontacts.events.contact.created.ObservableCreatedContactEvent; +import org.mxchange.jcontacts.events.contact.deleted.AdminDeletedContactEvent; +import org.mxchange.jcontacts.events.contact.deleted.ObservableAdminDeletedContactEvent; import org.mxchange.jcontacts.events.contact.update.AdminUpdatedContactEvent; import org.mxchange.jcontacts.events.contact.update.ObservableAdminUpdatedContactEvent; import org.mxchange.jcontacts.exceptions.ContactAlreadyAddedException; +import org.mxchange.jcontacts.exceptions.ContactNotFoundException; import org.mxchange.jcontacts.model.contact.AdminContactSessionBeanRemote; import org.mxchange.jcontacts.model.contact.Contact; import org.mxchange.jcontacts.model.contact.ContactSessionBeanRemote; -import org.mxchange.jcontacts.model.contact.ContactUtils; +import org.mxchange.jcontacts.model.contact.Contacts; import org.mxchange.jcontacts.model.contact.UserContact; import org.mxchange.jcontacts.model.contact.title.PersonalTitle; import org.mxchange.jcountry.model.data.Country; -import org.mxchange.jjobs.beans.BaseJobsController; +import org.mxchange.jjobs.beans.BaseJobsBean; +import org.mxchange.jjobs.beans.contact.list.JobsContactListWebViewController; import org.mxchange.jphone.model.phonenumbers.DialableNumber; import org.mxchange.jphone.model.phonenumbers.fax.DialableFaxNumber; import org.mxchange.jphone.model.phonenumbers.fax.FaxNumber; @@ -57,7 +60,7 @@ import org.mxchange.jphone.model.phonenumbers.mobileprovider.MobileProvider; */ @Named ("adminContactController") @RequestScoped -public class JobsAdminContactWebRequestBean extends BaseJobsController implements JobsAdminContactWebRequestController { +public class JobsAdminContactWebRequestBean extends BaseJobsBean implements JobsAdminContactWebRequestController { /** * Serial number @@ -79,7 +82,7 @@ public class JobsAdminContactWebRequestBean extends BaseJobsController implement /** * Administrative contact EJB */ - @EJB (lookup = "java:global/jjobs-ejb/adminContact!org.mxchange.jcontacts.contact.AdminContactSessionBeanRemote") + @EJB (lookup = "java:global/jjobs-ejb/adminContact!org.mxchange.jcontacts.model.contact.AdminContactSessionBeanRemote") private AdminContactSessionBeanRemote adminContactBean; /** @@ -97,10 +100,15 @@ public class JobsAdminContactWebRequestBean extends BaseJobsController implement */ private String comment; + /** + * Current contact instance + */ + private Contact contact; + /** * EJB for general contact purposes */ - @EJB (lookup = "java:global/jjobs-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote") + @EJB (lookup = "java:global/jjobs-ejb/contact!org.mxchange.jcontacts.model.contact.ContactSessionBeanRemote") private ContactSessionBeanRemote contactBean; /** @@ -119,6 +127,19 @@ public class JobsAdminContactWebRequestBean extends BaseJobsController implement */ private Long contactId; + /** + * An instance of a contact list controller + */ + @Inject + private JobsContactListWebViewController contactListController; + + /** + * Event being fired when an administrator has deleted a contact + */ + @Any + @Inject + private Event deletedContactEvent; + /** * Email address */ @@ -247,10 +268,8 @@ public class JobsAdminContactWebRequestBean extends BaseJobsController implement /** * Adds contact data to database and redirects on success. If the contact is * already found, a proper exception is thrown. - *

- * @return Redirect outcome */ - public String addContact () { + public void addContact () { // Are all minimum fields set? if (this.getPersonalTitle() == null) { // Throw NPE @@ -270,12 +289,12 @@ public class JobsAdminContactWebRequestBean extends BaseJobsController implement } // Create new contact instance - final Contact contact = this.createContactInstance(); + final Contact createdContact = this.createContactInstance(); // Default is not same contact - if (this.isSameContactFound(contact)) { + if (this.contactListController.isContactFound(createdContact)) { // Already registered - throw new FaceletException(new ContactAlreadyAddedException(contact)); + throw new FacesException(new ContactAlreadyAddedException(createdContact)); } // Init contact @@ -284,10 +303,10 @@ public class JobsAdminContactWebRequestBean extends BaseJobsController implement // Try to call EJB try { // Call EJB - updatedContact = this.adminContactBean.addContact(contact); + updatedContact = this.adminContactBean.addContact(createdContact); } catch (final ContactAlreadyAddedException ex) { // Throw again - throw new FaceletException(ex); + throw new FacesException(ex); } // Fire event @@ -295,9 +314,6 @@ public class JobsAdminContactWebRequestBean extends BaseJobsController implement // Clear this bean this.clear(); - - // Return outcome - return "admin_list_contact"; //NOI18N } /** @@ -316,58 +332,61 @@ public class JobsAdminContactWebRequestBean extends BaseJobsController implement throw new NullPointerException("event is null"); //NOI18N } else if (event.getCreatedContact() == null) { // Throw NPE again - throw new NullPointerException("event.createdContact is null"); //NOI18N //NOI18N + throw new NullPointerException("event.createdContact is null"); //NOI18N } else if (event.getCreatedContact().getContactId() == null) { // Throw NPE again - throw new NullPointerException("event.createdContact.contactId is null"); //NOI18N //NOI18N + throw new NullPointerException("event.createdContact.contactId is null"); //NOI18N } else if (event.getCreatedContact().getContactId() < 1) { // Not valid throw new IllegalStateException(MessageFormat.format("event.createdContact.contactId={0} is not valid.", event.getCreatedContact().getContactId())); //NOI18N } + // Set contact for e.g. delete method + this.setContact(event.getCreatedContact()); + // Get contact instance from event - final Contact contact = event.getCreatedContact(); + final Contact createdContact = event.getCreatedContact(); // Set all fields: contact - this.setContactId(contact.getContactId()); - this.setAcademicTitle(contact.getContactTitle()); - this.setBirthday(contact.getContactBirthday()); - this.setCity(contact.getContactCity()); - this.setComment(contact.getContactComment()); - this.setContactCountry(contact.getContactCountry()); - this.setEmailAddress(contact.getContactEmailAddress()); - this.setFamilyName(contact.getContactFamilyName()); - this.setFirstName(contact.getContactFirstName()); - this.setPersonalTitle(contact.getContactPersonalTitle()); - this.setHouseNumber(contact.getContactHouseNumber()); - this.setHouseNumberExtension(contact.getContactHouseNumberExtension()); - this.setStreet(contact.getContactStreet()); - this.setZipCode(contact.getContactZipCode()); + this.setContactId(createdContact.getContactId()); + this.setAcademicTitle(createdContact.getContactTitle()); + this.setBirthday(createdContact.getContactBirthday()); + this.setCity(createdContact.getContactCity()); + this.setComment(createdContact.getContactComment()); + this.setContactCountry(createdContact.getContactCountry()); + this.setEmailAddress(createdContact.getContactEmailAddress()); + this.setFamilyName(createdContact.getContactFamilyName()); + this.setFirstName(createdContact.getContactFirstName()); + this.setPersonalTitle(createdContact.getContactPersonalTitle()); + this.setHouseNumber(createdContact.getContactHouseNumber()); + this.setHouseNumberExtension(createdContact.getContactHouseNumberExtension()); + this.setStreet(createdContact.getContactStreet()); + this.setZipCode(createdContact.getContactZipCode()); // Is the cell phone set? - if (contact.getContactMobileNumber() instanceof DialableMobileNumber) { + if (createdContact.getContactMobileNumber() instanceof DialableMobileNumber) { // ... cmobile data - this.setMobileId(contact.getContactMobileNumber().getPhoneId()); - this.setMobileProvider(contact.getContactMobileNumber().getMobileProvider()); - this.setMobileNumber(contact.getContactMobileNumber().getPhoneNumber()); + this.setMobileId(createdContact.getContactMobileNumber().getMobileId()); + this.setMobileProvider(createdContact.getContactMobileNumber().getMobileProvider()); + this.setMobileNumber(createdContact.getContactMobileNumber().getMobileNumber()); } // Is the fax set? - if (contact.getContactFaxNumber() instanceof DialableFaxNumber) { + if (createdContact.getContactFaxNumber() instanceof DialableFaxNumber) { // ... fax data - this.setFaxId(contact.getContactFaxNumber().getPhoneId()); - this.setFaxAreaCode(contact.getContactFaxNumber().getPhoneAreaCode()); - this.setFaxCountry(contact.getContactFaxNumber().getPhoneCountry()); - this.setFaxNumber(contact.getContactFaxNumber().getPhoneNumber()); + this.setFaxId(createdContact.getContactFaxNumber().getPhoneId()); + this.setFaxAreaCode(createdContact.getContactFaxNumber().getPhoneAreaCode()); + this.setFaxCountry(createdContact.getContactFaxNumber().getPhoneCountry()); + this.setFaxNumber(createdContact.getContactFaxNumber().getPhoneNumber()); } // Is the land-line number set? - if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) { + if (createdContact.getContactLandLineNumber() instanceof DialableLandLineNumber) { // .. land-line data - this.setLandLineId(contact.getContactLandLineNumber().getPhoneId()); - this.setLandLineAreaCode(contact.getContactLandLineNumber().getPhoneAreaCode()); - this.setLandLineCountry(contact.getContactLandLineNumber().getPhoneCountry()); - this.setLandLineNumber(contact.getContactLandLineNumber().getPhoneNumber()); + this.setLandLineId(createdContact.getContactLandLineNumber().getPhoneId()); + this.setLandLineAreaCode(createdContact.getContactLandLineNumber().getPhoneAreaCode()); + this.setLandLineCountry(createdContact.getContactLandLineNumber().getPhoneCountry()); + this.setLandLineNumber(createdContact.getContactLandLineNumber().getPhoneNumber()); } // Log message @@ -395,33 +414,49 @@ public class JobsAdminContactWebRequestBean extends BaseJobsController implement } // Generate phone number - final DialableLandLineNumber landLine = new LandLineNumber(this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber()); - final DialableMobileNumber mobile = new MobileNumber(this.getMobileProvider(), this.getMobileNumber()); - final DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber()); + DialableLandLineNumber landLine = null; + DialableMobileNumber mobile = null; + DialableFaxNumber fax = null; + + // Are all fields set? + if (this.getLandLineAreaCode() != null && this.getLandLineCountry() instanceof Country && this.getLandLineNumber() != null) { + // Init instance + landLine = new LandLineNumber(this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber()); + } - // Create new instance - final Contact contact = new UserContact(this.getPersonalTitle(), this.getFirstName(), this.getFamilyName()); + // Are all fields set? + if (this.getMobileProvider() instanceof MobileProvider && this.getMobileNumber() != null) { + // Initialize instance + mobile = new MobileNumber(this.getMobileProvider(), this.getMobileNumber()); + } - // Is contact id set? - if (this.getContactId() instanceof Long) { - // Set it, too - contact.setContactId(this.getContactId()); + // Are all fields set? + if (this.getFaxAreaCode() != null && this.getFaxCountry() instanceof Country && this.getFaxNumber() != null) { + // Initialize instance + fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber()); } + // Create new instance + final Contact localContact = new UserContact( + this.getPersonalTitle(), + this.getFirstName(), + this.getFamilyName() + ); + // Add all others - contact.setContactTitle(this.getAcademicTitle()); - contact.setContactBirthday(this.getBirthday()); - contact.setContactStreet(this.getStreet()); - contact.setContactHouseNumber(this.getHouseNumber()); - contact.setContactZipCode(this.getZipCode()); - contact.setContactCity(this.getCity()); - contact.setContactCountry(this.getContactCountry()); - contact.setContactEmailAddress(this.getEmailAddress()); - contact.setContactBirthday(this.getBirthday()); - contact.setContactComment(this.getComment()); + localContact.setContactBirthday(this.getBirthday()); + localContact.setContactCity(this.getCity()); + localContact.setContactComment(this.getComment()); + localContact.setContactCountry(this.getContactCountry()); + localContact.setContactEmailAddress(this.getEmailAddress()); + localContact.setContactHouseNumber(this.getHouseNumber()); + localContact.setContactId(this.getContactId()); + localContact.setContactStreet(this.getStreet()); + localContact.setContactTitle(this.getAcademicTitle()); + localContact.setContactZipCode(this.getZipCode()); // Set ownContact - contact.setContactOwnContact(Boolean.TRUE); + localContact.setContactOwnContact(Boolean.TRUE); // Don't set null or wrong references if ((landLine instanceof DialableLandLineNumber) && (landLine.getPhoneCountry() instanceof Country) && (this.getLandLineAreaCode() != null) && (this.getLandLineNumber() != null) && (this.getLandLineAreaCode() > 0) && (this.getLandLineNumber() > 0)) { @@ -441,7 +476,7 @@ public class JobsAdminContactWebRequestBean extends BaseJobsController implement } // Set phone number - contact.setContactLandLineNumber(landLine); + localContact.setContactLandLineNumber(landLine); } // Don't set null or wrong references @@ -462,63 +497,99 @@ public class JobsAdminContactWebRequestBean extends BaseJobsController implement } // Set fax number - contact.setContactFaxNumber(fax); + localContact.setContactFaxNumber(fax); } // Is the provider set? if ((mobile instanceof DialableMobileNumber) && (this.getMobileProvider() instanceof MobileProvider) && (this.getMobileNumber() != null) && (this.getMobileNumber() > 0)) { // Is the number set? - if (mobile.getPhoneNumber() == null) { + if (mobile.getMobileNumber() == null) { // Is null throw new NullPointerException("cmobile.phoneNumber is null"); //NOI18N - } else if (mobile.getPhoneNumber() < 1) { + } else if (mobile.getMobileNumber() < 1) { // Abort here throw new IllegalArgumentException("cmobile.phoneNumber is zero or below."); //NOI18N } // Set cmobile number - contact.setContactMobileNumber(mobile); + localContact.setContactMobileNumber(mobile); } // Return it - return contact; + return localContact; + } + + /** + * Deletes currently chosen contact and returns to list view + * + * @return + */ + public String deleteContactData () { + // Is contact set? + if (this.getContact() == null) { + // Throw NPE + throw new NullPointerException("this.contact is null"); + } else if (this.getContact().getContactId() == null) { + // Throw NPE again + throw new NullPointerException("this.contact.contactId is null"); + } else if (this.getContact().getContactId() < 1) { + // Throw IAE + throw new NullPointerException(MessageFormat.format("this.contact.contactId={0} is not valid.", this.getContact().getContactId())); + } + + try { + // Invoke EJB + this.adminContactBean.deleteContactData(this.getContact()); + } catch (final ContactNotFoundException ex) { + // Throw it again + throw new FacesException(ex); + } + + // Fire event + this.deletedContactEvent.fire(new AdminDeletedContactEvent(this.getContact())); + + // Return to list view + return "admin_list_contacts"; } /** * Edits currently loaded contact's data in database. - *

- * @return Redirect outcome */ - public String editContactData () { + public void editContactData () { // Get contact instance - final Contact contact = this.createContactInstance(); + final Contact createdContact = this.createContactInstance(); // Check if contact instance is in helper and valid - if (null == contact) { + if (null == createdContact) { // Throw NPE throw new NullPointerException("beanHelper.contact is null"); //NOI18N - } else if (contact.getContactId() == null) { + } else if (createdContact.getContactId() == null) { // Throw NPE again - throw new NullPointerException("beanHelper.contact.contactId is null"); //NOI18N //NOI18N - } else if (contact.getContactId() < 1) { + throw new NullPointerException("beanHelper.contact.contactId is null"); //NOI18N + } else if (createdContact.getContactId() < 1) { // Invalid id - throw new IllegalStateException(MessageFormat.format("beanHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N + throw new IllegalStateException(MessageFormat.format("beanHelper.contact.contactId={0} is invalid", createdContact.getContactId())); //NOI18N } // Update all data in contact - this.updateContactData(contact); + this.updateContactData(createdContact); - // Call EJB for updating contact data - final Contact updatedContact = this.contactBean.updateContactData(contact, this.isMobileNumberUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked); + // Init updated contact instance + final Contact updatedContact; + + try { + // Call EJB for updating contact data + updatedContact = this.contactBean.updateContactData(createdContact, this.isMobileNumberUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked); + } catch (final ContactNotFoundException ex) { + // Throw as a cause + throw new FacesException(ex); + } // Fire event this.updatedContactEvent.fire(new AdminUpdatedContactEvent(updatedContact)); // Clear bean this.clear(); - - // Return to contact list (for now) - return "admin_list_contact"; //NOI18N } /** @@ -540,7 +611,7 @@ public class JobsAdminContactWebRequestBean extends BaseJobsController implement "%s%d%d", //NOI18N mobileNumber.getMobileProvider().getProviderCountry().getCountryExternalDialPrefix(), mobileNumber.getMobileProvider().getProviderDialPrefix(), - mobileNumber.getPhoneNumber() + mobileNumber.getMobileNumber() ); // Return it @@ -648,6 +719,24 @@ public class JobsAdminContactWebRequestBean extends BaseJobsController implement this.comment = comment; } + /** + * Getter for contact instance + *

+ * @return Contact instance + */ + public Contact getContact () { + return this.contact; + } + + /** + * Setter for contact instance + *

+ * @param contact Contact instance + */ + public void setContact (final Contact contact) { + this.contact = contact; + } + /** * Getter for contactCountry instance *

@@ -689,12 +778,6 @@ public class JobsAdminContactWebRequestBean extends BaseJobsController implement return "admin"; //NOI18N } - @Override - @Deprecated - public void setControllerType (final String controllerType) { - throw new UnsupportedOperationException("Setting controller type is not supported."); //NOI18N - } - /** * Getter for email address *

@@ -1069,6 +1152,8 @@ public class JobsAdminContactWebRequestBean extends BaseJobsController implement */ private void clear () { // Clear all data + this.setContact(null); + // - personal data this.setAcademicTitle(null); this.setFirstName(null); @@ -1096,37 +1181,6 @@ public class JobsAdminContactWebRequestBean extends BaseJobsController implement this.setComment(null); } - /** - * Checks whether the given contact is found - *

- * @param contact Contact instance - * - * @return Whether contact has been found - */ - private boolean isSameContactFound (final Contact contact) { - // Default is not found - boolean IsFound = false; - - // Get iterator - final Iterator iterator = this.contactController.allContacts().iterator(); - - // Loop through all - while (iterator.hasNext()) { - // Get next contact - final 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. *

@@ -1139,7 +1193,7 @@ public class JobsAdminContactWebRequestBean extends BaseJobsController implement throw new NullPointerException("contact is null"); //NOI18N } else if (contact.getContactId() == null) { // Throw NPE again - throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N + throw new NullPointerException("contact.contactId is null"); //NOI18N } else if (contact.getContactId() < 1) { // Invalid id throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is invalid", contact.getContactId())); //NOI18N @@ -1158,13 +1212,13 @@ public class JobsAdminContactWebRequestBean extends BaseJobsController implement contact.setContactCountry(this.getContactCountry()); // Update contact's cmobile number - this.isMobileNumberUnlinked = ContactUtils.updateMobileNumber(contact, this.getMobileProvider(), this.getMobileNumber()); + this.isMobileNumberUnlinked = Contacts.updateMobileNumber(contact, this.getMobileProvider(), this.getMobileNumber()); // Update contact's land-line number - this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber()); + this.isLandLineUnlinked = Contacts.updateLandLineNumber(contact, this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber()); // Update contact's fax number - this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber()); + this.isFaxUnlinked = Contacts.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber()); } }