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