]> git.mxchange.org Git - addressbook-core.git/commitdiff
added more methods and updated others to latest stuff ... yeah, I'm to lazy now ...
authorRoland Haeder <roland@mxchange.org>
Wed, 27 Apr 2016 19:26:45 +0000 (21:26 +0200)
committerRoland Haeder <roland@mxchange.org>
Wed, 27 Apr 2016 19:26:45 +0000 (21:26 +0200)
src/org/mxchange/pizzaaplication/database/BasePizzaDatabaseBean.java

index c3dcea80a0b5475453d3219f62de8d4399cceb9b..821020ae710492d6c44e7ec33fc0b0cdc41a13b7 100644 (file)
@@ -18,11 +18,13 @@ package org.mxchange.pizzaaplication.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.
@@ -44,6 +46,200 @@ public abstract class BasePizzaDatabaseBean extends BaseDatabaseBean {
                super();
        }
 
+       /**
+        * Updates all contacts's phone entry's created timestamps
+        * <p>
+        * @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
+        * <p>
+        * @param cellphoneNumber Cellphone instance
+        * @param fetchedNumber Found cellphone number in database
+        * <p>
+        * @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
+        * <p>
+        * @param landLineNumber Land-line instance
+        * @param fetchedNumber Found land-line number in database
+        * <p>
+        * @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
+        * <p>
+        * @param faxNumber Fax instance
+        * @param fetchedNumber Found fax number in database
+        * <p>
+        * @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
         * <p>
@@ -52,6 +248,9 @@ public abstract class BasePizzaDatabaseBean extends BaseDatabaseBean {
         * @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
@@ -64,6 +263,9 @@ public abstract class BasePizzaDatabaseBean extends BaseDatabaseBean {
                        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());
 
@@ -79,6 +281,9 @@ public abstract class BasePizzaDatabaseBean extends BaseDatabaseBean {
                // Copy all
                detachedContact.copyAll(contact);
 
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("mergeContactData: detachedContact={0} - EXIT!", detachedContact)); //NOI18N
+
                // Return detached contact
                return detachedContact;
        }
@@ -89,6 +294,9 @@ public abstract class BasePizzaDatabaseBean extends BaseDatabaseBean {
         * @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
@@ -177,6 +385,90 @@ public abstract class BasePizzaDatabaseBean extends BaseDatabaseBean {
                        // 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.
+        * <p>
+        * @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
        }
 
        /**
@@ -191,6 +483,9 @@ public abstract class BasePizzaDatabaseBean extends BaseDatabaseBean {
         * 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
@@ -211,32 +506,44 @@ public abstract class BasePizzaDatabaseBean extends BaseDatabaseBean {
                // Flags and instances must be constistent
                if (isCellphoneUnlinked && cellphoneNumber instanceof DialableCellphoneNumber) {
                        // Bad state
-                       throw new IllegalStateException("isCellPhoneUnlinked is TRUE, but cellphoneNumber is set.");
+                       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.");
+                       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.");
+                       throw new IllegalStateException("isFaxUnlinked is TRUE, but faxNumber is set."); //NOI18N
                }
 
                // Is a phone number instance set?
-               if (landLineNumber instanceof DialableLandLineNumber) {
+               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) {
+               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) {
+               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
        }
 
 }