package org.mxchange.jcontacts.contact;
import java.text.MessageFormat;
+import java.util.Iterator;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.NoResultException;
import javax.persistence.Query;
import org.mxchange.jcontacts.exceptions.ContactNotFoundException;
import org.mxchange.pizzaaplication.database.BasePizzaDatabaseBean;;
+import org.mxchange.jcontacts.contact.utils.ContactUtils;
/**
* A contact EJB
return emailAddresses;
}
+ @Override
+ public boolean isContactFound (final Contact contact) {
+ // Log trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("isContactFound: contact={0} - CALLED!", contact)); //NOI18N
+
+ // Parameter should be valid
+ if (null == contact) {
+ // Throw NPE
+ throw new NullPointerException("contact is null"); //NOI18N
+ } else if (contact.getContactId() > 0) {
+ try {
+ // Id set, ask other method
+ return (this.findContactById(contact.getContactId()) instanceof Contact);
+ } catch (final ContactNotFoundException ex) {
+ // Not found, should not happen
+ throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is set, but not found.", contact.getContactId()), ex); //NOI18N
+ }
+ }
+
+ // Default is not found
+ boolean isFound = false;
+
+ // Get whole list
+ List<Contact> contacts = this.getAllContacts();
+
+ // Is the list empty?
+ if (contacts.isEmpty()) {
+ // Then abort here
+ this.getLoggerBeanLocal().logTrace("isContactFound: No contacts registered, returning 'false' ..."); //NOI18N
+ return false;
+ }
+
+ // Get iterator
+ Iterator<Contact> iterator = contacts.iterator();
+
+ // Loop through all
+ while (iterator.hasNext()) {
+ // Get contact
+ Contact next = iterator.next();
+
+ // Is same contact?
+ if (ContactUtils.isSameContact(contact, next)) {
+ // Found it
+ isFound = true;
+ break;
+ }
+ }
+
+ // Return status
+ return isFound;
+ }
+
@Override
public Contact updateContactData (final Contact contact, final boolean isCellphoneUnlinked, final boolean isLandlineUnlinked, final boolean isFaxUnlinked) {
- throw new UnsupportedOperationException("Not supported yet."); //NOI18N
+ // Log trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateContactData: contact={0},isCellphoneUnlinked={1},isLandlineUnlinked={2},isFaxUnlinked={3} - CALLED!", contact, isCellphoneUnlinked, isLandlineUnlinked, isFaxUnlinked)); //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 updated timestamp
+ this.setAllContactPhoneEntriesUpdated(contact, isCellphoneUnlinked, isLandlineUnlinked, isFaxUnlinked);
+
+ // Merge cellphone, land-line and fix
+ Contact detachedContact = this.mergeContactData(contact);
+
+ // Trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateContactData: detachedContact={0} - EXIT!", detachedContact)); //NOI18N
+
+ // Return it
+ return detachedContact;
+ }
+
+ @Override
+ public Contact updateContactData (final Contact contact) {
+ // Log trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateContactData: 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
+ }
+
+ // Is cell phone/land-line/fax number unlinked?
+ boolean isCellphoneUnlinked = (contact.getContactCellphoneNumber() == null);
+ boolean isLandLineUnlinked = (contact.getContactLandLineNumber() == null);
+ boolean isFaxUnlinked = (contact.getContactFaxNumber() == null);
+
+ // Call other Method
+ Contact detachedContact = this.updateContactData(contact, isCellphoneUnlinked, isLandLineUnlinked, isFaxUnlinked);
+
+ // Trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateContactData: detachedContact={0} - EXIT!", detachedContact)); //NOI18N
+
+ // Return it
+ return detachedContact;
}
}
package org.mxchange.pizzaapplication.model.customer;
import java.text.MessageFormat;
+import java.util.GregorianCalendar;
import java.util.List;
+import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.NoResultException;
import javax.persistence.Query;
+import org.mxchange.jcontacts.contact.Contact;
+import org.mxchange.jcontacts.contact.ContactSessionBeanRemote;
import org.mxchange.jcustomercore.model.customer.Customer;
import org.mxchange.jcustomercore.utils.CustomerUtils;
import org.mxchange.pizzaaplication.database.BasePizzaDatabaseBean;
*/
private static final long serialVersionUID = 19_845_893_648_175_427L;
+ /**
+ * Contact instance
+ */
+ @EJB
+ private ContactSessionBeanRemote customerBean;
+
+ @Override
+ public Customer addCustomer (final Customer customer) {
+ // Trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("addCustomer: customer={0} - CALLED!", customer)); //NOI18N
+
+ // Paramerter customer should be valid
+ if (null == customer) {
+ // Throw NPE
+ throw new NullPointerException("customer is null"); //NOI18N
+ } else if (customer.getCustomerId() > 0) {
+ // Not allowed
+ throw new IllegalArgumentException(MessageFormat.format("customer.customerId={0} is not allowed here.", customer.getCustomerId())); //NOI18N
+ } else if (customer.getCustomerContact() == null) {
+ // Throw NPE again
+ throw new NullPointerException("customer.customerContact is null"); //NOI18N
+ } else if (customer.getCustomerNumber() == null) {
+ // Customer numbers should be set, at least generate one with CustomerUtils
+ throw new NullPointerException("customer.customerNumber is null"); //NOI18N
+ } else if (customer.getCustomerNumber().length() < RateCalcAdminCustomerSessionBeanRemote.CUSTOMER_NUMBER_LENGTH) {
+ // To short number
+ throw new IllegalArgumentException(MessageFormat.format("customer.customerNumber.length={0} is shorter than expected: {1}", customer.getCustomerNumber().length(), RateCalcAdminCustomerSessionBeanRemote.CUSTOMER_NUMBER_LENGTH)); //NOI18N
+ }
+
+ // Get contact instance
+ Contact contact = customer.getCustomerContact();
+ Contact updatedContact = null;
+
+ // Is a customer found?
+ if (this.customerBean.isContactFound(contact)) {
+ // Yes, then get updated version
+ updatedContact = this.customerBean.updateContactData(contact);
+
+ // Remove it from customer so it won't get persisted again
+ customer.setCustomerContact(null);
+ }
+
+ // Set created timestamp
+ customer.setCustomerCreated(new GregorianCalendar());
+
+ // Persist the customer
+ this.getEntityManager().persist(customer);
+
+ // Flush it to get id number set
+ this.getEntityManager().flush();
+
+ // Has the contact being updated?
+ if (updatedContact instanceof Contact) {
+ // Set it again
+ customer.setCustomerContact(updatedContact);
+ }
+
+ // Trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("addCustomer: customer.customerId={0} - EXIT!", customer.getCustomerId())); //NOI18N
+
+ // Return updated instance
+ return customer;
+ }
+
@Override
@SuppressWarnings ("unchecked")
public List<Customer> allCustomers () {
@Override
public String createCustomerNumber () {
+ // Trace message
+ this.getLoggerBeanLocal().logTrace("createCustomerNumber: CALLED!"); //NOI18N
+
// Init named query
Query query = this.getEntityManager().createNamedQuery("SearchCustomerByNumber", Customer.class); //NOI18N
// Create new number
String cn = CustomerUtils.generateCustomerNumber(PizzaAdminCustomerSessionBeanRemote.CUSTOMER_NUMBER_LENGTH, PizzaAdminCustomerSessionBeanRemote.CUSTOMER_NUMBER_BLOCK_SIZE, PizzaAdminCustomerSessionBeanRemote.CUSTOMER_NUMBER_SEPARATOR);
+ // Debug message#
+ this.getLoggerBeanLocal().logDebug(MessageFormat.format("createCustomerNumber: cn={0}", cn)); //NOI18N
+
// Set the generated number as param
query.setParameter("customerNumber", cn); //NOI18N
try {
// Get a single result
Customer customer = (Customer) query.getSingleResult();
+
+ // Debug message
+ this.getLoggerBeanLocal().logDebug(MessageFormat.format("createCustomerNumber: Found customer={0} with customerNumber={1} - continuing ...", customer, customerNumber)); //NOI18N
} catch (final NoResultException ex) {
+ // Debug message
+ this.getLoggerBeanLocal().logDebug(MessageFormat.format("createCustomerNumber: Found free number {0}, exception message:{1}", cn, ex.getMessage())); //NOI18N
+
// Not found, okay
customerNumber = cn;
}
}
+ // Trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("createCustomerNumber: customerNumber={0} - EXIT!", customerNumber)); //NOI18N
+
// Return generated number
return customerNumber;
}