]> git.mxchange.org Git - pizzaservice-ejb.git/blob - src/java/org/mxchange/jcontacts/contact/PizzaContactSessionBean.java
8d6ce0739a21c12ed473e025a615d0741dde72ca
[pizzaservice-ejb.git] / src / java / org / mxchange / jcontacts / contact / PizzaContactSessionBean.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.jcontacts.contact;
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.jcontacts.exceptions.ContactNotFoundException;
24 import org.mxchange.jcoreee.database.BaseDatabaseBean;
25
26 /**
27  * A contact EJB
28  * <p>
29  * @author Roland Haeder<roland@mxchange.org>
30  */
31 @Stateless (name = "contact", mappedName = "ejb/stateless-pizza-contact", description = "A bean handling contact data")
32 public class PizzaContactSessionBean extends BaseDatabaseBean implements ContactSessionBeanRemote {
33
34         /**
35          * Serial number
36          */
37         private static final long serialVersionUID = 542_145_347_916L;
38
39         /**
40          * Default constructor
41          */
42         public PizzaContactSessionBean () {
43         }
44
45         @Override
46         public Contact findContactById (final Long contactId) throws ContactNotFoundException {
47                 // Trace message
48                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("findContactById: contactId={0} - CALLED!", contactId)); //NOI18N
49
50                 // The parameter must be valid
51                 if (null == contactId) {
52                         // Throw NPE
53                         throw new NullPointerException("contactId is null"); //NOI18N
54                 } else if (contactId < 1) {
55                         // Not valid
56                         throw new IllegalArgumentException(MessageFormat.format("contactId={0} is not valid", contactId)); //NOI18N
57                 }
58
59                 // Get query instance
60                 Query query = this.getEntityManager().createNamedQuery("SearchContactId", UserContact.class); //NOI18N
61
62                 // Set parameter
63                 query.setParameter("contactId", contactId); //NOI18N
64
65                 // Init contact instance
66                 Contact contact;
67
68                 // Try to find a result
69                 try {
70                         // Find a single result
71                         contact = (Contact) query.getSingleResult();
72                 } catch (final NoResultException ex) {
73                         // No result found
74                         throw new ContactNotFoundException(contactId, ex);
75                 }
76
77                 // Trace message
78                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("findContactById: contact={0} - EXIT!", contact)); //NOI18N
79
80                 // Return found instance
81                 return contact;
82         }
83
84 }