]> git.mxchange.org Git - pizzaservice-core.git/blob - src/org/mxchange/pizzaaplication/database/BasePizzaDatabaseBean.java
Added protected method detachAllContactPhoneEntries()
[pizzaservice-core.git] / src / org / mxchange / pizzaaplication / database / BasePizzaDatabaseBean.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 General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (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 General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.pizzaaplication.database;
18
19 import java.text.MessageFormat;
20 import java.util.GregorianCalendar;
21 import java.util.Objects;
22 import java.util.Properties;
23 import javax.ejb.EJBException;
24 import javax.faces.FacesException;
25 import javax.jms.Connection;
26 import javax.jms.JMSException;
27 import javax.jms.MessageProducer;
28 import javax.jms.ObjectMessage;
29 import javax.jms.Queue;
30 import javax.jms.QueueConnectionFactory;
31 import javax.jms.Session;
32 import javax.mail.Address;
33 import javax.naming.Context;
34 import javax.naming.InitialContext;
35 import javax.naming.NamingException;
36 import org.mxchange.jcontacts.contact.Contact;
37 import org.mxchange.jcoreee.database.BaseDatabaseBean;
38 import org.mxchange.jmailee.model.delivery.wrapper.EmailDeliveryWrapper;
39 import org.mxchange.jmailee.model.delivery.wrapper.WrapableEmailDelivery;
40 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
41 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
42 import org.mxchange.jphone.phonenumbers.mobile.DialableMobileNumber;
43 import org.mxchange.jphone.utils.PhoneUtils;
44 import org.mxchange.jusercore.model.user.User;
45 import org.mxchange.jusercore.model.user.UserUtils;
46
47 /**
48  * A helper class for beans that access the database.
49  * <p>
50  * @author Roland Haeder<roland@mxchange.org>
51  */
52 public abstract class BasePizzaDatabaseBean extends BaseDatabaseBean {
53
54         /**
55          * Serial number
56          */
57         private static final long serialVersionUID = 12_895_410_275_811_963L;
58
59         /**
60          * Connection
61          */
62         private Connection connection;
63
64         /**
65          * Message producer
66          */
67         private MessageProducer messageProducer;
68
69         /**
70          * Mailer message queue
71          */
72         private Queue queue;
73
74         /**
75          * Session instance
76          */
77         private Session session;
78
79         /**
80          * Protected constructor
81          */
82         protected BasePizzaDatabaseBean () {
83                 // Call super constructor
84                 super();
85
86                 try {
87                         // Get initial context
88                         Context context = new InitialContext();
89
90                         // Get factory from JMS resource
91                         QueueConnectionFactory connectionFactory = (QueueConnectionFactory) context.lookup("jms/jlandingpage-queue-factory"); //NOI18N
92
93                         // Lookup queue
94                         this.queue = (Queue) context.lookup("jms/jlandingpage-email-queue"); //NOI18N
95
96                         // Create connection
97                         this.connection = connectionFactory.createConnection();
98
99                         // Init session instance
100                         this.session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
101
102                         // And message producer
103                         this.messageProducer = this.session.createProducer(this.queue);
104                 } catch (final NamingException | JMSException e) {
105                         // Continued to throw
106                         throw new FacesException(e);
107                 }
108         }
109
110         /**
111          * Detaches all contact's phone instances.
112          * <p>
113          * @param contact Contact to set instances
114          */
115         protected void detachAllContactPhoneEntries (final Contact contact) {
116                 // Trace message
117                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("detachAllContactPhoneEntries: contact={0} - CALLED!", contact)); //NOI18N
118
119                 // Both must be the same and not null
120                 if (null == contact) {
121                         // Throw NPE
122                         throw new NullPointerException("contact is null"); //NOI18N
123                 }
124
125                 // Debug message
126                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("detachAllContactPhoneEntries: contact.contactMobileNumber={0}", contact.getContactMobileNumber())); //NOI18N
127
128                 // Is contact cellphone set?
129                 if (contact.getContactMobileNumber() instanceof DialableMobileNumber) {
130                         // Copy cellphone number
131                         contact.setContactMobileNumber(this.getDetached(contact.getContactMobileNumber(), contact.getContactMobileNumber()));
132                 }
133
134                 // Debug message
135                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("detachAllContactPhoneEntries: contact.contactLandLineNumber={0}", contact.getContactLandLineNumber())); //NOI18N
136
137                 // Is the land-line number set?
138                 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
139                         // Copy land-line number
140                         contact.setContactLandLineNumber(this.getDetached(contact.getContactLandLineNumber(), contact.getContactLandLineNumber()));
141                 }
142
143                 // Debug message
144                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("detachAllContactPhoneEntries: contact.contactFaxNumber={0}", contact.getContactFaxNumber())); //NOI18N
145
146                 // Is the fax number set?
147                 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
148                         // Copy fax number
149                         contact.setContactFaxNumber(this.getDetached(contact.getContactFaxNumber(), contact.getContactFaxNumber()));
150                 }
151
152                 // Trace message
153                 this.getLoggerBeanLocal().logTrace("detachAllContactPhoneEntries: EXIT!"); //NOI18N
154         }
155
156         /**
157          * Updates all contacts's phone entry's created timestamps
158          * <p>
159          * @param contact Contact instance to update
160          */
161         protected void setAllContactPhoneEntriesCreated (final Contact contact) {
162                 // Trace message
163                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("setAllContactPhoneEntriesCreated: contact={0} - CALLED!", contact)); //NOI18N
164
165                 // The contact instance must be valid
166                 if (null == contact) {
167                         // Throw NPE again
168                         throw new NullPointerException("contact is null"); //NOI18N
169                 }
170
171                 // Get all phone instances
172                 DialableLandLineNumber landLineNumber = contact.getContactLandLineNumber();
173                 DialableFaxNumber faxNumber = contact.getContactFaxNumber();
174                 DialableMobileNumber mobileNumber = contact.getContactMobileNumber();
175
176                 // Debug message
177                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("setAllContactPhoneEntriesCreated: landLineNumber={0},faxNumber={1},cellphoneNumber={2}", landLineNumber, faxNumber, mobileNumber)); //NOI18N
178
179                 // Is a phone number instance set?
180                 if ((landLineNumber instanceof DialableLandLineNumber) && (landLineNumber.getPhoneId() == null)) {
181                         // Debug message
182                         this.getLoggerBeanLocal().logDebug("setAllContactPhoneEntriesCreated: Setting created timestamp for land-line number ..."); //NOI18N
183
184                         // Set updated timestamp
185                         landLineNumber.setPhoneEntryCreated(new GregorianCalendar());
186                 }
187
188                 // Is a fax number instance set?
189                 if ((faxNumber instanceof DialableFaxNumber) && (faxNumber.getPhoneId() == null)) {
190                         // Debug message
191                         this.getLoggerBeanLocal().logDebug("setAllContactPhoneEntriesCreated: Setting created timestamp for fax number ..."); //NOI18N
192
193                         // Set updated timestamp
194                         faxNumber.setPhoneEntryCreated(new GregorianCalendar());
195                 }
196
197                 // Is a mobile number instance set?
198                 if ((mobileNumber instanceof DialableMobileNumber) && (mobileNumber.getPhoneId() == null)) {
199                         // Debug message
200                         this.getLoggerBeanLocal().logDebug("setAllContactPhoneEntriesCreated: Setting created timestamp for cellphone number ..."); //NOI18N
201
202                         // Set updated timestamp
203                         mobileNumber.setPhoneEntryCreated(new GregorianCalendar());
204                 }
205
206                 // Trace message
207                 this.getLoggerBeanLocal().logTrace("setAllContactPhoneEntriesCreated: EXIT!"); //NOI18N
208         }
209
210         /**
211          * Returnes a detached instance from given cellphone instance
212          * <p>
213          * @param mobileNumber Mobile instance
214          * @param fetchedNumber Found cellphone number in database
215          * <p>
216          * @return Detached instance
217          */
218         protected DialableMobileNumber getDetached (final DialableMobileNumber mobileNumber, final DialableMobileNumber fetchedNumber) {
219                 // Trace message
220                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getDetached: cellphoneNumber={0},fetchedNumber={1} - CALLED!", mobileNumber, fetchedNumber)); //NOI18N
221
222                 // Should be valid
223                 if (null == mobileNumber) {
224                         // Throw NPE
225                         throw new NullPointerException("cellphoneNumber is null"); //NOI18N
226                 } else if (fetchedNumber.getPhoneId() == null) {
227                         // ..and again
228                         throw new NullPointerException("fetchedNumber.phoneId is null"); //NOI18N
229                 }
230
231                 // Debug message
232                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: fetchedNumber.phoneId={0}", fetchedNumber.getPhoneId())); //NOI18N
233
234                 // Init query instance
235                 DialableMobileNumber foundNumber = this.getEntityManager().getReference(fetchedNumber.getClass(), fetchedNumber.getPhoneId());
236
237                 // Debug message
238                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: foundNumber={0}", foundNumber)); //NOI18N
239
240                 // Default is null
241                 DialableMobileNumber detachedNumber = null;
242
243                 // Is there a difference?
244                 if (!PhoneUtils.isSameMobileNumber(mobileNumber, fetchedNumber)) {
245                         // Merge this entry
246                         detachedNumber = this.getEntityManager().merge(foundNumber);
247
248                         // Copy all
249                 }
250
251                 // Trace message
252                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getDetached: detachedNumber={0} - EXIT!", detachedNumber)); //NOI18N
253
254                 // Return it
255                 return detachedNumber;
256         }
257
258         /**
259          * Returnes a detached instance from given land-line instance
260          * <p>
261          * @param landLineNumber Land-line instance
262          * @param fetchedNumber Found land-line number in database
263          * <p>
264          * @return Detached instance
265          */
266         protected DialableLandLineNumber getDetached (final DialableLandLineNumber landLineNumber, final DialableLandLineNumber fetchedNumber) {
267                 // Trace message
268                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getDetached: landLineNumber={0},fetchedNumber={1} - CALLED!", landLineNumber, fetchedNumber)); //NOI18N
269
270                 // Should be valid
271                 if (null == landLineNumber) {
272                         // Throw NPE
273                         throw new NullPointerException("landLineNumber is null"); //NOI18N
274                 } else if (fetchedNumber.getPhoneId() == null) {
275                         // ..and again
276                         throw new NullPointerException("landLineNumber.phoneId is null"); //NOI18N
277                 }
278
279                 // Debug message
280                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: fetchedNumber.phoneId={0}", fetchedNumber.getPhoneId())); //NOI18N
281
282                 // Init query instance
283                 DialableLandLineNumber foundNumber = this.getEntityManager().getReference(fetchedNumber.getClass(), fetchedNumber.getPhoneId());
284
285                 // Debug message
286                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: foundNumber={0}", foundNumber)); //NOI18N
287
288                 // Default is null
289                 DialableLandLineNumber detachedNumber = null;
290
291                 // Is there a difference?
292                 if (!PhoneUtils.isSameLandLineNumber(landLineNumber, fetchedNumber)) {
293                         // Merge this entry
294                         detachedNumber = this.getEntityManager().merge(foundNumber);
295                 }
296
297                 // Trace message
298                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getDetached: detachedNumber={0} - EXIT!", detachedNumber)); //NOI18N
299
300                 // Return it
301                 return detachedNumber;
302         }
303
304         /**
305          * Returnes a detached instance from given fax instance
306          * <p>
307          * @param faxNumber Fax instance
308          * @param fetchedNumber Found fax number in database
309          * <p>
310          * @return Detached instance
311          */
312         protected DialableFaxNumber getDetached (final DialableFaxNumber faxNumber, final DialableFaxNumber fetchedNumber) {
313                 // Trace message
314                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getDetached: faxNumber={0},fetchedNumber={1} - CALLED!", faxNumber, fetchedNumber)); //NOI18N
315
316                 // Should be valid
317                 if (null == faxNumber) {
318                         // Throw NPE
319                         throw new NullPointerException("faxNumber is null"); //NOI18N
320                 } else if (fetchedNumber.getPhoneId() == null) {
321                         // ..and again
322                         throw new NullPointerException("fetchedNumber.phoneId is null"); //NOI18N
323                 }
324
325                 // Debug message
326                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: fetchedNumber.phoneId={0}", fetchedNumber.getPhoneId())); //NOI18N
327
328                 // Init query instance
329                 DialableFaxNumber foundNumber = this.getEntityManager().getReference(fetchedNumber.getClass(), fetchedNumber.getPhoneId());
330
331                 // Debug message
332                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: foundNumber={0}", foundNumber)); //NOI18N
333
334                 // Default is null
335                 DialableFaxNumber detachedNumber = null;
336
337                 // Is there a difference?
338                 if (!PhoneUtils.isSameFaxNumber(faxNumber, fetchedNumber)) {
339                         // Merge this entry
340                         detachedNumber = this.getEntityManager().merge(foundNumber);
341                 }
342
343                 // Trace message
344                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getDetached: detachedNumber={0} - EXIT!", detachedNumber)); //NOI18N
345
346                 // Return it
347                 return detachedNumber;
348         }
349
350         /**
351          * Merges given contact's data
352          * <p>
353          * @param contact Contact instance to merge
354          * <p>
355          * @return Detached contact instance
356          */
357         protected Contact mergeContactData (final Contact contact) {
358                 // Trace message
359                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("mergeContactData: contact={0} - CALLED!", contact)); //NOI18N
360
361                 // The contact instance must be valid
362                 if (null == contact) {
363                         // Throw NPE again
364                         throw new NullPointerException("contact is null"); //NOI18N
365                 } else if (contact.getContactId() == null) {
366                         // Throw NPE again
367                         throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
368                 } else if (contact.getContactId() < 1) {
369                         // Not valid
370                         throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
371                 }
372
373                 // Set updated timestamp
374                 contact.setContactUpdated(new GregorianCalendar());
375
376                 // Get contact from it and find it
377                 Contact foundContact = this.getEntityManager().getReference(contact.getClass(), contact.getContactId());
378
379                 // Should be found
380                 assert (foundContact instanceof Contact) : MessageFormat.format("Contact with id {0} not found, but should be.", contact.getContactId()); //NOI18N
381
382                 // Debug message
383                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: foundContact.contactId={0}", foundContact.getContactId())); //NOI18N
384
385                 // Merge contact instance
386                 Contact detachedContact = this.getEntityManager().merge(foundContact);
387
388                 // Copy all
389                 detachedContact.copyAll(contact);
390
391                 // Trace message
392                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("mergeContactData: detachedContact={0} - EXIT!", detachedContact)); //NOI18N
393
394                 // Return detached contact
395                 return detachedContact;
396         }
397
398         /**
399          * Merges given (detached) contact's cellphone, land-line and fax numbers
400          * <p>
401          * @param detachedContact Detached contact instance
402          */
403         protected void mergeContactsMobileLandLineFaxNumbers (final Contact detachedContact) {
404                 // Trace message
405                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("mergeContactsMobileLandLineFaxNumbers: detachedContact={0} - CALLED!", detachedContact)); //NOI18N
406
407                 // The contact instance must be valid
408                 if (null == detachedContact) {
409                         // Throw NPE again
410                         throw new NullPointerException("detachedContact is null"); //NOI18N
411                 } else if (detachedContact.getContactId() == null) {
412                         // Throw NPE again
413                         throw new NullPointerException("detachedContact.contactId is null"); //NOI18N //NOI18N
414                 } else if (detachedContact.getContactId() < 1) {
415                         // Not valid
416                         throw new IllegalStateException(MessageFormat.format("detachedContact.contactId={0} is not valid.", detachedContact.getContactId())); //NOI18N
417                 }
418
419                 // Get all instances
420                 DialableMobileNumber cellphone = detachedContact.getContactMobileNumber();
421                 DialableLandLineNumber landLine = detachedContact.getContactLandLineNumber();
422                 DialableFaxNumber fax = detachedContact.getContactFaxNumber();
423
424                 // Is there a  cellphone instance set?
425                 if (cellphone instanceof DialableMobileNumber) {
426                         // Debug message
427                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: cellphone.phoneId={0} is being updated ...", cellphone.getPhoneId())); //NOI18N
428
429                         // Then find it, too
430                         DialableMobileNumber foundMobile = this.getEntityManager().getReference(cellphone.getClass(), cellphone.getPhoneId());
431
432                         // Should be there
433                         assert (foundMobile instanceof DialableMobileNumber) : MessageFormat.format("Mobile number with id {0} not found but should be.", foundMobile.getPhoneId()); //NOI18N
434
435                         // Then merge it, too
436                         DialableMobileNumber detachedMobile = this.getEntityManager().merge(foundMobile);
437
438                         // Should be there
439                         assert (detachedMobile instanceof DialableMobileNumber) : MessageFormat.format("Mobile number with id {0} not found but should be.", detachedMobile.getPhoneId()); //NOI18N
440
441                         // Copy all
442                         detachedMobile.copyAll(detachedContact.getContactMobileNumber());
443
444                         // Set it back
445                         detachedContact.setContactMobileNumber(detachedMobile);
446                 }
447
448                 // Is there a  fax instance set?
449                 if (fax instanceof DialableFaxNumber) {
450                         // Debug message
451                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: fax.phoneId={0} is being updated ...", fax.getPhoneId())); //NOI18N
452
453                         // Then find it, too
454                         DialableFaxNumber foundFax = this.getEntityManager().getReference(fax.getClass(), fax.getPhoneId());
455
456                         // Should be there
457                         assert (foundFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", foundFax.getPhoneId()); //NOI18N
458
459                         // Then merge it, too
460                         DialableFaxNumber detachedFax = this.getEntityManager().merge(foundFax);
461
462                         // Should be there
463                         assert (detachedFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", detachedFax.getPhoneId()); //NOI18N
464
465                         // Copy all
466                         detachedFax.copyAll(detachedContact.getContactFaxNumber());
467
468                         // Set it back
469                         detachedContact.setContactFaxNumber(detachedFax);
470                 }
471
472                 // Is there a  fax instance set?
473                 if (landLine instanceof DialableLandLineNumber) {
474                         // Debug message
475                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: landLine.phoneId={0} is being updated ...", landLine.getPhoneId())); //NOI18N
476
477                         // Then find it, too
478                         DialableLandLineNumber foundLandLine = this.getEntityManager().getReference(landLine.getClass(), landLine.getPhoneId());
479
480                         // Should be there
481                         assert (foundLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", foundLandLine.getPhoneId()); //NOI18N
482
483                         // Then merge it, too
484                         DialableLandLineNumber detachedLandLine = this.getEntityManager().merge(foundLandLine);
485
486                         // Should be there
487                         assert (detachedLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", detachedLandLine.getPhoneId()); //NOI18N
488
489                         // Copy all
490                         detachedLandLine.copyAll(detachedContact.getContactLandLineNumber());
491
492                         // Set it back
493                         detachedContact.setContactLandLineNumber(detachedLandLine);
494                 }
495
496                 // Trace message
497                 this.getLoggerBeanLocal().logTrace("mergeContactsMobileLandLineFaxNumbers: EXIT!"); //NOI18N
498         }
499
500         /**
501          * Sends an email with given subject line, template name to given recipient
502          * and user data
503          * <p>
504          * @param subjectLine Subject line
505          * @param templateName Template name
506          * @param emailAddress Recipient's email address
507          * @param user User instance
508          * @param baseUrl Base URL
509          */
510         protected void sendEmail (final String subjectLine, final String templateName, final Address emailAddress, final User user, final String baseUrl) {
511                 // Trace message
512                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("sendEmail: subjectLine={0},templateName={1},emailAddress={2},user={3},baseUrl={4} - CALLED!", subjectLine, templateName, emailAddress, user, baseUrl)); //NOI18N
513
514                 // All should be set
515                 if (null == subjectLine) {
516                         // Throw NPE
517                         throw new NullPointerException("subjectLine is null"); //NOI18N
518                 } else if (subjectLine.isEmpty()) {
519                         // No subject line
520                         throw new IllegalArgumentException("subjectLine is empty"); //NOI18N
521                 } else if (null == templateName) {
522                         // Throw NPE
523                         throw new NullPointerException("templateName is null"); //NOI18N
524                 } else if (templateName.isEmpty()) {
525                         // No template name
526                         throw new IllegalArgumentException("templateName is empty"); //NOI18N
527                 } else if (null == emailAddress) {
528                         // Throw NPE
529                         throw new NullPointerException("emailAddress is null"); //NOI18N
530                 }
531
532                 // Prepare mail wrapper
533                 WrapableEmailDelivery emailWrapper = new EmailDeliveryWrapper();
534
535                 // Set all values
536                 Properties variables = UserUtils.getAllUserFields(user);
537
538                 // Set base URL
539                 variables.put("baseUrl", baseUrl); //NOI18N
540
541                 // Set all
542                 // @TODO Language from message bundle
543                 emailWrapper.setRecipient(emailAddress);
544                 emailWrapper.setLocale(user.getUserLocale());
545                 emailWrapper.setSubjectLine(subjectLine);
546                 emailWrapper.setTemplateName(templateName);
547                 emailWrapper.setTemplateVariables(variables);
548
549                 try {
550                         // Send out email change
551                         ObjectMessage message = this.session.createObjectMessage();
552                         message.setObject(emailWrapper);
553
554                         // Send message
555                         this.sendMessage(message, this.messageProducer);
556                 } catch (final JMSException ex) {
557                         // Throw again
558                         throw new EJBException(ex);
559                 }
560
561                 // Trace message
562                 this.getLoggerBeanLocal().logTrace("sendEmail: EXIT!"); //NOI18N
563         }
564
565         /**
566          * Updates all contact's phone instances from other contact, both contacts
567          * should be the same.
568          * <p>
569          * @param contact Contact to set instances
570          * @param other Other contact to get instances from
571          */
572         protected void setAllContactPhoneEntries (final Contact contact, final Contact other) {
573                 // Trace message
574                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("setAllContactPhoneEntries: contact={0},other={1} - CALLED!", contact, other)); //NOI18N
575
576                 // Both must be the same and not null
577                 if (null == contact) {
578                         // Throw NPE
579                         throw new NullPointerException("contact is null"); //NOI18N
580                 } else if (null == other) {
581                         // Throw NPE
582                         throw new NullPointerException("other is null"); //NOI18N
583                 } else if (!Objects.equals(contact, other)) {
584                         // Not same instances
585                         throw new IllegalArgumentException(MessageFormat.format("contact={0} and other={1} are not equal!", contact, other)); //NOI18N
586                 }
587
588                 // Debug message
589                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("setAllContactPhoneEntries: other.contactMobileNumber={0}", other.getContactMobileNumber())); //NOI18N
590
591                 // Is other cellphone not set?
592                 if ((other.getContactMobileNumber() == null) || (PhoneUtils.isSameMobileNumber(contact.getContactMobileNumber(), other.getContactMobileNumber()))) {
593                         // Debug message
594                         this.getLoggerBeanLocal().logDebug("setAllContactPhoneEntries: Copying cellphone entry ..."); //NOI18N
595
596                         // Is the fax number set?
597                         if (other.getContactMobileNumber() instanceof DialableMobileNumber) {
598                                 // Copy cellphone number
599                                 contact.setContactMobileNumber(this.getDetached(other.getContactMobileNumber(), contact.getContactMobileNumber()));
600                         } else {
601                                 // Null it
602                                 contact.setContactMobileNumber(null);
603                         }
604                 }
605
606                 // Debug message
607                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("setAllContactPhoneEntries: other.contactLandLineNumber={0}", other.getContactLandLineNumber())); //NOI18N
608
609                 // Is other cellphone not set?
610                 if ((other.getContactLandLineNumber() == null) || (PhoneUtils.isSameLandLineNumber(contact.getContactLandLineNumber(), other.getContactLandLineNumber()))) {
611                         // Debug message
612                         this.getLoggerBeanLocal().logDebug("setAllContactPhoneEntries: Copying land-line entry ..."); //NOI18N
613
614                         // Is the land-line number set?
615                         if (other.getContactLandLineNumber() instanceof DialableLandLineNumber) {
616                                 // Copy land-line number
617                                 contact.setContactLandLineNumber(this.getDetached(other.getContactLandLineNumber(), contact.getContactLandLineNumber()));
618                         } else {
619                                 // Null it
620                                 contact.setContactLandLineNumber(null);
621                         }
622                 }
623
624                 // Debug message
625                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("setAllContactPhoneEntries: other.contactFaxNumber={0}", other.getContactFaxNumber())); //NOI18N
626
627                 // Is other cellphone not set?
628                 if ((other.getContactFaxNumber() == null) || (PhoneUtils.isSameFaxNumber(contact.getContactFaxNumber(), other.getContactFaxNumber()))) {
629                         // Debug message
630                         this.getLoggerBeanLocal().logDebug("setAllContactPhoneEntries: Copying fax entry ..."); //NOI18N
631
632                         // Is the fax number set?
633                         if (other.getContactFaxNumber() instanceof DialableFaxNumber) {
634                                 // Copy fax number
635                                 contact.setContactFaxNumber(this.getDetached(other.getContactFaxNumber(), contact.getContactFaxNumber()));
636                         } else {
637                                 // Null it
638                                 contact.setContactFaxNumber(null);
639                         }
640                 }
641
642                 // Trace message
643                 this.getLoggerBeanLocal().logTrace("setAllContactPhoneEntries: EXIT!"); //NOI18N
644         }
645
646         /**
647          * Updates all contacts's phone entry's updated timestamps
648          * <p>
649          * @param contact Contact instance to update
650          * @param isMobileUnlinked Whether a cellphone entry has been unlinked in
651          * contact instance
652          * @param isLandlineUnlinked Whether a land-line entry has been unlinked in
653          * contact instance
654          * @param isFaxUnlinked Whether a fax entry has been unlinked in contact
655          * instance
656          */
657         protected void setAllContactPhoneEntriesUpdated (final Contact contact, final boolean isMobileUnlinked, final boolean isLandlineUnlinked, final boolean isFaxUnlinked) {
658                 // Trace message
659                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("setAllContactPhoneEntriesUpdated: contact={0},isMobileUnlinked={1},isLandlineUnlinked={2},isFaxUnlinked={3} - CALLED", contact, isMobileUnlinked, isLandlineUnlinked, isFaxUnlinked)); //NOI18N
660
661                 // The contact instance must be valid
662                 if (null == contact) {
663                         // Throw NPE again
664                         throw new NullPointerException("contact is null"); //NOI18N
665                 } else if (contact.getContactId() == null) {
666                         // Throw NPE again
667                         throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
668                 } else if (contact.getContactId() < 1) {
669                         // Not valid
670                         throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
671                 }
672
673                 // Get all phone instances
674                 DialableLandLineNumber landLineNumber = contact.getContactLandLineNumber();
675                 DialableFaxNumber faxNumber = contact.getContactFaxNumber();
676                 DialableMobileNumber cellphoneNumber = contact.getContactMobileNumber();
677
678                 // Flags and instances must be constistent
679                 if (isMobileUnlinked && cellphoneNumber instanceof DialableMobileNumber) {
680                         // Bad state
681                         throw new IllegalStateException("isCellPhoneUnlinked is TRUE, but cellphoneNumber is set."); //NOI18N
682                 } else if (isLandlineUnlinked && landLineNumber instanceof DialableLandLineNumber) {
683                         // Bad state
684                         throw new IllegalStateException("isLandlineUnlinked is TRUE, but landLineNumber is set."); //NOI18N
685                 } else if (isFaxUnlinked && faxNumber instanceof DialableFaxNumber) {
686                         // Bad state
687                         throw new IllegalStateException("isFaxUnlinked is TRUE, but faxNumber is set."); //NOI18N
688                 }
689
690                 // Is a phone number instance set?
691                 if ((landLineNumber instanceof DialableLandLineNumber) && (landLineNumber.getPhoneId() instanceof Long) && (landLineNumber.getPhoneId() > 0)) {
692                         // Debug message
693                         this.getLoggerBeanLocal().logDebug("setAllContactPhoneEntriesUpdated: Setting updated timestamp for land-line number ..."); //NOI18N
694
695                         // Set updated timestamp
696                         landLineNumber.setPhoneEntryUpdated(new GregorianCalendar());
697                 }
698
699                 // Is a fax number instance set?
700                 if ((faxNumber instanceof DialableFaxNumber) && (faxNumber.getPhoneId() instanceof Long) && (faxNumber.getPhoneId() > 0)) {
701                         // Debug message
702                         this.getLoggerBeanLocal().logDebug("setAllContactPhoneEntriesUpdated: Setting updated timestamp for fax number ..."); //NOI18N
703
704                         // Set updated timestamp
705                         faxNumber.setPhoneEntryUpdated(new GregorianCalendar());
706                 }
707
708                 // Is a mobile number instance set?
709                 if ((cellphoneNumber instanceof DialableMobileNumber) && (cellphoneNumber.getPhoneId() instanceof Long) && (cellphoneNumber.getPhoneId() > 0)) {
710                         // Debug message
711                         this.getLoggerBeanLocal().logDebug("setAllContactPhoneEntriesUpdated: Setting updated timestamp for cellphone number ..."); //NOI18N
712
713                         // Set updated timestamp
714                         cellphoneNumber.setPhoneEntryUpdated(new GregorianCalendar());
715                 }
716
717                 // Trace message
718                 this.getLoggerBeanLocal().logTrace("setAllContactPhoneEntriesUpdated: EXIT!"); //NOI18N
719         }
720
721 }