From: Roland Haeder Date: Fri, 16 Oct 2015 07:50:22 +0000 (+0200) Subject: Continued: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=9a5e24909ce4df4536d1cdebc8ce5d0e403bff52;p=addressbook-war.git Continued: - moved bean (controller) method startSharing() with some open TODOs to proper bean - updated template accordingly Signed-off-by:Roland Häder --- diff --git a/src/java/org/mxchange/addressbook/beans/addressbook/AddressbookWebBean.java b/src/java/org/mxchange/addressbook/beans/addressbook/AddressbookWebBean.java index 411449fc..bfa9288c 100644 --- a/src/java/org/mxchange/addressbook/beans/addressbook/AddressbookWebBean.java +++ b/src/java/org/mxchange/addressbook/beans/addressbook/AddressbookWebBean.java @@ -421,39 +421,6 @@ public class AddressbookWebBean implements AddressbookWebController { return Objects.equals(this.getAddressbookUser(), this.loginController.getLoggedInUser()); } - @Override - public String startSharing () { - // Check conditions - if (!this.loginController.isUserLoggedIn()) { - // No, then throw exception - throw new FaceletException("This method can only be called as logged-in user."); //NOI18N - } else if (this.getAddressbookUser() == null) { - // Throw NPE - throw new NullPointerException("this.addressbookUser is null"); //NOI18N - } else if (this.getAddressbookUser().getUserId() == null) { - // Throw NPE again - throw new NullPointerException("this.addressbookUser.userId is null"); //NOI18N - } else if (this.getAddressbookUser().getUserId() < 1) { - // Invalid id number - throw new IllegalStateException(MessageFormat.format("this.addressbookUser.userId={0} is invalid", this.getAddressbookUser().getUserId())); //NOI18N - } else if (Objects.equals(this.getAddressbookUser(), this.loginController.getLoggedInUser())) { - // Sharing with yourself! - throw new IllegalStateException("User tries to share with himself."); //NOI18N - } else if (this.getAddressbook() == null) { - // Throw NPE again - throw new NullPointerException("this.addressbook is null"); //NOI18N - } else if (this.getAddressbook().getAddressbookId() == null) { - // Throw NPE again - throw new NullPointerException("this.addressbook.addressbookId is null"); //NOI18N - } else if (this.getAddressbook().getAddressbookId() < 1) { - // Invalid id number - throw new IllegalArgumentException(MessageFormat.format("this.addressbook.addressbookId={0} is invalid.", this.getAddressbook().getAddressbookId())); //NOI18N - } - - // TODO Unfinished - return null; - } - /** * Initializes the user user's address book list */ diff --git a/src/java/org/mxchange/addressbook/beans/addressbook/AddressbookWebController.java b/src/java/org/mxchange/addressbook/beans/addressbook/AddressbookWebController.java index 5cc2ffb7..97995957 100644 --- a/src/java/org/mxchange/addressbook/beans/addressbook/AddressbookWebController.java +++ b/src/java/org/mxchange/addressbook/beans/addressbook/AddressbookWebController.java @@ -209,12 +209,4 @@ public interface AddressbookWebController extends Serializable { * @return List of not sharing users */ List allUsersNotSharing (); - - /** - * Starts an address book share between currently logged-in user and - * assigned user for current address book. - *

- * @return Redirect target - */ - String startSharing (); } diff --git a/src/java/org/mxchange/addressbook/beans/shares/SharesWebBean.java b/src/java/org/mxchange/addressbook/beans/shares/SharesWebBean.java index 9041da43..1c34a583 100644 --- a/src/java/org/mxchange/addressbook/beans/shares/SharesWebBean.java +++ b/src/java/org/mxchange/addressbook/beans/shares/SharesWebBean.java @@ -16,6 +16,8 @@ */ package org.mxchange.addressbook.beans.shares; +import java.text.MessageFormat; +import java.util.Objects; import javax.enterprise.context.SessionScoped; import javax.faces.view.facelets.FaceletException; import javax.inject.Inject; @@ -24,7 +26,11 @@ import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import org.mxchange.addressbook.beans.login.UserLoginWebController; +import org.mxchange.addressbook.exceptions.UserAlreadySharingAddressbookException; +import org.mxchange.addressbook.model.addressbook.Addressbook; +import org.mxchange.addressbook.model.addressbook.shared.ShareableAddressbook; import org.mxchange.addressbook.model.shared.SharedAddressbooksSessionBeanRemote; +import org.mxchange.jusercore.model.user.User; /** * A bean for sharing address books with other users @@ -116,4 +122,49 @@ public class SharesWebBean implements SharesWebController { return this.isUserSharing; } + @Override + public String startSharing (final User user, final Addressbook addressbook) { + // Check conditions + if (!this.loginController.isUserLoggedIn()) { + // No, then throw exception + throw new FaceletException("This method can only be called as logged-in user."); //NOI18N + } else if (null == user) { + // Throw NPE + throw new NullPointerException("user is null"); //NOI18N + } else if (user.getUserId() == null) { + // Throw NPE again + throw new NullPointerException("user.userId is null"); //NOI18N + } else if (user.getUserId() < 1) { + // Invalid id number + throw new IllegalStateException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N + } else if (Objects.equals(user, this.loginController.getLoggedInUser())) { + // Sharing with yourself! + throw new IllegalStateException("User tries to share with himself."); //NOI18N + } else if (null == addressbook) { + // Throw NPE again + throw new NullPointerException("addressbook is null"); //NOI18N + } else if (addressbook.getAddressbookId() == null) { + // Throw NPE again + throw new NullPointerException("addressbook.addressbookId is null"); //NOI18N + } else if (addressbook.getAddressbookId() < 1) { + // Invalid id number + 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())); + } + + try { + // Init sharing + ShareableAddressbook share = this.shareBean.startSharing(user, addressbook); + + // TODO Set it here + } catch (final UserAlreadySharingAddressbookException ex) { + // Throw again + throw new FaceletException(ex); + } + + // TODO Unfinished + return null; + } } diff --git a/src/java/org/mxchange/addressbook/beans/shares/SharesWebController.java b/src/java/org/mxchange/addressbook/beans/shares/SharesWebController.java index b1480f03..bc70057b 100644 --- a/src/java/org/mxchange/addressbook/beans/shares/SharesWebController.java +++ b/src/java/org/mxchange/addressbook/beans/shares/SharesWebController.java @@ -17,6 +17,8 @@ package org.mxchange.addressbook.beans.shares; import java.io.Serializable; +import org.mxchange.addressbook.model.addressbook.Addressbook; +import org.mxchange.jusercore.model.user.User; /** * Controller interface sharing address books @@ -59,4 +61,14 @@ public interface SharesWebController extends Serializable { * @return Whether the sharee's user id is empty. */ boolean isShareeUserIdEmpty (); + + /** + * Starts an address book share between currently logged-in user and + * assigned user for current address book. + *

+ * @param user User instance + * @param addressbook Address book instance + * @return Redirect target + */ + String startSharing (final User user, final Addressbook addressbook); } diff --git a/web/login/login_start_sharing_addressbook.xhtml b/web/login/login_start_sharing_addressbook.xhtml index 1eee227e..aca87c51 100644 --- a/web/login/login_start_sharing_addressbook.xhtml +++ b/web/login/login_start_sharing_addressbook.xhtml @@ -44,7 +44,7 @@ #{msg.LOGIN_START_SHARING_TITLE} - +