]> git.mxchange.org Git - pizzaservice-ejb.git/commitdiff
Please cherry-pick:
authorRoland Häder <roland@mxchange.org>
Tue, 23 Aug 2016 09:33:14 +0000 (11:33 +0200)
committerRoland Haeder <roland@mxchange.org>
Wed, 24 Aug 2016 19:22:23 +0000 (21:22 +0200)
- detachAllContactPhoneEntries() was a bad idea, beter do it the old way.
- The returned instance was a managed instance that have been updated. First find it with find() then you have a managed instanced. Now simply update the fields you want and you are done.

src/java/org/mxchange/jcontacts/contact/PizzaContactSessionBean.java
src/java/org/mxchange/jcontacts/phone/PizzaAdminContactPhoneSessionBean.java

index 2e09138e19eb636f9bf7da88ae4ef02a6ab80e9a..1d1d5d6d51f9b88d5fdf4feaa84cbfbb170e055a 100644 (file)
@@ -17,7 +17,6 @@
 package org.mxchange.jcontacts.contact;
 
 import java.text.MessageFormat;
-import java.util.GregorianCalendar;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Objects;
@@ -283,20 +282,19 @@ public class PizzaContactSessionBean extends BasePizzaDatabaseBean implements Co
                        throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
                }
 
-               // Set updated timestamp
-               contact.setContactUpdated(new GregorianCalendar());
-
-               // Detach all phone numbers
-               this.detachAllContactPhoneEntries(contact);
+               // Remove all referenced phone numbers
+               contact.setContactFaxNumber(null);
+               contact.setContactLandLineNumber(null);
+               contact.setContactMobileNumber(null);
 
                // Merge cellphone, land-line and fix
-               Contact detachedContact = this.mergeContactData(contact);
+               Contact managedContact = this.mergeContactData(contact);
 
                // Trace message
-               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: detachedContact={1} - EXIT!", this.getClass().getSimpleName(), detachedContact)); //NOI18N
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: managedContact={1} - EXIT!", this.getClass().getSimpleName(), managedContact)); //NOI18N
 
                // Return it
-               return detachedContact;
+               return managedContact;
        }
 
        @Override
index 3cc997cd30ddfcaa228df5199a8d586544830cf9..1d83d18d7df622c6a3407067e3e64f8a870f4baf 100644 (file)
@@ -23,6 +23,7 @@ import javax.ejb.EJB;
 import javax.ejb.Stateless;
 import org.mxchange.jcontacts.contact.Contact;
 import org.mxchange.jcontacts.contact.ContactSessionBeanRemote;
+import org.mxchange.jcontacts.contact.UserContact;
 import org.mxchange.jphone.exceptions.PhoneNumberAlreadyLinkedException;
 import org.mxchange.jphone.exceptions.PhoneNumberNotLinkedException;
 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
@@ -93,20 +94,17 @@ public class PizzaAdminContactPhoneSessionBean extends BasePizzaDatabaseBean imp
                        throw new IllegalArgumentException(MessageFormat.format("faxNumber.phoneNumber={0} is not valid", faxNumber.getPhoneNumber())); //NOI18N
                }
 
-               // Set fax number in contact
-               contact.setContactFaxNumber(faxNumber);
-
-               // Detach all phone entries before persisting it
-               this.detachAllContactPhoneEntries(contact);
+               // Find contact
+               Contact managedContact = this.getEntityManager().find(UserContact.class, contact.getContactId());
 
-               // Update database
-               Contact updatedContact = this.contactBean.updateContactData(contact);
+               // Set fax number in contact
+               managedContact.setContactFaxNumber(faxNumber);
 
                // Trace message
-               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkExistingFaxNumberWithContact: updatedContact={1} - EXIT!", this.getClass().getSimpleName(), updatedContact)); //NOI18N
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkExistingFaxNumberWithContact: managedContact={1} - EXIT!", this.getClass().getSimpleName(), managedContact)); //NOI18N
 
                // Return it
-               return updatedContact;
+               return managedContact;
        }
 
        @Override
@@ -153,20 +151,17 @@ public class PizzaAdminContactPhoneSessionBean extends BasePizzaDatabaseBean imp
                        throw new IllegalArgumentException(MessageFormat.format("landLineNumber.phoneNumber={0} is not valid", landLineNumber.getPhoneNumber())); //NOI18N
                }
 
-               // Set land-line number in contact
-               contact.setContactLandLineNumber(landLineNumber);
-
-               // Detach all phone entries before persisting it
-               this.detachAllContactPhoneEntries(contact);
+               // Find contact
+               Contact managedContact = this.getEntityManager().find(UserContact.class, contact.getContactId());
 
