package org.mxchange.addressbook.model.addressbook;
import java.text.MessageFormat;
+import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.NoResultException;
return query.getResultList();
}
+ @Override
+ @SuppressWarnings ("unchecked")
+ public List<User> allUsersNotSharing (final User user, final Addressbook addressbook) {
+ // Trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("allUsersNotSharing: user={0},addressbook={1} - CALLED!", user, addressbook)); //NOI18N
+
+ // Test parameter
+ 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
+ throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
+ } else if (null == addressbook) {
+ // Again NPE
+ throw new NullPointerException("addressbook is null"); //NOI18N
+ } else if (addressbook.getAddressbookId() == null) {
+ // Again NPE
+ throw new NullPointerException("addressbook.addressbookId is null"); //NOI18N
+ } else if (addressbook.getAddressbookId() < 1) {
+ // Invalid id
+ throw new IllegalArgumentException(MessageFormat.format("addressbook.getAddressbookId={0} is invalid", addressbook.getAddressbookId())); //NOI18N
+ }
+
+ // Get named query for a user list without given user
+ Query allUsersExceptQuery = this.getEntityManager().createNamedQuery("SearchAllUsersExcept", List.class);
+
+ // Set parameter
+ allUsersExceptQuery.setParameter("user", user);
+
+ // Get full list
+ List<User> allUsersExcept = allUsersExceptQuery.getResultList();
+
+ // Debug message
+ this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: allUsersExcept.size()={0}", allUsersExcept.size()));
+
+ // Now get all users this user is sharing with, first a new query
+ Query allShareesQuery = this.getEntityManager().createNamedQuery("SearchUserSharedAddressbooks", List.class);
+
+ // Set parameter
+ allShareesQuery.setParameter("user", user);
+
+ // Get full list again
+ List<User> allSharees = allShareesQuery.getResultList();
+
+ // Debug message
+ this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: allSharees.size()={0}", allSharees.size()));
+
+ // Init final user list
+ List<User> userList = new ArrayList<>(0);
+
+ // Walk through all users
+ for (final User u : allUsersExcept) {
+ // Log message
+ this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: u={0}", u));
+
+ // Is this user not included?
+ if (!allSharees.contains(u)) {
+ // Found one to add!
+ // Log message
+ this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: u={0} - ADDING!", u));
+
+ // Add it
+ userList.add(u);
+ }
+ }
+
+ // Trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("allUsersNotSharing: userList.size()={0} - EXIT!", userList.size()));
+
+ // Return it
+ return userList;
+ }
+
@Override
@SuppressWarnings ("unchecked")
public Integer countAllUserSharedAddressbooks (final User user) {
@Override
@SuppressWarnings ("unchecked")
- public List<Addressbook> getUsersList (final User loggedInUser) {
+ public List<Addressbook> getUsersAddressbookList (final User loggedInUser) {
// Trace message
this.getLoggerBeanLocal().logTrace(MessageFormat.format("getUsersList: loggedInUser={0} - CALLED!", loggedInUser)); //NOI18N