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