From: Roland Haeder Date: Wed, 14 Oct 2015 08:17:13 +0000 (+0200) Subject: Continued: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=86c237803c419e9077f10451f6d42694a49b58f2;p=addressbook-mailer-ejb.git Continued: - added remote session bean for sharing address books - updated jar(s) Signed-off-by:Roland Häder --- diff --git a/lib/jcoreee.jar b/lib/jcoreee.jar index c2e022d..4cdb134 100644 Binary files a/lib/jcoreee.jar and b/lib/jcoreee.jar differ diff --git a/src/java/org/mxchange/addressbook/model/shared/SharedAddressbooksSessionBean.java b/src/java/org/mxchange/addressbook/model/shared/SharedAddressbooksSessionBean.java new file mode 100644 index 0000000..6759f9a --- /dev/null +++ b/src/java/org/mxchange/addressbook/model/shared/SharedAddressbooksSessionBean.java @@ -0,0 +1,111 @@ +/* + * 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 . + */ +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 + *

+ * @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 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; + } +}