]> git.mxchange.org Git - jjobs-mailer-ejb.git/blob - src/java/org/mxchange/addressbook/model/shared/SharedAddressbooksSessionBean.java
Continued:
[jjobs-mailer-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 java.util.Objects;
22 import javax.ejb.Stateless;
23 import javax.persistence.NoResultException;
24 import javax.persistence.Query;
25 import org.mxchange.addressbook.exceptions.UserAlreadySharingAddressbookException;
26 import org.mxchange.addressbook.model.addressbook.Addressbook;
27 import org.mxchange.addressbook.model.addressbook.shared.AddressbookShare;
28 import org.mxchange.addressbook.model.addressbook.shared.ShareableAddressbook;
29 import org.mxchange.jcoreee.database.BaseDatabaseBean;
30 import org.mxchange.jusercore.model.user.User;
31
32 /**
33  * A stateless bean for handling address book sharing
34  * <p>
35  * @author Roland Haeder
36  */
37 @Stateless (name = "share", mappedName = "ejb/stateless-share", description = "A stateless bean for handling shared addressbooks")
38 public class SharedAddressbooksSessionBean extends BaseDatabaseBean implements SharedAddressbooksSessionBeanRemote {
39
40         /**
41          * Serial number
42          */
43         private static final long serialVersionUID = 136_984_697_285_694_710L;
44
45         /**
46          * Shared address books
47          */
48         private List<ShareableAddressbook> shares;
49
50         /**
51          * Default constructor
52          */
53         public SharedAddressbooksSessionBean () {
54                 // Init shares list
55                 this.shares = null;
56         }
57
58         @Override
59         @SuppressWarnings ("unchecked")
60         public Boolean isUserSharingAddressbooks (final User user) {
61                 // Trace message
62                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserSharingAddressbooks: user={0} - CALLED!", user)); //NOI18N
63
64                 // Is user null?
65                 if (null == user) {
66                         // Throw NPE
67                         throw new NullPointerException("user is null"); //NOI18N
68                 } else if (user.getUserId() == null) {
69                         // Null userId is not allowed
70                         throw new NullPointerException("user.userId is null"); //NOI18N
71                 } else if (user.getUserId() < 1) {
72                         // Not allowed value
73                         throw new IllegalArgumentException(MessageFormat.format("user.UserId={0} is an invalid value", user.getUserId())); //NOI18N
74                 }
75
76                 // Get named query
77                 Query query = this.getEntityManager().createNamedQuery("SearchUserSharedAddressbooks", AddressbookShare.class); //NOI18N
78
79                 // Set parameter
80                 query.setParameter("user", user); //NOI18N
81
82                 // Default is not sharing
83                 Boolean isSharing = Boolean.FALSE;
84
85                 // Try it as an exception may be thrown
86                 try {
87                         // Get results
88                         this.shares = query.getResultList();
89
90                         // Debug message
91                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserSharingAddressbooks: shares.size()={0}", this.shares.size())); //NOI18N
92
93                         // Is it not empty?
94                         isSharing = (!this.shares.isEmpty());
95                 } catch (final NoResultException ex) {
96                         // Not sharing anything
97                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserSharingAddressbooks: User {0} is not sharing address books: {1}", user, ex)); //NOI18N
98                 }
99
100                 // Trace message
101                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserSharingAddressbooks: iSharing={0} - EXIT!", isSharing)); //NOI18N
102
103                 // Return it
104                 return isSharing;
105         }
106
107         @Override
108         public ShareableAddressbook startSharing (final User sharee, final Addressbook addressbook) throws UserAlreadySharingAddressbookException {
109                 // Trace message
110                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("startSharing: sharee={0},addressbook={1} - CALLED!", sharee, addressbook)); //NOI18N
111
112                 // Check all conditions
113                 if (null == sharee) {
114                         // Throw NPE
115                         throw new NullPointerException("sharee is null"); //NOI18N
116                 } else if (sharee.getUserId() == null) {
117                         // Throw NPE again
118                         throw new NullPointerException("sharee.userId is null"); //NOI18N
119                 } else if (sharee.getUserId() < 1) {
120                         // Invalid id number
121                         throw new IllegalStateException(MessageFormat.format("sharee.userId={0} is invalid", sharee.getUserId())); //NOI18N
122                 } else if (null == addressbook) {
123                         // Throw NPE again
124                         throw new NullPointerException("addressbook is null"); //NOI18N
125                 } else if (addressbook.getAddressbookId() == null) {
126                         // Throw NPE again
127                         throw new NullPointerException("addressbook.addressbookId is null"); //NOI18N
128                 } else if (addressbook.getAddressbookId() < 1) {
129                         // Invalid id number
130                         throw new IllegalArgumentException(MessageFormat.format("addressbook.addressbookId={0} is invalid.", addressbook.getAddressbookId())); //NOI18N
131                 } else if (Objects.equals(addressbook.getAddressbookUser(), sharee)) {
132                         // Sharing with yourself!
133                         throw new IllegalStateException("User tries to share with himself."); //NOI18N
134                 }
135
136                 // Is the entry already there?
137                 if (this.isUserAlreadySharingAddressbook(addressbook, sharee)) {
138                         // Abort here
139                         throw new UserAlreadySharingAddressbookException(addressbook, sharee);
140                 }
141
142                 // All fine so far, then create the instance
143                 ShareableAddressbook share = new AddressbookShare(addressbook, sharee);
144
145                 // Debug message
146                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("startSharing: share={0}", share));
147
148                 // Persist it
149                 this.getEntityManager().persist(share);
150
151                 // Flush to get id number
152                 this.getEntityManager().flush();
153
154                 // Return updated instance
155                 return share;
156         }
157
158         /**
159          * Checks whether the owner of the given address book is already sharing it
160          * with the sharee.
161          * <p>
162          * @param addressbook Address book to be shared with
163          * @param sharee User sharee instance
164          * @return Wether the address book is already shared with the sharee
165          */
166         private boolean isUserAlreadySharingAddressbook (final Addressbook addressbook, final User sharee) {
167                 // Trace message
168                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserAlreadySharingAddressbook: addressbook={0},sharee={1} - CALLED!", addressbook, sharee)); //NOI18N
169
170                 // Get named query
171                 Query query = this.getEntityManager().createNamedQuery("SearchShareeAddressbookShare", AddressbookShare.class); //NOI18N
172
173                 // Set parameter
174                 query.setParameter("addressbook", addressbook); //NOI18N
175                 query.setParameter("sharee", sharee); //NOI18N
176
177                 // Default is not found
178                 boolean isFound = false;
179
180                 // Try it
181                 try {
182                         // Get single instance
183                         ShareableAddressbook share = (ShareableAddressbook) query.getSingleResult();
184
185                         // Debug message
186                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserAlreadySharingAddressbook: share={0} - FOUND!", share)); //NOI18N
187
188                         // Set found
189                         isFound = true;
190                 } catch (final NoResultException ex) {
191                         // Not found, log exception
192                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserAlreadySharingAddressbook: Notfound. Exception: {0}", ex)); //NOI18N
193                 }
194
195                 // Trace message
196                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserAlreadySharingAddressbook: isFound={0} - EXIT!", isFound)); //NOI18N
197
198                 // Return it
199                 return isFound;
200         }
201 }