]> git.mxchange.org Git - jjobs-war.git/commitdiff
Continued with phone numbers: (please cherry-pick)
authorRoland Häder <roland@mxchange.org>
Tue, 9 Aug 2016 08:34:22 +0000 (10:34 +0200)
committerRoland Haeder <roland@mxchange.org>
Wed, 17 Aug 2016 19:24:33 +0000 (21:24 +0200)
- introduced updateContactPhoneNumbers() which updates all cellphone, land-line fax number entries in this controller's list
- added missing lists for land-line and fax numbers
- added uniqueAddFooNumber() for uniqly adding cellphone, land-line and fax numbers

src/java/org/mxchange/jjobs/beans/contact/phone/JobsContactPhoneWebSessionBean.java

index 9a258905fc9440cb7780093f7d26aab329ad4164..64e6c0b64c44c446da4ac8c0e74473e5794e5434 100644 (file)
@@ -38,6 +38,8 @@ import org.mxchange.jjobs.beans.BaseJobsController;
 import org.mxchange.jjobs.beans.contact.JobsContactWebSessionController;
 import org.mxchange.jjobs.beans.phone.JobsAdminPhoneWebRequestController;
 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.phonenumbers.phone.AdminPhoneSessionBeanRemote;
 import org.mxchange.jusercore.events.user.add.AdminAddedUserEvent;
 
@@ -71,6 +73,16 @@ public class JobsContactPhoneWebSessionBean extends BaseJobsController implement
         */
        private final List<DialableCellphoneNumber> cellphoneNumbers;
 
+       /**
+        * All fax numbers
+        */
+       private final List<DialableFaxNumber> faxNumbers;
+
+       /**
+        * All land-line numbers
+        */
+       private final List<DialableLandLineNumber> landLineNumbers;
+
        /**
         * General contact controller
         */
@@ -101,6 +113,8 @@ public class JobsContactPhoneWebSessionBean extends BaseJobsController implement
 
                // Init lists/maps
                this.cellphoneNumbers = new LinkedList<>();
+               this.faxNumbers = new LinkedList<>();
+               this.landLineNumbers = new LinkedList<>();
                this.contacts = new HashMap<>(10);
        }
 
@@ -121,14 +135,8 @@ public class JobsContactPhoneWebSessionBean extends BaseJobsController implement
                        throw new IllegalArgumentException(MessageFormat.format("event.addedContact.contactId={0} is not valid", event.getAddedContact().getContactId())); //NOI18N //NOI18N
                }
 
-               // Get contact
-               Contact contact = event.getAddedContact();
-
-               // Is cellphone set?
-               if (contact.getContactCellphoneNumber() instanceof DialableCellphoneNumber) {
-                       // Unique-add it
-                       this.uniqueAddCellphoneNumber(contact.getContactCellphoneNumber());
-               }
+               // Update contact's cellphone, land-line and fax number
+               this.updateContactPhoneNumbers(event.getAddedContact());
 
                // Clear this bean
                this.clear();
@@ -151,6 +159,9 @@ public class JobsContactPhoneWebSessionBean extends BaseJobsController implement
                        throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
                }
 
+               // Update contact's cellphone, land-line and fax number
+               this.updateContactPhoneNumbers(event.getAddedUser().getUserContact());
+
                // Clear all data
                this.clear();
        }
@@ -171,6 +182,12 @@ public class JobsContactPhoneWebSessionBean extends BaseJobsController implement
                        // Not avalid id
                        throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
                }
+
+               // Update contact's cellphone, land-line and fax number
+               this.updateContactPhoneNumbers(event.getUpdatedContact());
+
+               // Clear all data
+               this.clear();
        }
 
        @Override
@@ -219,4 +236,148 @@ public class JobsContactPhoneWebSessionBean extends BaseJobsController implement
                // Clear all data
        }
 
