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