-               // Update database
-               Contact updatedContact = this.contactBean.updateContactData(contact);
+               // Set landline number in contact
+               managedContact.setContactLandLineNumber(landLineNumber);
 
                // Trace message
-               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkExistingLandLineNumberWithContact: updatedContact={1} - EXIT!", this.getClass().getSimpleName(), updatedContact)); //NOI18N
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkExistingLandLineNumberWithContact: managedContact={1} - EXIT!", this.getClass().getSimpleName(), managedContact)); //NOI18N
 
                // Return it
-               return updatedContact;
+               return managedContact;
        }
 
        @Override
@@ -207,20 +202,17 @@ public class PizzaAdminContactPhoneSessionBean extends BasePizzaDatabaseBean imp
                        throw new IllegalArgumentException(MessageFormat.format("mobileNumber.mobileProvider.providerId={0} is not valid", mobileNumber.getMobileProvider().getProviderId())); //NOI18N
                }
 
-               // Set mobile number in contact
-               contact.setContactMobileNumber(mobileNumber);
+               // Find contact
+               Contact managedContact = this.getEntityManager().find(UserContact.class, contact.getContactId());
 
-               // Detach all phone entries before persisting it
-               this.detachAllContactPhoneEntries(contact);
-
-               // Update database
-               Contact updatedContact = this.contactBean.updateContactData(contact);
+               // Set landline number in contact
+               managedContact.setContactMobileNumber(mobileNumber);
 
                // Trace message
-               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkExistingMobileNumberWithContact: updatedContact={1} - EXIT!", this.getClass().getSimpleName(), updatedContact)); //NOI18N
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkExistingMobileNumberWithContact: managedContact={1} - EXIT!", this.getClass().getSimpleName(), managedContact)); //NOI18N
 
                // Return it
-               return updatedContact;
+               return managedContact;
        }
 
        @Override
@@ -267,20 +259,20 @@ public class PizzaAdminContactPhoneSessionBean extends BasePizzaDatabaseBean imp
                // Set created instance
                faxNumber.setPhoneEntryCreated(new GregorianCalendar());
 
-               // Set fax number in contact
-               contact.setContactFaxNumber(faxNumber);
+               // Persist it
+               this.getEntityManager().persist(faxNumber);
 
-               // Detach all phone entries before persisting it
-               this.detachAllContactPhoneEntries(contact);
+               // Find contact
+               Contact managedContact = this.getEntityManager().find(UserContact.class, contact.getContactId());
 
-               // Update database
-               Contact updatedContact = this.contactBean.updateContactData(contact);
+               // Set fax number in contact
+               managedContact.setContactFaxNumber(faxNumber);
 
                // Trace message
-               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkNewFaxNumberWithContact: updatedContact={1} - EXIT!", this.getClass().getSimpleName(), updatedContact)); //NOI18N
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkNewFaxNumberWithContact: managedContact={1} - EXIT!", this.getClass().getSimpleName(), managedContact)); //NOI18N
 
                // Return it
-               return updatedContact;
+               return managedContact;
        }
 
        @Override
@@ -327,20 +319,20 @@ public class PizzaAdminContactPhoneSessionBean extends BasePizzaDatabaseBean imp
                // Set created instance
                landLineNumber.setPhoneEntryCreated(new GregorianCalendar());
 
-               // Set landLine number in contact
-               contact.setContactLandLineNumber(landLineNumber);
+               // Persist it
+               this.getEntityManager().persist(landLineNumber);
 
-               // Detach all phone entries before persisting it
-               this.detachAllContactPhoneEntries(contact);
+               // Find contact
+               Contact managedContact = this.getEntityManager().find(UserContact.class, contact.getContactId());
 
-               // Update database
-               Contact updatedContact = this.contactBean.updateContactData(contact);
+               // Set land-line number in contact
+               managedContact.setContactLandLineNumber(landLineNumber);
 
                // Trace message
-               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkNewLandLineNumberWithContact: updatedContact={1} - EXIT!", this.getClass().getSimpleName(), updatedContact)); //NOI18N
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkNewLandLineNumberWithContact: managedContact={1} - EXIT!", this.getClass().getSimpleName(), managedContact)); //NOI18N
 
                // Return it
-               return updatedContact;
+               return managedContact;
        }
 
        @Override
@@ -381,20 +373,20 @@ public class PizzaAdminContactPhoneSessionBean extends BasePizzaDatabaseBean imp
                // Set created instance
                mobileNumber.setPhoneEntryCreated(new GregorianCalendar());
 
-               // Set mobile number in contact
-               contact.setContactMobileNumber(mobileNumber);
+               // Persist it
+               this.getEntityManager().persist(mobileNumber);
 
