]> git.mxchange.org Git - jfinancials-ejb.git/blob - src/java/org/mxchange/addressbook/model/addressbook/AddressbookSessionBean.java
02f8f3f11c2a6409f3d1d959c533f64ba9b2de1b
[jfinancials-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.jcoreee.database.BaseDatabaseBean;
28 import org.mxchange.jusercore.model.user.User;
29
30 /**
31  * A stateless bean handling addressbooks
32  * <p>
33  * @author Roland Haeder
34  */
35 @Stateless (name = "addressbook", mappedName = "ejb/stateless-addressbook", description = "A stateless bean for handling addressbooks")
36 public class AddressbookSessionBean 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                 // Generate query
47                 Query query = this.getEntityManager().createNamedQuery("AllAddressbookEntries"); //NOI18N
48
49                 // Set parameters
50                 query.setParameter("addressbook", addressbook); //NOI18N
51                 query.setParameter("owner", addressbook.getAddressbookUser()); //NOI18N
52                 query.setParameter("sharer", addressbook.getAddressbookUser()); //NOI18N
53
54                 // Return it
55                 return query.getResultList();
56         }
57
58         @Override
59         public Addressbook createAddressbook (final Addressbook addressbook) throws AddressbookNameAlreadyUsedException {
60                 // Is it not null?
61                 if (null == addressbook) {
62                         // Abort here
63                         throw new NullPointerException("addressbook is null"); //NOI18N
64                 } else if (addressbook.getAddressbookUser() == null) {
65                         // User instance is null
66                         throw new NullPointerException("addressbook.user should not be null."); //NOI18N
67                 } else if (addressbook.getAddressbookName() == null) {
68                         // Address book name not set
69                         throw new NullPointerException("addressbook.addressbookName should not be null"); //NOI18N
70                 } else if (addressbook.getAddressbookName().isEmpty()) {
71                         // Address book name not set
72                         throw new IllegalArgumentException("addressbook.addressbookName should not be empty"); //NOI18N
73                 } else if (this.isAddressbookNameUsed(addressbook)) {
74                         // The assigned user already used that name
75                         throw new AddressbookNameAlreadyUsedException(addressbook);
76                 }
77
78                 // Persist it now
79                 this.getEntityManager().persist(addressbook);
80
81                 // Flush it to get all data
82                 this.getEntityManager().flush();
83
84                 // Return it updated
85                 return addressbook;
86         }
87
88         @Override
89         public Addressbook getAddressbookById (final Long addressbookId) throws AddressbookNotFoundException {
90                 // Trace message
91                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getAddressbookById: addressbookId={0} - CALLED!", addressbookId)); //NOI18N
92
93                 // addressbookId should not be null or below 1
94                 if (null == addressbookId) {
95                         // Throw NPE
96                         throw new NullPointerException("addressbookId is null"); //NOI18N
97                 } else if (addressbookId < 1) {
98                         // Not valid
99                         throw new IllegalArgumentException(MessageFormat.format("addressbookId is not valid: {0}", addressbookId)); //NOI18N
100                 } else if (!this.isAddressbookIdUsed(addressbookId)) {
101                         // No address book found
102                         throw new AddressbookNotFoundException(addressbookId);
103                 }
104
105                 // Get named query instance
106                 Query query = this.getEntityManager().createNamedQuery("SearchAddressbookById"); //NOI18N
107
108                 // Set parameter
109                 query.setParameter("id", addressbookId); //NOI18N
110
111                 // Return it
112                 return (Addressbook) query.getSingleResult();
113         }
114
115         @Override
116         @SuppressWarnings ("unchecked")
117         public List<Addressbook> getUsersList (final User loggedInUser) {
118                 // Trace message
119                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getUsersList: loggedInUser={0} - CALLED!", loggedInUser)); //NOI18N
120
121                 // Is the user instance null?
122                 if (null == loggedInUser) {
123                         // Abort here
124                         throw new NullPointerException("loggedInUser is null"); //NOI18N
125                 }
126
127                 // Get query instance
128                 Query query = this.getEntityManager().createNamedQuery("AllUsersAddressbooks", List.class); //NOI18N
129
130                 // Set parameter
131                 query.setParameter("param", loggedInUser); //NOI18N
132
133                 // Get full list from JPA
134                 List<Addressbook> addressbooks = query.getResultList();
135
136                 // Return it
137                 return addressbooks;
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                 Query query = this.getEntityManager().createNamedQuery("SearchAddressbookById"); //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                         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                 // Is it not null?
188                 if (null == addressbook) {
189                         // Abort here
190                         throw new NullPointerException("addressbook is null"); //NOI18N
191                 } else if (addressbook.getAddressbookUser() == null) {
192                         // User instance is null
193                         throw new NullPointerException("addressbook.user should not be null."); //NOI18N
194                 } else if (addressbook.getAddressbookName() == null) {
195                         // Address book name not set
196                         throw new NullPointerException("addressbook.addressbookName should not be null"); //NOI18N
197                 } else if (addressbook.getAddressbookName().isEmpty()) {
198                         // Address book name not set
199                         throw new IllegalArgumentException("addressbook.addressbookName should not be empty"); //NOI18N
200                 }
201
202                 // Get query instance
203                 Query query = this.getEntityManager().createNamedQuery("SearchUserAddressbookName", Addressbook.class); //NOI18N
204
205                 // Set parameter
206                 query.setParameter("user", addressbook.getAddressbookUser()); //NOI18N
207                 query.setParameter("name", addressbook.getAddressbookName()); //NOI18N
208
209                 // Default is not found
210                 boolean isUsed = false;
211
212                 // Try it
213                 try {
214                         // Get a single result
215                         Addressbook dummy = (Addressbook) query.getSingleResult();
216
217                         // Log it
218                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookNameUsed: Found an address book: {0}", dummy)); //NOI18N
219
220                         // Found one
221                         isUsed = true;
222                 } catch (final NoResultException ex) {
223                         // No result found, so log it away
224                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookNameUsed: getSingleResult() did not return a result: {0}", ex)); //NOI18N
225                 }
226
227                 // Return result
228                 return isUsed;
229         }
230 }