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