]> git.mxchange.org Git - addressbook-war.git/commitdiff
Continued:
authorRoland Haeder <roland@mxchange.org>
Fri, 16 Oct 2015 09:29:47 +0000 (11:29 +0200)
committerRoland Haeder <roland@mxchange.org>
Fri, 16 Oct 2015 09:29:47 +0000 (11:29 +0200)
- added event for starting/ending sharing of address books, more observers can now easily join
- added started/ended observer which add/remove the share entry from the internal list
- added post-construct method init()
- method validateEvent() does the evnt validation process
Signed-off-by:Roland Häder <roland@mxchange.org>

src/java/org/mxchange/addressbook/beans/shares/SharesWebBean.java
src/java/org/mxchange/addressbook/beans/shares/SharesWebController.java

index cecf1234bbc2c12a681b07553b2ad10328f693fa..809c4392145d7fa146384c94f8c541f6106adb8f 100644 (file)
@@ -20,7 +20,11 @@ import java.text.MessageFormat;
 import java.util.Collections;
 import java.util.List;
 import java.util.Objects;
+import javax.annotation.PostConstruct;
 import javax.enterprise.context.SessionScoped;
+import javax.enterprise.event.Event;
+import javax.enterprise.event.Observes;
+import javax.enterprise.inject.Any;
 import javax.faces.view.facelets.FaceletException;
 import javax.inject.Inject;
 import javax.inject.Named;
@@ -28,6 +32,9 @@ import javax.naming.Context;
 import javax.naming.InitialContext;
 import javax.naming.NamingException;
 import org.mxchange.addressbook.beans.login.UserLoginWebController;
+import org.mxchange.addressbook.events.sharing.AddressbookSharingEvent;
+import org.mxchange.addressbook.events.sharing.StartedAddressbookSharingEvent;
+import org.mxchange.addressbook.events.sharing.type.SharingType;
 import org.mxchange.addressbook.exceptions.UserAlreadySharingAddressbookException;
 import org.mxchange.addressbook.model.addressbook.Addressbook;
 import org.mxchange.addressbook.model.addressbook.shared.ShareableAddressbook;
@@ -79,6 +86,13 @@ public class SharesWebBean implements SharesWebController {
         */
        private Long shareeUserId;
 
+       /**
+        * An event triggered when address book sharing starts or ends
+        */
+       @Inject
+       @Any
+       private Event<AddressbookSharingEvent> sharingEvent;
+
        /**
         * Default constructor
         */
@@ -96,6 +110,12 @@ public class SharesWebBean implements SharesWebController {
                }
        }
 
