]> git.mxchange.org Git - addressbook-mailer-ejb.git/blob - src/java/org/mxchange/addressbook/model/shared/SharedAddressbooksSessionBean.java
Don't cherry-pick:
[addressbook-mailer-ejb.git] / src / java / org / mxchange / addressbook / model / shared / SharedAddressbooksSessionBean.java
1 /*
2  * Copyright (C) 2016 Roland Häder
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Affero General Public License as
6  * published by the Free Software Foundation, either version 3 of the
7  * License, or (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 Affero General Public License for more details.
13  *
14  * You should have received a copy of the GNU Affero 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.jaddressbookcore.exceptions.UserAlreadySharingAddressbookException;
26 import org.mxchange.jaddressbookcore.model.addressbook.Addressbook;
27 import org.mxchange.jaddressbookcore.model.addressbook.shared.AddressbookShare;
28 import org.mxchange.jaddressbookcore.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 Häder<roland@mxchange.org>
36  */
37 @Stateless (name = "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         @Override
46         @SuppressWarnings ("unchecked")
47         public List<ShareableAddressbook> allSharedAddressbooks (final User user) {
48                 // Trace message
49                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allSharedAddressbooks: user={0} - CALLED!", user)); //NOI18N
50
51                 // Is user null?
52                 if (null == user) {
53                         // Throw NPE
54                         throw new NullPointerException("user is null"); //NOI18N
55                 } else if (user.getUserId() == null) {
56                         // Null userId is not allowed
57                         throw new NullPointerException("user.userId is null"); //NOI18N
58                 } else if (user.getUserId() < 1) {
59                         // Not allowed value
60                         throw new IllegalArgumentException(MessageFormat.format("user.UserId={0} is an invalid value", user.getUserId())); //NOI18N
61                 }
62
63                 // Get named query
64                 Query query = this.getEntityManager().createNamedQuery("SearchUserSharedAddressbooks", List.class); //NOI18N
65
66                 // Set parameter
67                 query.setParameter("user", user); //NOI18N
68
69                 // Return full list
70                 List<ShareableAddressbook> list = query.getResultList();
71
72                 // Trace message
73                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allSharedAddressbooks: list.size()={0} - EXIT!", list.size()));
74
75                 // Return list
76                 return list;
77         }
78
79         @Override
80         public Boolean isUserSharingAddressbooks (final User user) {
81                 // Trace message
82                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserSharingAddressbooks: user={0} - CALLED!", user)); //NOI18N
83
84                 // Is user null?
85                 if (null == user) {
86                         // Throw NPE
87                         throw new NullPointerException("user is null"); //NOI18N
88                 } else if (user.getUserId() == null) {
89                         // Null userId is not allowed
90                         throw new NullPointerException("user.userId is null"); //NOI18N
91                 } else if (user.getUserId() < 1) {
92                         // Not allowed value
93                         throw new IllegalArgumentException(MessageFormat.format("user.UserId={0} is an invalid value", user.getUserId())); //NOI18N
94                 }
95
96                 // Get results
97                 List<ShareableAddressbook> list = this.allSharedAddressbooks(user);
98
99                 // Debug message
100                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserSharingAddressbooks: list.size()={0}", list.size())); //NOI18N
101
102                 // Is it not empty?
103                 Boolean isSharing = (!list.isEmpty());
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
112         @Override
113         public ShareableAddressbook startSharing (final User sharee, final Addressbook addressbook) throws UserAlreadySharingAddressbookException {
114                 // Trace message
115                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("startSharing: sharee={0},addressbook={1} - CALLED!", sharee, addressbook)); //NOI18N
116
117                 // Check all conditions
118                 if (null == sharee) {
119                         // Throw NPE
120                         throw new NullPointerException("sharee is null"); //NOI18N
121                 } else if (sharee.getUserId() == null) {
122                         // Throw NPE again
123                         throw new NullPointerException("sharee.userId is null"); //NOI18N
124                 } else if (sharee.getUserId() < 1) {
125                         // Invalid id number
126                         throw new IllegalStateException(MessageFormat.format("sharee.userId={0} is invalid", sharee.getUserId())); //NOI18N
127                 } else if (null == addressbook) {
128                         // Throw NPE again
129                         throw new NullPointerException("addressbook is null"); //NOI18N
130                 } else if (addressbook.getAddressbookId() == null) {
131                         // Throw NPE again
132                         throw new NullPointerException("addressbook.addressbookId is null"); //NOI18N
133                 } else if (addressbook.getAddressbookId() < 1) {
134                         // Invalid id number
135                         throw new IllegalArgumentException(MessageFormat.format("addressbook.addressbookId={0} is invalid.", addressbook.getAddressbookId())); //NOI18N
136                 } else if (Objects.equals(addressbook.getAddressbookUser(), sharee)) {
137                         // Sharing with yourself!
138                         throw new IllegalStateException("User tries to share with himself."); //NOI18N
139                 }
140
141                 // Is the entry already there?
142                 if (this.isUserAlreadySharingAddressbook(addressbook, sharee)) {
143                         // Abort here
144                         throw new UserAlreadySharingAddressbookException(addressbook, sharee);
145                 }
146
147                 // All fine so far, then create the instance
148                 ShareableAddressbook share = new AddressbookShare(addressbook, sharee);
149
150                 // Debug message
151                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("startSharing: share={0}", share)); //NOI18N
152
153                 // Persist it
154                 this.getEntityManager().persist(share);
155
156                 // Flush to get id number
157                 this.getEntityManager().flush();
158
159                 // Return updated instance
160                 return share;
161         }
162
163         /**
164          * Checks whether the owner of the given address book is already sharing it
165          * with the sharee.
166          * <p>
167          * @param addressbook Address book to be shared with
168          * @param sharee User sharee instance
169          * <p>
170          * @return Wether the address book is already shared with the sharee
171          */
172         private boolean isUserAlreadySharingAddressbook (final Addressbook addressbook, final User sharee) {
173                 // Trace message
174                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserAlreadySharingAddressbook: addressbook={0},sharee={1} - CALLED!", addressbook, sharee)); //NOI18N
175
176                 // Get named query
177                 Query query = this.getEntityManager().createNamedQuery("SearchShareeAddressbookShare", AddressbookShare.class); //NOI18N
178
179                 // Set parameter
180                 query.setParameter("addressbook", addressbook); //NOI18N
181                 query.setParameter("sharee", sharee); //NOI18N
182
183                 // Default is not found
184                 boolean isFound = false;
185
186                 // Try it
187                 try {
188                         // Get single instance
189                         ShareableAddressbook share = (ShareableAddressbook) query.getSingleResult();
190
191                         // Debug message
192                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserAlreadySharingAddressbook: share={0} - FOUND!", share)); //NOI18N
193
194                         // Set found
195                         isFound = true;
196                 } catch (final NoResultException ex) {
197                         // Not found, log exception
198                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserAlreadySharingAddressbook: Notfound. Exception: {0}", ex)); //NOI18N
199                 }
200
201                 // Trace message
202                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserAlreadySharingAddressbook: isFound={0} - EXIT!", isFound)); //NOI18N
203
204                 // Return it
205                 return isFound;
206         }
207 }