From: Roland Haeder Date: Thu, 28 Apr 2016 19:04:11 +0000 (+0200) Subject: derived from addressbook-core for jjobs X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=e66d4c1f092325dff96cfee37ec26a0be436e9b0;p=jjobs-core.git derived from addressbook-core for jjobs --- diff --git a/build.xml b/build.xml index 694b90b..8a76039 100644 --- a/build.xml +++ b/build.xml @@ -1,73 +1,73 @@ - - - - - - - - - - - Builds, tests, and runs the project addressbook-core. - - - + + + + + + + + + + + Builds, tests, and runs the project jjobs-core. + + + diff --git a/nbproject/build-impl.xml b/nbproject/build-impl.xml index eaa3933..15aea30 100644 --- a/nbproject/build-impl.xml +++ b/nbproject/build-impl.xml @@ -19,7 +19,7 @@ is divided into following sections: - cleanup --> - + @@ -474,7 +474,7 @@ is divided into following sections: - + @@ -625,7 +625,7 @@ is divided into following sections: - + @@ -917,7 +917,7 @@ is divided into following sections: - + @@ -1402,7 +1402,7 @@ is divided into following sections: - + diff --git a/nbproject/project.properties b/nbproject/project.properties index 83833de..c72ae4f 100644 --- a/nbproject/project.properties +++ b/nbproject/project.properties @@ -3,7 +3,7 @@ annotation.processing.enabled.in.editor=true annotation.processing.processors.list= annotation.processing.run.all.processors=true annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output -application.title=addressbook-core +application.title=jjobs-core application.vendor=rhaeder auxiliary.org-netbeans-spi-editor-hints-projects.perProjectHintSettingsFile=nbproject/cfg_hints.xml build.classes.dir=${build.dir}/classes @@ -26,7 +26,7 @@ debug.test.classpath=\ dist.archive.excludes= # This directory is removed when the project is cleaned: dist.dir=dist -dist.jar=${dist.dir}/addressbook-core.jar +dist.jar=${dist.dir}/jjobs-core.jar dist.javadoc.dir=${dist.dir}/javadoc endorsed.classpath= excludes= @@ -75,7 +75,7 @@ javadoc.private=true javadoc.splitindex=true javadoc.use=true javadoc.version=true -javadoc.windowtitle=Addressbook Core Library +javadoc.windowtitle=JJobs Core Library jnlp.codebase.type=no.codebase jnlp.descriptor=application jnlp.enabled=false diff --git a/nbproject/project.xml b/nbproject/project.xml index 1232b24..6741e87 100644 --- a/nbproject/project.xml +++ b/nbproject/project.xml @@ -3,7 +3,7 @@ org.netbeans.modules.java.j2seproject - addressbook-core + jjobs-core diff --git a/src/org/mxchange/addressbook/database/BaseAddressbookDatabaseBean.java b/src/org/mxchange/addressbook/database/BaseAddressbookDatabaseBean.java deleted file mode 100644 index bbd21e4..0000000 --- a/src/org/mxchange/addressbook/database/BaseAddressbookDatabaseBean.java +++ /dev/null @@ -1,549 +0,0 @@ -/* - * Copyright (C) 2016 Roland Haeder - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.mxchange.addressbook.database; - -import java.text.MessageFormat; -import java.util.GregorianCalendar; -import java.util.Objects; -import org.mxchange.jcontacts.contact.Contact; -import org.mxchange.jcoreee.database.BaseDatabaseBean; -import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber; -import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber; -import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber; -import org.mxchange.jphone.utils.PhoneUtils; - -/** - * A helper class for beans that access the database. - *