+       @PostConstruct
+       public void init () {
+               // Init share list
+               this.sharedAddressbooks = this.shareBean.allSharedAddressbooks(this.loginController.getLoggedInUser());
+       }
+
        @Override
        public Long getShareeUserId () {
                return this.shareeUserId;
@@ -163,7 +183,7 @@ public class SharesWebBean implements SharesWebController {
                        throw new IllegalArgumentException(MessageFormat.format("addressbook.addressbookId={0} is invalid.", addressbook.getAddressbookId())); //NOI18N
                } else if (!Objects.equals(addressbook.getAddressbookUser(), this.loginController.getLoggedInUser())) {
                        // Not the same user!
-                       throw new IllegalStateException(MessageFormat.format("Address book id {0} owner id {1} mismatching logged-in user id {2}", addressbook.getAddressbookId(), addressbook.getAddressbookUser().getUserId(), this.loginController.getLoggedInUser().getUserId()));
+                       throw new IllegalStateException(MessageFormat.format("Address book id {0} owner id {1} mismatching logged-in user id {2}", addressbook.getAddressbookId(), addressbook.getAddressbookUser().getUserId(), this.loginController.getLoggedInUser().getUserId())); //NOI18N
                }
 
                try {
@@ -172,6 +192,9 @@ public class SharesWebBean implements SharesWebController {
 
                        // TODO Set it here
                        this.setShare(shared);
+
+                       /// Trigger event
+                       this.sharingEvent.fire(new StartedAddressbookSharingEvent(shared));
                } catch (final UserAlreadySharingAddressbookException ex) {
                        // Throw again
                        throw new FaceletException(ex);
@@ -201,4 +224,91 @@ public class SharesWebBean implements SharesWebController {
        public void setShare (final ShareableAddressbook share) {
                this.share = share;
        }
+
+       @Override
+       public void afterAdressbookShareStarted (final @Observes AddressbookSharingEvent event) {
+               // Validate parameter
+               if (null == event) {
+                       // Throw NPE
+                       throw new NullPointerException("event is null"); //NOI18N
+               } else if (event.getSharingType() != SharingType.STARTED) {
+                       // Wrong event
+                       return;
+               }
+
+               // Validate event
+               this.validateEvent(event);
+
+               // Add it to list
+               this.sharedAddressbooks.add(event.getShareableAddressbook());
+       }
+
+       @Override
+       public void afterAdressbookShareEnded (final @Observes AddressbookSharingEvent event) {
+               // Validate parameter
+               if (null == event) {
+                       // Throw NPE
+                       throw new NullPointerException("event is null"); //NOI18N
+               } else if (event.getSharingType() != SharingType.ENDED) {
+                       // Wrong event
+                       return;
+               }
+
+               // Validate event
+               this.validateEvent(event);
+
+               // Add it to list
+               this.sharedAddressbooks.remove(event.getShareableAddressbook());
+       }
+
+       /**
+        * Validates given event for all values and throws exceptions
+        * <p>
+        * @param event Event to validate
+        */
+       private void validateEvent (final AddressbookSharingEvent event) {
+               if (null == event) {
+                       // Throw NPE
+                       throw new NullPointerException("event is null"); //NOI18N
+               } else if (event.getSharingType() == null) {
+                       // Throw NPE
+                       throw new NullPointerException("event.sharingType is null"); //NOI18N
+               } else if (event.getShareableAddressbook() == null) {
+                       // Throw NPE again
+                       throw new NullPointerException("event.shareableAddressbook is null"); //NOI18N
+               } else if (event.getShareableAddressbook().getShareId() == null) {
+                       // Throw NPE again
+                       throw new NullPointerException("event.shareableAddressbook.shareId is null"); //NOI18N
+               } else if (event.getShareableAddressbook().getShareId() < 1) {
+                       // Throw NPE again
+                       throw new IllegalArgumentException(MessageFormat.format("event.shareableAddressbook.shareId={0} is invalid", event.getShareableAddressbook().getShareId())); //NOI18N
+               } else if (event.getShareableAddressbook().getShareAddressbook() == null) {
+                       // Throw NPE again
+                       throw new NullPointerException("event.shareableAddressbook.shareAddressbook is null"); //NOI18N
+               } else if (event.getShareableAddressbook().getShareAddressbook().getAddressbookId() == null) {
+                       // Throw NPE again
+                       throw new NullPointerException("event.shareableAddressbook.shareAddressbook.addressbookId is null"); //NOI18N
+               } else if (event.getShareableAddressbook().getShareAddressbook().getAddressbookId() < 1) {
+                       // Throw NPE again
+                       throw new IllegalArgumentException(MessageFormat.format("event.shareableAddressbook.shareAddressbook.addressbookId={0} is invalid", event.getShareableAddressbook().getShareAddressbook().getAddressbookId())); //NOI18N
+               } else if (event.getShareableAddressbook().getShareUserOwner() == null) {
+                       // Throw NPE again
+                       throw new NullPointerException("event.shareableAddressbook.shareUserOwner is null"); //NOI18N
+               } else if (event.getShareableAddressbook().getShareUserOwner().getUserId() == null) {
+                       // Throw NPE again
+                       throw new NullPointerException("event.shareableAddressbook.shareUserOwner.userId is null"); //NOI18N
+               } else if (event.getShareableAddressbook().getShareUserOwner().getUserId() < 1) {
+                       // Throw NPE again
+                       throw new IllegalArgumentException(MessageFormat.format("event.shareableAddressbook.shareUserOwner.userId={0} is invalid", event.getShareableAddressbook().getShareUserOwner().getUserId())); //NOI18N
+               } else if (event.getShareableAddressbook().getShareUserSharee() == null) {
+                       // Throw NPE again
+                       throw new NullPointerException("event.shareableAddressbook.shareUserSharee is null"); //NOI18N
+               } else if (event.getShareableAddressbook().getShareUserSharee().getUserId() == null) {
+                       // Throw NPE again
+                       throw new NullPointerException("event.shareableAddressbook.shareUserSharee.userId is null"); //NOI18N
+               } else if (event.getShareableAddressbook().getShareUserSharee().getUserId() < 1) {
+                       // Throw NPE again
+                       throw new IllegalArgumentException(MessageFormat.format("event.shareableAddressbook.shareUserSharee.userId={0} is invalid", event.getShareableAddressbook().getShareUserOwner().getUserId())); //NOI18N
+               }
+       }
 }
index 4a7baae4496a57c383a410dfa888ff2085971e5d..3a4196bfc1012190c2d76afcfe170efeb5beef1a 100644 (file)
@@ -18,6 +18,7 @@ package org.mxchange.addressbook.beans.shares;
 
 import java.io.Serializable;
 import java.util.List;
+import org.mxchange.addressbook.events.sharing.AddressbookSharingEvent;
 import org.mxchange.addressbook.model.addressbook.Addressbook;
 import org.mxchange.addressbook.model.addressbook.shared.ShareableAddressbook;
 import org.mxchange.jusercore.model.user.User;
@@ -94,4 +95,18 @@ public interface SharesWebController extends Serializable {
         * @return List of all shared address books
         */
        List<ShareableAddressbook> allShares ();
+
+       /**
+        * Observer method for started sharing events
+        * <p>
+        * @param event Event instance
+        */
+       void afterAdressbookShareStarted (final AddressbookSharingEvent event);
+
+       /**
+        * Observer method for ended sharing events
+        * <p>
+        * @param event Event instance
+        */
+       void afterAdressbookShareEnded (final AddressbookSharingEvent event);
 }