From: Roland Häder Date: Thu, 27 Apr 2017 12:30:22 +0000 (+0200) Subject: ported project to new libraries jaddressbook-share-core/lib X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=8987c930305be2b65144febebe739ec84bbecd63;p=addressbook-war.git ported project to new libraries jaddressbook-share-core/lib Signed-off-by: Roland Häder --- diff --git a/nbproject/build-impl.xml b/nbproject/build-impl.xml index 961c7991..453bca0e 100644 --- a/nbproject/build-impl.xml +++ b/nbproject/build-impl.xml @@ -1042,13 +1042,10 @@ exists or setup the property manually. For example like this: - - - - + diff --git a/nbproject/genfiles.properties b/nbproject/genfiles.properties index ce320316..f1e9f3a6 100644 --- a/nbproject/genfiles.properties +++ b/nbproject/genfiles.properties @@ -3,6 +3,6 @@ build.xml.script.CRC32=be5f34f7 build.xml.stylesheet.CRC32=651128d4@1.68.1.1 # This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. # Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. -nbproject/build-impl.xml.data.CRC32=f07494a8 -nbproject/build-impl.xml.script.CRC32=8dc59c00 +nbproject/build-impl.xml.data.CRC32=25847443 +nbproject/build-impl.xml.script.CRC32=b9d55450 nbproject/build-impl.xml.stylesheet.CRC32=99ea4b56@1.75.1.1 diff --git a/nbproject/project.properties b/nbproject/project.properties index 203ad966..f0a373be 100644 --- a/nbproject/project.properties +++ b/nbproject/project.properties @@ -39,6 +39,8 @@ file.reference.cdi-api.jar=lib/cdi-api.jar file.reference.gf-client.jar=lib/gf-client.jar file.reference.jaddressbook-core.jar=lib/jaddressbook-core.jar file.reference.jaddressbook-lib.jar=lib/jaddressbook-lib.jar +file.reference.jaddressbook-share-core.jar=lib/jaddressbook-share-core.jar +file.reference.jaddressbook-share-lib.jar=lib/jaddressbook-share-lib.jar file.reference.jcontacts-business-core.jar=lib/jcontacts-business-core.jar file.reference.jcontacts-core.jar=lib/jcontacts-core.jar file.reference.jcontacts-lib.jar=lib/jcontacts-lib.jar @@ -79,6 +81,8 @@ javac.classpath=\ ${file.reference.juser-lib.jar}:\ ${file.reference.jaddressbook-core.jar}:\ ${file.reference.jaddressbook-lib.jar}:\ + ${file.reference.jaddressbook-share-core.jar}:\ + ${file.reference.jaddressbook-share-lib.jar}:\ ${reference.addressbook-core.jar}:\ ${reference.addressbook-lib.jar}:\ ${reference.addressbook-mailer.jar}:\ @@ -134,6 +138,8 @@ runmain.jvmargs= source.encoding=UTF-8 source.reference.jaddressbook-core.jar=../jaddressbook-core/src/ source.reference.jaddressbook-lib.jar=../jaddressbook-lib/src/ +source.reference.jaddressbook-share-core.jar=../jaddressbook-share-core/src +source.reference.jaddressbook-share-lib.jar=../jaddressbook-share-lib/src source.reference.jcontacts-business-core.jar=../jcontacts-business-core/src/ source.reference.jcontacts-core.jar=../jcontacts-core/src/ source.reference.jcontacts-lib.jar=../jcontacts-lib/src/ diff --git a/nbproject/project.xml b/nbproject/project.xml index a7ea36a5..4d9d8bb4 100644 --- a/nbproject/project.xml +++ b/nbproject/project.xml @@ -45,9 +45,14 @@ ${file.reference.jaddressbook-lib.jar} + + ${file.reference.jaddressbook-share-core.jar} + + + ${file.reference.jaddressbook-share-lib.jar} + ${reference.addressbook-core.jar} - WEB-INF/lib ${reference.addressbook-lib.jar} diff --git a/src/java/org/mxchange/addressbook/beans/addressbook/AddressbookWebSessionBean.java b/src/java/org/mxchange/addressbook/beans/addressbook/AddressbookWebSessionBean.java index 6a6b9fc2..e0363432 100644 --- a/src/java/org/mxchange/addressbook/beans/addressbook/AddressbookWebSessionBean.java +++ b/src/java/org/mxchange/addressbook/beans/addressbook/AddressbookWebSessionBean.java @@ -22,8 +22,6 @@ import java.util.Collections; import java.util.LinkedList; import java.util.List; import java.util.Objects; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; import javax.annotation.PostConstruct; import javax.enterprise.context.SessionScoped; import javax.enterprise.event.Event; @@ -38,13 +36,13 @@ import javax.naming.NamingException; import org.mxchange.addressbook.beans.BaseAddressbookController; import org.mxchange.addressbook.beans.login.user.AddressbookUserLoginWebSessionController; import org.mxchange.addressbook.model.addressbook.AddressbookSessionBeanRemote; -import org.mxchange.jaddressbookcore.events.addressbook.AddressbookLoadedEvent; -import org.mxchange.jaddressbookcore.events.addressbook.ObservableAddressbookLoadedEvent; -import org.mxchange.jaddressbookcore.exceptions.AddressbookNameAlreadyUsedException; -import org.mxchange.jaddressbookcore.exceptions.AddressbookNotFoundException; -import org.mxchange.jaddressbookcore.model.addressbook.Addressbook; -import org.mxchange.jaddressbookcore.model.addressbook.UserAddressbook; -import org.mxchange.jaddressbookcore.model.addressbook.entry.AddressbookEntry; +import org.mxchange.jaddressbook.events.addressbook.AddressbookLoadedEvent; +import org.mxchange.jaddressbook.events.addressbook.ObservableAddressbookLoadedEvent; +import org.mxchange.jaddressbook.exceptions.AddressbookNameAlreadyUsedException; +import org.mxchange.jaddressbook.exceptions.AddressbookNotFoundException; +import org.mxchange.jaddressbook.model.addressbook.Addressbook; +import org.mxchange.jaddressbook.model.addressbook.UserAddressbook; +import org.mxchange.jaddressbook.model.addressbook.entry.AddressbookEntry; import org.mxchange.jusercore.events.login.ObservableUserLoggedInEvent; import org.mxchange.jusercore.model.user.User; @@ -57,11 +55,6 @@ import org.mxchange.jusercore.model.user.User; @SessionScoped public class AddressbookWebSessionBean extends BaseAddressbookController implements AddressbookWebSessionController { - /** - * Map for count of user's shared addresses - */ - private static ConcurrentMap countSharesList; - /** * Serial number */ @@ -121,9 +114,6 @@ public class AddressbookWebSessionBean extends BaseAddressbookController impleme public AddressbookWebSessionBean () { // Call super constructor super(); - - // Init list - AddressbookWebSessionBean.countSharesList = new ConcurrentHashMap<>(0); } @Override @@ -164,7 +154,11 @@ public class AddressbookWebSessionBean extends BaseAddressbookController impleme } } - @Override + /** + * Event being fired after an address book has been loaded + *

