--- /dev/null
+/*
+ * Copyright (C) 2015 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.
+ *
+ * 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.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.mxchange.addressbook.model.shared;
+
+import java.text.MessageFormat;
+import java.util.List;
+import javax.ejb.Stateless;
+import javax.persistence.NoResultException;
+import javax.persistence.Query;
+import org.mxchange.addressbook.model.addressbook.shared.AddressbookShare;
+import org.mxchange.addressbook.model.addressbook.shared.ShareableAddressbook;
+import org.mxchange.jcoreee.database.BaseDatabaseBean;
+import org.mxchange.jusercore.model.user.User;
+
+/**
+ * A stateless bean for handling address book sharing
+ * <p>
+ * @author Roland Haeder
+ */
+@Stateless (name = "share", mappedName = "ejb/stateless-share", description = "A stateless bean for handling shared addressbooks")
+public class SharedAddressbooksSessionBean extends BaseDatabaseBean implements SharedAddressbooksSessionBeanRemote {
+
+ /**
+ * Serial number
+ */
+ private static final long serialVersionUID = 136_984_697_285_694_710L;
+
+ /**
+ * Shared address books. If you add another share to the database, please
+ * also update this list as some methods may prefer this over asking the JPA
+ * all over again.
+ */
+ private List<ShareableAddressbook> shares;
+
+ /**
+ * Default constructor
+ */
+ public SharedAddressbooksSessionBean () {
+ // Init shares list
+ this.shares = null;
+ }
+
+ @Override
+ @SuppressWarnings ("unchecked")
+ public boolean isUserSharingAddressbooks (final User user) {
+ // Trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserSharingAddressbooks: user={0} - CALLED!", user)); //NOI18N
+
+ // Is user null?
+ if (null == user) {
+ // Throw NPE
+ throw new NullPointerException("user is null"); //NOI18N
+ } else if (user.getUserId() == null) {
+ // Null userId is not allowed
+ throw new NullPointerException("user.userId is null"); //NOI18N
+ } else if (user.getUserId() < 1) {
+ // Not allowed value
+ throw new IllegalArgumentException(MessageFormat.format("user.UserId={0} is an invalid value", user.getUserId())); //NOI18N
+ } else if (this.shares instanceof List) {
+ // Trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserSharingAddressbooks: this.shares.isEmpty()={0} - EXIT!", this.shares.isEmpty()));
+
+ // Is already initialized
+ return (!this.shares.isEmpty());
+ }
+
+ // Get named query
+ Query query = this.getEntityManager().createNamedQuery("SearchUserSharedAddressbooks", AddressbookShare.class); //NOI18N
+
+ // Set parameter
+ query.setParameter("user", user); //NOI18N
+
+ // Default is not sharing
+ boolean isSharing = false;
+
+ // Try it as an exception may be thrown
+ try {
+ // Get results
+ this.shares = query.getResultList();
+
+ // Debug message
+ this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserSharingAddressbooks: shares.size()={0}", this.shares.size()));
+
+ // Is it not empty?
+ isSharing = (!this.shares.isEmpty());
+ } catch (final NoResultException ex) {
+ // Not sharing anything
+ this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserSharingAddressbooks: User {0} is not sharing address books: {1}", user, ex)); //NOI18N
+ }
+
+ // Trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserSharingAddressbooks: iSharing={0} - EXIT!", isSharing)); //NOI18N
+
+ // Return it
+ return isSharing;
+ }
+}