]> git.mxchange.org Git - pizzaservice-ejb.git/blob - src/java/org/mxchange/pizzaapplication/model/customer/PizzaCustomerSessionBean.java
implemented business method findCustomerById()
[pizzaservice-ejb.git] / src / java / org / mxchange / pizzaapplication / model / customer / PizzaCustomerSessionBean.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 javax.ejb.Stateless;
21 import javax.persistence.NoResultException;
22 import javax.persistence.Query;
23 import org.mxchange.jcustomercore.exceptions.CustomerAlreadyRegisteredException;
24 import org.mxchange.jcustomercore.exceptions.CustomerNotFoundException;
25 import org.mxchange.jcustomercore.model.customer.Customer;
26 import org.mxchange.pizzaaplication.database.BasePizzaDatabaseBean;
27
28 /**
29  * A stateless customer session bean (EJB)
30  * <p>
31  * @author Roland Haeder<rhaeder@cho-time.de>
32  */
33 @Stateless (name = "customer", description = "A bean handling customer data")
34 public class PizzaCustomerSessionBean extends BasePizzaDatabaseBean implements PizzaCustomerSessionBeanRemote {
35
36         /**
37          * Serial number
38          */
39         private static final long serialVersionUID = 15_624_759_104_372L;
40
41         @Override
42         public Customer fillCustomerData (final Customer customer) {
43                 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. //NOI18N
44         }
45
46         @Override
47         public Customer findCustomerById (final Long customerId) throws CustomerNotFoundException {
48                 // Trace message
49                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("findCustomerById: customerId={0} - CALLED!", customerId)); //NOI18N
50
51                 // The parameter must be valid
52                 if (null == customerId) {
53                         // Throw NPE
54                         throw new NullPointerException("customerId is null"); //NOI18N
55                 } else if (customerId < 1) {
56                         // Invalid id number
57                         throw new IllegalArgumentException(MessageFormat.format("customerId={0} - is not valid", customerId)); //NOI18N
58                 }
59
60                 // Init instance
61                 Customer customer;
62
63                 // Get named query
64                 Query query = this.getEntityManager().createNamedQuery("SearchCustomerById", Customer.class); //NOI18N
65
66                 // Set customer id
67                 query.setParameter("customerId", customerId); //NOI18N
68
69                 // Try to find it
70                 try {
71                         // Get single result
72                         customer = (Customer) query.getSingleResult();
73
74                         // Debug message (success)
75                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("findCustomerById: Found customer={0} with customerId={1}", customer, customerId)); //NOI18N
76                 } catch (final NoResultException ex) {
77                         // Not found, throw again
78                         throw new CustomerNotFoundException(customerId, ex);
79                 }
80
81                 // Trace message
82                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("findCustomerById; customer={0} - EXIT!", customer)); //NOI18N
83
84                 // Return found instance
85                 return customer;
86         }
87
88         @Override
89         public boolean isReqistered (final Customer customer) {
90                 // Trace message
91                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isReqistered: customer={0} - CALLED!", customer)); //NOI18N
92
93                 // Paramerter customer should be valid
94                 if (null == customer) {
95                         // Throw NPE
96                         throw new NullPointerException("customer is null"); //NOI18N
97                 } else if ((customer.getCustomerId() instanceof Long) && (customer.getCustomerId() > 0)) {
98                         // Not allowed
99                         throw new IllegalArgumentException(MessageFormat.format("customer.customerId={0} is not allowed here.", customer.getCustomerId())); //NOI18N
100                 } else if (customer.getCustomerContact() == null) {
101                         // Throw NPE again
102                         throw new NullPointerException("customer.customerContact is null"); //NOI18N
103                 } else if (customer.getCustomerNumber() == null) {
104                         // Customer numbers should be set, at least generate one with CustomerUtils
105                         throw new NullPointerException("customer.customerNumber is null"); //NOI18N
106                 } else if (customer.getCustomerNumber().length() < PizzaAdminCustomerSessionBeanRemote.CUSTOMER_NUMBER_LENGTH) {
107                         // To short number
108                         throw new IllegalArgumentException(MessageFormat.format("customer.customerNumber.length={0} is shorter than expected: {1}", customer.getCustomerNumber().length(), PizzaAdminCustomerSessionBeanRemote.CUSTOMER_NUMBER_LENGTH)); //NOI18N
109                 }
110
111                 // Init query instance
112                 Query query = this.getEntityManager().createNamedQuery("SearchCustomerByNumber", Customer.class); //NOI18N
113
114                 // Set parameter
115                 query.setParameter("customerNumber", customer.getCustomerNumber()); //NOI18N
116
117                 // Default is not found
118                 boolean isFound = false;
119
120                 // Try to find it
121                 try {
122                         Customer dummy = (Customer) query.getSingleResult();
123
124                         // Debug message
125                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("isReqistered: Customer {0} has number{1} and contact {2}", dummy.getCustomerId(), customer.getCustomerNumber(), customer.getCustomerContact().getContactId())); //NOI18N
126
127                         // Mark as found
128                         isFound = true;
129                 } catch (final NoResultException ex) {
130                         // Not found
131                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("isReqistered: Not found: {0}", ex.getMessage())); //NOI18N
132                 }
133
134                 // Trace message
135                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isReqistered: isFound={0} - EXIT!", isFound)); //NOI18N
136
137                 // Return it
138                 return isFound;
139         }
140
141         @Override
142         public Customer registerCustomer (final Customer customer) throws CustomerAlreadyRegisteredException {
143                 throw new UnsupportedOperationException("Registration by guests is not supported with Pizza application."); //NOI18N
144         }
145
146 }