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