2 * Copyright (C) 2016 - 2024 Free Software Foundation
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License as
6 * published by the Free Software Foundation, either version 3 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Affero General Public License for more details.
14 * You should have received a copy of the GNU Affero General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package org.mxchange.pizzaapplication.model.customer;
19 import java.text.MessageFormat;
20 import java.util.GregorianCalendar;
21 import java.util.List;
23 import javax.ejb.Stateless;
24 import javax.persistence.NoResultException;
25 import javax.persistence.Query;
26 import org.mxchange.jcontacts.exceptions.ContactAlreadyAddedException;
27 import org.mxchange.jcontacts.model.contact.Contact;
28 import org.mxchange.jcontacts.model.contact.ContactSessionBeanRemote;
29 import org.mxchange.jcustomercore.exceptions.CustomerAlreadyRegisteredException;
30 import org.mxchange.jcustomercore.model.customer.Customer;
31 import org.mxchange.jcustomercore.utils.CustomerUtils;
32 import org.mxchange.pizzaaplication.enterprise.BasePizzaEnterpriseBean;
35 * A stateless administrative customer session-scoped bean (EJB)
37 * @author Roland Häder<roland@mxchange.org>
39 @Stateless (name = "admincustomer", description = "Administrative bean handling customer data")
40 public class PizzaAdminCustomerSessionBean extends BasePizzaEnterpriseBean implements PizzaAdminCustomerSessionBeanRemote {
45 private static final long serialVersionUID = 19_845_893_648_175_427L;
51 private ContactSessionBeanRemote contactBean;
54 * General customer bean
57 private PizzaCustomerSessionBeanRemote customerBean;
62 public PizzaAdminCustomerSessionBean () {
63 // Call super constructor
68 public Customer addCustomer (final Customer customer) throws CustomerAlreadyRegisteredException, ContactAlreadyAddedException {
70 this.getLoggerBeanLocal().logTrace(MessageFormat.format("addCustomer: customer={0} - CALLED!", customer)); //NOI18N
72 // Paramerter customer should be valid
73 if (null == customer) {
75 throw new NullPointerException("customer is null"); //NOI18N
76 } else if ((customer.getCustomerId() instanceof Long) && (customer.getCustomerId() > 0)) {
78 throw new IllegalArgumentException(MessageFormat.format("customer.customerId={0} is not allowed here.", customer.getCustomerId())); //NOI18N
79 } else if (customer.getCustomerContact() == null) {
81 throw new NullPointerException("customer.customerContact is null"); //NOI18N
82 } else if (customer.getCustomerNumber() == null) {
83 // Customer numbers should be set, at least generate one with CustomerUtils
84 throw new NullPointerException("customer.customerNumber is null"); //NOI18N
85 } else if (customer.getCustomerNumber().length() < PizzaAdminCustomerSessionBeanRemote.CUSTOMER_NUMBER_LENGTH) {
87 throw new IllegalArgumentException(MessageFormat.format("customer.customerNumber.length={0} is shorter than expected: {1}", customer.getCustomerNumber().length(), PizzaAdminCustomerSessionBeanRemote.CUSTOMER_NUMBER_LENGTH)); //NOI18N
88 } else if (this.customerBean.isRegistered(customer)) {
90 throw new CustomerAlreadyRegisteredException(customer);
93 // Get contact instance
94 Contact contact = customer.getCustomerContact();
95 Contact updatedContact = this.contactBean.lookupContact(contact);
98 this.getLoggerBeanLocal().logDebug(MessageFormat.format("addCustomer: contact={0},updatedContact={1}", contact, updatedContact));
100 // Is a customer found?
101 if (updatedContact instanceof Contact) {
102 // Don't double-create
103 throw new ContactAlreadyAddedException(contact);
106 // Set created timestamp
107 contact.setContactCreated(new GregorianCalendar());
108 customer.setCustomerCreated(new GregorianCalendar());
110 // Set all "created" timestamps
111 this.setAllContactPhoneEntriesCreated(contact);
113 // Persist the customer
114 this.getEntityManager().persist(customer);
116 // Flush it to get id number set
117 this.getEntityManager().flush();
120 this.getLoggerBeanLocal().logTrace(MessageFormat.format("addCustomer: customer.customerId={0} - EXIT!", customer.getCustomerId())); //NOI18N
122 // Return updated instance
127 @SuppressWarnings ("unchecked")
128 public List<Customer> allCustomers () {
130 this.getLoggerBeanLocal().logTrace("allCustomers: CALLED!"); //NOI18N
133 Query query = this.getEntityManager().createNamedQuery("AllCustomers", List.class); //NOI18N
136 List<Customer> customers = query.getResultList();
139 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allCustomers: customers.size()={0} - EXIT!", customers.size())); //NOI18N
146 public String createCustomerNumber () {
148 this.getLoggerBeanLocal().logTrace("createCustomerNumber: CALLED!"); //NOI18N
151 Query query = this.getEntityManager().createNamedQuery("SearchCustomerByNumber", Customer.class); //NOI18N
153 // Default is not found
154 String customerNumber = null;
156 // Search until a free number was found
157 while (null == customerNumber) {
159 String cn = CustomerUtils.generateCustomerNumber(PizzaAdminCustomerSessionBeanRemote.CUSTOMER_NUMBER_LENGTH, PizzaAdminCustomerSessionBeanRemote.CUSTOMER_NUMBER_BLOCK_SIZE, PizzaAdminCustomerSessionBeanRemote.CUSTOMER_NUMBER_SEPARATOR);
162 this.getLoggerBeanLocal().logDebug(MessageFormat.format("createCustomerNumber: cn={0}", cn)); //NOI18N
164 // Set the generated number as param
165 query.setParameter("customerNumber", cn); //NOI18N
169 // Get a single result
170 Customer customer = (Customer) query.getSingleResult();
173 this.getLoggerBeanLocal().logDebug(MessageFormat.format("createCustomerNumber: Found customer={0} with customerNumber={1} - continuing ...", customer, customerNumber)); //NOI18N
174 } catch (final NoResultException ex) {
176 this.getLoggerBeanLocal().logDebug(MessageFormat.format("createCustomerNumber: Found free number {0}, exception message:{1}", cn, ex.getMessage())); //NOI18N
184 this.getLoggerBeanLocal().logTrace(MessageFormat.format("createCustomerNumber: customerNumber={0} - EXIT!", customerNumber)); //NOI18N
186 // Return generated number
187 return customerNumber;
191 public Customer linkCustomer (final Customer customer) throws CustomerAlreadyRegisteredException {
192 // Parameters must be valid
193 if (null == customer) {
195 throw new NullPointerException("customer is null");
196 } else if (customer.getCustomerContact() == null) {
198 throw new NullPointerException("customer.customerContact is null"); //NOI18N
199 } else if (customer.getCustomerContact().getContactId() == null) {
201 throw new NullPointerException("customer.customerContact.contactId is null"); //NOI18N
202 } else if (customer.getCustomerContact().getContactId() < 1) {
204 throw new NullPointerException(MessageFormat.format("customer.customerContact.contactId={0} is not valid", customer.getCustomerContact().getContactId())); //NOI18N
205 } else if (customer.getCustomerNumber() == null) {
206 // Customer numbers should be set, at least generate one with CustomerUtils
207 throw new NullPointerException("customer.customerNumber is null"); //NOI18N
208 } else if (customer.getCustomerNumber().length() < PizzaAdminCustomerSessionBeanRemote.CUSTOMER_NUMBER_LENGTH) {
210 throw new IllegalArgumentException(MessageFormat.format("customer.customerNumber.length={0} is shorter than expected: {1}", customer.getCustomerNumber().length(), PizzaAdminCustomerSessionBeanRemote.CUSTOMER_NUMBER_LENGTH)); //NOI18N
211 } else if (this.customerBean.isRegistered(customer)) {
213 throw new CustomerAlreadyRegisteredException(customer);
216 // Set created timestamp
217 customer.setCustomerCreated(new GregorianCalendar());
219 // Try to find the contact instance
220 Contact foundContact = this.getEntityManager().find(customer.getCustomerContact().getClass(), customer.getCustomerContact().getContactId());
223 Contact detachedContact = this.getEntityManager().merge(foundContact);
225 // Set it in customer
226 customer.setCustomerContact(detachedContact);
229 this.getEntityManager().persist(customer);
231 // Flush it to get id
232 this.getEntityManager().flush();
235 this.getLoggerBeanLocal().logTrace(MessageFormat.format("linkUser: customer.customerId={0} - EXIT!", customer.getCustomerId())); //NOI18N
237 // Return updated instance