]> git.mxchange.org Git - pizzaservice-mailer-ejb.git/blob - src/java/org/mxchange/jphone/phonenumbers/phone/AddressbookPhoneSessionBean.java
96ca2e335ec20b2530f6c7c10c947c46b7281ed3
[pizzaservice-mailer-ejb.git] / src / java / org / mxchange / jphone / phonenumbers / phone / AddressbookPhoneSessionBean.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.jcoreee.database.BaseDatabaseBean;
24 import org.mxchange.jphone.exceptions.PhoneEntityNotFoundException;
25 import org.mxchange.jphone.phonenumbers.cellphone.CellphoneNumber;
26 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
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 AddressbookPhoneSessionBean extends BaseDatabaseBean 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 AddressbookPhoneSessionBean () {
45         }
46
47         @Override
48         public DialableCellphoneNumber findCellphoneById (final Long cellphoneId) throws PhoneEntityNotFoundException {
49                 // The id number should be valid
50                 if (null == cellphoneId) {
51                         // Throw NPE
52                         throw new NullPointerException("cellphoneId is null"); //NOI18N
53                 } else if (cellphoneId < 1) {
54                         // Not valid
55                         throw new IllegalArgumentException(MessageFormat.format("cellphoneId={0} is not valid.", cellphoneId)); //NOI18N
56                 }
57
58                 // Now find it
59                 Query query = this.getEntityManager().createNamedQuery("SearchCellphoneId", CellphoneNumber.class); //NOI18N
60
61                 // Set parameter
62                 query.setParameter("cellphoneId", cellphoneId); //NOI18N
63
64                 // Init instance
65                 DialableCellphoneNumber cellphone = null;
66
67                 // Try to get a result
68                 try {
69                         // Get a single result
70                         cellphone = (DialableCellphoneNumber) query.getSingleResult();
71                 } catch (NoResultException ex) {
72                         // The entry was not found, so throw it again
73                         throw new PhoneEntityNotFoundException(cellphoneId, ex);
74                 }
75
76                 // Return found instance
77                 return cellphone;
78         }
79
80 }