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