]> git.mxchange.org Git - pizzaservice-mailer-ejb.git/blob - src/java/org/mxchange/jcontacts/contact/AddressbookContactSessionBean.java
9e8a66a3b2e1694f30bf3df1300e06ed646a4992
[pizzaservice-mailer-ejb.git] / src / java / org / mxchange / jcontacts / contact / AddressbookContactSessionBean.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 java.util.List;
21 import javax.ejb.Stateless;
22 import javax.persistence.NoResultException;
23 import javax.persistence.Query;
24 import org.mxchange.jcontacts.exceptions.ContactNotFoundException;
25 import org.mxchange.jcoreee.database.BaseDatabaseBean;
26
27 /**
28  * A contact EJB
29  * <p>
30  * @author Roland Haeder<roland@mxchange.org>
31  */
32 @Stateless (name = "contact", mappedName = "ejb/stateless-addressbook-contact", description = "A bean handling contact data")
33 public class AddressbookContactSessionBean extends BaseDatabaseBean implements ContactSessionBeanRemote {
34
35         /**
36          * Serial number
37          */
38         private static final long serialVersionUID = 542_145_347_916L;
39
40         /**
41          * Default constructor
42          */
43         public AddressbookContactSessionBean () {
44         }
45
46         @Override
47         public Contact findContactById (final Long contactId) throws ContactNotFoundException {
48                 // Log trace message
49                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("findContactById: contactId={0} - CALLED!", contactId)); //NOI18N
50
51                 // The parameter must be valid
52                 if (null == contactId) {
53                         // Throw NPE
54                         throw new NullPointerException("contactId is null"); //NOI18N
55                 } else if (contactId < 1) {
56                         // Not valid
57                         throw new IllegalArgumentException(MessageFormat.format("contactId={0} is not valid", contactId)); //NOI18N
58                 }
59
60                 // Get query instance
61                 Query query = this.getEntityManager().createNamedQuery("SearchContactById", UserContact.class); //NOI18N
62
63                 // Set parameter
64                 query.setParameter("contactId", contactId); //NOI18N
65
66                 // Init contact instance
67                 Contact contact;
68
69                 // Try to find a result
70                 try {
71                         // Find a single result
72                         contact = (Contact) query.getSingleResult();
73
74                         // Log trace message
75                         this.getLoggerBeanLocal().logTrace(MessageFormat.format("findContactById: Found contact={0}", contact)); //NOI18N
76                 } catch (final NoResultException ex) {
77                         // No result found
78                         throw new ContactNotFoundException(contactId, ex);
79                 }
80
81                 // Log trace message
82                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("findContactById: contact={0} - EXIT!", contact)); //NOI18N
83
84                 // Return found instance
85                 return contact;
86         }
87
88         @Override
89         @SuppressWarnings ("unchecked")
90         public List<Contact> getAllContacts () {
91                 // Log trace message
92                 this.getLoggerBeanLocal().logTrace("getAllContacts - CALLED!"); //NOI18N
93
94                 // Create query instance
95                 Query query = this.getEntityManager().createNamedQuery("AllContacts", List.class); //NOI18N
96
97                 // Get list
98                 List<Contact> contacts = query.getResultList();
99
100                 // Log trace message
101                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getAllContacts: contacts.size()={0} - EXIT!", contacts.size())); //NOI18N
102
103                 // Return it
104                 return contacts;
105         }
106
107         @Override
108         @SuppressWarnings ("unchecked")
109         public List<String> getEmailAddressList () {
110                 // Log trace message
111                 this.getLoggerBeanLocal().logTrace("getEmailAddressList - CALLED!"); //NOI18N
112
113                 // Create query instance
114                 Query query = this.getEntityManager().createNamedQuery("AllContactEmailAddresses", List.class); //NOI18N
115
116                 // Get list
117                 List<String> emailAddresses = query.getResultList();
118
119                 // Log trace message
120                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getEmailAddressList: emailAddresses.size()={0} - EXIT!", emailAddresses.size())); //NOI18N
121
122                 // Return it
123                 return emailAddresses;
124         }
125
126         @Override
127         public void updateContactPersonalData (final Contact contact) {
128                 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
129         }
130
131 }