]> git.mxchange.org Git - pizzaservice-mailer-ejb.git/blob - src/java/org/mxchange/addressbook/model/addressbook/AddressbookSessionBean.java
Implemented business method countAllUserSharedAddressbooks()
[pizzaservice-mailer-ejb.git] / src / java / org / mxchange / addressbook / model / addressbook / AddressbookSessionBean.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.addressbook;
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.exceptions.AddressbookNameAlreadyUsedException;
25 import org.mxchange.addressbook.exceptions.AddressbookNotFoundException;
26 import org.mxchange.addressbook.model.addressbook.entry.AddressbookEntry;
27 import org.mxchange.addressbook.model.addressbook.shared.ShareableAddressbook;
28 import org.mxchange.jcoreee.database.BaseDatabaseBean;
29 import org.mxchange.jusercore.model.user.User;
30
31 /**
32  * A stateless bean handling addressbooks
33  * <p>
34  * @author Roland Haeder
35  */
36 @Stateless (name = "addressbook", mappedName = "ejb/stateless-addressbook", description = "A stateless bean for handling addressbooks")
37 public class AddressbookSessionBean extends BaseDatabaseBean implements AddressbookSessionBeanRemote {
38
39         /**
40          * Serial number
41          */
42         private static final long serialVersionUID = 129_857_871_287_691L;
43
44         @Override
45         @SuppressWarnings ("unchecked")
46         public List<AddressbookEntry> allEntries (final Addressbook addressbook) {
47                 // Generate query
48                 Query query = this.getEntityManager().createNamedQuery("AllAddressbookEntries"); //NOI18N
49
50                 // Set parameters
51                 query.setParameter("addressbook", addressbook); //NOI18N
52                 query.setParameter("owner", addressbook.getAddressbookUser()); //NOI18N
53                 query.setParameter("sharer", addressbook.getAddressbookUser()); //NOI18N
54
55                 // Return it
56                 return query.getResultList();
57         }
58
59         @Override
60         @SuppressWarnings ("unchecked")
61         public Integer countAllUserSharedAddressbooks (final User user) {
62                 // Trace message
63                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("countAllUserSharedAddressbooks: user={0} - CALLED!", user)); //NOI18N
64
65                 // user should be valid
66                 if (null == user) {
67                         // Throw NPE
68                         throw new NullPointerException("user is null"); //NOI18N
69                 } else if (user.getUserId() == null) {
70                         // Throw NPE again
71                         throw new NullPointerException("user.userId is null"); //NOI18N
72                 } else if (user.getUserId() < 1) {
73                         // Invalid id
74                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
75                 }
76
77                 // Get named query
78                 Query query = this.getEntityManager().createNamedQuery("SearchUserSharedAddressbooks"); //NOI18N
79
80                 // Set parameter
81                 query.setParameter("user", user); //NOI18N
82
83                 // Default is zero
84                 Integer count = 0;
85
86                 // Try it
87                 try {
88                         // Get whole list
89                         List<ShareableAddressbook> dummy = query.getResultList();
90
91                         // Set size
92                         count = dummy.size();
93                 } catch (final NoResultException ex) {
94                         // Need to catch this, so log it
95                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("countAllUserSharedAddressbooks: getResultList() failed: {0}", ex)); //NOI18N
96                 }
97
98                 // Trace message
99                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("countAllUserSharedAddressbooks: count={0} - EXIT!", count)); //NOI18N
100
101                 // Return count
102                 return count;
103         }
104
105         @Override
106         public Addressbook createAddressbook (final Addressbook addressbook) throws AddressbookNameAlreadyUsedException {
107                 // Is it not null?
108                 if (null == addressbook) {
109                         // Abort here
110                         throw new NullPointerException("addressbook is null"); //NOI18N
111                 } else if (addressbook.getAddressbookUser() == null) {
112                         // User instance is null
113                         throw new NullPointerException("addressbook.user should not be null."); //NOI18N
114                 } else if (addressbook.getAddressbookName() == null) {
115                         // Address book name not set
116                         throw new NullPointerException("addressbook.addressbookName should not be null"); //NOI18N
117                 } else if (addressbook.getAddressbookName().isEmpty()) {
118                         // Address book name not set
119                         throw new IllegalArgumentException("addressbook.addressbookName should not be empty"); //NOI18N
120                 } else if (this.isAddressbookNameUsed(addressbook)) {
121                         // The assigned user already used that name
122                         throw new AddressbookNameAlreadyUsedException(addressbook);
123                 }
124
125                 // Persist it now
126                 this.getEntityManager().persist(addressbook);
127
128                 // Flush it to get all data
129                 this.getEntityManager().flush();
130
131                 // Return it updated
132                 return addressbook;
133         }
134
135         @Override
136         public Addressbook getAddressbookById (final Long addressbookId) throws AddressbookNotFoundException {
137                 // Trace message
138                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getAddressbookById: addressbookId={0} - CALLED!", addressbookId)); //NOI18N
139
140                 // addressbookId should not be null or below 1
141                 if (null == addressbookId) {
142                         // Throw NPE
143                         throw new NullPointerException("addressbookId is null"); //NOI18N
144                 } else if (addressbookId < 1) {
145                         // Not valid
146                         throw new IllegalArgumentException(MessageFormat.format("addressbookId is not valid: {0}", addressbookId)); //NOI18N
147                 } else if (!this.isAddressbookIdUsed(addressbookId)) {
148                         // No address book found
149                         throw new AddressbookNotFoundException(addressbookId);
150                 }
151
152                 // Get named query instance
153                 Query query = this.getEntityManager().createNamedQuery("SearchAddressbookById"); //NOI18N
154
155                 // Set parameter
156                 query.setParameter("id", addressbookId); //NOI18N
157
158                 // Return it
159                 return (Addressbook) query.getSingleResult();
160         }
161
162         @Override
163         @SuppressWarnings ("unchecked")
164         public List<Addressbook> getUsersList (final User loggedInUser) {
165                 // Trace message
166                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getUsersList: loggedInUser={0} - CALLED!", loggedInUser)); //NOI18N
167
168                 // Is the user instance null?
169                 if (null == loggedInUser) {
170                         // Abort here
171                         throw new NullPointerException("loggedInUser is null"); //NOI18N
172                 }
173
174                 // Get query instance
175                 Query query = this.getEntityManager().createNamedQuery("AllUsersAddressbooks", List.class); //NOI18N
176
177                 // Set parameter
178                 query.setParameter("param", loggedInUser); //NOI18N
179
180                 // Get full list from JPA
181                 List<Addressbook> addressbooks = query.getResultList();
182
183                 // Return it
184                 return addressbooks;
185         }
186
187         @Override
188         public boolean isAddressbookIdUsed (final Long addressbookId) {
189                 // Trace message
190                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isAddressbookIdUsed: addressbookId={0} - CALLED!", addressbookId)); //NOI18N
191
192                 // Is it null or zero?
193                 if (null == addressbookId) {
194                         // Throw NPE
195                         throw new NullPointerException("addressbookId is null"); //NOI18N
196                 } else if (addressbookId < 1) {
197                         // Not valid id number
198                         throw new IllegalArgumentException(MessageFormat.format("addressbookId is not valid: {0}", addressbookId)); //NOI18N
199                 }
200
201                 // Get query instance
202                 Query query = this.getEntityManager().createNamedQuery("SearchAddressbookById"); //NOI18N
203
204                 // Set parameter
205                 query.setParameter("id", addressbookId); //NOI18N
206
207                 // Default is not valid
208                 boolean isValid = false;
209
210                 // Try it again, yes no other way
211                 try {
212                         // Get single result
213                         Addressbook addressbook = (Addressbook) query.getSingleResult();
214
215                         // Debug message
216                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookIdUsed: addressbook={0} - FOUND!", addressbook)); //NOI18N
217
218                         // Found one!
219                         isValid = true;
220                 } catch (final NoResultException ex) {
221                         // Debug log only, maybe out-dated link followed
222                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookIdUsed: addressbookId={0} is not valid: {1}", addressbookId, ex)); //NOI18N
223                 }
224
225                 // Trace message
226                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isAddressbookIdUsed: isValid={0} - EXIT!", isValid)); //NOI18N
227
228                 // Return result
229                 return isValid;
230         }
231
232         @Override
233         public boolean isAddressbookNameUsed (final Addressbook addressbook) {
234                 // Is it not null?
235                 if (null == addressbook) {
236                         // Abort here
237                         throw new NullPointerException("addressbook is null"); //NOI18N
238                 } else if (addressbook.getAddressbookUser() == null) {
239                         // User instance is null
240                         throw new NullPointerException("addressbook.user should not be null."); //NOI18N
241                 } else if (addressbook.getAddressbookName() == null) {
242                         // Address book name not set
243                         throw new NullPointerException("addressbook.addressbookName should not be null"); //NOI18N
244                 } else if (addressbook.getAddressbookName().isEmpty()) {
245                         // Address book name not set
246                         throw new IllegalArgumentException("addressbook.addressbookName should not be empty"); //NOI18N
247                 }
248
249                 // Get query instance
250                 Query query = this.getEntityManager().createNamedQuery("SearchUserAddressbookName", Addressbook.class); //NOI18N
251
252                 // Set parameter
253                 query.setParameter("user", addressbook.getAddressbookUser()); //NOI18N
254                 query.setParameter("name", addressbook.getAddressbookName()); //NOI18N
255
256                 // Default is not found
257                 boolean isUsed = false;
258
259                 // Try it
260                 try {
261                         // Get a single result
262                         Addressbook dummy = (Addressbook) query.getSingleResult();
263
264                         // Log it
265                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookNameUsed: Found an address book: {0}", dummy)); //NOI18N
266
267                         // Found one
268                         isUsed = true;
269                 } catch (final NoResultException ex) {
270                         // No result found, so log it away
271                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookNameUsed: getSingleResult() did not return a result: {0}", ex)); //NOI18N
272                 }
273
274                 // Return result
275                 return isUsed;
276         }
277 }