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