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