/*
- * Copyright (C) 2015 Roland Haeder
+ * Copyright (C) 2016 Roland Haeder
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
package org.mxchange.addressbook.model.addressbook;
import java.text.MessageFormat;
+import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import javax.ejb.Stateless;
/**
* A stateless bean handling addressbooks
* <p>
- * @author Roland Haeder
+ * @author Roland Haeder<roland@mxchange.org>
*/
-@Stateless (name = "addressbook", mappedName = "ejb/stateless-addressbook", description = "A stateless bean for handling addressbooks")
+@Stateless (name = "addressbook", mappedName = "ejb/stateless-addressbook-adr", description = "A stateless bean for handling Addressbook addressbooks")
public class AddressbookSessionBean extends BaseDatabaseBean implements AddressbookSessionBeanRemote {
/**
@Override
@SuppressWarnings ("unchecked")
public List<AddressbookEntry> allEntries (final Addressbook addressbook) {
+ // Trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("allEntries: addressbook={0} - CALLED!", addressbook)); //NOI18N
+
+ // Validate parameter
+ if (null == addressbook) {
+ // Throw NPE
+ throw new NullPointerException("addressbook is null");
+ } else if (addressbook.getAddressbookId() == null) {
+ // Throw NPE again
+ throw new NullPointerException("addressbook.addressbookId is null");
+ } else if (addressbook.getAddressbookId() < 1) {
+ // Invalid id number
+ throw new IllegalArgumentException(MessageFormat.format("addressbook.addressbookId={0} is invalid.", addressbook.getAddressbookId()));
+ } else if (addressbook.getAddressbookUser() == null) {
+ // Throw again NPE
+ throw new NullPointerException("addressbook.addressbookUser is null");
+ } else if (addressbook.getAddressbookUser().getUserId() == null) {
+ // Throw again NPE
+ throw new NullPointerException("addressbook.addressbookUser.userId is null");
+ } else if (addressbook.getAddressbookUser().getUserId() < 1) {
+ // Invalid id number again
+ throw new IllegalArgumentException(MessageFormat.format("addressbook.addressbookUser.userId={0} is invalid", addressbook.getAddressbookUser().getUserId()));
+ }
+
// Generate query
- Query query = this.getEntityManager().createNamedQuery("AllAddressbookEntries", List.class); //NOI18N
+ Query query = this.getEntityManager().createNamedQuery("SearchUsersAddressbookEntries", List.class); //NOI18N
// Set parameters
query.setParameter("addressbook", addressbook); //NOI18N
query.setParameter("owner", addressbook.getAddressbookUser()); //NOI18N
- query.setParameter("sharer", addressbook.getAddressbookUser()); //NOI18N
// Return it
return query.getResultList();
// Debug message
this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: allUsersExcept.size()={0}", allUsersExcept.size())); //NOI18N
- // Now get all users this user is sharing with, first a new query
- Query allShareesQuery = this.getEntityManager().createNamedQuery("SearchUserSharedAddressbooks", List.class); //NOI18N
+ // Now get all shares this user has created
+ Query allSharesQuery = this.getEntityManager().createNamedQuery("SearchUserSharedAddressbooks", List.class); //NOI18N
// Set parameter
- allShareesQuery.setParameter("user", user); //NOI18N
+ allSharesQuery.setParameter("user", user); //NOI18N
// Get full list again
- List<User> allSharees = allShareesQuery.getResultList();
+ List<ShareableAddressbook> allShares = allSharesQuery.getResultList();
// Debug message
- this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: allSharees.size()={0}", allSharees.size())); //NOI18N
+ this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: allShares.size()={0}", allShares.size())); //NOI18N
+
+ // List for users aharing with given
+ List<User> sharingUsers = new ArrayList<>(allShares.size());
+
+ // Check all entries
+ for (final ShareableAddressbook share : allShares) {
+ // Debug message
+ this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: share.shareUserSharee={0}", share.getShareUserSharee())); //NOI18N
+
+ // Add it
+ sharingUsers.add(share.getShareUserSharee());
+ }
+
+ // Debug message
+ this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: sharingUsers.size()={0}", sharingUsers.size())); //NOI18N
// Init final user list
List<User> userList = new LinkedList<>();
// Walk through all users
- for (final User u : allUsersExcept) {
- // Log message
- this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: u={0}", u)); //NOI18N
-
- // Some entries in allSharees?
- if (!allSharees.isEmpty()) {
- // Default is not found
- boolean isFound = false;
-
- // Check all entries (user name matching)
- for (final User sharee : allSharees) {
- // Debug message
- this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: sharee={0}", sharee));
-
- // Does it match?
- if (sharee.equals(u)) {
- // Skip this one
- isFound = true;
- break;
- }
- }
-
- // Still not found?
- if (!isFound) {
- // Found one to add
- this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: u={0} - ADDING!", u)); //NOI18N
-
- // Add it
- userList.add(u);
- }
+ for (final User foundUser : allUsersExcept) {
+ // Debug message
+ this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: foundUser={0}", foundUser)); //NOI18N
+
+ // Does the list contain it ?
+ if (!sharingUsers.contains(foundUser)) {
+ // Found one to add
+ this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: foundUser={0} - ADDING!", foundUser)); //NOI18N
+
+ // Add it
+ userList.add(foundUser);
}
}
throw new NullPointerException("addressbook is null"); //NOI18N
} else if (addressbook.getAddressbookUser() == null) {
// User instance is null
- throw new NullPointerException("addressbook.user should not be null."); //NOI18N
+ throw new NullPointerException("addressbook.addressbookUser is null."); //NOI18N
+ } else if (addressbook.getAddressbookUser().getUserId() == null) {
+ // User instance is null
+ throw new NullPointerException("addressbook.addressbookUser.userId is null."); //NOI18N
+ } else if (addressbook.getAddressbookUser().getUserId() < 1) {
+ // User instance is null
+ throw new NullPointerException(MessageFormat.format("addressbook.addressbookUser.userId={0} is invalid.", addressbook.getAddressbookUser().getUserId())); //NOI18N
} else if (addressbook.getAddressbookName() == null) {
// Address book name not set
throw new NullPointerException("addressbook.addressbookName should not be null"); //NOI18N