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