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