]> git.mxchange.org Git - pizzaservice-ejb.git/blob - src/java/org/mxchange/pizzaapplication/model/customer/PizzaCustomerSessionBean.java
Implemented business method isReqistered()
[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.model.customer.Customer;
25 import org.mxchange.pizzaaplication.database.BasePizzaDatabaseBean;
26
27 /**
28  * A stateless customer session bean (EJB)
29  * <p>
30  * @author Roland Haeder<rhaeder@cho-time.de>
31  */
32 @Stateless (name = "customer", description = "A bean handling customer data")
33 public class PizzaCustomerSessionBean extends BasePizzaDatabaseBean implements PizzaCustomerSessionBeanRemote {
34
35         /**
36          * Serial number
37          */
38         private static final long serialVersionUID = 15_624_759_104_372L;
39
40         @Override
41         public Customer fillCustomerData (final Customer customer) {
42                 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. //NOI18N
43         }
44
45         @Override
46         public boolean isReqistered (final Customer customer) {
47                 // Trace message
48                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isReqistered: customer={0} - CALLED!", customer)); //NOI18N
49
50                 // Paramerter customer should be valid
51                 if (null == customer) {
52                         // Throw NPE
53                         throw new NullPointerException("customer is null"); //NOI18N
54                 } else if ((customer.getCustomerId() instanceof Long) && (customer.getCustomerId() > 0)) {
55                         // Not allowed
56                         throw new IllegalArgumentException(MessageFormat.format("customer.customerId={0} is not allowed here.", customer.getCustomerId())); //NOI18N
57                 } else if (customer.getCustomerContact() == null) {
58                         // Throw NPE again
59                         throw new NullPointerException("customer.customerContact is null"); //NOI18N
60                 } else if (customer.getCustomerNumber() == null) {
61                         // Customer numbers should be set, at least generate one with CustomerUtils
62                         throw new NullPointerException("customer.customerNumber is null"); //NOI18N
63                 } else if (customer.getCustomerNumber().length() < RateCalcAdminCustomerSessionBeanRemote.CUSTOMER_NUMBER_LENGTH) {
64                         // To short number
65                         throw new IllegalArgumentException(MessageFormat.format("customer.customerNumber.length={0} is shorter than expected: {1}", customer.getCustomerNumber().length(), RateCalcAdminCustomerSessionBeanRemote.CUSTOMER_NUMBER_LENGTH)); //NOI18N
66                 }
67
68                 // Init query instance
69                 Query query = this.getEntityManager().createNamedQuery("SearchCustomerByNumber", Customer.class); //NOI18N
70
71                 // Set parameter
72                 query.setParameter("customerNumber", customer.getCustomerNumber()); //NOI18N
73
74                 // Default is not found
75                 boolean isFound = false;
76
77                 // Try to find it
78                 try {
79                         Customer dummy = (Customer) query.getSingleResult();
80
81                         // Debug message
82                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("isReqistered: Customer {0} has number{1} and contact {2}", dummy.getCustomerId(), customer.getCustomerNumber(), customer.getCustomerContact().getContactId())); //NOI18N
83
84                         // Mark as found
85                         isFound = true;
86                 } catch (final NoResultException ex) {
87                         // Not found
88                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("isReqistered: Not found: {0}", ex.getMessage())); //NOI18N
89                 }
90
91                 // Trace message
92                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isReqistered: isFound={0} - EXIT!", isFound)); //NOI18N
93
94                 // Return it
95                 return isFound;
96         }
97
98         @Override
99         public Customer registerCustomer (final Customer customer) throws CustomerAlreadyRegisteredException {
100                 throw new UnsupportedOperationException("Registration by guests is not supported with Pizza application."); //NOI18N
101         }
102
103 }