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