From 0dbaa257b594703aca185a87c8d93552dce85c63 Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Fri, 16 Oct 2015 11:29:47 +0200 Subject: [PATCH] =?utf8?q?Continued:=20-=20added=20event=20for=20starting/?= =?utf8?q?ending=20sharing=20of=20address=20books,=20more=20observers=20ca?= =?utf8?q?n=20now=20easily=20join=20-=20added=20started/ended=20observer?= =?utf8?q?=20which=20add/remove=20the=20share=20entry=20from=20the=20inter?= =?utf8?q?nal=20list=20-=20added=20post-construct=20method=20init()=20-=20?= =?utf8?q?method=20validateEvent()=20does=20the=20evnt=20validation=20proc?= =?utf8?q?ess=20Signed-off-by:Roland=20H=C3=A4der=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../beans/shares/SharesWebBean.java | 112 +++++++++++++++++- .../beans/shares/SharesWebController.java | 15 +++ 2 files changed, 126 insertions(+), 1 deletion(-) diff --git a/src/java/org/mxchange/addressbook/beans/shares/SharesWebBean.java b/src/java/org/mxchange/addressbook/beans/shares/SharesWebBean.java index cecf1234..809c4392 100644 --- a/src/java/org/mxchange/addressbook/beans/shares/SharesWebBean.java +++ b/src/java/org/mxchange/addressbook/beans/shares/SharesWebBean.java @@ -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 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 + *

+ * @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 + } + } } diff --git a/src/java/org/mxchange/addressbook/beans/shares/SharesWebController.java b/src/java/org/mxchange/addressbook/beans/shares/SharesWebController.java index 4a7baae4..3a4196bf 100644 --- a/src/java/org/mxchange/addressbook/beans/shares/SharesWebController.java +++ b/src/java/org/mxchange/addressbook/beans/shares/SharesWebController.java @@ -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 allShares (); + + /** + * Observer method for started sharing events + *

+ * @param event Event instance + */ + void afterAdressbookShareStarted (final AddressbookSharingEvent event); + + /** + * Observer method for ended sharing events + *

+ * @param event Event instance + */ + void afterAdressbookShareEnded (final AddressbookSharingEvent event); } -- 2.39.5