2 * Copyright (C) 2015 Roland Haeder
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package org.mxchange.addressbook.model.shared;
19 import java.text.MessageFormat;
20 import java.util.List;
21 import javax.ejb.Stateless;
22 import javax.persistence.NoResultException;
23 import javax.persistence.Query;
24 import org.mxchange.addressbook.model.addressbook.shared.AddressbookShare;
25 import org.mxchange.addressbook.model.addressbook.shared.ShareableAddressbook;
26 import org.mxchange.jcoreee.database.BaseDatabaseBean;
27 import org.mxchange.jusercore.model.user.User;
30 * A stateless bean for handling address book sharing
32 * @author Roland Haeder
34 @Stateless (name = "share", mappedName = "ejb/stateless-share", description = "A stateless bean for handling shared addressbooks")
35 public class SharedAddressbooksSessionBean extends BaseDatabaseBean implements SharedAddressbooksSessionBeanRemote {
40 private static final long serialVersionUID = 136_984_697_285_694_710L;
43 * Shared address books. If you add another share to the database, please
44 * also update this list as some methods may prefer this over asking the JPA
47 private List<ShareableAddressbook> shares;
52 public SharedAddressbooksSessionBean () {
58 @SuppressWarnings ("unchecked")
59 public boolean isUserSharingAddressbooks (final User user) {
61 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserSharingAddressbooks: user={0} - CALLED!", user)); //NOI18N
66 throw new NullPointerException("user is null"); //NOI18N
67 } else if (user.getUserId() == null) {
68 // Null userId is not allowed
69 throw new NullPointerException("user.userId is null"); //NOI18N
70 } else if (user.getUserId() < 1) {
72 throw new IllegalArgumentException(MessageFormat.format("user.UserId={0} is an invalid value", user.getUserId())); //NOI18N
73 } else if (this.shares instanceof List) {
75 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserSharingAddressbooks: this.shares.isEmpty()={0} - EXIT!", this.shares.isEmpty()));
77 // Is already initialized
78 return (!this.shares.isEmpty());
82 Query query = this.getEntityManager().createNamedQuery("SearchUserSharedAddressbooks", AddressbookShare.class); //NOI18N
85 query.setParameter("user", user); //NOI18N
87 // Default is not sharing
88 boolean isSharing = false;
90 // Try it as an exception may be thrown
93 this.shares = query.getResultList();
96 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserSharingAddressbooks: shares.size()={0}", this.shares.size()));
99 isSharing = (!this.shares.isEmpty());
100 } catch (final NoResultException ex) {
101 // Not sharing anything
102 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserSharingAddressbooks: User {0} is not sharing address books: {1}", user, ex)); //NOI18N
106 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserSharingAddressbooks: iSharing={0} - EXIT!", isSharing)); //NOI18N