]> git.mxchange.org Git - jjobs-ejb.git/blob - src/java/org/mxchange/jjobs/beans/ejb/BaseJobsEnterpriseBean.java
c18d1bd083da27d366dfa468ebeaa631e3210d00
[jjobs-ejb.git] / src / java / org / mxchange / jjobs / beans / ejb / BaseJobsEnterpriseBean.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.jjobs.beans.ejb;
18
19 import java.text.MessageFormat;
20 import java.util.Date;
21 import java.util.List;
22 import java.util.Objects;
23 import java.util.Properties;
24 import javax.ejb.EJBException;
25 import javax.jms.JMSException;
26 import javax.jms.ObjectMessage;
27 import javax.mail.Address;
28 import javax.mail.internet.AddressException;
29 import javax.mail.internet.InternetAddress;
30 import org.mxchange.jcontacts.model.contact.Contact;
31 import org.mxchange.jcontacts.model.contact.Contacts;
32 import org.mxchange.jcontacts.model.contact.UserContact;
33 import org.mxchange.jcontactsbusiness.model.basicdata.BasicData;
34 import org.mxchange.jcontactsbusiness.model.basicdata.BusinessBasicData;
35 import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOffice;
36 import org.mxchange.jcontactsbusiness.model.branchoffice.BusinessBranchOffice;
37 import org.mxchange.jcontactsbusiness.model.employee.BusinessEmployee;
38 import org.mxchange.jcontactsbusiness.model.employee.Employable;
39 import org.mxchange.jcontactsbusiness.model.headquarter.BusinessHeadquarter;
40 import org.mxchange.jcontactsbusiness.model.headquarter.Headquarter;
41 import org.mxchange.jcontactsbusiness.model.opening_time.OpeningTime;
42 import org.mxchange.jcoreee.bean.ejb.BaseEnterpriseBean;
43 import org.mxchange.jcountry.model.data.Country;
44 import org.mxchange.jcountry.model.data.CountryData;
45 import org.mxchange.jmailee.model.delivery.wrapper.EmailDeliveryWrapper;
46 import org.mxchange.jmailee.model.delivery.wrapper.WrapableEmailDelivery;
47 import org.mxchange.jphone.model.phonenumbers.fax.DialableFaxNumber;
48 import org.mxchange.jphone.model.phonenumbers.fax.FaxNumbers;
49 import org.mxchange.jphone.model.phonenumbers.landline.DialableLandLineNumber;
50 import org.mxchange.jphone.model.phonenumbers.landline.LandLineNumbers;
51 import org.mxchange.jphone.model.phonenumbers.mobile.DialableMobileNumber;
52 import org.mxchange.jphone.model.phonenumbers.mobile.MobileNumbers;
53 import org.mxchange.jphone.utils.PhoneUtils;
54 import org.mxchange.jusercore.model.user.LoginUser;
55 import org.mxchange.jusercore.model.user.User;
56 import org.mxchange.jusercore.model.user.Users;
57
58 /**
59  * A helper class for beans that access the database.
60  * <p>
61  * @author Roland Häder<roland@mxchange.org>
62  */
63 public abstract class BaseJobsEnterpriseBean extends BaseEnterpriseBean {
64
65         /**
66          * Serial number
67          */
68         private static final long serialVersionUID = 12_895_410_275_811_963L;
69
70         /**
71          * Protected constructor
72          */
73         protected BaseJobsEnterpriseBean () {
74                 // Call super constructor
75                 super();
76         }
77
78         /**
79          * Constructor with queue factory JNDI and queue JNDI names
80          * <p>
81          * @param factoryJndi JNDI name for queue factory
82          * @param queueJndi   JNDI name for email queue
83          */
84         protected BaseJobsEnterpriseBean (final String factoryJndi, final String queueJndi) {
85                 // Call super constructor
86                 super(factoryJndi, queueJndi);
87         }
88
89         /**
90          * Get back a managed instance from given contact
91          * <p>
92          * @param contact Unmanaged/detached contact instance
93          * <p>
94          * @return Managed contact instance
95          */
96         protected Contact createManaged (final Contact contact) {
97                 // Trace message
98                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.createManaged: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
99
100                 // user should not be null
101                 if (null == contact) {
102                         // Abort here
103                         throw new NullPointerException("contact is null"); //NOI18N
104                 } else if (contact.getContactId() == null) {
105                         // Id is set
106                         throw new NullPointerException("contact.contactId is null"); //NOI18N
107                 } else if (contact.getContactId() < 1) {
108                         // Id is set
109                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
110                 }
111
112                 // Try to find it (should be there)
113                 final Contact managedContact = this.getEntityManager().find(UserContact.class, contact.getContactId());
114
115                 // Should be there
116                 assert (managedContact instanceof Contact) : "managedContact is null"; //NOI18N
117
118                 // Trace message
119                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.createManaged: managedContact={1} - EXIT!", this.getClass().getSimpleName(), managedContact)); //NOI18N
120
121                 // Return it
122                 return managedContact;
123         }
124
125         /**
126          * Get back a managed instance from given country
127          * <p>
128          * @param country Unmanaged/detached country instance
129          * <p>
130          * @return Managed country instance
131          */
132         protected Country createManaged (final Country country) {
133                 // Trace message
134                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.createManaged: country={1} - CALLED!", this.getClass().getSimpleName(), country)); //NOI18N
135
136                 // user should not be null
137                 if (null == country) {
138                         // Abort here
139                         throw new NullPointerException("country is null"); //NOI18N
140                 } else if (country.getCountryId() == null) {
141                         // Id is set
142                         throw new NullPointerException("country.countryId is null"); //NOI18N
143                 } else if (country.getCountryId() < 1) {
144                         // Id is set
145                         throw new IllegalArgumentException(MessageFormat.format("country.countryId={0} is invalid", country.getCountryId())); //NOI18N
146                 }
147
148                 // Try to find it (should be there)
149                 final Country managedCountry = this.getEntityManager().find(CountryData.class, country.getCountryId());
150
151                 // Should be there
152                 assert (managedCountry instanceof Country) : "managedCountry is null"; //NOI18N
153
154                 // Trace message
155                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.createManaged: managedCountry={1} - EXIT!", this.getClass().getSimpleName(), managedCountry)); //NOI18N
156
157                 // Return it
158                 return managedCountry;
159         }
160
161         /**
162          * Get back a managed instance from given basic data
163          * <p>
164          * @param basicData Unmanaged/detached basic data instance
165          * <p>
166          * @return Managed basic data instance
167          */
168         protected BasicData createManaged (final BasicData basicData) {
169                 // Trace message
170                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.createManaged: basicData={1} - CALLED!", this.getClass().getSimpleName(), basicData)); //NOI18N
171
172                 // user should not be null
173                 if (null == basicData) {
174                         // Abort here
175                         throw new NullPointerException("basicData is null"); //NOI18N
176                 } else if (basicData.getBasicDataId() == null) {
177                         // Id is set
178                         throw new NullPointerException("basicData.basicDataId is null"); //NOI18N
179                 } else if (basicData.getBasicDataId() < 1) {
180                         // Id is set
181                         throw new IllegalArgumentException(MessageFormat.format("basicData.basicDataId={0} is invalid", basicData.getBasicDataId())); //NOI18N
182                 }
183
184                 // Try to find it (should be there)
185                 final BasicData managedBasicData = this.getEntityManager().find(BusinessBasicData.class, basicData.getBasicDataId());
186
187                 // Should be there
188                 assert (managedBasicData instanceof BasicData) : "managedBasicData is null"; //NOI18N
189
190                 // Trace message
191                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.createManaged: managedBasicData={1} - EXIT!", this.getClass().getSimpleName(), managedBasicData)); //NOI18N
192
193                 // Return it
194                 return managedBasicData;
195         }
196
197         /**
198          * Get back a managed instance from given branch office
199          * <p>
200          * @param headquarter Unmanaged/detached branch office instance
201          * <p>
202          * @return Managed basic data instance
203          */
204         protected BranchOffice createManaged (final BranchOffice headquarter) {
205                 // Trace message
206                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.createManaged: headquarter={1} - CALLED!", this.getClass().getSimpleName(), headquarter)); //NOI18N
207
208                 // user should not be null
209                 if (null == headquarter) {
210                         // Abort here
211                         throw new NullPointerException("headquarter is null"); //NOI18N
212                 } else if (headquarter.getBranchId() == null) {
213                         // Id is set
214                         throw new NullPointerException("headquarter.headquarterId is null"); //NOI18N
215                 } else if (headquarter.getBranchId() < 1) {
216                         // Id is set
217                         throw new IllegalArgumentException(MessageFormat.format("headquarter.headquarterId={0} is invalid", headquarter.getBranchId())); //NOI18N
218                 }
219
220                 // Try to find it (should be there)
221                 final BranchOffice managedBranchOffice = this.getEntityManager().find(BusinessBranchOffice.class, headquarter.getBranchId());
222
223                 // Should be there
224                 assert (managedBranchOffice instanceof BranchOffice) : "managedBranchOffice is null"; //NOI18N
225
226                 // Trace message
227                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.createManaged: managedBranchOffice={1} - EXIT!", this.getClass().getSimpleName(), managedBranchOffice)); //NOI18N
228
229                 // Return it
230                 return managedBranchOffice;
231         }
232
233         /**
234          * Get back a managed instance from given employee
235          * <p>
236          * @param employee Unmanaged/detached employee instance
237          * <p>
238          * @return Managed employee instance
239          */
240         protected Employable createManaged (final Employable employee) {
241                 // Trace message
242                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.createManaged: employee={1} - CALLED!", this.getClass().getSimpleName(), employee)); //NOI18N
243
244                 // user should not be null
245                 if (null == employee) {
246                         // Abort here
247                         throw new NullPointerException("employee is null"); //NOI18N
248                 } else if (employee.getEmployeeId() == null) {
249                         // Id is set
250                         throw new NullPointerException("employee.employeeId is null"); //NOI18N
251                 } else if (employee.getEmployeeId() < 1) {
252                         // Id is set
253                         throw new IllegalArgumentException(MessageFormat.format("employee.employeeId={0} is invalid", employee.getEmployeeId())); //NOI18N
254                 }
255
256                 // Try to find it (should be there)
257                 final Employable managedEmployee = this.getEntityManager().find(BusinessEmployee.class, employee.getEmployeeId());
258
259                 // Should be there
260                 assert (managedEmployee instanceof Employable) : "managedEmployee is null"; //NOI18N
261
262                 // Trace message
263                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.createManaged: managedEmployee={1} - EXIT!", this.getClass().getSimpleName(), managedEmployee)); //NOI18N
264
265                 // Return it
266                 return managedEmployee;
267         }
268
269         /**
270          * Get back a managed instance from given headquarter
271          * <p>
272          * @param headquarter Unmanaged/detached headquarter instance
273          * <p>
274          * @return Managed basic data instance
275          */
276         protected Headquarter createManaged (final Headquarter headquarter) {
277                 // Trace message
278                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.createManaged: headquarter={1} - CALLED!", this.getClass().getSimpleName(), headquarter)); //NOI18N
279
280                 // user should not be null
281                 if (null == headquarter) {
282                         // Abort here
283                         throw new NullPointerException("headquarter is null"); //NOI18N
284                 } else if (headquarter.getHeadquarterId() == null) {
285                         // Id is set
286                         throw new NullPointerException("headquarter.headquarterId is null"); //NOI18N
287                 } else if (headquarter.getHeadquarterId() < 1) {
288                         // Id is set
289                         throw new IllegalArgumentException(MessageFormat.format("headquarter.headquarterId={0} is invalid", headquarter.getHeadquarterId())); //NOI18N
290                 }
291
292                 // Try to find it (should be there)
293                 final Headquarter managedHeadquarter = this.getEntityManager().find(BusinessHeadquarter.class, headquarter.getHeadquarterId());
294
295                 // Should be there
296                 assert (managedHeadquarter instanceof Headquarter) : "managedHeadquarter is null"; //NOI18N
297
298                 // Trace message
299                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.createManaged: managedHeadquarter={1} - EXIT!", this.getClass().getSimpleName(), managedHeadquarter)); //NOI18N
300
301                 // Return it
302                 return managedHeadquarter;
303         }
304
305         /**
306          * Get back a managed instance from given user
307          * <p>
308          * @param user Unmanaged/detached user instance
309          * <p>
310          * @return Managed user instance
311          */
312         protected User createManaged (final User user) {
313                 // Trace message
314                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.createManaged: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
315
316                 // user should not be null
317                 if (null == user) {
318                         // Abort here
319                         throw new NullPointerException("user is null"); //NOI18N
320                 } else if (user.getUserId() == null) {
321                         // Id is set
322                         throw new NullPointerException("user.userId is null"); //NOI18N
323                 } else if (user.getUserId() < 1) {
324                         // Id is set
325                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
326                 } else if (user.getUserContact() == null) {
327                         // Throw NPE again
328                         throw new NullPointerException("user.userContact is null"); //NOI18N
329                 } else if (user.getUserContact().getContactId() == null) {
330                         // Throw NPE again
331                         throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
332                 } else if (user.getUserContact().getContactId() < 1) {
333                         // Not valid id number
334                         throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N#
335                 }
336
337                 // Try to find it (should be there)
338                 final User managedUser = this.getEntityManager().find(LoginUser.class, user.getUserId());
339
340                 // Should be there
341                 assert (managedUser instanceof User) : "managedUser is null"; //NOI18N
342
343                 // Trace message
344                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.createManaged: managedUser={1} - EXIT!", this.getClass().getSimpleName(), managedUser)); //NOI18N
345
346                 // Return it
347                 return managedUser;
348         }
349
350         /**
351          * Updates all contact's phone entry's created timestamps
352          * <p>
353          * @param contact Contact instance to update
354          */
355         protected void setAllContactPhoneEntriesCreated (final Contact contact) {
356                 // Trace message
357                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.setAllContactPhoneEntriesCreated: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
358
359                 // The contact instance must be valid
360                 if (null == contact) {
361                         // Throw NPE again
362                         throw new NullPointerException("contact is null"); //NOI18N
363                 }
364
365                 // Get all phone instances
366                 final DialableLandLineNumber landLineNumber = contact.getContactLandLineNumber();
367                 final DialableFaxNumber faxNumber = contact.getContactFaxNumber();
368                 final DialableMobileNumber mobileNumber = contact.getContactMobileNumber();
369
370                 // Debug message
371                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntriesCreated: landLineNumber={1},faxNumber={2},mobileNumber={3}", this.getClass().getSimpleName(), landLineNumber, faxNumber, mobileNumber)); //NOI18N
372
373                 // Is a phone number instance set?
374                 if ((landLineNumber instanceof DialableLandLineNumber) && (landLineNumber.getPhoneId() == null)) {
375                         // Debug message
376                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntriesCreated: Setting created timestamp for land-line number ...", this.getClass().getSimpleName())); //NOI18N
377
378                         // Set updated timestamp
379                         landLineNumber.setPhoneEntryCreated(new Date());
380                 }
381
382                 // Is a fax number instance set?
383                 if ((faxNumber instanceof DialableFaxNumber) && (faxNumber.getPhoneId() == null)) {
384                         // Debug message
385                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntriesCreated: Setting created timestamp for fax number ...", this.getClass().getSimpleName())); //NOI18N
386
387                         // Set updated timestamp
388                         faxNumber.setPhoneEntryCreated(new Date());
389                 }
390
391                 // Is a mobile number instance set?
392                 if ((mobileNumber instanceof DialableMobileNumber) && (mobileNumber.getPhoneId() == null)) {
393                         // Debug message
394                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntriesCreated: Setting created timestamp for mobile number ...", this.getClass().getSimpleName())); //NOI18N
395
396                         // Set updated timestamp
397                         mobileNumber.setPhoneEntryCreated(new Date());
398                 }
399
400                 // Trace message
401                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.setAllContactPhoneEntriesCreated: EXIT!", this.getClass().getSimpleName())); //NOI18N
402         }
403
404         /**
405          * Sets created timestamp on all opening times.
406          * <p>
407          * @param openingTimes Opening times list
408          * <p>
409          * @throws NullPointerException If given list is null
410          * @throws IllegalArgumentException If given list is empty
411          * @throws IllegalStateException If openingId is not null
412          */
413         protected void setAllOpeningTimesCreated (final List<OpeningTime> openingTimes) {
414                 // Validate parameter
415                 if (null == openingTimes) {
416                         // Throw NPE
417                         throw new NullPointerException("openingTimes is null");
418                 } else if (openingTimes.isEmpty()) {
419                         // Throw IAE
420                         throw new IllegalArgumentException("openingTimes is empty");
421                 }
422
423                 // Walk through whole list
424                 for (final OpeningTime openingTime : openingTimes) {
425                         // Id should not be set
426                         if (openingTime.getOpeningId() != null) {
427                                 // Abort here
428                                 throw new IllegalStateException("openingTime.openingId is not null");
429                         }
430
431                         // Set created timestamp
432                         openingTime.setOpeningCreated(new Date());
433                 }
434         }
435
436         /**
437          * Updates all contact's phone entry's created timestamps
438          * <p>
439          * @param contact Contact instance to update
440          */
441         protected void setAllPhoneEntriesCreated (final Contact contact) {
442                 // Trace message
443                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("setAllPhoneEntriesCreated: contact={0} - CALLED!", contact)); //NOI18N
444
445                 // The contact instance must be valid
446                 if (null == contact) {
447                         // Throw NPE
448                         throw new NullPointerException("contact is null"); //NOI18N
449                 } else if (contact.getContactId() != null) {
450                         // Throw IAE
451                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is unexpected.", contact.getContactId()));
452                 }
453
454                 // Get all phone instances
455                 final DialableLandLineNumber landLineNumber = contact.getContactLandLineNumber();
456                 final DialableFaxNumber faxNumber = contact.getContactFaxNumber();
457                 final DialableMobileNumber mobileNumber = contact.getContactMobileNumber();
458
459                 // Debug message
460                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("setAllPhoneEntriesCreated: landLineNumber={0},faxNumber={1},mobileNumber={2}", landLineNumber, faxNumber, mobileNumber)); //NOI18N
461
462                 // Is a phone number instance set?
463                 if ((landLineNumber instanceof DialableLandLineNumber) && (landLineNumber.getPhoneId() == null)) {
464                         // Debug message
465                         this.getLoggerBeanLocal().logDebug("setAllPhoneEntriesCreated: Setting created timestamp for land-line number ..."); //NOI18N
466
467                         // Set updated timestamp
468                         landLineNumber.setPhoneEntryCreated(new Date());
469                 }
470
471                 // Is a fax number instance set?
472                 if ((faxNumber instanceof DialableFaxNumber) && (faxNumber.getPhoneId() == null)) {
473                         // Debug message
474                         this.getLoggerBeanLocal().logDebug("setAllPhoneEntriesCreated: Setting created timestamp for fax number ..."); //NOI18N
475
476                         // Set updated timestamp
477                         faxNumber.setPhoneEntryCreated(new Date());
478                 }
479
480                 // Is a mobile number instance set?
481                 if ((mobileNumber instanceof DialableMobileNumber) && (mobileNumber.getPhoneId() == null)) {
482                         // Debug message
483                         this.getLoggerBeanLocal().logDebug("setAllPhoneEntriesCreated: Setting created timestamp for mobile number ..."); //NOI18N
484
485                         // Set updated timestamp
486                         mobileNumber.setPhoneEntryCreated(new Date());
487                 }
488
489                 // Trace message
490                 this.getLoggerBeanLocal().logTrace("setAllPhoneEntriesCreated: EXIT!"); //NOI18N
491         }
492
493         /**
494          * Updates all branch office's phone entry's created timestamps
495          * <p>
496          * @param branchOffice Branch office instance to update
497          */
498         protected void setAllPhoneEntriesCreated (final BranchOffice branchOffice) {
499                 // Trace message
500                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("setAllPhoneEntriesCreated: branchOffice={0} - CALLED!", branchOffice)); //NOI18N
501
502                 // The contact instance must be valid
503                 if (null == branchOffice) {
504                         // Throw NPE again
505                         throw new NullPointerException("branchOffice is null"); //NOI18N
506                 } else if (branchOffice.getBranchId() != null) {
507                         // Throw IAE
508                         throw new IllegalArgumentException(MessageFormat.format("branchOffice.branchId={0} is unexpected.", branchOffice.getBranchId()));
509                 }
510
511                 // Get all phone instances
512                 final DialableLandLineNumber landLineNumber = branchOffice.getBranchLandLineNumber();
513                 final DialableFaxNumber faxNumber = branchOffice.getBranchFaxNumber();
514
515                 // Debug message
516                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("setAllPhoneEntriesCreated: landLineNumber={0},faxNumber={1}", landLineNumber, faxNumber)); //NOI18N
517
518                 // Is a phone number instance set?
519                 if ((landLineNumber instanceof DialableLandLineNumber) && (landLineNumber.getPhoneId() == null)) {
520                         // Debug message
521                         this.getLoggerBeanLocal().logDebug("setAllPhoneEntriesCreated: Setting created timestamp for land-line number ..."); //NOI18N
522
523                         // Set updated timestamp
524                         landLineNumber.setPhoneEntryCreated(new Date());
525                 }
526
527                 // Is a fax number instance set?
528                 if ((faxNumber instanceof DialableFaxNumber) && (faxNumber.getPhoneId() == null)) {
529                         // Debug message
530                         this.getLoggerBeanLocal().logDebug("setAllPhoneEntriesCreated: Setting created timestamp for fax number ..."); //NOI18N
531
532                         // Set updated timestamp
533                         faxNumber.setPhoneEntryCreated(new Date());
534                 }
535
536                 // Trace message
537                 this.getLoggerBeanLocal().logTrace("setAllPhoneEntriesCreated: EXIT!"); //NOI18N
538         }
539
540         /**
541          * Updates all branch office's phone entry's created timestamps
542          * <p>
543          * @param headquarter Headquarters instance to update
544          */
545         protected void setAllPhoneEntriesCreated (final Headquarter headquarter) {
546                 // Trace message
547                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("setAllPhoneEntriesCreated: headquarter={0} - CALLED!", headquarter)); //NOI18N
548
549                 // The contact instance must be valid
550                 if (null == headquarter) {
551                         // Throw NPE again
552                         throw new NullPointerException("headquarter is null"); //NOI18N
553                 } else if (headquarter.getHeadquarterId() != null) {
554                         // Throw IAE
555                         throw new IllegalArgumentException(MessageFormat.format("headquarter.headquarterId={0} is unexpected.", headquarter.getHeadquarterId()));
556                 }
557
558                 // Get all phone instances
559                 final DialableLandLineNumber landLineNumber = headquarter.getHeadquarterLandLineNumber();
560                 final DialableFaxNumber faxNumber = headquarter.getHeadquarterFaxNumber();
561
562                 // Debug message
563                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("setAllPhoneEntriesCreated: landLineNumber={0},faxNumber={1}", landLineNumber, faxNumber)); //NOI18N
564
565                 // Is a phone number instance set?
566                 if ((landLineNumber instanceof DialableLandLineNumber) && (landLineNumber.getPhoneId() == null)) {
567                         // Debug message
568                         this.getLoggerBeanLocal().logDebug("setAllPhoneEntriesCreated: Setting created timestamp for land-line number ..."); //NOI18N
569
570                         // Set updated timestamp
571                         landLineNumber.setPhoneEntryCreated(new Date());
572                 }
573
574                 // Is a fax number instance set?
575                 if ((faxNumber instanceof DialableFaxNumber) && (faxNumber.getPhoneId() == null)) {
576                         // Debug message
577                         this.getLoggerBeanLocal().logDebug("setAllPhoneEntriesCreated: Setting created timestamp for fax number ..."); //NOI18N
578
579                         // Set updated timestamp
580                         faxNumber.setPhoneEntryCreated(new Date());
581                 }
582
583                 // Trace message
584                 this.getLoggerBeanLocal().logTrace("setAllPhoneEntriesCreated: EXIT!"); //NOI18N
585         }
586
587         /**
588          * Updates all company's phone entry's created timestamps
589          * <p>
590          * @param basicData Company basic data instance to update
591          */
592         protected void setAllPhoneEntriesCreated (final BasicData basicData) {
593                 // Trace message
594                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("setAllPhoneEntriesCreated: basicData={0} - CALLED!", basicData)); //NOI18N
595
596                 // The contact instance must be valid
597                 if (null == basicData) {
598                         // Throw NPE again
599                         throw new NullPointerException("basicData is null"); //NOI18N
600                 } else if (basicData.getBasicDataId() != null) {
601                         // Throw IAE
602                         throw new IllegalArgumentException(MessageFormat.format("basicData.basicDataId={0} is unexpected.", basicData.getBasicDataId()));
603                 }
604
605                 // Get all phone instances
606                 final DialableLandLineNumber landLineNumber = basicData.getCompanyLandLineNumber();
607                 final DialableFaxNumber faxNumber = basicData.getCompanyFaxNumber();
608
609                 // Debug message
610                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("setAllPhoneEntriesCreated: landLineNumber={0},faxNumber={1}", landLineNumber, faxNumber)); //NOI18N
611
612                 // Is a phone number instance set?
613                 if ((landLineNumber instanceof DialableLandLineNumber) && (landLineNumber.getPhoneId() == null)) {
614                         // Debug message
615                         this.getLoggerBeanLocal().logDebug("setAllPhoneEntriesCreated: Setting created timestamp for land-line number ..."); //NOI18N
616
617                         // Set updated timestamp
618                         landLineNumber.setPhoneEntryCreated(new Date());
619
620                         // Set it back in basic data
621                         basicData.setCompanyLandLineNumber(landLineNumber);
622                 }
623
624                 // Is a fax number instance set?
625                 if ((faxNumber instanceof DialableFaxNumber) && (faxNumber.getPhoneId() == null)) {
626                         // Debug message
627                         this.getLoggerBeanLocal().logDebug("setAllPhoneEntriesCreated: Setting created timestamp for fax number ..."); //NOI18N
628
629                         // Set updated timestamp
630                         faxNumber.setPhoneEntryCreated(new Date());
631
632                         // Set it back in basic data
633                         basicData.setCompanyFaxNumber(faxNumber);
634                 }
635
636                 // Trace message
637                 this.getLoggerBeanLocal().logTrace("setAllPhoneEntriesCreated: EXIT!"); //NOI18N
638         }
639
640         /**
641          * Returns a detached instance from given mobile instance
642          * <p>
643          * @param mobileNumber  Mobile instance
644          * @param fetchedNumber Found mobile number in database
645          * <p>
646          * @return Detached instance
647          */
648         protected DialableMobileNumber getDetached (final DialableMobileNumber mobileNumber, final DialableMobileNumber fetchedNumber) {
649                 // Trace message
650                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getDetached: mobileNumber={0},fetchedNumber={1} - CALLED!", mobileNumber, fetchedNumber)); //NOI18N
651
652                 // Should be valid
653                 if (null == mobileNumber) {
654                         // Throw NPE
655                         throw new NullPointerException("mobileNumber is null"); //NOI18N
656                 } else if (fetchedNumber.getPhoneId() == null) {
657                         // ..and again
658                         throw new NullPointerException("fetchedNumber.phoneId is null"); //NOI18N
659                 }
660
661                 // Debug message
662                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: fetchedNumber.phoneId={0}", fetchedNumber.getPhoneId())); //NOI18N
663
664                 // Init query instance
665                 final DialableMobileNumber foundNumber = this.getEntityManager().find(fetchedNumber.getClass(), fetchedNumber.getPhoneId());
666
667                 // Debug message
668                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: foundNumber={0}", foundNumber)); //NOI18N
669
670                 // Default is null
671                 DialableMobileNumber detachedNumber = null;
672
673                 // Is there a difference?
674                 if (!PhoneUtils.isSameMobileNumber(mobileNumber, fetchedNumber)) {
675                         // Merge this entry
676                         detachedNumber = this.getEntityManager().merge(foundNumber);
677
678                         // @TODO Copy all
679                 }
680
681                 // Trace message
682                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getDetached: detachedNumber={0} - EXIT!", detachedNumber)); //NOI18N
683
684                 // Return it
685                 return detachedNumber;
686         }
687
688         /**
689          * Returns a detached instance from given land-line instance
690          * <p>
691          * @param landLineNumber Land-line instance
692          * @param fetchedNumber  Found land-line number in database
693          * <p>
694          * @return Detached instance
695          */
696         protected DialableLandLineNumber getDetached (final DialableLandLineNumber landLineNumber, final DialableLandLineNumber fetchedNumber) {
697                 // Trace message
698                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getDetached: landLineNumber={0},fetchedNumber={1} - CALLED!", landLineNumber, fetchedNumber)); //NOI18N
699
700                 // Should be valid
701                 if (null == landLineNumber) {
702                         // Throw NPE
703                         throw new NullPointerException("landLineNumber is null"); //NOI18N
704                 } else if (fetchedNumber.getPhoneId() == null) {
705                         // ..and again
706                         throw new NullPointerException("landLineNumber.phoneId is null"); //NOI18N
707                 }
708
709                 // Debug message
710                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: fetchedNumber.phoneId={0}", fetchedNumber.getPhoneId())); //NOI18N
711
712                 // Init query instance
713                 final DialableLandLineNumber foundNumber = this.getEntityManager().find(fetchedNumber.getClass(), fetchedNumber.getPhoneId());
714
715                 // Debug message
716                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: foundNumber={0}", foundNumber)); //NOI18N
717
718                 // Default is null
719                 DialableLandLineNumber detachedNumber = null;
720
721                 // Is there a difference?
722                 if (!PhoneUtils.isSameLandLineNumber(landLineNumber, fetchedNumber)) {
723                         // Merge this entry
724                         detachedNumber = this.getEntityManager().merge(foundNumber);
725
726                         // @TODO Copy all
727                 }
728
729                 // Trace message
730                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getDetached: detachedNumber={0} - EXIT!", detachedNumber)); //NOI18N
731
732                 // Return it
733                 return detachedNumber;
734         }
735
736         /**
737          * Returns a detached instance from given fax instance
738          * <p>
739          * @param faxNumber     Fax instance
740          * @param fetchedNumber Found fax number in database
741          * <p>
742          * @return Detached instance
743          */
744         protected DialableFaxNumber getDetached (final DialableFaxNumber faxNumber, final DialableFaxNumber fetchedNumber) {
745                 // Trace message
746                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getDetached: faxNumber={0},fetchedNumber={1} - CALLED!", faxNumber, fetchedNumber)); //NOI18N
747
748                 // Should be valid
749                 if (null == faxNumber) {
750                         // Throw NPE
751                         throw new NullPointerException("faxNumber is null"); //NOI18N
752                 } else if (fetchedNumber.getPhoneId() == null) {
753                         // ..and again
754                         throw new NullPointerException("fetchedNumber.phoneId is null"); //NOI18N
755                 }
756
757                 // Debug message
758                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: fetchedNumber.phoneId={0}", fetchedNumber.getPhoneId())); //NOI18N
759
760                 // Init query instance
761                 final DialableFaxNumber foundNumber = this.getEntityManager().find(fetchedNumber.getClass(), fetchedNumber.getPhoneId());
762
763                 // Debug message
764                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: foundNumber={0}", foundNumber)); //NOI18N
765
766                 // Default is null
767                 DialableFaxNumber detachedNumber = null;
768
769                 // Is there a difference?
770                 if (!PhoneUtils.isSameFaxNumber(faxNumber, fetchedNumber)) {
771                         // Merge this entry
772                         detachedNumber = this.getEntityManager().merge(foundNumber);
773
774                         // @TODO Copy all
775                 }
776
777                 // Trace message
778                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getDetached: detachedNumber={0} - EXIT!", detachedNumber)); //NOI18N
779
780                 // Return it
781                 return detachedNumber;
782         }
783
784         /**
785          * Returns a managed instance from given mobile number
786          * <p>
787          * @param mobileNumber  Mobile instance
788          * @param fetchedNumber Found mobile number in database
789          * <p>
790          * @return Managed instance
791          */
792         protected DialableMobileNumber getManaged (final DialableMobileNumber mobileNumber, final DialableMobileNumber fetchedNumber) {
793                 // Trace message
794                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getDetached: mobileNumber={1},fetchedNumber={2} - CALLED!", this.getClass().getSimpleName(), mobileNumber, fetchedNumber)); //NOI18N
795
796                 // Should be valid
797                 if (null == mobileNumber) {
798                         // Throw NPE
799                         throw new NullPointerException("mobileNumber is null"); //NOI18N
800                 } else if (null == fetchedNumber) {
801                         // Throw NPE again
802                         throw new NullPointerException("fetchedNumber is null"); //NOI18N
803                 } else if (fetchedNumber.getPhoneId() == null) {
804                         // ..and again
805                         throw new NullPointerException("fetchedNumber.phoneId is null"); //NOI18N
806                 }
807
808                 // Debug message
809                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: fetchedNumber.phoneId={0}", fetchedNumber.getPhoneId())); //NOI18N
810
811                 // Init query instance
812                 final DialableMobileNumber foundNumber = this.getEntityManager().find(fetchedNumber.getClass(), fetchedNumber.getPhoneId());
813
814                 // Debug message
815                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: foundNumber={0}", foundNumber)); //NOI18N
816
817                 // Default is null
818                 DialableMobileNumber detachedNumber = null;
819
820                 // Is there a difference?
821                 if (!PhoneUtils.isSameMobileNumber(mobileNumber, fetchedNumber)) {
822                         // Merge this entry
823                         detachedNumber = this.getEntityManager().merge(foundNumber);
824
825                         // @TODO Copy all
826                 }
827
828                 // Trace message
829                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getDetached: detachedNumber={1} - EXIT!", this.getClass().getSimpleName(), detachedNumber)); //NOI18N
830
831                 // Return it
832                 return detachedNumber;
833         }
834
835         /**
836          * Returns a managed instance from given land-line number
837          * <p>
838          * @param landLineNumber Land-line instance
839          * @param fetchedNumber  Found land-line number in database
840          * <p>
841          * @return Managed instance
842          */
843         protected DialableLandLineNumber getManaged (final DialableLandLineNumber landLineNumber, final DialableLandLineNumber fetchedNumber) {
844                 // Trace message
845                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getDetached: landLineNumber={1},fetchedNumber={2} - CALLED!", this.getClass().getSimpleName(), landLineNumber, fetchedNumber)); //NOI18N
846
847                 // Should be valid
848                 if (null == landLineNumber) {
849                         // Throw NPE
850                         throw new NullPointerException("landLineNumber is null"); //NOI18N
851                 } else if (null == fetchedNumber) {
852                         // Throw NPE again
853                         throw new NullPointerException("fetchedNumber is null"); //NOI18N
854                 } else if (fetchedNumber.getPhoneId() == null) {
855                         // ..and again
856                         throw new NullPointerException("landLineNumber.phoneId is null"); //NOI18N
857                 }
858
859                 // Debug message
860                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: fetchedNumber.phoneId={0}", fetchedNumber.getPhoneId())); //NOI18N
861
862                 // Init query instance
863                 final DialableLandLineNumber foundNumber = this.getEntityManager().find(fetchedNumber.getClass(), fetchedNumber.getPhoneId());
864
865                 // Debug message
866                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: foundNumber={0}", foundNumber)); //NOI18N
867
868                 // Default is null
869                 DialableLandLineNumber detachedNumber = null;
870
871                 // Is there a difference?
872                 if (!PhoneUtils.isSameLandLineNumber(landLineNumber, fetchedNumber)) {
873                         // Merge this entry
874                         detachedNumber = this.getEntityManager().merge(foundNumber);
875
876                         // @TODO Copy all
877                 }
878
879                 // Trace message
880                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getDetached: managedNumber={1} - EXIT!", this.getClass().getSimpleName(), detachedNumber)); //NOI18N
881
882                 // Return it
883                 return detachedNumber;
884         }
885
886         /**
887          * Returns a managed instance from given fax number
888          * <p>
889          * @param faxNumber     Fax instance
890          * @param fetchedNumber Found fax number in database
891          * <p>
892          * @return Managed instance
893          */
894         protected DialableFaxNumber getManaged (final DialableFaxNumber faxNumber, final DialableFaxNumber fetchedNumber) {
895                 // Trace message
896                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getDetached: faxNumber={1},fetchedNumber={2} - CALLED!", this.getClass().getSimpleName(), faxNumber, fetchedNumber)); //NOI18N
897
898                 // Should be valid
899                 if (null == faxNumber) {
900                         // Throw NPE
901                         throw new NullPointerException("faxNumber is null"); //NOI18N
902                 } else if (null == fetchedNumber) {
903                         // Throw NPE again
904                         throw new NullPointerException("fetchedNumber is null"); //NOI18N
905                 } else if (fetchedNumber.getPhoneId() == null) {
906                         // ..and again
907                         throw new NullPointerException("fetchedNumber.phoneId is null"); //NOI18N
908                 }
909
910                 // Debug message
911                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: fetchedNumber.phoneId={0}", fetchedNumber.getPhoneId())); //NOI18N
912
913                 // Init query instance
914                 final DialableFaxNumber foundNumber = this.getEntityManager().find(fetchedNumber.getClass(), fetchedNumber.getPhoneId());
915
916                 // Debug message
917                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: foundNumber={0}", foundNumber)); //NOI18N
918
919                 // Default is null
920                 DialableFaxNumber detachedNumber = null;
921
922                 // Is there a difference?
923                 if (!PhoneUtils.isSameFaxNumber(faxNumber, fetchedNumber)) {
924                         // Merge this entry
925                         detachedNumber = this.getEntityManager().merge(foundNumber);
926
927                         // @TODO Copy all
928                 }
929
930                 // Trace message
931                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getDetached: managedNumber={1} - EXIT!", this.getClass().getSimpleName(), detachedNumber)); //NOI18N
932
933                 // Return it
934                 return detachedNumber;
935         }
936
937         /**
938          * Merges given contact's data
939          * <p>
940          * @param detachedContact Contact instance to merge
941          * <p>
942          * @return Detached contact instance
943          */
944         protected Contact mergeContactData (final Contact detachedContact) {
945                 // Trace message
946                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.mergeContactData: contact={1} - CALLED!", this.getClass().getSimpleName(), detachedContact)); //NOI18N
947
948                 // The contact instance must be valid
949                 if (null == detachedContact) {
950                         // Throw NPE again
951                         throw new NullPointerException("detachedContact is null"); //NOI18N
952                 } else if (detachedContact.getContactId() == null) {
953                         // Throw NPE again
954                         throw new NullPointerException("detachedContact.contactId is null"); //NOI18N //NOI18N
955                 } else if (detachedContact.getContactId() < 1) {
956                         // Not valid
957                         throw new IllegalStateException(MessageFormat.format("{0}.detachedContact.contactId={1} is not valid.", this.getClass().getSimpleName(), detachedContact.getContactId())); //NOI18N
958                 }
959
960                 // Set updated timestamp
961                 detachedContact.setContactUpdated(new Date());
962
963                 // Get contact from it and find it
964                 final Contact foundContact = this.getEntityManager().find(detachedContact.getClass(), detachedContact.getContactId());
965
966                 // Should be found
967                 assert (foundContact instanceof Contact) : MessageFormat.format("Contact with id {0} not found, but should be.", detachedContact.getContactId()); //NOI18N
968
969                 // Debug message
970                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.updateUserPersonalData: foundContact.contactId={1}", this.getClass().getSimpleName(), foundContact.getContactId())); //NOI18N
971
972                 // Is a fax number set?
973                 if (detachedContact.getContactFaxNumber() instanceof DialableFaxNumber) {
974                         // Make fax numbers managed
975                         foundContact.setContactFaxNumber(this.getManaged(detachedContact.getContactFaxNumber(), detachedContact.getContactFaxNumber()));
976                 }
977
978                 // Is a land-line number set?
979                 if (detachedContact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
980                         // Make land-line numbers managed
981                         foundContact.setContactLandLineNumber(this.getManaged(detachedContact.getContactLandLineNumber(), detachedContact.getContactLandLineNumber()));
982                 }
983
984                 // Is a mobile number set?
985                 if (detachedContact.getContactMobileNumber() instanceof DialableMobileNumber) {
986                         // Make mobile numbers managed
987                         foundContact.setContactMobileNumber(this.getManaged(detachedContact.getContactMobileNumber(), detachedContact.getContactMobileNumber()));
988                 }
989
990                 // Merge contact instance
991                 final Contact managedContact = this.getEntityManager().merge(foundContact);
992
993                 // Copy all
994                 Contacts.copyAll(detachedContact, managedContact);
995
996                 // Trace message
997                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.mergeContactData: foundContact={1} - EXIT!", this.getClass().getSimpleName(), managedContact)); //NOI18N
998
999                 // Return detached contact
1000                 return managedContact;
1001         }
1002
1003         /**
1004          * Merges given (detached) contact's mobile, land-line and fax numbers
1005          * <p>
1006          * @param detachedContact Detached contact instance
1007          */
1008         protected void mergeContactsMobileLandLineFaxNumbers (final Contact detachedContact) {
1009                 // Trace message
1010                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.mergeContactsMobileLandLineFaxNumbers: detachedContact={1} - CALLED!", this.getClass().getSimpleName(), detachedContact)); //NOI18N
1011
1012                 // The contact instance must be valid
1013                 if (null == detachedContact) {
1014                         // Throw NPE again
1015                         throw new NullPointerException("detachedContact is null"); //NOI18N
1016                 } else if (detachedContact.getContactId() == null) {
1017                         // Throw NPE again
1018                         throw new NullPointerException("detachedContact.contactId is null"); //NOI18N //NOI18N
1019                 } else if (detachedContact.getContactId() < 1) {
1020                         // Not valid
1021                         throw new IllegalStateException(MessageFormat.format("{0}.detachedContact.contactId={1} is not valid.", this.getClass().getSimpleName(), detachedContact.getContactId())); //NOI18N
1022                 }
1023
1024                 // Get all instances
1025                 final DialableMobileNumber mobile = detachedContact.getContactMobileNumber();
1026                 final DialableLandLineNumber landLine = detachedContact.getContactLandLineNumber();
1027                 final DialableFaxNumber fax = detachedContact.getContactFaxNumber();
1028
1029                 // Is there a  mobile instance set?
1030                 if (mobile instanceof DialableMobileNumber) {
1031                         // Debug message
1032                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: mobile.phoneId={0} is being updated ...", mobile.getPhoneId())); //NOI18N
1033
1034                         // Then find it, too
1035                         final DialableMobileNumber foundMobile = this.getEntityManager().find(mobile.getClass(), mobile.getPhoneId());
1036
1037                         // Should be there
1038                         assert (foundMobile instanceof DialableMobileNumber) : MessageFormat.format("Mobile number with id {0} not found but should be.", foundMobile.getPhoneId()); //NOI18N
1039
1040                         // Then merge it, too
1041                         final DialableMobileNumber managedMobile = this.getEntityManager().merge(foundMobile);
1042
1043                         // Should be there
1044                         assert (managedMobile instanceof DialableMobileNumber) : MessageFormat.format("Mobile number with id {0} not found but should be.", managedMobile.getPhoneId()); //NOI18N
1045
1046                         // Copy all
1047                         MobileNumbers.copyAll(detachedContact.getContactMobileNumber(), managedMobile);
1048
1049                         // Set it back
1050                         detachedContact.setContactMobileNumber(managedMobile);
1051                 }
1052
1053                 // Is there a  fax instance set?
1054                 if (fax instanceof DialableFaxNumber) {
1055                         // Debug message
1056                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: fax.phoneId={0} is being updated ...", fax.getPhoneId())); //NOI18N
1057
1058                         // Then find it, too
1059                         final DialableFaxNumber foundFax = this.getEntityManager().find(fax.getClass(), fax.getPhoneId());
1060
1061                         // Should be there
1062                         assert (foundFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", foundFax.getPhoneId()); //NOI18N
1063
1064                         // Then merge it, too
1065                         final DialableFaxNumber managedFax = this.getEntityManager().merge(foundFax);
1066
1067                         // Should be there
1068                         assert (managedFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", managedFax.getPhoneId()); //NOI18N
1069
1070                         // Copy all
1071                         FaxNumbers.copyAll(detachedContact.getContactFaxNumber(), managedFax);
1072
1073                         // Set it back
1074                         detachedContact.setContactFaxNumber(managedFax);
1075                 }
1076
1077                 // Is there a  fax instance set?
1078                 if (landLine instanceof DialableLandLineNumber) {
1079                         // Debug message
1080                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: landLine.phoneId={0} is being updated ...", landLine.getPhoneId())); //NOI18N
1081
1082                         // Then find it, too
1083                         final DialableLandLineNumber foundLandLine = this.getEntityManager().find(landLine.getClass(), landLine.getPhoneId());
1084
1085                         // Should be there
1086                         assert (foundLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", foundLandLine.getPhoneId()); //NOI18N
1087
1088                         // Then merge it, too
1089                         final DialableLandLineNumber managedLandLine = this.getEntityManager().merge(foundLandLine);
1090
1091                         // Should be there
1092                         assert (managedLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", managedLandLine.getPhoneId()); //NOI18N
1093
1094                         // Copy all
1095                         LandLineNumbers.copyAll(detachedContact.getContactLandLineNumber(), managedLandLine);
1096
1097                         // Set it back
1098                         detachedContact.setContactLandLineNumber(managedLandLine);
1099                 }
1100
1101                 // Trace message
1102                 this.getLoggerBeanLocal().logTrace("mergeContactsMobileLandLineFaxNumbers: EXIT!"); //NOI18N
1103         }
1104
1105         /**
1106          * Sends an email with given subject line, template name to given recipient
1107          * and user data
1108          * <p>
1109          * @param subjectLine    Subject line
1110          * @param templateName   Template name
1111          * @param user           User instance
1112          * @param baseUrl        Base URL
1113          * @param randomPassword A randomly-generated password or NULL if user had
1114          *                       to enter it.
1115          */
1116         protected void sendEmail (final String subjectLine, final String templateName, final User user, final String baseUrl, final String randomPassword) {
1117                 // Trace message
1118                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("sendEmail: subjectLine={0},templateName={1},user={2},baseUrl={3} - CALLED!", subjectLine, templateName, user, baseUrl)); //NOI18N
1119
1120                 // All should be set
1121                 if (null == subjectLine) {
1122                         // Throw NPE
1123                         throw new NullPointerException("subjectLine is null"); //NOI18N
1124                 } else if (subjectLine.isEmpty()) {
1125                         // No subject line
1126                         throw new IllegalArgumentException("subjectLine is empty"); //NOI18N
1127                 } else if (null == templateName) {
1128                         // Throw NPE
1129                         throw new NullPointerException("templateName is null"); //NOI18N
1130                 } else if (templateName.isEmpty()) {
1131                         // No template name
1132                         throw new IllegalArgumentException("templateName is empty"); //NOI18N
1133                 } else if (null == user) {
1134                         // Throw NPE
1135                         throw new NullPointerException("user is null"); //NOI18N
1136                 } else if (user.getUserId() == null) {
1137                         // Throw NPE again
1138                         throw new NullPointerException("user.userId is null"); //NOI18N
1139                 } else if (user.getUserId() < 1) {
1140                         // Not valid number
1141                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N
1142                 } else if (user.getUserName() == null) {
1143                         // Throw NPE again
1144                         throw new NullPointerException("user.userName is null"); //NOI18N
1145                 } else if (user.getUserName().isEmpty()) {
1146                         // Empty string
1147                         throw new IllegalArgumentException("user.userName is empty"); //NOI18N
1148                 } else if (user.getUserAccountStatus() == null) {
1149                         // Throw NPE
1150                         throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
1151                 } else if (user.getUserContact() == null) {
1152                         // Throw it again
1153                         throw new NullPointerException("user.userContact is null"); //NOI18N
1154                 } else if (user.getUserContact().getContactId() == null) {
1155                         // .. and again
1156                         throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
1157                 } else if (user.getUserContact().getContactId() < 1) {
1158                         // Invalid id
1159                         throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is invalid", user.getUserContact().getContactId())); //NOI18N
1160                 } else if (user.getUserContact().getContactPersonalTitle() == null) {
1161                         // Throw NPE again
1162                         throw new NullPointerException("user.userContact.contactPersonalTitle is null"); //NOI18N
1163                 } else if (user.getUserLocale() == null) {
1164                         // Throw NPE again
1165                         throw new NullPointerException("user.userLocale is null"); //NOI18N
1166                 } else if (this.getSession() == null) {
1167                         // Throw NPE
1168                         throw new NullPointerException("this.session is not set. Have you forgotten to call super(String, String) and called only super() ?"); //NOI18N
1169                 }
1170
1171                 // Set all values
1172                 final Properties variables = Users.getAllUserFields(user);
1173
1174                 // Set base URL and random password
1175                 variables.put("baseUrl", baseUrl); //NOI18N
1176                 variables.put("randomPassword", ""); //NOI18N
1177
1178                 // Is the random password set?
1179                 if ((randomPassword instanceof String) && (!randomPassword.isEmpty())) {
1180                         variables.put("randomPassword", randomPassword); //NOI18N
1181                 }
1182
1183                 // Init addresss
1184                 final Address recipientAddress;
1185
1186                 try {
1187                         // Create email address and set
1188                         recipientAddress = new InternetAddress(user.getUserContact().getContactEmailAddress());
1189                 } catch (final AddressException ex) {
1190                         // Throw again
1191                         throw new EJBException(ex);
1192                 }
1193
1194                 // Prepare mail wrapper
1195                 // @TODO Language from message bundle
1196                 final WrapableEmailDelivery emailWrapper = new EmailDeliveryWrapper(recipientAddress, subjectLine, templateName, variables, user.getUserLocale());
1197
1198                 try {
1199                         // Send out email change
1200                         final ObjectMessage message = this.getSession().createObjectMessage();
1201                         message.setObject(emailWrapper);
1202
1203                         // Send message
1204                         this.sendMessage(message);
1205                 } catch (final JMSException ex) {
1206                         // Throw again
1207                         throw new EJBException(ex);
1208                 }
1209
1210                 // Trace message
1211                 this.getLoggerBeanLocal().logTrace("sendEmail: EXIT!"); //NOI18N
1212         }
1213
1214         /**
1215          * Updates all contact's phone instances from other contact, both contacts
1216          * should be the same.
1217          * <p>
1218          * @param contact Contact to set instances
1219          * @param other   Other contact to get instances from
1220          */
1221         protected void setAllContactPhoneEntries (final Contact contact, final Contact other) {
1222                 // Trace message
1223                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.setAllContactPhoneEntries: contact={1},other={2} - CALLED!", this.getClass().getSimpleName(), contact, other)); //NOI18N
1224
1225                 // Both must be the same and not null
1226                 if (null == contact) {
1227                         // Throw NPE
1228                         throw new NullPointerException("contact is null"); //NOI18N
1229                 } else if (null == other) {
1230                         // Throw NPE
1231                         throw new NullPointerException("other is null"); //NOI18N
1232                 } else if (!Objects.equals(contact, other)) {
1233                         // Not same instances
1234                         throw new IllegalArgumentException(MessageFormat.format("contact={0} and other={1} are not equal!", contact, other)); //NOI18N
1235                 }
1236
1237                 // Debug message
1238                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntries: other.contactMobileNumber={0}", other.getContactMobileNumber())); //NOI18N
1239
1240                 // Is other mobile not set?
1241                 if ((other.getContactMobileNumber() == null) || (PhoneUtils.isSameMobileNumber(contact.getContactMobileNumber(), other.getContactMobileNumber()))) {
1242                         // Debug message
1243                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntries: Copying mobile entry ...", this.getClass().getSimpleName())); //NOI18N
1244
1245                         // Is the fax number set?
1246                         if (other.getContactMobileNumber() instanceof DialableMobileNumber) {
1247                                 // Copy mobile number
1248                                 contact.setContactMobileNumber(this.getManaged(other.getContactMobileNumber(), contact.getContactMobileNumber()));
1249                         } else {
1250                                 // Null it
1251                                 contact.setContactMobileNumber(null);
1252                         }
1253                 }
1254
1255                 // Debug message
1256                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntries: other.contactLandLineNumber={0}", other.getContactLandLineNumber())); //NOI18N
1257
1258                 // Is other mobile not set?
1259                 if ((other.getContactLandLineNumber() == null) || (PhoneUtils.isSameLandLineNumber(contact.getContactLandLineNumber(), other.getContactLandLineNumber()))) {
1260                         // Debug message
1261                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntries: Copying land-line entry ...", this.getClass().getSimpleName())); //NOI18N
1262
1263                         // Is the land-line number set?
1264                         if (other.getContactLandLineNumber() instanceof DialableLandLineNumber) {
1265                                 // Copy land-line number
1266                                 contact.setContactLandLineNumber(this.getManaged(other.getContactLandLineNumber(), contact.getContactLandLineNumber()));
1267                         } else {
1268                                 // Null it
1269                                 contact.setContactLandLineNumber(null);
1270                         }
1271                 }
1272
1273                 // Debug message
1274                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntries: other.contactFaxNumber={1}", this.getClass().getSimpleName(), other.getContactFaxNumber())); //NOI18N
1275
1276                 // Is other mobile not set?
1277                 if ((other.getContactFaxNumber() == null) || (PhoneUtils.isSameFaxNumber(contact.getContactFaxNumber(), other.getContactFaxNumber()))) {
1278                         // Debug message
1279                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntries: Copying fax entry ...", this.getClass().getSimpleName())); //NOI18N
1280
1281                         // Is the fax number set?
1282                         if (other.getContactFaxNumber() instanceof DialableFaxNumber) {
1283                                 // Copy fax number
1284                                 contact.setContactFaxNumber(this.getManaged(other.getContactFaxNumber(), contact.getContactFaxNumber()));
1285                         } else {
1286                                 // Null it
1287                                 contact.setContactFaxNumber(null);
1288                         }
1289                 }
1290
1291                 // Trace message
1292                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.setAllContactPhoneEntries: EXIT!", this.getClass().getSimpleName())); //NOI18N
1293         }
1294
1295         /**
1296          * Updates all contact's phone entry's updated timestamps
1297          * <p>
1298          * @param contact            Contact instance to update
1299          * @param isMobileUnlinked   Whether a mobile entry has been unlinked in
1300          *                           contact instance
1301          * @param isLandlineUnlinked Whether a land-line entry has been unlinked in
1302          *                           contact instance
1303          * @param isFaxUnlinked      Whether a fax entry has been unlinked in
1304          *                           contact instance
1305          */
1306         protected void setAllContactPhoneEntriesUpdated (final Contact contact, final boolean isMobileUnlinked, final boolean isLandlineUnlinked, final boolean isFaxUnlinked) {
1307                 // Trace message
1308                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.setAllContactPhoneEntriesUpdated: contact={1},isMobileUnlinked={2},isLandlineUnlinked={3},isFaxUnlinked={4} - CALLED", this.getClass().getSimpleName(), contact, isMobileUnlinked, isLandlineUnlinked, isFaxUnlinked)); //NOI18N
1309
1310                 // The contact instance must be valid
1311                 if (null == contact) {
1312                         // Throw NPE again
1313                         throw new NullPointerException("contact is null"); //NOI18N
1314                 } else if (contact.getContactId() == null) {
1315                         // Throw NPE again
1316                         throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
1317                 } else if (contact.getContactId() < 1) {
1318                         // Not valid
1319                         throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1320                 }
1321
1322                 // Get all phone instances
1323                 final DialableLandLineNumber landLineNumber = contact.getContactLandLineNumber();
1324                 final DialableFaxNumber faxNumber = contact.getContactFaxNumber();
1325                 final DialableMobileNumber mobileNumber = contact.getContactMobileNumber();
1326
1327                 // Flags and instances must be constistent
1328                 if (isMobileUnlinked && mobileNumber instanceof DialableMobileNumber) {
1329                         // Bad state
1330                         throw new IllegalStateException("isCellPhoneUnlinked is TRUE, but mobileNumber is set."); //NOI18N
1331                 } else if (isLandlineUnlinked && landLineNumber instanceof DialableLandLineNumber) {
1332                         // Bad state
1333                         throw new IllegalStateException("isLandlineUnlinked is TRUE, but landLineNumber is set."); //NOI18N
1334                 } else if (isFaxUnlinked && faxNumber instanceof DialableFaxNumber) {
1335                         // Bad state
1336                         throw new IllegalStateException("isFaxUnlinked is TRUE, but faxNumber is set."); //NOI18N
1337                 }
1338
1339                 // Is a phone number instance set?
1340                 if ((landLineNumber instanceof DialableLandLineNumber) && (landLineNumber.getPhoneId() instanceof Long) && (landLineNumber.getPhoneId() > 0)) {
1341                         // Debug message
1342                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntriesUpdated: Setting updated timestamp for land-line number ...", this.getClass().getSimpleName())); //NOI18N
1343
1344                         // Set updated timestamp
1345                         landLineNumber.setPhoneEntryUpdated(new Date());
1346                 }
1347
1348                 // Is a fax number instance set?
1349                 if ((faxNumber instanceof DialableFaxNumber) && (faxNumber.getPhoneId() instanceof Long) && (faxNumber.getPhoneId() > 0)) {
1350                         // Debug message
1351                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntriesUpdated: Setting updated timestamp for fax number ...", this.getClass().getSimpleName())); //NOI18N
1352
1353                         // Set updated timestamp
1354                         faxNumber.setPhoneEntryUpdated(new Date());
1355                 }
1356
1357                 // Is a mobile number instance set?
1358                 if ((mobileNumber instanceof DialableMobileNumber) && (mobileNumber.getPhoneId() instanceof Long) && (mobileNumber.getPhoneId() > 0)) {
1359                         // Debug message
1360                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntriesUpdated: Setting updated timestamp for mobile number ...", this.getClass().getSimpleName())); //NOI18N
1361
1362                         // Set updated timestamp
1363                         mobileNumber.setPhoneEntryUpdated(new Date());
1364                 }
1365
1366                 // Trace message
1367                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.setAllContactPhoneEntriesUpdated: EXIT!", this.getClass().getSimpleName())); //NOI18N
1368         }
1369
1370 }