]> git.mxchange.org Git - pizzaservice-ejb.git/blob - src/java/org/mxchange/jphone/phonenumbers/phone/PizzaPhoneSessionBean.java
type-hints to the JPA can also be interfaces, good! :-)
[pizzaservice-ejb.git] / src / java / org / mxchange / jphone / phonenumbers / phone / PizzaPhoneSessionBean.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.jphone.phonenumbers.phone;
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.jphone.exceptions.PhoneEntityNotFoundException;
24 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
25 import org.mxchange.pizzaaplication.database.BasePizzaDatabaseBean;
26
27 /**
28  * A general phone EJB
29  * <p>
30  * @author Roland Haeder<roland@mxchange.org>
31  */
32 @Stateless (name = "phone", mappedName = "ejb/stateless-pizza-phone", description = "A bean handling phone data")
33 public class PizzaPhoneSessionBean extends BasePizzaDatabaseBean implements PhoneSessionBeanRemote {
34
35         /**
36          * Serial number
37          */
38         private static final long serialVersionUID = 134_945_698_127_601L;
39
40         /**
41          * Default constructor
42          */
43         public PizzaPhoneSessionBean () {
44         }
45
46         @Override
47         public DialableCellphoneNumber findCellphoneById (final Long cellphoneId) throws PhoneEntityNotFoundException {
48                 // Trace message
49                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("findCellphoneById: cellphoneId={0} - CALLED!", cellphoneId)); //NOI18N
50
51                 // The id number should be valid
52                 if (null == cellphoneId) {
53                         // Throw NPE
54                         throw new NullPointerException("cellphoneId is null"); //NOI18N
55                 } else if (cellphoneId < 1) {
56                         // Not valid
57                         throw new IllegalArgumentException(MessageFormat.format("cellphoneId={0} is not valid.", cellphoneId)); //NOI18N
58                 }
59
60                 // Now find it
61                 Query query = this.getEntityManager().createNamedQuery("SearchCellphoneId", DialableCellphoneNumber.class); //NOI18N
62
63                 // Set parameter
64                 query.setParameter("cellphoneId", cellphoneId); //NOI18N
65
66                 // Init instance
67                 DialableCellphoneNumber cellphone = null;
68
69                 // Try to get a result
70                 try {
71                         // Get a single result
72                         cellphone = (DialableCellphoneNumber) query.getSingleResult();
73                 } catch (NoResultException ex) {
74                         // The entry was not found, so throw it again
75                         throw new PhoneEntityNotFoundException(cellphoneId, ex);
76                 }
77
78                 // Trace message
79                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("findCellphoneById: cellphone={0} - EXIT!", cellphone)); //NOI18N
80
81                 // Return found instance
82                 return cellphone;
83         }
84
85 }