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