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