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