-               // Detach all phone entries before persisting it
-               this.detachAllContactPhoneEntries(contact);
+               // Find contact
+               Contact managedContact = this.getEntityManager().find(UserContact.class, contact.getContactId());
 
-               // Update database
-               Contact updatedContact = this.contactBean.updateContactData(contact);
+               // Set land-line number in contact
+               managedContact.setContactMobileNumber(mobileNumber);
 
                // Trace message
-               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkNewMobileNumberWithContact: updatedContact={1} - EXIT!", this.getClass().getSimpleName(), updatedContact)); //NOI18N
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkNewMobileNumberWithContact: managedContact={1} - EXIT!", this.getClass().getSimpleName(), managedContact)); //NOI18N
 
                // Return it
-               return updatedContact;
+               return managedContact;
        }
 
        @Override
@@ -426,20 +418,17 @@ public class PizzaAdminContactPhoneSessionBean extends BasePizzaDatabaseBean imp
                        throw new IllegalArgumentException(MessageFormat.format("contact.contactFaxNumber.phoneId={0} and faxNumber.phoneId={1} are not the same.", contact.getContactFaxNumber().getPhoneId(), faxNumber.getPhoneId())); //NOI18N
                }
 
-               // Remove it from contact
-               contact.setContactFaxNumber(null);
-
-               // Detach all phone entries before persisting it
-               this.detachAllContactPhoneEntries(contact);
+               // Find contact
+               Contact managedContact = this.getEntityManager().find(UserContact.class, contact.getContactId());
 
-               // Update database
-               Contact updatedContact = this.contactBean.updateContactData(contact);
+               // Remove it from contact
+               managedContact.setContactFaxNumber(null);
 
                // Trace message
-               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.unlinkFaxDataFromContact: updatedContact={1} - EXIT!", this.getClass().getSimpleName(), updatedContact)); //NOI18N
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.unlinkFaxDataFromContact: managedContact={1} - EXIT!", this.getClass().getSimpleName(), managedContact)); //NOI18N
 
                // Return it
-               return updatedContact;
+               return managedContact;
        }
 
        @Override
@@ -471,20 +460,17 @@ public class PizzaAdminContactPhoneSessionBean extends BasePizzaDatabaseBean imp
                        throw new IllegalArgumentException(MessageFormat.format("contact.contactLandLineNumber.phoneId={0} and landLineNumber.phoneId={1} are not the same.", contact.getContactLandLineNumber().getPhoneId(), landLineNumber.getPhoneId())); //NOI18N
                }
 
-               // Remove it from contact
-               contact.setContactLandLineNumber(null);
-
-               // Detach all phone entries before persisting it
-               this.detachAllContactPhoneEntries(contact);
+               // Find contact
+               Contact managedContact = this.getEntityManager().find(UserContact.class, contact.getContactId());
 
-               // Update database
-               Contact updatedContact = this.contactBean.updateContactData(contact);
+               // Remove it from contact
+               managedContact.setContactLandLineNumber(null);
 
                // Trace message
-               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.unlinkLandLineDataFromContact: updatedContact={1} - EXIT!", this.getClass().getSimpleName(), updatedContact)); //NOI18N
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.unlinkLandLineDataFromContact: managedContact={1} - EXIT!", this.getClass().getSimpleName(), managedContact)); //NOI18N
 
                // Return it
-               return updatedContact;
+               return managedContact;
        }
 
        @Override
@@ -516,20 +502,17 @@ public class PizzaAdminContactPhoneSessionBean extends BasePizzaDatabaseBean imp
                        throw new IllegalArgumentException(MessageFormat.format("contact.contactMobileNumber.phoneId={0} and mobileNumber.phoneId={1} are not the same.", contact.getContactMobileNumber().getPhoneId(), mobileNumber.getPhoneId())); //NOI18N
                }
 
-               // Remove it from contact
-               contact.setContactMobileNumber(null);
-
-               // Detach all phone entries before persisting it
-               this.detachAllContactPhoneEntries(contact);
+               // Find contact
+               Contact managedContact = this.getEntityManager().find(UserContact.class, contact.getContactId());
 
-               // Update database
-               Contact updatedContact = this.contactBean.updateContactData(contact);
+               // Remove it from contact
+               managedContact.setContactLandLineNumber(null);
 
                // Trace message
-               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.unlinkMobileDataFromContact: updatedContact={1} - EXIT!", this.getClass().getSimpleName(), updatedContact)); //NOI18N
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.unlinkMobileDataFromContact: managedContact={1} - EXIT!", this.getClass().getSimpleName(), managedContact)); //NOI18N
 
                // Return it
-               return updatedContact;
+               return managedContact;
        }
 
 }