2 * Copyright (C) 2016 Roland Haeder
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.
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.
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/>.
17 package org.mxchange.jcontacts.contact;
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;
29 * @author Roland Haeder<roland@mxchange.org>
31 @Stateless (name = "contact", mappedName = "ejb/stateless-pizza-contact", description = "A bean handling contact data")
32 public class PizzaContactSessionBean extends BaseDatabaseBean implements ContactSessionBeanRemote {
37 private static final long serialVersionUID = 542_145_347_916L;
42 public PizzaContactSessionBean () {
46 public Contact findContactById (final Long contactId) throws ContactNotFoundException {
48 this.getLoggerBeanLocal().logTrace(MessageFormat.format("findContactById: contactId={0} - CALLED!", contactId)); //NOI18N
50 // The parameter must be valid
51 if (null == contactId) {
53 throw new NullPointerException("contactId is null"); //NOI18N
54 } else if (contactId < 1) {
56 throw new IllegalArgumentException(MessageFormat.format("contactId={0} is not valid", contactId)); //NOI18N
60 Query query = this.getEntityManager().createNamedQuery("SearchContactById", UserContact.class); //NOI18N
63 query.setParameter("contactId", contactId); //NOI18N
65 // Init contact instance
68 // Try to find a result
70 // Find a single result
71 contact = (Contact) query.getSingleResult();
72 } catch (final NoResultException ex) {
74 throw new ContactNotFoundException(contactId, ex);
78 this.getLoggerBeanLocal().logTrace(MessageFormat.format("findContactById: contact={0} - EXIT!", contact)); //NOI18N
80 // Return found instance