- * @author Roland Haeder - */ -public abstract class BaseAddressbookDatabaseBean extends BaseDatabaseBean { - - /** - * Serial number - */ - private static final long serialVersionUID = 12_895_410_275_811_963L; - - /** - * Protected constructor - */ - protected BaseAddressbookDatabaseBean () { - // Call super constructor - super(); - } - - /** - * Updates all contacts's phone entry's created timestamps - *

- * @param contact Contact instance to update - */ - protected void setAllContactPhoneEntriesCreated (final Contact contact) { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("setAllContactPhoneEntriesCreated: contact={0} - CALLED!", contact)); //NOI18N - - // The contact instance must be valid - if (null == contact) { - // Throw NPE again - throw new NullPointerException("contact is null"); //NOI18N - } - - // Get all phone instances - DialableLandLineNumber landLineNumber = contact.getContactLandLineNumber(); - DialableFaxNumber faxNumber = contact.getContactFaxNumber(); - DialableCellphoneNumber cellphoneNumber = contact.getContactCellphoneNumber(); - - // Debug message - this.getLoggerBeanLocal().logDebug(MessageFormat.format("setAllContactPhoneEntriesCreated: landLineNumber={0},faxNumber={1},cellphoneNumber={2}", landLineNumber, faxNumber, cellphoneNumber)); //NOI18N - - // Is a phone number instance set? - if ((landLineNumber instanceof DialableLandLineNumber) && (landLineNumber.getPhoneId() == null)) { - // Debug message - this.getLoggerBeanLocal().logDebug("setAllContactPhoneEntriesCreated: Setting created timestamp for land-line number ..."); //NOI18N - - // Set updated timestamp - landLineNumber.setPhoneEntryCreated(new GregorianCalendar()); - } - - // Is a fax number instance set? - if ((faxNumber instanceof DialableFaxNumber) && (faxNumber.getPhoneId() == null)) { - // Debug message - this.getLoggerBeanLocal().logDebug("setAllContactPhoneEntriesCreated: Setting created timestamp for fax number ..."); //NOI18N - - // Set updated timestamp - faxNumber.setPhoneEntryCreated(new GregorianCalendar()); - } - - // Is a mobile number instance set? - if ((cellphoneNumber instanceof DialableCellphoneNumber) && (cellphoneNumber.getPhoneId() == null)) { - // Debug message - this.getLoggerBeanLocal().logDebug("setAllContactPhoneEntriesCreated: Setting created timestamp for cellphone number ..."); //NOI18N - - // Set updated timestamp - cellphoneNumber.setPhoneEntryCreated(new GregorianCalendar()); - } - - // Trace message - this.getLoggerBeanLocal().logTrace("setAllContactPhoneEntriesCreated: EXIT!"); //NOI18N - } - - /** - * Returnes a detached instance from given cellphone instance - *

- * @param cellphoneNumber Cellphone instance - * @param fetchedNumber Found cellphone number in database - *

- * @return Detached instance - */ - protected DialableCellphoneNumber getDetached (final DialableCellphoneNumber cellphoneNumber, final DialableCellphoneNumber fetchedNumber) { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("getDetached: cellphoneNumber={0},fetchedNumber={1} - CALLED!", cellphoneNumber, fetchedNumber)); - - // Should be valid - if (null == cellphoneNumber) { - // Throw NPE - throw new NullPointerException("cellphoneNumber is null"); - } else if (fetchedNumber.getPhoneId() == null) { - // ..and again - throw new NullPointerException("fetchedNumber.phoneId is null"); - } - - // Debug message - this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: fetchedNumber.phoneId={0}", fetchedNumber.getPhoneId())); - - // Init query instance - DialableCellphoneNumber foundNumber = this.getEntityManager().find(fetchedNumber.getClass(), fetchedNumber.getPhoneId()); - - // Debug message - this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: foundNumber={0}", foundNumber)); - - // Default is null - DialableCellphoneNumber detachedNumber = null; - - // Is there a difference? - if (!PhoneUtils.isSameCellphoneNumber(cellphoneNumber, fetchedNumber)) { - // Merge this entry - detachedNumber = this.getEntityManager().merge(foundNumber); - - // Copy all - } - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("getDetached: detachedNumber={0} - EXIT!", detachedNumber)); - - // Return it - return detachedNumber; - } - - /** - * Returnes a detached instance from given land-line instance - *

- * @param landLineNumber Land-line instance - * @param fetchedNumber Found land-line number in database - *

- * @return Detached instance - */ - protected DialableLandLineNumber getDetached (final DialableLandLineNumber landLineNumber, final DialableLandLineNumber fetchedNumber) { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("getDetached: landLineNumber={0},fetchedNumber={1} - CALLED!", landLineNumber, fetchedNumber)); - - // Should be valid - if (null == landLineNumber) { - // Throw NPE - throw new NullPointerException("landLineNumber is null"); - } else if (fetchedNumber.getPhoneId() == null) { - // ..and again - throw new NullPointerException("landLineNumber.phoneId is null"); - } - - // Debug message - this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: fetchedNumber.phoneId={0}", fetchedNumber.getPhoneId())); - - // Init query instance - DialableLandLineNumber foundNumber = this.getEntityManager().find(fetchedNumber.getClass(), fetchedNumber.getPhoneId()); - - // Debug message - this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: foundNumber={0}", foundNumber)); - - // Default is null - DialableLandLineNumber detachedNumber = null; - - // Is there a difference? - if (!PhoneUtils.isSameLandLineNumber(landLineNumber, fetchedNumber)) { - // Merge this entry - detachedNumber = this.getEntityManager().merge(foundNumber); - } - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("getDetached: detachedNumber={0} - EXIT!", detachedNumber)); - - // Return it - return detachedNumber; - } - - /** - * Returnes a detached instance from given fax instance - *

- * @param faxNumber Fax instance - * @param fetchedNumber Found fax number in database - *

- * @return Detached instance - */ - protected DialableFaxNumber getDetached (final DialableFaxNumber faxNumber, final DialableFaxNumber fetchedNumber) { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("getDetached: faxNumber={0},fetchedNumber={1} - CALLED!", faxNumber, fetchedNumber)); - - // Should be valid - if (null == faxNumber) { - // Throw NPE - throw new NullPointerException("faxNumber is null"); - } else if (fetchedNumber.getPhoneId() == null) { - // ..and again - throw new NullPointerException("fetchedNumber.phoneId is null"); - } - - // Debug message - this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: fetchedNumber.phoneId={0}", fetchedNumber.getPhoneId())); - - // Init query instance - DialableFaxNumber foundNumber = this.getEntityManager().find(fetchedNumber.getClass(), fetchedNumber.getPhoneId()); - - // Debug message - this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: foundNumber={0}", foundNumber)); - - // Default is null - DialableFaxNumber detachedNumber = null; - - // Is there a difference? - if (!PhoneUtils.isSameFaxNumber(faxNumber, fetchedNumber)) { - // Merge this entry - detachedNumber = this.getEntityManager().merge(foundNumber); - } - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("getDetached: detachedNumber={0} - EXIT!", detachedNumber)); - - // Return it - return detachedNumber; - } - - /** - * Merges given contact's data - *

- * @param contact Contact instance to merge - *

- * @return Detached contact instance - */ - protected Contact mergeContactData (final Contact contact) { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("mergeContactData: contact={0} - CALLED!", contact)); //NOI18N - - // The contact instance must be valid - if (null == contact) { - // Throw NPE again - throw new NullPointerException("contact is null"); //NOI18N - } else if (contact.getContactId() == null) { - // Throw NPE again - throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N - } else if (contact.getContactId() < 1) { - // Not valid - throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N - } - - // Set updated timestamp - contact.setContactUpdated(new GregorianCalendar()); - - // Get contact from it and find it - Contact foundContact = this.getEntityManager().find(contact.getClass(), contact.getContactId()); - - // Should be found - assert (foundContact instanceof Contact) : MessageFormat.format("Contact with id {0} not found, but should be.", contact.getContactId()); //NOI18N - - // Debug message - this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: contact.contactId={0}", foundContact.getContactId())); //NOI18N - - // Merge contact instance - Contact detachedContact = this.getEntityManager().merge(foundContact); - - // Copy all - detachedContact.copyAll(contact); - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("mergeContactData: detachedContact={0} - EXIT!", detachedContact)); //NOI18N - - // Return detached contact - return detachedContact; - } - - /** - * Merges given (detached) contact's cellphone, land-line and fax numbers - *

- * @param detachedContact Detached contact instance - */ - protected void mergeContactsCellphoneLandLineFaxNumbers (final Contact detachedContact) { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("mergeContactsCellphoneLandLineFaxNumbers: detachedContact={0} - CALLED!", detachedContact)); //NOI18N - - // The contact instance must be valid - if (null == detachedContact) { - // Throw NPE again - throw new NullPointerException("detachedContact is null"); //NOI18N - } else if (detachedContact.getContactId() == null) { - // Throw NPE again - throw new NullPointerException("detachedContact.contactId is null"); //NOI18N //NOI18N - } else if (detachedContact.getContactId() < 1) { - // Not valid - throw new IllegalStateException(MessageFormat.format("detachedContact.contactId={0} is not valid.", detachedContact.getContactId())); //NOI18N - } - - // Get all instances - DialableCellphoneNumber cellphone = detachedContact.getContactCellphoneNumber(); - DialableLandLineNumber landLine = detachedContact.getContactLandLineNumber(); - DialableFaxNumber fax = detachedContact.getContactFaxNumber(); - - // Is there a cellphone instance set? - if (cellphone instanceof DialableCellphoneNumber) { - // Debug message - this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: cellphone.phoneId={0} is being updated ...", cellphone.getPhoneId())); //NOI18N - - // Then find it, too - DialableCellphoneNumber foundCellphone = this.getEntityManager().find(cellphone.getClass(), cellphone.getPhoneId()); - - // Should be there - assert (foundCellphone instanceof DialableCellphoneNumber) : MessageFormat.format("Cellphone number with id {0} not found but should be.", foundCellphone.getPhoneId()); //NOI18N - - // Then merge it, too - DialableCellphoneNumber detachedCellphone = this.getEntityManager().merge(foundCellphone); - - // Should be there - assert (detachedCellphone instanceof DialableCellphoneNumber) : MessageFormat.format("Cellphone number with id {0} not found but should be.", detachedCellphone.getPhoneId()); //NOI18N - - // Copy all - detachedCellphone.copyAll(detachedContact.getContactCellphoneNumber()); - - // Set it back - detachedContact.setContactCellphoneNumber(detachedCellphone); - } - - // Is there a fax instance set? - if (fax instanceof DialableFaxNumber) { - // Debug message - this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: fax.phoneId={0} is being updated ...", fax.getPhoneId())); //NOI18N - - // Then find it, too - DialableFaxNumber foundFax = this.getEntityManager().find(fax.getClass(), fax.getPhoneId()); - - // Should be there - assert (foundFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", foundFax.getPhoneId()); //NOI18N - - // Then merge it, too - DialableFaxNumber detachedFax = this.getEntityManager().merge(foundFax); - - // Should be there - assert (detachedFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", detachedFax.getPhoneId()); //NOI18N - - // Copy all - detachedFax.copyAll(detachedContact.getContactFaxNumber()); - - // Set it back - detachedContact.setContactFaxNumber(detachedFax); - } - - // Is there a fax instance set? - if (landLine instanceof DialableLandLineNumber) { - // Debug message - this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: landLine.phoneId={0} is being updated ...", landLine.getPhoneId())); //NOI18N - - // Then find it, too - DialableLandLineNumber foundLandLine = this.getEntityManager().find(landLine.getClass(), landLine.getPhoneId()); - - // Should be there - assert (foundLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", foundLandLine.getPhoneId()); //NOI18N - - // Then merge it, too - DialableLandLineNumber detachedLandLine = this.getEntityManager().merge(foundLandLine); - - // Should be there - assert (detachedLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", detachedLandLine.getPhoneId()); //NOI18N - - // Copy all - detachedLandLine.copyAll(detachedContact.getContactLandLineNumber()); - - // Set it back - detachedContact.setContactLandLineNumber(detachedLandLine); - } - - // Trace message - this.getLoggerBeanLocal().logTrace("mergeContactsCellphoneLandLineFaxNumbers: EXIT!"); //NOI18N - } - - /** - * Updates all contact's phone instances from other contact, both contacts - * should be the same. - *

- * @param contact Contact to set instances - * @param other Other contact to get instances from - */ - protected void setAllContactPhoneEntries (final Contact contact, final Contact other) { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("setAllContactPhoneEntries: contact={0},other={1} - CALLED!", contact, other)); //NOI18N - - // Both must be the same and not null - if (null == contact) { - // Throw NPE - throw new NullPointerException("contact is null"); //NOI18N - } else if (null == other) { - // Throw NPE - throw new NullPointerException("other is null"); //NOI18N - } else if (!Objects.equals(contact, other)) { - // Not same instances - throw new IllegalArgumentException(MessageFormat.format("contact={0} and other={1} are not equal!", contact, other)); //NOI18N - } - - // Debug message - this.getLoggerBeanLocal().logDebug(MessageFormat.format("setAllContactPhoneEntries: other.contactCellphoneNumber={0}", other.getContactCellphoneNumber())); //NOI18N - - // Is other cellphone not set? - if ((other.getContactCellphoneNumber() == null) || (PhoneUtils.isSameCellphoneNumber(contact.getContactCellphoneNumber(), other.getContactCellphoneNumber()))) { - // Debug message - this.getLoggerBeanLocal().logDebug("setAllContactPhoneEntries: Copying cellphone entry ..."); //NOI18N - - // Is the fax number set? - if (other.getContactCellphoneNumber() instanceof DialableCellphoneNumber) { - // Copy cellphone number - contact.setContactCellphoneNumber(this.getDetached(other.getContactCellphoneNumber(), contact.getContactCellphoneNumber())); - } else { - // Null it - contact.setContactCellphoneNumber(null); - } - } - - // Debug message - this.getLoggerBeanLocal().logDebug(MessageFormat.format("setAllContactPhoneEntries: other.contactLandLineNumber={0}", other.getContactLandLineNumber())); //NOI18N - - // Is other cellphone not set? - if ((other.getContactLandLineNumber() == null) || (PhoneUtils.isSameLandLineNumber(contact.getContactLandLineNumber(), other.getContactLandLineNumber()))) { - // Debug message - this.getLoggerBeanLocal().logDebug("setAllContactPhoneEntries: Copying land-line entry ..."); //NOI18N - - // Is the land-line number set? - if (other.getContactLandLineNumber() instanceof DialableLandLineNumber) { - // Copy land-line number - contact.setContactLandLineNumber(this.getDetached(other.getContactLandLineNumber(), contact.getContactLandLineNumber())); - } else { - // Null it - contact.setContactLandLineNumber(null); - } - } - - // Debug message - this.getLoggerBeanLocal().logDebug(MessageFormat.format("setAllContactPhoneEntries: other.contactFaxNumber={0}", other.getContactFaxNumber())); //NOI18N - - // Is other cellphone not set? - if ((other.getContactFaxNumber() == null) || (PhoneUtils.isSameFaxNumber(contact.getContactFaxNumber(), other.getContactFaxNumber()))) { - // Debug message - this.getLoggerBeanLocal().logDebug("setAllContactPhoneEntries: Copying fax entry ..."); //NOI18N - - // Is the fax number set? - if (other.getContactFaxNumber() instanceof DialableFaxNumber) { - // Copy fax number - contact.setContactFaxNumber(this.getDetached(other.getContactFaxNumber(), contact.getContactFaxNumber())); - } else { - // Null it - contact.setContactFaxNumber(null); - } - } - - // Trace message - this.getLoggerBeanLocal().logTrace("setAllContactPhoneEntries: EXIT!"); //NOI18N - } - - /** - * Updates all contacts's phone entry's updated timestamps - *

- * @param contact Contact instance to update - * @param isCellphoneUnlinked Whether a cellphone entry has been unlinked in - * contact instance - * @param isLandlineUnlinked Whether a land-line entry has been unlinked in - * contact instance - * @param isFaxUnlinked Whether a fax entry has been unlinked in contact - * instance - */ - protected void setAllContactPhoneEntriesUpdated (final Contact contact, final boolean isCellphoneUnlinked, final boolean isLandlineUnlinked, final boolean isFaxUnlinked) { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("setAllContactPhoneEntriesUpdated: contact={0},isCellphoneUnlinked={1},isLandlineUnlinked={2},isFaxUnlinked={3} - CALLED", contact, isCellphoneUnlinked, isLandlineUnlinked, isFaxUnlinked)); //NOI18N - - // The contact instance must be valid - if (null == contact) { - // Throw NPE again - throw new NullPointerException("contact is null"); //NOI18N - } else if (contact.getContactId() == null) { - // Throw NPE again - throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N - } else if (contact.getContactId() < 1) { - // Not valid - throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N - } - - // Get all phone instances - DialableLandLineNumber landLineNumber = contact.getContactLandLineNumber(); - DialableFaxNumber faxNumber = contact.getContactFaxNumber(); - DialableCellphoneNumber cellphoneNumber = contact.getContactCellphoneNumber(); - - // Flags and instances must be constistent - if (isCellphoneUnlinked && cellphoneNumber instanceof DialableCellphoneNumber) { - // Bad state - throw new IllegalStateException("isCellPhoneUnlinked is TRUE, but cellphoneNumber is set."); //NOI18N - } else if (isLandlineUnlinked && landLineNumber instanceof DialableLandLineNumber) { - // Bad state - throw new IllegalStateException("isLandlineUnlinked is TRUE, but landLineNumber is set."); //NOI18N - } else if (isFaxUnlinked && faxNumber instanceof DialableFaxNumber) { - // Bad state - throw new IllegalStateException("isFaxUnlinked is TRUE, but faxNumber is set."); //NOI18N - } - - // Is a phone number instance set? - if ((landLineNumber instanceof DialableLandLineNumber) && (landLineNumber.getPhoneId() instanceof Long) && (landLineNumber.getPhoneId() > 0)) { - // Debug message - this.getLoggerBeanLocal().logDebug("setAllContactPhoneEntriesUpdated: Setting updated timestamp for land-line number ..."); //NOI18N - - // Set updated timestamp - landLineNumber.setPhoneEntryUpdated(new GregorianCalendar()); - } - - // Is a fax number instance set? - if ((faxNumber instanceof DialableFaxNumber) && (faxNumber.getPhoneId() instanceof Long) && (faxNumber.getPhoneId() > 0)) { - // Debug message - this.getLoggerBeanLocal().logDebug("setAllContactPhoneEntriesUpdated: Setting updated timestamp for fax number ..."); //NOI18N - - // Set updated timestamp - faxNumber.setPhoneEntryUpdated(new GregorianCalendar()); - } - - // Is a mobile number instance set? - if ((cellphoneNumber instanceof DialableCellphoneNumber) && (cellphoneNumber.getPhoneId() instanceof Long) && (cellphoneNumber.getPhoneId() > 0)) { - // Debug message - this.getLoggerBeanLocal().logDebug("setAllContactPhoneEntriesUpdated: Setting updated timestamp for cellphone number ..."); //NOI18N - - // Set updated timestamp - cellphoneNumber.setPhoneEntryUpdated(new GregorianCalendar()); - } - - // Trace message - this.getLoggerBeanLocal().logTrace("setAllContactPhoneEntriesUpdated: EXIT!"); //NOI18N - } - -} diff --git a/src/org/mxchange/jjobs/database/BaseJobsDatabaseBean.java b/src/org/mxchange/jjobs/database/BaseJobsDatabaseBean.java new file mode 100644 index 0000000..51d1346 --- /dev/null +++ b/src/org/mxchange/jjobs/database/BaseJobsDatabaseBean.java @@ -0,0 +1,549 @@ +/* + * Copyright (C) 2016 Roland Haeder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.jjobs.database; + +import java.text.MessageFormat; +import java.util.GregorianCalendar; +import java.util.Objects; +import org.mxchange.jcontacts.contact.Contact; +import org.mxchange.jcoreee.database.BaseDatabaseBean; +import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber; +import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber; +import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber; +import org.mxchange.jphone.utils.PhoneUtils; + +/** + * A helper class for beans that access the database. + *

+ * @author Roland Haeder + */ +public abstract class BaseJobsDatabaseBean extends BaseDatabaseBean { + + /** + * Serial number + */ + private static final long serialVersionUID = 12_895_410_275_811_963L; + + /** + * Protected constructor + */ + protected BaseJobsDatabaseBean () { + // Call super constructor + super(); + } + + /** + * Updates all contacts's phone entry's created timestamps + *

+ * @param contact Contact instance to update + */ + protected void setAllContactPhoneEntriesCreated (final Contact contact) { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("setAllContactPhoneEntriesCreated: contact={0} - CALLED!", contact)); //NOI18N + + // The contact instance must be valid + if (null == contact) { + // Throw NPE again + throw new NullPointerException("contact is null"); //NOI18N + } + + // Get all phone instances + DialableLandLineNumber landLineNumber = contact.getContactLandLineNumber(); + DialableFaxNumber faxNumber = contact.getContactFaxNumber(); + DialableCellphoneNumber cellphoneNumber = contact.getContactCellphoneNumber(); + + // Debug message + this.getLoggerBeanLocal().logDebug(MessageFormat.format("setAllContactPhoneEntriesCreated: landLineNumber={0},faxNumber={1},cellphoneNumber={2}", landLineNumber, faxNumber, cellphoneNumber)); //NOI18N + + // Is a phone number instance set? + if ((landLineNumber instanceof DialableLandLineNumber) && (landLineNumber.getPhoneId() == null)) { + // Debug message + this.getLoggerBeanLocal().logDebug("setAllContactPhoneEntriesCreated: Setting created timestamp for land-line number ..."); //NOI18N + + // Set updated timestamp + landLineNumber.setPhoneEntryCreated(new GregorianCalendar()); + } + + // Is a fax number instance set? + if ((faxNumber instanceof DialableFaxNumber) && (faxNumber.getPhoneId() == null)) { + // Debug message + this.getLoggerBeanLocal().logDebug("setAllContactPhoneEntriesCreated: Setting created timestamp for fax number ..."); //NOI18N + + // Set updated timestamp + faxNumber.setPhoneEntryCreated(new GregorianCalendar()); + } + + // Is a mobile number instance set? + if ((cellphoneNumber instanceof DialableCellphoneNumber) && (cellphoneNumber.getPhoneId() == null)) { + // Debug message + this.getLoggerBeanLocal().logDebug("setAllContactPhoneEntriesCreated: Setting created timestamp for cellphone number ..."); //NOI18N + + // Set updated timestamp + cellphoneNumber.setPhoneEntryCreated(new GregorianCalendar()); + } + + // Trace message + this.getLoggerBeanLocal().logTrace("setAllContactPhoneEntriesCreated: EXIT!"); //NOI18N + } + + /** + * Returnes a detached instance from given cellphone instance + *

+ * @param cellphoneNumber Cellphone instance + * @param fetchedNumber Found cellphone number in database + *

+ * @return Detached instance + */ + protected DialableCellphoneNumber getDetached (final DialableCellphoneNumber cellphoneNumber, final DialableCellphoneNumber fetchedNumber) { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("getDetached: cellphoneNumber={0},fetchedNumber={1} - CALLED!", cellphoneNumber, fetchedNumber)); + + // Should be valid + if (null == cellphoneNumber) { + // Throw NPE + throw new NullPointerException("cellphoneNumber is null"); + } else if (fetchedNumber.getPhoneId() == null) { + // ..and again + throw new NullPointerException("fetchedNumber.phoneId is null"); + } + + // Debug message + this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: fetchedNumber.phoneId={0}", fetchedNumber.getPhoneId())); + + // Init query instance + DialableCellphoneNumber foundNumber = this.getEntityManager().find(fetchedNumber.getClass(), fetchedNumber.getPhoneId()); + + // Debug message + this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: foundNumber={0}", foundNumber)); + + // Default is null + DialableCellphoneNumber detachedNumber = null; + + // Is there a difference? + if (!PhoneUtils.isSameCellphoneNumber(cellphoneNumber, fetchedNumber)) { + // Merge this entry + detachedNumber = this.getEntityManager().merge(foundNumber); + + // Copy all + } + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("getDetached: detachedNumber={0} - EXIT!", detachedNumber)); + + // Return it + return detachedNumber; + } + + /** + * Returnes a detached instance from given land-line instance + *

+ * @param landLineNumber Land-line instance + * @param fetchedNumber Found land-line number in database + *

+ * @return Detached instance + */ + protected DialableLandLineNumber getDetached (final DialableLandLineNumber landLineNumber, final DialableLandLineNumber fetchedNumber) { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("getDetached: landLineNumber={0},fetchedNumber={1} - CALLED!", landLineNumber, fetchedNumber)); + + // Should be valid + if (null == landLineNumber) { + // Throw NPE + throw new NullPointerException("landLineNumber is null"); + } else if (fetchedNumber.getPhoneId() == null) { + // ..and again + throw new NullPointerException("landLineNumber.phoneId is null"); + } + + // Debug message + this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: fetchedNumber.phoneId={0}", fetchedNumber.getPhoneId())); + + // Init query instance + DialableLandLineNumber foundNumber = this.getEntityManager().find(fetchedNumber.getClass(), fetchedNumber.getPhoneId()); + + // Debug message + this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: foundNumber={0}", foundNumber)); + + // Default is null + DialableLandLineNumber detachedNumber = null; + + // Is there a difference? + if (!PhoneUtils.isSameLandLineNumber(landLineNumber, fetchedNumber)) { + // Merge this entry + detachedNumber = this.getEntityManager().merge(foundNumber); + } + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("getDetached: detachedNumber={0} - EXIT!", detachedNumber)); + + // Return it + return detachedNumber; + } + + /** + * Returnes a detached instance from given fax instance + *

+ * @param faxNumber Fax instance + * @param fetchedNumber Found fax number in database + *

+ * @return Detached instance + */ + protected DialableFaxNumber getDetached (final DialableFaxNumber faxNumber, final DialableFaxNumber fetchedNumber) { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("getDetached: faxNumber={0},fetchedNumber={1} - CALLED!", faxNumber, fetchedNumber)); + + // Should be valid + if (null == faxNumber) { + // Throw NPE + throw new NullPointerException("faxNumber is null"); + } else if (fetchedNumber.getPhoneId() == null) { + // ..and again + throw new NullPointerException("fetchedNumber.phoneId is null"); + } + + // Debug message + this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: fetchedNumber.phoneId={0}", fetchedNumber.getPhoneId())); + + // Init query instance + DialableFaxNumber foundNumber = this.getEntityManager().find(fetchedNumber.getClass(), fetchedNumber.getPhoneId()); + + // Debug message + this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: foundNumber={0}", foundNumber)); + + // Default is null + DialableFaxNumber detachedNumber = null; + + // Is there a difference? + if (!PhoneUtils.isSameFaxNumber(faxNumber, fetchedNumber)) { + // Merge this entry + detachedNumber = this.getEntityManager().merge(foundNumber); + } + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("getDetached: detachedNumber={0} - EXIT!", detachedNumber)); + + // Return it + return detachedNumber; + } + + /** + * Merges given contact's data + *

+ * @param contact Contact instance to merge + *

+ * @return Detached contact instance + */ + protected Contact mergeContactData (final Contact contact) { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("mergeContactData: contact={0} - CALLED!", contact)); //NOI18N + + // The contact instance must be valid + if (null == contact) { + // Throw NPE again + throw new NullPointerException("contact is null"); //NOI18N + } else if (contact.getContactId() == null) { + // Throw NPE again + throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N + } else if (contact.getContactId() < 1) { + // Not valid + throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N + } + + // Set updated timestamp + contact.setContactUpdated(new GregorianCalendar()); + + // Get contact from it and find it + Contact foundContact = this.getEntityManager().find(contact.getClass(), contact.getContactId()); + + // Should be found + assert (foundContact instanceof Contact) : MessageFormat.format("Contact with id {0} not found, but should be.", contact.getContactId()); //NOI18N + + // Debug message + this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: contact.contactId={0}", foundContact.getContactId())); //NOI18N + + // Merge contact instance + Contact detachedContact = this.getEntityManager().merge(foundContact); + + // Copy all + detachedContact.copyAll(contact); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("mergeContactData: detachedContact={0} - EXIT!", detachedContact)); //NOI18N + + // Return detached contact + return detachedContact; + } + + /** + * Merges given (detached) contact's cellphone, land-line and fax numbers + *

+ * @param detachedContact Detached contact instance + */ + protected void mergeContactsCellphoneLandLineFaxNumbers (final Contact detachedContact) { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("mergeContactsCellphoneLandLineFaxNumbers: detachedContact={0} - CALLED!", detachedContact)); //NOI18N + + // The contact instance must be valid + if (null == detachedContact) { + // Throw NPE again + throw new NullPointerException("detachedContact is null"); //NOI18N + } else if (detachedContact.getContactId() == null) { + // Throw NPE again + throw new NullPointerException("detachedContact.contactId is null"); //NOI18N //NOI18N + } else if (detachedContact.getContactId() < 1) { + // Not valid + throw new IllegalStateException(MessageFormat.format("detachedContact.contactId={0} is not valid.", detachedContact.getContactId())); //NOI18N + } + + // Get all instances + DialableCellphoneNumber cellphone = detachedContact.getContactCellphoneNumber(); + DialableLandLineNumber landLine = detachedContact.getContactLandLineNumber(); + DialableFaxNumber fax = detachedContact.getContactFaxNumber(); + + // Is there a cellphone instance set? + if (cellphone instanceof DialableCellphoneNumber) { + // Debug message + this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: cellphone.phoneId={0} is being updated ...", cellphone.getPhoneId())); //NOI18N + + // Then find it, too + DialableCellphoneNumber foundCellphone = this.getEntityManager().find(cellphone.getClass(), cellphone.getPhoneId()); + + // Should be there + assert (foundCellphone instanceof DialableCellphoneNumber) : MessageFormat.format("Cellphone number with id {0} not found but should be.", foundCellphone.getPhoneId()); //NOI18N + + // Then merge it, too + DialableCellphoneNumber detachedCellphone = this.getEntityManager().merge(foundCellphone); + + // Should be there + assert (detachedCellphone instanceof DialableCellphoneNumber) : MessageFormat.format("Cellphone number with id {0} not found but should be.", detachedCellphone.getPhoneId()); //NOI18N + + // Copy all + detachedCellphone.copyAll(detachedContact.getContactCellphoneNumber()); + + // Set it back + detachedContact.setContactCellphoneNumber(detachedCellphone); + } + + // Is there a fax instance set? + if (fax instanceof DialableFaxNumber) { + // Debug message + this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: fax.phoneId={0} is being updated ...", fax.getPhoneId())); //NOI18N + + // Then find it, too + DialableFaxNumber foundFax = this.getEntityManager().find(fax.getClass(), fax.getPhoneId()); + + // Should be there + assert (foundFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", foundFax.getPhoneId()); //NOI18N + + // Then merge it, too + DialableFaxNumber detachedFax = this.getEntityManager().merge(foundFax); + + // Should be there + assert (detachedFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", detachedFax.getPhoneId()); //NOI18N + + // Copy all + detachedFax.copyAll(detachedContact.getContactFaxNumber()); + + // Set it back + detachedContact.setContactFaxNumber(detachedFax); + } + + // Is there a fax instance set? + if (landLine instanceof DialableLandLineNumber) { + // Debug message + this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: landLine.phoneId={0} is being updated ...", landLine.getPhoneId())); //NOI18N + + // Then find it, too + DialableLandLineNumber foundLandLine = this.getEntityManager().find(landLine.getClass(), landLine.getPhoneId()); + + // Should be there + assert (foundLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", foundLandLine.getPhoneId()); //NOI18N + + // Then merge it, too + DialableLandLineNumber detachedLandLine = this.getEntityManager().merge(foundLandLine); + + // Should be there + assert (detachedLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", detachedLandLine.getPhoneId()); //NOI18N + + // Copy all + detachedLandLine.copyAll(detachedContact.getContactLandLineNumber()); + + // Set it back + detachedContact.setContactLandLineNumber(detachedLandLine); + } + + // Trace message + this.getLoggerBeanLocal().logTrace("mergeContactsCellphoneLandLineFaxNumbers: EXIT!"); //NOI18N + } + + /** + * Updates all contact's phone instances from other contact, both contacts + * should be the same. + *

+ * @param contact Contact to set instances + * @param other Other contact to get instances from + */ + protected void setAllContactPhoneEntries (final Contact contact, final Contact other) { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("setAllContactPhoneEntries: contact={0},other={1} - CALLED!", contact, other)); //NOI18N + + // Both must be the same and not null + if (null == contact) { + // Throw NPE + throw new NullPointerException("contact is null"); //NOI18N + } else if (null == other) { + // Throw NPE + throw new NullPointerException("other is null"); //NOI18N + } else if (!Objects.equals(contact, other)) { + // Not same instances + throw new IllegalArgumentException(MessageFormat.format("contact={0} and other={1} are not equal!", contact, other)); //NOI18N + } + + // Debug message + this.getLoggerBeanLocal().logDebug(MessageFormat.format("setAllContactPhoneEntries: other.contactCellphoneNumber={0}", other.getContactCellphoneNumber())); //NOI18N + + // Is other cellphone not set? + if ((other.getContactCellphoneNumber() == null) || (PhoneUtils.isSameCellphoneNumber(contact.getContactCellphoneNumber(), other.getContactCellphoneNumber()))) { + // Debug message + this.getLoggerBeanLocal().logDebug("setAllContactPhoneEntries: Copying cellphone entry ..."); //NOI18N + + // Is the fax number set? + if (other.getContactCellphoneNumber() instanceof DialableCellphoneNumber) { + // Copy cellphone number + contact.setContactCellphoneNumber(this.getDetached(other.getContactCellphoneNumber(), contact.getContactCellphoneNumber())); + } else { + // Null it + contact.setContactCellphoneNumber(null); + } + } + + // Debug message + this.getLoggerBeanLocal().logDebug(MessageFormat.format("setAllContactPhoneEntries: other.contactLandLineNumber={0}", other.getContactLandLineNumber())); //NOI18N + + // Is other cellphone not set? + if ((other.getContactLandLineNumber() == null) || (PhoneUtils.isSameLandLineNumber(contact.getContactLandLineNumber(), other.getContactLandLineNumber()))) { + // Debug message + this.getLoggerBeanLocal().logDebug("setAllContactPhoneEntries: Copying land-line entry ..."); //NOI18N + + // Is the land-line number set? + if (other.getContactLandLineNumber() instanceof DialableLandLineNumber) { + // Copy land-line number + contact.setContactLandLineNumber(this.getDetached(other.getContactLandLineNumber(), contact.getContactLandLineNumber())); + } else { + // Null it + contact.setContactLandLineNumber(null); + } + } + + // Debug message + this.getLoggerBeanLocal().logDebug(MessageFormat.format("setAllContactPhoneEntries: other.contactFaxNumber={0}", other.getContactFaxNumber())); //NOI18N + + // Is other cellphone not set? + if ((other.getContactFaxNumber() == null) || (PhoneUtils.isSameFaxNumber(contact.getContactFaxNumber(), other.getContactFaxNumber()))) { + // Debug message + this.getLoggerBeanLocal().logDebug("setAllContactPhoneEntries: Copying fax entry ..."); //NOI18N + + // Is the fax number set? + if (other.getContactFaxNumber() instanceof DialableFaxNumber) { + // Copy fax number + contact.setContactFaxNumber(this.getDetached(other.getContactFaxNumber(), contact.getContactFaxNumber())); + } else { + // Null it + contact.setContactFaxNumber(null); + } + } + + // Trace message + this.getLoggerBeanLocal().logTrace("setAllContactPhoneEntries: EXIT!"); //NOI18N + } + + /** + * Updates all contacts's phone entry's updated timestamps + *

+ * @param contact Contact instance to update + * @param isCellphoneUnlinked Whether a cellphone entry has been unlinked in + * contact instance + * @param isLandlineUnlinked Whether a land-line entry has been unlinked in + * contact instance + * @param isFaxUnlinked Whether a fax entry has been unlinked in contact + * instance + */ + protected void setAllContactPhoneEntriesUpdated (final Contact contact, final boolean isCellphoneUnlinked, final boolean isLandlineUnlinked, final boolean isFaxUnlinked) { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("setAllContactPhoneEntriesUpdated: contact={0},isCellphoneUnlinked={1},isLandlineUnlinked={2},isFaxUnlinked={3} - CALLED", contact, isCellphoneUnlinked, isLandlineUnlinked, isFaxUnlinked)); //NOI18N + + // The contact instance must be valid + if (null == contact) { + // Throw NPE again + throw new NullPointerException("contact is null"); //NOI18N + } else if (contact.getContactId() == null) { + // Throw NPE again + throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N + } else if (contact.getContactId() < 1) { + // Not valid + throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N + } + + // Get all phone instances + DialableLandLineNumber landLineNumber = contact.getContactLandLineNumber(); + DialableFaxNumber faxNumber = contact.getContactFaxNumber(); + DialableCellphoneNumber cellphoneNumber = contact.getContactCellphoneNumber(); + + // Flags and instances must be constistent + if (isCellphoneUnlinked && cellphoneNumber instanceof DialableCellphoneNumber) { + // Bad state + throw new IllegalStateException("isCellPhoneUnlinked is TRUE, but cellphoneNumber is set."); //NOI18N + } else if (isLandlineUnlinked && landLineNumber instanceof DialableLandLineNumber) { + // Bad state + throw new IllegalStateException("isLandlineUnlinked is TRUE, but landLineNumber is set."); //NOI18N + } else if (isFaxUnlinked && faxNumber instanceof DialableFaxNumber) { + // Bad state + throw new IllegalStateException("isFaxUnlinked is TRUE, but faxNumber is set."); //NOI18N + } + + // Is a phone number instance set? + if ((landLineNumber instanceof DialableLandLineNumber) && (landLineNumber.getPhoneId() instanceof Long) && (landLineNumber.getPhoneId() > 0)) { + // Debug message + this.getLoggerBeanLocal().logDebug("setAllContactPhoneEntriesUpdated: Setting updated timestamp for land-line number ..."); //NOI18N + + // Set updated timestamp + landLineNumber.setPhoneEntryUpdated(new GregorianCalendar()); + } + + // Is a fax number instance set? + if ((faxNumber instanceof DialableFaxNumber) && (faxNumber.getPhoneId() instanceof Long) && (faxNumber.getPhoneId() > 0)) { + // Debug message + this.getLoggerBeanLocal().logDebug("setAllContactPhoneEntriesUpdated: Setting updated timestamp for fax number ..."); //NOI18N + + // Set updated timestamp + faxNumber.setPhoneEntryUpdated(new GregorianCalendar()); + } + + // Is a mobile number instance set? + if ((cellphoneNumber instanceof DialableCellphoneNumber) && (cellphoneNumber.getPhoneId() instanceof Long) && (cellphoneNumber.getPhoneId() > 0)) { + // Debug message + this.getLoggerBeanLocal().logDebug("setAllContactPhoneEntriesUpdated: Setting updated timestamp for cellphone number ..."); //NOI18N + + // Set updated timestamp + cellphoneNumber.setPhoneEntryUpdated(new GregorianCalendar()); + } + + // Trace message + this.getLoggerBeanLocal().logTrace("setAllContactPhoneEntriesUpdated: EXIT!"); //NOI18N + } + +}