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