]> git.mxchange.org Git - pizzaservice-ejb.git/blob - src/java/org/mxchange/pizzaapplication/model/customer/PizzaAdminCustomerSessionBean.java
ed9e57834de05a8380dc23109ed1930dfc9fc227
[pizzaservice-ejb.git] / src / java / org / mxchange / pizzaapplication / model / customer / PizzaAdminCustomerSessionBean.java
1 /*
2  * Copyright (C) 2016 Roland Haeder
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.pizzaapplication.model.customer;
18
19 import java.text.MessageFormat;
20 import java.util.GregorianCalendar;
21 import java.util.List;
22 import javax.ejb.EJB;
23 import javax.ejb.Stateless;
24 import javax.persistence.NoResultException;
25 import javax.persistence.Query;
26 import org.mxchange.jcontacts.contact.Contact;
27 import org.mxchange.jcontacts.contact.ContactSessionBeanRemote;
28 import org.mxchange.jcontacts.exceptions.ContactAlreadyAddedException;
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.database.BasePizzaDatabaseBean;
33
34 /**
35  * A stateless administrative customer session bean (EJB)
36  * <p>
37  * @author Roland Haeder<roland@mxchange.org>
38  */
39 @Stateless (name = "admincustomer", description = "Administrative bean handling customer data")
40 public class PizzaAdminCustomerSessionBean extends BasePizzaDatabaseBean implements PizzaAdminCustomerSessionBeanRemote {
41
42         /**
43          * Serial number
44          */
45         private static final long serialVersionUID = 19_845_893_648_175_427L;
46
47         /**
48          * Contact instance
49          */
50         @EJB
51         private ContactSessionBeanRemote contactBean;
52
53         /**
54          * General customer bean
55          */
56         @EJB
57         private RateCalcCustomerSessionBeanRemote customerBean;
58
59         @Override
60         public Customer addCustomer (final Customer customer) throws CustomerAlreadyRegisteredException, ContactAlreadyAddedException {
61                 // Trace message
62                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("addCustomer: customer={0} - CALLED!", customer)); //NOI18N
63
64                 // Paramerter customer should be valid
65                 if (null == customer) {
66                         // Throw NPE
67                         throw new NullPointerException("customer is null"); //NOI18N
68                 } else if ((customer.getCustomerId() instanceof Long) && (customer.getCustomerId() > 0)) {
69                         // Not allowed
70                         throw new IllegalArgumentException(MessageFormat.format("customer.customerId={0} is not allowed here.", customer.getCustomerId())); //NOI18N
71                 } else if (customer.getCustomerContact() == null) {
72                         // Throw NPE again
73                         throw new NullPointerException("customer.customerContact is null"); //NOI18N
74                 } else if (customer.getCustomerNumber() == null) {
75                         // Customer numbers should be set, at least generate one with CustomerUtils
76                         throw new NullPointerException("customer.customerNumber is null"); //NOI18N
77                 } else if (customer.getCustomerNumber().length() < RateCalcAdminCustomerSessionBeanRemote.CUSTOMER_NUMBER_LENGTH) {
78                         // To short number
79                         throw new IllegalArgumentException(MessageFormat.format("customer.customerNumber.length={0} is shorter than expected: {1}", customer.getCustomerNumber().length(), RateCalcAdminCustomerSessionBeanRemote.CUSTOMER_NUMBER_LENGTH)); //NOI18N
80                 } else if (this.customerBean.isReqistered(customer)) {
81                         // Throw exception
82                         throw new CustomerAlreadyRegisteredException(customer);
83                 }
84
85                 // Get contact instance
86                 Contact contact = customer.getCustomerContact();
87                 Contact updatedContact = this.contactBean.lookupContact(contact);
88
89                 // Debug message
90                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("addCustomer: contact={0},updatedContact={1}", contact, updatedContact));
91
92                 // Is a customer found?
93                 if (updatedContact instanceof Contact) {
94                         // Don't double-create
95                         throw new ContactAlreadyAddedException(contact);
96                 }
97
98                 // Set created timestamp
99                 contact.setContactCreated(new GregorianCalendar());
100
101                 // Set all "created" timestamps
102                 this.setAllContactPhoneEntriesCreated(contact);
103
104                 // Persist the customer
105                 this.getEntityManager().persist(customer);
106
107                 // Flush it to get id number set
108                 this.getEntityManager().flush();
109
110                 // Trace message
111                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("addCustomer: customer.customerId={0} - EXIT!", customer.getCustomerId())); //NOI18N
112
113                 // Return updated instance
114                 return customer;
115         }
116
117         @Override
118         @SuppressWarnings ("unchecked")
119         public List<Customer> allCustomers () {
120                 // Trace message
121                 this.getLoggerBeanLocal().logTrace("allCustomers: CALLED!"); //NOI18N
122
123                 // Get named query
124                 Query query = this.getEntityManager().createNamedQuery("AllCustomers", List.class); //NOI18N
125
126                 // Get result
127                 List<Customer> customers = query.getResultList();
128
129                 // Trace message
130                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allCustomers: customers.size()={0} - EXIT!", customers.size())); //NOI18N
131
132                 // Return full list
133                 return customers;
134         }
135
136         @Override
137         public String createCustomerNumber () {
138                 // Trace message
139                 this.getLoggerBeanLocal().logTrace("createCustomerNumber: CALLED!"); //NOI18N
140
141                 // Init named query
142                 Query query = this.getEntityManager().createNamedQuery("SearchCustomerByNumber", Customer.class); //NOI18N
143
144                 // Default is not found
145                 String customerNumber = null;
146
147                 // Search until a free number was found
148                 while (null == customerNumber) {
149                         // Create new number
150                         String cn = CustomerUtils.generateCustomerNumber(PizzaAdminCustomerSessionBeanRemote.CUSTOMER_NUMBER_LENGTH, PizzaAdminCustomerSessionBeanRemote.CUSTOMER_NUMBER_BLOCK_SIZE, PizzaAdminCustomerSessionBeanRemote.CUSTOMER_NUMBER_SEPARATOR);
151
152                         // Debug message#
153                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("createCustomerNumber: cn={0}", cn)); //NOI18N
154
155                         // Set the generated number as param
156                         query.setParameter("customerNumber", cn); //NOI18N
157
158                         // Try to find it
159                         try {
160                                 // Get a single result
161                                 Customer customer = (Customer) query.getSingleResult();
162
163                                 // Debug message
164                                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("createCustomerNumber: Found customer={0} with customerNumber={1} - continuing ...", customer, customerNumber)); //NOI18N
165                         } catch (final NoResultException ex) {
166                                 // Debug message
167                                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("createCustomerNumber: Found free number {0}, exception message:{1}", cn, ex.getMessage())); //NOI18N
168
169                                 // Not found, okay
170                                 customerNumber = cn;
171                         }
172                 }
173
174                 // Trace message
175                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("createCustomerNumber: customerNumber={0} - EXIT!", customerNumber)); //NOI18N
176
177                 // Return generated number
178                 return customerNumber;
179         }
180
181 }