/*
- * Copyright (C) 2016 - 2020 Free Software Foundation
+ * 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
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;
*/
private String comment;
+ /**
+ * Current contact instance
+ */
+ private Contact contact;
+
/**
* EJB for general contact purposes
*/
@Inject
private JobsContactListWebViewController contactListController;
+ /**
+ * Event being fired when an administrator has deleted a contact
+ */
+ @Any
+ @Inject
+ private Event<ObservableAdminDeletedContactEvent> deletedContactEvent;
+
/**
* Email address
*/
}
// Create new contact instance
- final Contact contact = this.createContactInstance();
+ final Contact createdContact = this.createContactInstance();
// Default is not same contact
- if (this.contactListController.isContactFound(contact)) {
+ if (this.contactListController.isContactFound(createdContact)) {
// Already registered
- throw new FaceletException(new ContactAlreadyAddedException(contact));
+ throw new FacesException(new ContactAlreadyAddedException(createdContact));
}
// Init contact
// 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
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
}
// 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)) {
}
// Set phone number
- contact.setContactLandLineNumber(landLine);
+ localContact.setContactLandLineNumber(landLine);
}
// Don't set null or wrong references
}
// 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";
}
/**
*/
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));
"%s%d%d", //NOI18N
mobileNumber.getMobileProvider().getProviderCountry().getCountryExternalDialPrefix(),
mobileNumber.getMobileProvider().getProviderDialPrefix(),
- mobileNumber.getPhoneNumber()
+ mobileNumber.getMobileNumber()
);
// Return it
this.comment = comment;
}
+ /**
+ * Getter for contact instance
+ * <p>
+ * @return Contact instance
+ */
+ public Contact getContact () {
+ return this.contact;
+ }
+
+ /**
+ * Setter for contact instance
+ * <p>
+ * @param contact Contact instance
+ */
+ public void setContact (final Contact contact) {
+ this.contact = contact;
+ }
+
/**
* Getter for contactCountry instance
* <p>
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
* <p>
*/
private void clear () {
// Clear all data
+ this.setContact(null);
+
// - personal data
this.setAcademicTitle(null);
this.setFirstName(null);
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