]> git.mxchange.org Git - jjobs-ejb.git/blob - src/java/org/mxchange/jcontacts/model/contact/JobsContactSessionBean.java
Updated copyright year
[jjobs-ejb.git] / src / java / org / mxchange / jcontacts / model / contact / JobsContactSessionBean.java
1 /*
2  * Copyright (C) 2016 - 2020 Free Software Foundation
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.model.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.jjobs.enterprise.BaseJobsEnterpriseBean;
28
29 /**
30  * A contact EJB
31  * <p>
32  * @author Roland Häder<roland@mxchange.org>
33  */
34 @Stateless (name = "contact", description = "A bean handling contact data")
35 public class JobsContactSessionBean extends BaseJobsEnterpriseBean 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 JobsContactSessionBean () {
46                 // Call super constructor
47                 super();
48         }
49
50         @Override
51         @SuppressWarnings ("unchecked")
52         public List<Contact> allContacts () {
53                 // Log trace message
54                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getAllContacts - CALLED!", this.getClass().getSimpleName())); //NOI18N
55
56                 // Create query instance
57                 final Query query = this.getEntityManager().createNamedQuery("AllContacts"); //NOI18N
58
59                 // Get list
60                 final List<Contact> contacts = query.getResultList();
61
62                 // Log trace message
63                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getAllContacts: contacts.size()={1} - EXIT!", this.getClass().getSimpleName(), contacts.size())); //NOI18N
64
65                 // Return it
66                 return contacts;
67         }
68
69         @Override
70         public Contact lookupContact (final Contact contact) {
71                 // Log trace message
72                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isContactFound: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
73
74                 // Parameter should be valid
75                 if (null == contact) {
76                         // Throw NPE
77                         throw new NullPointerException("contact is null"); //NOI18N
78                 } else if (contact.getContactId() > 0) {
79                         try {
80                                 // Id set, ask other method
81                                 return this.findContactById(contact.getContactId());
82                         } catch (final ContactNotFoundException ex) {
83                                 // Not found, should not happen
84                                 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is set, but not found.", contact.getContactId()), ex); //NOI18N
85                         }
86                 }
87
88                 // Default is not found
89                 Contact foundContact = null;
90
91                 // Get whole list
92                 final List<Contact> contacts = this.allContacts();
93
94                 // Is the list empty?
95                 if (contacts.isEmpty()) {
96                         // Then abort here
97                         this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isContactFound: No contacts registered, returning NULL ...", this.getClass().getSimpleName())); //NOI18N
98                         return null;
99                 }
100
101                 // Get iterator
102                 final Iterator<Contact> iterator = contacts.iterator();
103
104                 // Loop through all
105                 while (iterator.hasNext()) {
106                         // Get contact
107                         final Contact next = iterator.next();
108
109                         // Is same contact?
110                         if ((Objects.equals(contact, next)) || (Contacts.isSameContact(contact, next))) {
111                                 // Debug message
112                                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isContactFound: Found same contact: contactId={1}", this.getClass().getSimpleName(), next.getContactId())); //NOI18N
113
114                                 // Found it
115                                 foundContact = next;
116                                 break;
117                         }
118                 }
119
120                 // Trace message
121                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isContactFound: foundContact={1} - EXIT!", this.getClass().getSimpleName(), foundContact)); //NOI18N
122
123                 // Return found contact
124                 return foundContact;
125         }
126
127         @Override
128         public Contact updateContactData (final Contact contact, final boolean isCellphoneUnlinked, final boolean isLandlineUnlinked, final boolean isFaxUnlinked) {
129                 // Log trace message
130                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: contact={1},isCellphoneUnlinked={2},isLandlineUnlinked={3},isFaxUnlinked={4} - CALLED!", this.getClass().getSimpleName(), contact, isCellphoneUnlinked, isLandlineUnlinked, isFaxUnlinked)); //NOI18N
131
132                 // The contact instance must be valid
133                 if (null == contact) {
134                         // Throw NPE again
135                         throw new NullPointerException("contact is null"); //NOI18N
136                 } else if (contact.getContactId() == null) {
137                         // Throw NPE again
138                         throw new NullPointerException("contact.contactId is null"); //NOI18N
139                 } else if (contact.getContactId() < 1) {
140                         // Not valid
141                         throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
142                 }
143
144                 // Set updated timestamp
145                 this.setAllContactPhoneEntriesUpdated(contact, isCellphoneUnlinked, isLandlineUnlinked, isFaxUnlinked);
146
147                 // Merge mobile, land-line and fix
148                 final Contact detachedContact = this.mergeContactData(contact);
149
150                 // Trace message
151                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: detachedContact={1} - EXIT!", this.getClass().getSimpleName(), detachedContact)); //NOI18N
152
153                 // Return it
154                 return detachedContact;
155         }
156
157         @Override
158         public Contact updateContactData (final Contact contact) {
159                 // Log trace message
160                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
161
162                 // The contact instance must be valid
163                 if (null == contact) {
164                         // Throw NPE again
165                         throw new NullPointerException("contact is null"); //NOI18N
166                 } else if (contact.getContactId() == null) {
167                         // Throw NPE again
168                         throw new NullPointerException("contact.contactId is null"); //NOI18N
169                 } else if (contact.getContactId() < 1) {
170                         // Not valid
171                         throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
172                 }
173
174                 // Is cell phone/land-line/fax number unlinked?
175                 final boolean isCellphoneUnlinked = (contact.getContactMobileNumber() == null);
176                 final boolean isLandLineUnlinked = (contact.getContactLandLineNumber() == null);
177                 final boolean isFaxUnlinked = (contact.getContactFaxNumber() == null);
178
179                 // Call other Method
180                 final Contact detachedContact = this.updateContactData(contact, isCellphoneUnlinked, isLandLineUnlinked, isFaxUnlinked);
181
182                 // Trace message
183                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: detachedContact={1} - EXIT!", this.getClass().getSimpleName(), detachedContact)); //NOI18N
184
185                 // Return it
186                 return detachedContact;
187         }
188
189         /**
190          * Returns a contact instance which has the given id number.
191          * <p>
192          * @param contactId Contact id
193          * <p>
194          * @return Contact instance
195          * <p>
196          * @throws ContactNotFoundException If the contact was not found
197          */
198         private Contact findContactById (final Long contactId) throws ContactNotFoundException {
199                 // Log trace message
200                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactById: contactId={1} - CALLED!", this.getClass().getSimpleName(), contactId)); //NOI18N
201
202                 // The parameter must be valid
203                 if (null == contactId) {
204                         // Throw NPE
205                         throw new NullPointerException("contactId is null"); //NOI18N
206                 } else if (contactId < 1) {
207                         // Not valid
208                         throw new IllegalArgumentException(MessageFormat.format("contactId={0} is not valid", contactId)); //NOI18N
209                 }
210
211                 // Get query instance
212                 final Query query = this.getEntityManager().createNamedQuery("SearchContactById", UserContact.class); //NOI18N
213
214                 // Set parameter
215                 query.setParameter("contactId", contactId); //NOI18N
216                 query.setMaxResults(1);
217
218                 // Init contact instance
219                 final Contact contact;
220
221                 // Try to find a result
222                 try {
223                         // Find a single result
224                         contact = (Contact) query.getSingleResult();
225
226                         // Log trace message
227                         this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactById: Found contact={1}", this.getClass().getSimpleName(), contact)); //NOI18N
228                 } catch (final NoResultException ex) {
229                         // No result found
230                         throw new ContactNotFoundException(contactId, ex);
231                 }
232
233                 // Log trace message
234                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactById: contact={1} - EXIT!", this.getClass().getSimpleName(), contact)); //NOI18N
235
236                 // Return found instance
237                 return contact;
238         }
239
240 }