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