]> git.mxchange.org Git - pizzaservice-ejb.git/blob - src/java/org/mxchange/jcontacts/contact/PizzaContactSessionBean.java
Continued with customer:
[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 java.util.Iterator;
21 import java.util.List;
22 import javax.ejb.Stateless;
23 import javax.persistence.NoResultException;
24 import javax.persistence.Query;
25 import org.mxchange.jcontacts.exceptions.ContactNotFoundException;
26 import org.mxchange.pizzaaplication.database.BasePizzaDatabaseBean;;
27 import org.mxchange.jcontacts.contact.utils.ContactUtils;
28
29 /**
30  * A contact EJB
31  * <p>
32  * @author Roland Haeder<roland@mxchange.org>
33  */
34 @Stateless (name = "contact", mappedName = "ejb/stateless-pizza-contact", description = "A bean handling contact data")
35 public class PizzaContactSessionBean extends BasePizzaDatabaseBean implements ContactSessionBeanRemote {
36
37         /**
38          * Serial number
39          */
40         private static final long serialVersionUID = 542_145_347_916L;
41
42         /**
43          * Default constructor
44          */
45         public PizzaContactSessionBean () {
46         }
47
48         @Override
49         public Contact findContactById (final Long contactId) throws ContactNotFoundException {
50                 // Log trace message
51                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("findContactById: contactId={0} - CALLED!", contactId)); //NOI18N
52
53                 // The parameter must be valid
54                 if (null == contactId) {
55                         // Throw NPE
56                         throw new NullPointerException("contactId is null"); //NOI18N
57                 } else if (contactId < 1) {
58                         // Not valid
59                         throw new IllegalArgumentException(MessageFormat.format("contactId={0} is not valid", contactId)); //NOI18N
60                 }
61
62                 // Get query instance
63                 Query query = this.getEntityManager().createNamedQuery("SearchContactById", Contact.class); //NOI18N
64
65                 // Set parameter
66                 query.setParameter("contactId", contactId); //NOI18N
67
68                 // Init contact instance
69                 Contact contact;
70
71                 // Try to find a result
72                 try {
73                         // Find a single result
74                         contact = (Contact) query.getSingleResult();
75
76                         // Log trace message
77                         this.getLoggerBeanLocal().logTrace(MessageFormat.format("findContactById: Found contact={0}", contact)); //NOI18N
78                 } catch (final NoResultException ex) {
79                         // No result found
80                         throw new ContactNotFoundException(contactId, ex);
81                 }
82
83                 // Log trace message
84                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("findContactById: contact={0} - EXIT!", contact)); //NOI18N
85
86                 // Return found instance
87                 return contact;
88         }
89
90         @Override
91         @SuppressWarnings ("unchecked")
92         public List<Contact> getAllContacts () {
93                 // Log trace message
94                 this.getLoggerBeanLocal().logTrace("getAllContacts - CALLED!"); //NOI18N
95
96                 // Create query instance
97                 Query query = this.getEntityManager().createNamedQuery("AllContacts", List.class); //NOI18N
98
99                 // Get list
100                 List<Contact> contacts = query.getResultList();
101
102                 // Log trace message
103                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getAllContacts: contacts.size()={0} - EXIT!", contacts.size())); //NOI18N
104
105                 // Return it
106                 return contacts;
107         }
108
109         @Override
110         @SuppressWarnings ("unchecked")
111         public List<String> getEmailAddressList () {
112                 // Log trace message
113                 this.getLoggerBeanLocal().logTrace("getEmailAddressList - CALLED!"); //NOI18N
114
115                 // Create query instance
116                 Query query = this.getEntityManager().createNamedQuery("AllContactEmailAddresses", List.class); //NOI18N
117
118                 // Get list
119                 List<String> emailAddresses = query.getResultList();
120
121                 // Log trace message
122                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getEmailAddressList: emailAddresses.size()={0} - EXIT!", emailAddresses.size())); //NOI18N
123
124                 // Return it
125                 return emailAddresses;
126         }
127
128         @Override
129         public boolean isContactFound (final Contact contact) {
130                 // Log trace message
131                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isContactFound: contact={0} - CALLED!", contact)); //NOI18N
132
133                 // Parameter should be valid
134                 if (null == contact) {
135                         // Throw NPE
136                         throw new NullPointerException("contact is null"); //NOI18N
137                 } else if (contact.getContactId() > 0) {
138                         try {
139                                 // Id set, ask other method
140                                 return (this.findContactById(contact.getContactId()) instanceof Contact);
141                         } catch (final ContactNotFoundException ex) {
142                                 // Not found, should not happen
143                                 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is set, but not found.", contact.getContactId()), ex); //NOI18N
144                         }
145                 }
146
147                 // Default is not found
148                 boolean isFound = false;
149
150                 // Get whole list
151                 List<Contact> contacts = this.getAllContacts();
152
153                 // Is the list empty?
154                 if (contacts.isEmpty()) {
155                         // Then abort here
156                         this.getLoggerBeanLocal().logTrace("isContactFound: No contacts registered, returning 'false' ..."); //NOI18N
157                         return false;
158                 }
159
160                 // Get iterator
161                 Iterator<Contact> iterator = contacts.iterator();
162
163                 // Loop through all
164                 while (iterator.hasNext()) {
165                         // Get contact
166                         Contact next = iterator.next();
167
168                         // Is same contact?
169                         if (ContactUtils.isSameContact(contact, next)) {
170                                 // Found it
171                                 isFound = true;
172                                 break;
173                         }
174                 }
175
176                 // Return status
177                 return isFound;
178         }
179
180         @Override
181         public Contact updateContactData (final Contact contact, final boolean isCellphoneUnlinked, final boolean isLandlineUnlinked, final boolean isFaxUnlinked) {
182                 // Log trace message
183                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateContactData: contact={0},isCellphoneUnlinked={1},isLandlineUnlinked={2},isFaxUnlinked={3} - CALLED!", contact, isCellphoneUnlinked, isLandlineUnlinked, isFaxUnlinked)); //NOI18N
184
185                 // The contact instance must be valid
186                 if (null == contact) {
187                         // Throw NPE again
188                         throw new NullPointerException("contact is null"); //NOI18N
189                 } else if (contact.getContactId() == null) {
190                         // Throw NPE again
191                         throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
192                 } else if (contact.getContactId() < 1) {
193                         // Not valid
194                         throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
195                 }
196
197                 // Set updated timestamp
198                 this.setAllContactPhoneEntriesUpdated(contact, isCellphoneUnlinked, isLandlineUnlinked, isFaxUnlinked);
199
200                 // Merge cellphone, land-line and fix
201                 Contact detachedContact = this.mergeContactData(contact);
202
203                 // Trace message
204                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateContactData: detachedContact={0} - EXIT!", detachedContact)); //NOI18N
205
206                 // Return it
207                 return detachedContact;
208         }
209
210         @Override
211         public Contact updateContactData (final Contact contact) {
212                 // Log trace message
213                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateContactData: contact={0} - CALLED!", contact)); //NOI18N
214
215                 // The contact instance must be valid
216                 if (null == contact) {
217                         // Throw NPE again
218                         throw new NullPointerException("contact is null"); //NOI18N
219                 } else if (contact.getContactId() == null) {
220                         // Throw NPE again
221                         throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
222                 } else if (contact.getContactId() < 1) {
223                         // Not valid
224                         throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
225                 }
226
227                 // Is cell phone/land-line/fax number unlinked?
228                 boolean isCellphoneUnlinked = (contact.getContactCellphoneNumber() == null);
229                 boolean isLandLineUnlinked = (contact.getContactLandLineNumber() == null);
230                 boolean isFaxUnlinked = (contact.getContactFaxNumber() == null);
231
232                 // Call other Method
233                 Contact detachedContact = this.updateContactData(contact, isCellphoneUnlinked, isLandLineUnlinked, isFaxUnlinked);
234
235                 // Trace message
236                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateContactData: detachedContact={0} - EXIT!", detachedContact)); //NOI18N
237
238                 // Return it
239                 return detachedContact;
240         }
241
242 }