]> git.mxchange.org Git - addressbook-ejb.git/blob - src/java/org/mxchange/addressbook/model/shared/SharedAddressbooksSessionBean.java
Continued:
[addressbook-ejb.git] / src / java / org / mxchange / addressbook / model / shared / SharedAddressbooksSessionBean.java
1 /*
2  * Copyright (C) 2015 Roland Haeder
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.addressbook.model.shared;
18
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;
28
29 /**
30  * A stateless bean for handling address book sharing
31  * <p>
32  * @author Roland Haeder
33  */
34 @Stateless (name = "share", mappedName = "ejb/stateless-share", description = "A stateless bean for handling shared addressbooks")
35 public class SharedAddressbooksSessionBean extends BaseDatabaseBean implements SharedAddressbooksSessionBeanRemote {
36
37         /**
38          * Serial number
39          */
40         private static final long serialVersionUID = 136_984_697_285_694_710L;
41
42         /**
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
45          * all over again.
46          */
47         private List<ShareableAddressbook> shares;
48
49         /**
50          * Default constructor
51          */
52         public SharedAddressbooksSessionBean () {
53                 // Init shares list
54                 this.shares = null;
55         }
56
57         @Override
58         @SuppressWarnings ("unchecked")
59         public boolean isUserSharingAddressbooks (final User user) {
60                 // Trace message
61                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserSharingAddressbooks: user={0} - CALLED!", user)); //NOI18N
62
63                 // Is user null?
64                 if (null == user) {
65                         // Throw NPE
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) {
71                         // Not allowed value
72                         throw new IllegalArgumentException(MessageFormat.format("user.UserId={0} is an invalid value", user.getUserId())); //NOI18N
73                 } else if (this.shares instanceof List) {
74                         // Trace message
75                         this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserSharingAddressbooks: this.shares.isEmpty()={0} - EXIT!", this.shares.isEmpty()));
76
77                         // Is already initialized
78                         return (!this.shares.isEmpty());
79                 }
80
81                 // Get named query
82                 Query query = this.getEntityManager().createNamedQuery("SearchUserSharedAddressbooks", AddressbookShare.class); //NOI18N
83
84                 // Set parameter
85                 query.setParameter("user", user); //NOI18N
86
87                 // Default is not sharing
88                 boolean isSharing = false;
89
90                 // Try it as an exception may be thrown
91                 try {
92                         // Get results
93                         this.shares = query.getResultList();
94
95                         // Debug message
96                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserSharingAddressbooks: shares.size()={0}", this.shares.size()));
97
98                         // Is it not empty?
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
103                 }
104
105                 // Trace message
106                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserSharingAddressbooks: iSharing={0} - EXIT!", isSharing)); //NOI18N
107
108                 // Return it
109                 return isSharing;
110         }
111 }