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;
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;
*/
private Long shareeUserId;
+ /**
+ * An event triggered when address book sharing starts or ends
+ */
+ @Inject
+ @Any
+ private Event<AddressbookSharingEvent> sharingEvent;
+
/**
* Default constructor
*/
}
}
+ @PostConstruct
+ public void init () {
+ // Init share list
+ this.sharedAddressbooks = this.shareBean.allSharedAddressbooks(this.loginController.getLoggedInUser());
+ }
+
@Override
public Long getShareeUserId () {
return this.shareeUserId;
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 {
// 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);
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
+ }
+ }
}