+ * @param event Event being fired + */ public void afterAddressbookLoadedEvent (@Observes final ObservableAddressbookLoadedEvent event) { // event should not be null if (null == event) { @@ -197,7 +191,11 @@ public class AddressbookWebSessionBean extends BaseAddressbookController impleme this.setAddressbook(book); } - @Override + /** + * Event being fired after user has logged-in + *

+ * @param event Event being fired + */ public void afterLoginEvent (@Observes final ObservableUserLoggedInEvent event) { // Is the user logged in? if (null == event) { @@ -247,36 +245,6 @@ public class AddressbookWebSessionBean extends BaseAddressbookController impleme return this.allEntries(addressbook).size(); } - @Override - public List allUsersNotSharing () { - // Is the user logged in? - if (!this.userLoginController.isUserLoggedIn()) { - // Not logged in - throw new FaceletException("This method can only be called as logged-in user."); //NOI18N - } - - // Call EJB - return this.addressbookBean.allUsersNotSharing(this.userLoginController.getLoggedInUser(), this.getAddressbook()); - } - - @Override - public Integer countAllUserSharedAddressbooks (final User user) { - // Is there cache? - if (AddressbookWebSessionBean.countSharesList.containsKey(user)) { - // Return it instead - return AddressbookWebSessionBean.countSharesList.get(user); - } - - // Call EJB ("expensive") - Integer count = this.addressbookBean.countAllUserSharedAddressbooks(user); - - // Add to list - AddressbookWebSessionBean.countSharesList.put(user, count); - - // Return it - return count; - } - @Override public Addressbook getAddressbook () { return this.addressbook; diff --git a/src/java/org/mxchange/addressbook/beans/addressbook/AddressbookWebSessionController.java b/src/java/org/mxchange/addressbook/beans/addressbook/AddressbookWebSessionController.java index 43d84c30..2bf8014c 100644 --- a/src/java/org/mxchange/addressbook/beans/addressbook/AddressbookWebSessionController.java +++ b/src/java/org/mxchange/addressbook/beans/addressbook/AddressbookWebSessionController.java @@ -19,11 +19,9 @@ package org.mxchange.addressbook.beans.addressbook; import java.io.Serializable; import java.util.Calendar; import java.util.List; -import org.mxchange.jaddressbookcore.model.addressbook.Addressbook; -import org.mxchange.jaddressbookcore.model.addressbook.entry.AddressbookEntry; +import org.mxchange.jaddressbook.model.addressbook.Addressbook; +import org.mxchange.jaddressbook.model.addressbook.entry.AddressbookEntry; import org.mxchange.jusercore.model.user.User; -import org.mxchange.jaddressbookcore.events.addressbook.ObservableAddressbookLoadedEvent; -import org.mxchange.jusercore.events.login.ObservableUserLoggedInEvent; /** * An interface for address book beans @@ -139,31 +137,6 @@ public interface AddressbookWebSessionController extends Serializable { */ void setAddressbookCreated (final Calendar addressbookCreated); - /** - * This method is called when an address book has been successfully loaded - * from JPA. - *

- * @param event Event with address book instance - */ - void afterAddressbookLoadedEvent (final ObservableAddressbookLoadedEvent event); - - /** - * Count all shared address books by given user id - *

- * @param user User instance to look for - *

- * @return Count of user's shared address books - */ - Integer countAllUserSharedAddressbooks (final User user); - - /** - * This method is called when a user has successfully logged in his/her - * account. - *

- * @param event - */ - void afterLoginEvent (final ObservableUserLoggedInEvent event); - /** * Checks if the user is logged in and if so if it matches the current * address book owner. @@ -194,14 +167,6 @@ public interface AddressbookWebSessionController extends Serializable { */ void setAddressbook (final Addressbook addressbook); - /** - * Retrieves a list of all users this user is not sharing this address book - * with. - *

- * @return List of not sharing users - */ - List allUsersNotSharing (); - /** * Checks wether an address book has been loaded by checking the id number. *

diff --git a/src/java/org/mxchange/addressbook/beans/addressbook/share/AddressbookSharesWebSessionBean.java b/src/java/org/mxchange/addressbook/beans/addressbook/share/AddressbookSharesWebSessionBean.java new file mode 100644 index 00000000..5f90d78c --- /dev/null +++ b/src/java/org/mxchange/addressbook/beans/addressbook/share/AddressbookSharesWebSessionBean.java @@ -0,0 +1,406 @@ +/* + * Copyright (C) 2016 Roland Häder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package org.mxchange.addressbook.beans.addressbook.share; + +import java.text.MessageFormat; +import java.util.Collections; +import java.util.List; +import java.util.Objects; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; +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.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; +import org.mxchange.addressbook.beans.BaseAddressbookController; +import org.mxchange.addressbook.beans.addressbook.AddressbookWebSessionController; +import org.mxchange.addressbook.beans.login.user.AddressbookUserLoginWebSessionController; +import org.mxchange.addressbook.model.shared.AddressbookShareSessionBeanRemote; +import org.mxchange.jaddressbook.model.addressbook.Addressbook; +import org.mxchange.jaddressbookshare.events.sharing.ObservableAddressbookSharingEvent; +import org.mxchange.jaddressbookshare.events.sharing.StartedAddressbookSharingEvent; +import org.mxchange.jaddressbookshare.events.sharing.type.SharingType; +import org.mxchange.jaddressbookshare.exceptions.UserAlreadySharingAddressbookException; +import org.mxchange.jaddressbookshare.model.addressbook.shared.ShareableAddressbook; +import org.mxchange.jusercore.events.login.ObservableUserLoggedInEvent; +import org.mxchange.jusercore.model.user.User; +import org.mxchange.jusercore.model.user.profilemodes.ProfileMode; + +/** + * A bean for sharing address books with other users + *

+ * @author Roland Häder + */ +@Named (value = "shareController") +@SessionScoped +public class AddressbookSharesWebSessionBean extends BaseAddressbookController implements AddressbookSharesWebSessionController { + + /** + * Map for count of user's shared addresses + */ + private static ConcurrentMap countSharesList; + + /** + * Serial number + */ + private static final long serialVersionUID = 19_868_976_871_976_780L; + + /** + * Address book controller + */ + @Inject + private AddressbookWebSessionController addressbookBean; + + /** + * Remote bean for sharing address books + */ + private AddressbookShareSessionBeanRemote addressbookShareBean; + + /** + * Cached flag whether the user is sharing address books + */ + private Boolean isUserSharing = null; + + /** + * Share instance + */ + private ShareableAddressbook share; + + /** + * A list of all user's shared (with others) address books + */ + private List sharedAddressbooks; + + /** + * User id of sharee + */ + private Long shareeUserId; + + /** + * An event triggered when address book sharing starts or ends + */ + @Inject + @Any + private Event sharingEvent; + + /** + * Login controller injection + */ + @Inject + private AddressbookUserLoginWebSessionController userLoginController; + + /** + * Default constructor + */ + public AddressbookSharesWebSessionBean () { + // Init list + countSharesList = new ConcurrentHashMap<>(0); + } + + /** + * Observer method for ended sharing events + *

+ * @param event Event instance + */ + public void afterAdressbookShareEndedEvent (@Observes final ObservableAddressbookSharingEvent event) { + // Validate parameter + 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.getSharingType() != SharingType.ENDED) { + // Wrong event + return; + } + + // Validate event + this.validateEvent(event); + + // Add it to list + this.sharedAddressbooks.remove(event.getShareableAddressbook()); + } + + /** + * Observer method for started sharing events + *

+ * @param event Event instance + */ + public void afterAdressbookShareStartedEvent (@Observes final ObservableAddressbookSharingEvent event) { + // Validate parameter + 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.getSharingType() != SharingType.STARTED) { + // Wrong event + return; + } + + // Validate event + this.validateEvent(event); + + // Add it to list + this.sharedAddressbooks.add(event.getShareableAddressbook()); + } + + /** + * This method is called when a user has successfully logged in his/her + * account. + *

+ * @param event Event instance + */ + public void afterLoginEvent (@Observes final ObservableUserLoggedInEvent event) { + // Is the user logged in? + if (null == event) { + // Is null + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getLoggedInUser() == null) { + // user is null + throw new NullPointerException("event.user is null"); //NOI18N + } + + // Init share list + this.sharedAddressbooks = this.addressbookShareBean.allSharedAddressbooks(event.getLoggedInUser()); + } + + @Override + public List allShares () { + // Is the user logged in? + if (!this.userLoginController.isUserLoggedIn()) { + // Not logged in + throw new FaceletException("This method can only be called as logged-in user."); //NOI18N + } + + return Collections.unmodifiableList(this.sharedAddressbooks); + } + + @Override + public List allUsersNotSharing () { + // Is the user logged in? + if (!this.userLoginController.isUserLoggedIn()) { + // Not logged in + throw new FaceletException("This method can only be called as logged-in user."); //NOI18N + } + + // Call EJB + return this.addressbookShareBean.allUsersNotSharing(this.userLoginController.getLoggedInUser(), this.addressbookBean.getAddressbook()); + } + + @Override + public Integer countAllUserSharedAddressbooks (final User user) { + // Is there cache? + if (countSharesList.containsKey(user)) { + // Return it instead + return countSharesList.get(user); + } + + // Call EJB ("expensive") + Integer count = this.addressbookShareBean.countAllUserSharedAddressbooks(user); + + // Add to list + countSharesList.put(user, count); + + // Return it + return count; + } + + @Override + public ShareableAddressbook getShare () { + return this.share; + } + + @Override + public void setShare (final ShareableAddressbook share) { + this.share = share; + } + + @Override + public Long getShareeUserId () { + return this.shareeUserId; + } + + @Override + public void setShareeUserId (final Long shareeUserId) { + this.shareeUserId = shareeUserId; + } + + /** + * Post-initialization of this class + */ + @PostConstruct + public void init () { + // Try it + try { + // Get initial context + Context context = new InitialContext(); + + // Look up bean + this.addressbookShareBean = (AddressbookShareSessionBeanRemote) context.lookup("ejb/stateless-addressbook-share"); //NOI18N + } catch (final NamingException ex) { + // Continue to throw + throw new FaceletException(ex); + } + + } + + @Override + public boolean isShareeUserIdEmpty () { + return (!this.isShareeUserIdSet()); + } + + @Override + public boolean isShareeUserIdSet () { + return ((this.getShareeUserId() instanceof Long) && (this.getShareeUserId() > 0)); + } + + @Override + public boolean isSharingAddressbooks () { + // Only to be called for logged-in users + if (!this.userLoginController.isUserLoggedIn()) { + // Not logged in + throw new FaceletException("This method can only be called as logged-in user."); //NOI18N + } else if (this.isUserSharing instanceof Boolean) { + // Return cached value + return this.isUserSharing; + } + + // Call the proper bean + this.isUserSharing = this.addressbookShareBean.isUserSharingAddressbooks(this.userLoginController.getLoggedInUser()); + + // Return it + return this.isUserSharing; + } + + @Override + public String startSharing (final User user, final Addressbook addressbook) { + // Check conditions + if (!this.userLoginController.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.userLoginController.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.userLoginController.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.userLoginController.getLoggedInUser().getUserId())); //NOI18N + } else if (this.userLoginController.getLoggedInUser().getUserProfileMode() == ProfileMode.INVISIBLE) { + // User is invisible + throw new FaceletException(MessageFormat.format("user {0} is invisible and cannot start sharing address books.", this.userLoginController.getLoggedInUser().getUserId())); //NOI18N + } else if (user.getUserProfileMode() == ProfileMode.INVISIBLE) { + // User is invisible + throw new FaceletException(MessageFormat.format("user {0} is invisible and cannot be selected for sharing.", user.getUserId())); //NOI18N + } + + try { + // Init sharing + ShareableAddressbook shared = this.addressbookShareBean.startSharing(user, addressbook); + + // 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); + } + + // TODO Unfinished + return null; + } + + /** + * Validates given event for all values and throws exceptions + *

+ * @param event Event to validate + */ + private void validateEvent (final ObservableAddressbookSharingEvent 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/addressbook/share/AddressbookSharesWebSessionController.java b/src/java/org/mxchange/addressbook/beans/addressbook/share/AddressbookSharesWebSessionController.java new file mode 100644 index 00000000..18b44edc --- /dev/null +++ b/src/java/org/mxchange/addressbook/beans/addressbook/share/AddressbookSharesWebSessionController.java @@ -0,0 +1,116 @@ +/* + * Copyright (C) 2016 Roland Häder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package org.mxchange.addressbook.beans.addressbook.share; + +import java.io.Serializable; +import java.util.List; +import org.mxchange.jaddressbook.model.addressbook.Addressbook; +import org.mxchange.jaddressbookshare.model.addressbook.shared.ShareableAddressbook; +import org.mxchange.jusercore.model.user.User; + +/** + * Controller interface sharing address books + *

+ * @author Roland Häder + */ +public interface AddressbookSharesWebSessionController extends Serializable { + + /** + * Returns a list of all address books the user is sharing with others. + *

+ * @return List of all shared address books + */ + List allShares (); + + /** + * Getter for share instance + *

+ * @return Share instance + */ + ShareableAddressbook getShare (); + + /** + * Setter for share instance + *

+ * @param share Share instance + */ + void setShare (final ShareableAddressbook share); + + /** + * Getter for sharee's user id + *

+ * @return Sharee's user id + */ + Long getShareeUserId (); + + /** + * Setter for sharee's user id + *

+ * @param shareeUserId Sharee's user id + */ + void setShareeUserId (final Long shareeUserId); + + /** + * Checks if the sharee's user id is empty. + *

+ * @return Whether the sharee's user id is empty. + */ + boolean isShareeUserIdEmpty (); + + /** + * Checks whether the sharee's user id is set + *

+ * @return Whether the sharee's user id is set + */ + boolean isShareeUserIdSet (); + + /** + * Checks wether the current user is sharing address books with others + *

+ * @return Whether the current user is sharing address books + */ + boolean isSharingAddressbooks (); + + /** + * 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); + + /** + * Count all shared address books by given user id + *

+ * @param user User instance to look for + *

+ * @return Count of user's shared address books + */ + Integer countAllUserSharedAddressbooks (final User user); + + /** + * Retrieves a list of all users this user is not sharing this address book + * with. + *

+ * @return List of not sharing users + */ + List allUsersNotSharing (); + +} diff --git a/src/java/org/mxchange/addressbook/beans/shares/AddressbookSharesWebSessionBean.java b/src/java/org/mxchange/addressbook/beans/shares/AddressbookSharesWebSessionBean.java deleted file mode 100644 index eb1210a6..00000000 --- a/src/java/org/mxchange/addressbook/beans/shares/AddressbookSharesWebSessionBean.java +++ /dev/null @@ -1,345 +0,0 @@ -/* - * Copyright (C) 2016 Roland Häder - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ -package org.mxchange.addressbook.beans.shares; - -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; -import javax.naming.Context; -import javax.naming.InitialContext; -import javax.naming.NamingException; -import org.mxchange.addressbook.beans.BaseAddressbookController; -import org.mxchange.addressbook.beans.login.user.AddressbookUserLoginWebSessionController; -import org.mxchange.addressbook.model.shared.SharedAddressbooksSessionBeanRemote; -import org.mxchange.jaddressbookcore.events.sharing.StartedAddressbookSharingEvent; -import org.mxchange.jaddressbookcore.events.sharing.type.SharingType; -import org.mxchange.jaddressbookcore.exceptions.UserAlreadySharingAddressbookException; -import org.mxchange.jaddressbookcore.model.addressbook.Addressbook; -import org.mxchange.jaddressbookcore.model.addressbook.shared.ShareableAddressbook; -import org.mxchange.jusercore.model.user.User; -import org.mxchange.jusercore.model.user.profilemodes.ProfileMode; -import org.mxchange.jaddressbookcore.events.sharing.ObservableAddressbookSharingEvent; -import org.mxchange.jusercore.events.login.ObservableUserLoggedInEvent; - -/** - * A bean for sharing address books with other users - *

- * @author Roland Häder - */ -@Named (value = "shareController") -@SessionScoped -public class AddressbookSharesWebSessionBean extends BaseAddressbookController implements AddressbookSharesWebSessionController { - - /** - * Serial number - */ - private static final long serialVersionUID = 19_868_976_871_976_780L; - - /** - * Cached flag whether the user is sharing address books - */ - private Boolean isUserSharing = null; - - /** - * Login controller injection - */ - @Inject - private AddressbookUserLoginWebSessionController userLoginController; - - /** - * Share instance - */ - private ShareableAddressbook share; - - /** - * Remote bean for sharing address books - */ - private SharedAddressbooksSessionBeanRemote shareBean; - - /** - * A list of all user's shared (with others) address books - */ - private List sharedAddressbooks; - - /** - * User id of sharee - */ - private Long shareeUserId; - - /** - * An event triggered when address book sharing starts or ends - */ - @Inject - @Any - private Event sharingEvent; - - /** - * Default constructor - */ - public AddressbookSharesWebSessionBean () { - // Try it - try { - // Get initial context - Context context = new InitialContext(); - - // Look up bean - this.shareBean = (SharedAddressbooksSessionBeanRemote) context.lookup("ejb/stateless-addressbook-share"); //NOI18N - } catch (final NamingException ex) { - // Continue to throw - throw new FaceletException(ex); - } - } - - @Override - public void afterAdressbookShareEnded (@Observes final ObservableAddressbookSharingEvent event) { - // Validate parameter - 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.getSharingType() != SharingType.ENDED) { - // Wrong event - return; - } - - // Validate event - this.validateEvent(event); - - // Add it to list - this.sharedAddressbooks.remove(event.getShareableAddressbook()); - } - - @Override - public void afterAdressbookShareStarted (@Observes final ObservableAddressbookSharingEvent event) { - // Validate parameter - 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.getSharingType() != SharingType.STARTED) { - // Wrong event - return; - } - - // Validate event - this.validateEvent(event); - - // Add it to list - this.sharedAddressbooks.add(event.getShareableAddressbook()); - } - - @Override - public void afterLoginEvent (@Observes final ObservableUserLoggedInEvent event) { - // Is the user logged in? - if (null == event) { - // Is null - throw new NullPointerException("event is null"); //NOI18N - } else if (event.getLoggedInUser() == null) { - // user is null - throw new NullPointerException("event.user is null"); //NOI18N - } - - // Init share list - this.sharedAddressbooks = this.shareBean.allSharedAddressbooks(event.getLoggedInUser()); - } - - @Override - public List allShares () { - // Is the user logged in? - if (!this.userLoginController.isUserLoggedIn()) { - // Not logged in - throw new FaceletException("This method can only be called as logged-in user."); //NOI18N - } - - return Collections.unmodifiableList(this.sharedAddressbooks); - } - - @Override - public ShareableAddressbook getShare () { - return this.share; - } - - @Override - public void setShare (final ShareableAddressbook share) { - this.share = share; - } - - @Override - public Long getShareeUserId () { - return this.shareeUserId; - } - - @Override - public void setShareeUserId (final Long shareeUserId) { - this.shareeUserId = shareeUserId; - } - - /** - * Post-initialization of this class - */ - @PostConstruct - public void init () { - } - - @Override - public boolean isShareeUserIdEmpty () { - return (!this.isShareeUserIdSet()); - } - - @Override - public boolean isShareeUserIdSet () { - return ((this.getShareeUserId() instanceof Long) && (this.getShareeUserId() > 0)); - } - - @Override - public boolean isSharingAddressbooks () { - // Only to be called for logged-in users - if (!this.userLoginController.isUserLoggedIn()) { - // Not logged in - throw new FaceletException("This method can only be called as logged-in user."); //NOI18N - } else if (this.isUserSharing instanceof Boolean) { - // Return cached value - return this.isUserSharing; - } - - // Call the proper bean - this.isUserSharing = this.shareBean.isUserSharingAddressbooks(this.userLoginController.getLoggedInUser()); - - // Return it - return this.isUserSharing; - } - - @Override - public String startSharing (final User user, final Addressbook addressbook) { - // Check conditions - if (!this.userLoginController.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.userLoginController.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.userLoginController.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.userLoginController.getLoggedInUser().getUserId())); //NOI18N - } else if (this.userLoginController.getLoggedInUser().getUserProfileMode() == ProfileMode.INVISIBLE) { - // User is invisible - throw new FaceletException(MessageFormat.format("user {0} is invisible and cannot start sharing address books.", this.userLoginController.getLoggedInUser().getUserId())); //NOI18N - } else if (user.getUserProfileMode() == ProfileMode.INVISIBLE) { - // User is invisible - throw new FaceletException(MessageFormat.format("user {0} is invisible and cannot be selected for sharing.", user.getUserId())); //NOI18N - } - - try { - // Init sharing - ShareableAddressbook shared = this.shareBean.startSharing(user, addressbook); - - // 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); - } - - // TODO Unfinished - return null; - } - - /** - * Validates given event for all values and throws exceptions - *

- * @param event Event to validate - */ - private void validateEvent (final ObservableAddressbookSharingEvent 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/AddressbookSharesWebSessionController.java b/src/java/org/mxchange/addressbook/beans/shares/AddressbookSharesWebSessionController.java deleted file mode 100644 index f3dd5048..00000000 --- a/src/java/org/mxchange/addressbook/beans/shares/AddressbookSharesWebSessionController.java +++ /dev/null @@ -1,122 +0,0 @@ -/* - * Copyright (C) 2016 Roland Häder - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ -package org.mxchange.addressbook.beans.shares; - -import java.io.Serializable; -import java.util.List; -import org.mxchange.jaddressbookcore.model.addressbook.Addressbook; -import org.mxchange.jaddressbookcore.model.addressbook.shared.ShareableAddressbook; -import org.mxchange.jusercore.model.user.User; -import org.mxchange.jaddressbookcore.events.sharing.ObservableAddressbookSharingEvent; -import org.mxchange.jusercore.events.login.ObservableUserLoggedInEvent; - -/** - * Controller interface sharing address books - *

- * @author Roland Häder - */ -public interface AddressbookSharesWebSessionController extends Serializable { - - /** - * Observer method for ended sharing events - *

- * @param event Event instance - */ - void afterAdressbookShareEnded (final ObservableAddressbookSharingEvent event); - - /** - * Observer method for started sharing events - *

- * @param event Event instance - */ - void afterAdressbookShareStarted (final ObservableAddressbookSharingEvent event); - - /** - * This method is called when a user has successfully logged in his/her - * account. - *

- * @param event - */ - void afterLoginEvent (final ObservableUserLoggedInEvent event); - - /** - * Returns a list of all address books the user is sharing with others. - *

- * @return List of all shared address books - */ - List allShares (); - - /** - * Getter for share instance - *

- * @return Share instance - */ - ShareableAddressbook getShare (); - - /** - * Setter for share instance - *

- * @param share Share instance - */ - void setShare (final ShareableAddressbook share); - - /** - * Getter for sharee's user id - *

- * @return Sharee's user id - */ - Long getShareeUserId (); - - /** - * Setter for sharee's user id - *

- * @param shareeUserId Sharee's user id - */ - void setShareeUserId (final Long shareeUserId); - - /** - * Checks if the sharee's user id is empty. - *

- * @return Whether the sharee's user id is empty. - */ - boolean isShareeUserIdEmpty (); - - /** - * Checks whether the sharee's user id is set - *

- * @return Whether the sharee's user id is set - */ - boolean isShareeUserIdSet (); - - /** - * Checks wether the current user is sharing address books with others - *

- * @return Whether the current user is sharing address books - */ - boolean isSharingAddressbooks (); - - /** - * 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/src/java/org/mxchange/addressbook/validator/addressbook/AddressbookIdValidator.java b/src/java/org/mxchange/addressbook/validator/addressbook/AddressbookIdValidator.java index 78db21b3..ab8caeef 100644 --- a/src/java/org/mxchange/addressbook/validator/addressbook/AddressbookIdValidator.java +++ b/src/java/org/mxchange/addressbook/validator/addressbook/AddressbookIdValidator.java @@ -26,8 +26,8 @@ import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import org.mxchange.addressbook.model.addressbook.AddressbookSessionBeanRemote; -import org.mxchange.jaddressbookcore.exceptions.AddressbookNotFoundException; -import org.mxchange.jaddressbookcore.model.addressbook.Addressbook; +import org.mxchange.jaddressbook.exceptions.AddressbookNotFoundException; +import org.mxchange.jaddressbook.model.addressbook.Addressbook; import org.mxchange.jcoreee.validator.number.BaseLongValidator; import org.mxchange.jcoreeelogger.beans.local.logger.Log; import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal; diff --git a/web/guest/user/user_show_addressbook.xhtml b/web/guest/user/user_show_addressbook.xhtml index 0ebf6524..68315390 100644 --- a/web/guest/user/user_show_addressbook.xhtml +++ b/web/guest/user/user_show_addressbook.xhtml @@ -14,7 +14,7 @@ - + diff --git a/web/user/login_start_sharing_addressbook.xhtml b/web/user/login_start_sharing_addressbook.xhtml index 6eb82de6..16d25385 100644 --- a/web/user/login_start_sharing_addressbook.xhtml +++ b/web/user/login_start_sharing_addressbook.xhtml @@ -5,10 +5,9 @@ xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://xmlns.jcp.org/jsf/facelets" xmlns:h="http://xmlns.jcp.org/jsf/html" - xmlns:f="http://xmlns.jcp.org/jsf/core" - xmlns:c="http://xmlns.jcp.org/jsp/jstl/core"> + xmlns:f="http://xmlns.jcp.org/jsf/core"> - + @@ -28,7 +27,7 @@

- +