X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2Fjava%2Forg%2Fmxchange%2Faddressbook%2Fmodel%2Faddressbook%2FAddressbookSessionBean.java;h=38ea664a13e10eed38198022f8230c38b7dc439f;hb=e5535f69b337e252b6bf08dad34ce4d1c643b36c;hp=528f0dd82c591701fe4f1b6437751de01e966bcf;hpb=7e739251ea02a992875394eb22de5d35223c57de;p=addressbook-mailer-ejb.git diff --git a/src/java/org/mxchange/addressbook/model/addressbook/AddressbookSessionBean.java b/src/java/org/mxchange/addressbook/model/addressbook/AddressbookSessionBean.java index 528f0dd..38ea664 100644 --- a/src/java/org/mxchange/addressbook/model/addressbook/AddressbookSessionBean.java +++ b/src/java/org/mxchange/addressbook/model/addressbook/AddressbookSessionBean.java @@ -1,39 +1,44 @@ /* - * 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 - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * 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 General Public License for more details. + * GNU Affero General Public License for more details. * - * You should have received a copy of the GNU General Public License + * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ package org.mxchange.addressbook.model.addressbook; import java.text.MessageFormat; +import java.util.ArrayList; +import java.util.GregorianCalendar; +import java.util.LinkedList; import java.util.List; import javax.ejb.Stateless; import javax.persistence.NoResultException; import javax.persistence.Query; -import org.mxchange.addressbook.exceptions.AddressbookNameAlreadyUsedException; -import org.mxchange.addressbook.exceptions.AddressbookNotFoundException; -import org.mxchange.addressbook.model.addressbook.entry.AddressbookEntry; -import org.mxchange.addressbook.model.addressbook.shared.ShareableAddressbook; +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.jaddressbookcore.model.addressbook.shared.ShareableAddressbook; import org.mxchange.jcoreee.database.BaseDatabaseBean; import org.mxchange.jusercore.model.user.User; /** * A stateless bean handling addressbooks *

- * @author Roland Haeder + * @author Roland Haeder */ -@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 { /** @@ -44,18 +49,132 @@ public class AddressbookSessionBean extends BaseDatabaseBean implements Addressb @Override @SuppressWarnings ("unchecked") public List 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(); } + @Override + @SuppressWarnings ("unchecked") + public List 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); //NOI18N + + // Set parameter + allUsersExceptQuery.setParameter("user", user); //NOI18N + + // Get full list + List allUsersExcept = allUsersExceptQuery.getResultList(); + + // Debug message + this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: allUsersExcept.size()={0}", allUsersExcept.size())); //NOI18N + + // Now get all shares this user has created + Query allSharesQuery = this.getEntityManager().createNamedQuery("SearchUserSharedAddressbooks", List.class); //NOI18N + + // Set parameter + allSharesQuery.setParameter("user", user); //NOI18N + + // Get full list again + List allShares = allSharesQuery.getResultList(); + + // Debug message + this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: allShares.size()={0}", allShares.size())); //NOI18N + + // List for users aharing with given + List 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 userList = new LinkedList<>(); + + // Walk through all users + 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); + } + } + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("allUsersNotSharing: userList.size()={0} - EXIT!", userList.size())); //NOI18N + + // Return it + return userList; + } + @Override @SuppressWarnings ("unchecked") public Integer countAllUserSharedAddressbooks (final User user) { @@ -122,6 +241,9 @@ public class AddressbookSessionBean extends BaseDatabaseBean implements Addressb throw new AddressbookNameAlreadyUsedException(addressbook); } + // Add timestamp of creation + addressbook.setAddressbookCreated(new GregorianCalendar()); + // Persist it now this.getEntityManager().persist(addressbook); @@ -161,7 +283,7 @@ public class AddressbookSessionBean extends BaseDatabaseBean implements Addressb @Override @SuppressWarnings ("unchecked") - public List getUsersList (final User loggedInUser) { + public List getUsersAddressbookList (final User loggedInUser) { // Trace message this.getLoggerBeanLocal().logTrace(MessageFormat.format("getUsersList: loggedInUser={0} - CALLED!", loggedInUser)); //NOI18N @@ -237,7 +359,13 @@ public class AddressbookSessionBean extends BaseDatabaseBean implements Addressb 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