+       /**
+        * Uniquely add given cellphone number to this bean's list. First remove the
+        * old instance (by id number), then re-add it again.
+        * <p>
+        * @param cellphoneNumber Cellphone number to add
+        */
+       private void uniqueAddCellphoneNumber (final DialableCellphoneNumber cellphoneNumber) {
+               // Make sure the parameter is valid
+               if (null == cellphoneNumber) {
+                       // Throw NPE
+                       throw new NullPointerException("cellphoneNumber is null");
+               } else if (cellphoneNumber.getPhoneId() == null) {
+                       // Throw again ...
+                       throw new NullPointerException("cellphoneNumber.phoneId is null");
+               } else if (cellphoneNumber.getPhoneId() < 1) {
+                       // Not valid
+                       throw new IllegalArgumentException(MessageFormat.format("cellphoneNumber.phoneId={0} is not valid.", cellphoneNumber.getPhoneId()));
+               }
+
+               // First remove it by object
+               if (!this.cellphoneNumbers.remove(cellphoneNumber)) {
+                       // Did not work, try by id number
+                       for (final DialableCellphoneNumber cell : this.cellphoneNumbers) {
+                               // Is id number the same?
+                               if (Objects.equals(cell.getPhoneId(), cellphoneNumber.getPhoneId())) {
+                                       // Found it
+                                       this.cellphoneNumbers.remove(cell);
+                                       break;
+                               }
+                       }
+               }
+
+               // ... then add it
+               this.cellphoneNumbers.add(cellphoneNumber);
+       }
+
+       /**
+        * Uniquely add given fax number to this bean's list. First remove the old
+        * instance (by id number), then re-add it again.
+        * <p>
+        * @param faxNumber number to add
+        */
+       private void uniqueAddFaxNumber (final DialableFaxNumber faxNumber) {
+               // Make sure the parameter is valid
+               if (null == faxNumber) {
+                       // Throw NPE
+                       throw new NullPointerException("faxNumber is null");
+               } else if (faxNumber.getPhoneId() == null) {
+                       // Throw again ...
+                       throw new NullPointerException("faxNumber.phoneId is null");
+               } else if (faxNumber.getPhoneId() < 1) {
+                       // Not valid
+                       throw new IllegalArgumentException(MessageFormat.format("faxNumber.phoneId={0} is not valid.", faxNumber.getPhoneId()));
+               }
+
+               // First remove it
+               if (!this.faxNumbers.remove(faxNumber)) {
+                       // Did not work, try by id number
+                       for (final DialableFaxNumber fax : this.faxNumbers) {
+                               // Is id number the same?
+                               if (Objects.equals(fax.getPhoneId(), faxNumber.getPhoneId())) {
+                                       // Found it
+                                       this.faxNumbers.remove(fax);
+                                       break;
+                               }
+                       }
+               }
+
+               // ... then add it
+               this.faxNumbers.add(faxNumber);
+       }
+
+       /**
+        * Uniquely add given land-line number to this bean's list. First remove the
+        * old instance (by id number), then re-add it again.
+        * <p>
+        * @param landLineNumber Land-line number to add
+        */
+       private void uniqueAddLandLineNumber (final DialableLandLineNumber landLineNumber) {
+               // Make sure the parameter is valid
+               if (null == landLineNumber) {
+                       // Throw NPE
+                       throw new NullPointerException("landLineNumber is null");
+               } else if (landLineNumber.getPhoneId() == null) {
+                       // Throw again ...
+                       throw new NullPointerException("landLineNumber.phoneId is null");
+               } else if (landLineNumber.getPhoneId() < 1) {
+                       // Not valid
+                       throw new IllegalArgumentException(MessageFormat.format("landLineNumber.phoneId={0} is not valid.", landLineNumber.getPhoneId()));
+               }
+
+               // First remove it
+               if (!this.landLineNumbers.remove(landLineNumber)) {
+                       // Did not work, try by id number
+                       for (final DialableLandLineNumber landLine : this.landLineNumbers) {
+                               // Is id number the same?
+                               if (Objects.equals(landLine.getPhoneId(), landLineNumber.getPhoneId())) {
+                                       // Found it
+                                       this.landLineNumbers.remove(landLine);
+                                       break;
+                               }
+                       }
+               }
+
+               // ... then add it
+               this.landLineNumbers.add(landLineNumber);
+       }
+
+       /**
+        * Updates given contact's cellphone, land-line and fax number
+        * <p>
+        * @param contact Contact instance
+        */
+       private void updateContactPhoneNumbers (final Contact contact) {
+               // Parameter must be valid
+               if (null == contact) {
+                       // Throw NPE
+                       throw new NullPointerException("contact is null");
+               } else if (contact.getContactId() == null) {
+                       // Throw again
+                       throw new NullPointerException("contact.contactId is null");
+               } else if (contact.getContactId() < 1) {
+                       // Id number is not valid
+               }
+
+               // Is cellphone set?
+               if (contact.getContactCellphoneNumber() instanceof DialableCellphoneNumber) {
+                       // Unique-add it
+                       this.uniqueAddCellphoneNumber(contact.getContactCellphoneNumber());
+               }
+
+               // Is land-line set?
+               if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
+                       // Unique-add it
+                       this.uniqueAddLandLineNumber(contact.getContactLandLineNumber());
+               }
+
+               // Is fax set?
+               if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
+                       // Unique-add it
+                       this.uniqueAddFaxNumber(contact.getContactFaxNumber());
+               }
+       }
+
 }