]> git.mxchange.org Git - pizzaservice-mailer-ejb.git/blob - src/java/org/mxchange/jusercore/model/user/AddressbookAdminUserSessionBean.java
Please cherry-pick:
[pizzaservice-mailer-ejb.git] / src / java / org / mxchange / jusercore / model / user / AddressbookAdminUserSessionBean.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.jusercore.model.user;
18
19 import java.text.MessageFormat;
20 import java.util.GregorianCalendar;
21 import javax.ejb.EJB;
22 import javax.ejb.Stateless;
23 import org.mxchange.addressbook.database.BaseAddressbookDatabaseBean;
24 import org.mxchange.jcontacts.contact.Contact;
25 import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
26 import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
27 import org.mxchange.jusercore.exceptions.UserNotFoundException;
28 import org.mxchange.jusercore.exceptions.UserStatusConfirmedException;
29 import org.mxchange.jusercore.exceptions.UserStatusLockedException;
30 import org.mxchange.jusercore.exceptions.UserStatusUnconfirmedException;
31 import org.mxchange.jusercore.model.user.register.UserRegistrationSessionBeanRemote;
32 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
33
34 /**
35  * An administrative user EJB
36  * <p>
37  * @author Roland Häder<roland@mxchange.org>
38  */
39 @Stateless (name = "adminUser", description = "A bean handling the user data")
40 public class AddressbookAdminUserSessionBean extends BaseAddressbookDatabaseBean implements AdminUserSessionBeanRemote {
41
42         /**
43          * Serial number
44          */
45         private static final long serialVersionUID = 542_145_347_916L;
46
47         /**
48          * Registration EJB
49          */
50         @EJB
51         private UserRegistrationSessionBeanRemote registerBean;
52
53         /**
54          * Regular user bean
55          */
56         @EJB
57         private UserSessionBeanRemote userBean;
58
59         /**
60          * Default constructor
61          */
62         public AddressbookAdminUserSessionBean () {
63                 // Call super constructor
64                 super("jms/addressbook-queue-factory", "jms/addressbook-email-queue"); //NOI18N
65         }
66
67         @Override
68         public User addUser (final User user) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
69                 // Trace message
70                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addUser: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
71
72                 // user should not be null
73                 if (null == user) {
74                         // Abort here
75                         throw new NullPointerException("user is null"); //NOI18N
76                 } else if (user.getUserId() instanceof Long) {
77                         // Not allowed here
78                         throw new IllegalStateException(MessageFormat.format("user.userId must be null, is: {0}", user.getUserId())); //NOI18N
79                 }
80
81                 // Check if user is registered
82                 if (this.registerBean.isUserNameRegistered(user)) {
83                         // Abort here
84                         throw new UserNameAlreadyRegisteredException(user);
85                 } else if (this.registerBean.isEmailAddressRegistered(user)) {
86                         // Abort here
87                         throw new EmailAddressAlreadyRegisteredException(user);
88                 }
89
90                 // Set created timestamp
91                 user.setUserCreated(new GregorianCalendar());
92                 user.getUserContact().setContactCreated(new GregorianCalendar());
93
94                 // Update cellphone, land-line and fax instance
95                 this.setAllContactPhoneEntriesCreated(user.getUserContact());
96
97                 // Persist it
98                 this.getEntityManager().persist(user);
99
100                 // Flush to get id back
101                 this.getEntityManager().flush();
102
103                 // Trace message
104                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addUser: user={1},user.userId={2} - EXIT!", this.getClass().getSimpleName(), user, user.getUserId())); //NOI18N
105
106                 // Return it
107                 return user;
108         }
109
110         @Override
111         public void deleteUser (final User user, final String userDeleteReason) throws UserNotFoundException {
112                 // Trace message
113                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteUser: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
114
115                 // user should not be null
116                 if (null == user) {
117                         // Abort here
118                         throw new NullPointerException("user is null"); //NOI18N
119                 } else if (user.getUserId() == null) {
120                         // Id is set
121                         throw new NullPointerException("user.userId is null"); //NOI18N
122                 } else if (user.getUserId() < 1) {
123                         // Not valid id number
124                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", user.getUserId())); //NOI18N
125                 } else if (user.getUserContact() == null) {
126                         // Throw NPE again
127                         throw new NullPointerException("user.userContact is null"); //NOI18N
128                 } else if (user.getUserContact().getContactId() == null) {
129                         // Throw NPE again
130                         throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
131                 } else if (user.getUserContact().getContactId() < 1) {
132                         // Not valid id number
133                         throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N
134                 } else if (user.getUserAccountStatus() == null) {
135                         // Throw NPE again
136                         throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
137                 } else if (!this.userBean.ifUserExists(user)) {
138                         // Name already found
139                         throw new UserNotFoundException(user);
140                 }
141
142                 // Get a managed instance
143                 User managedUser = this.getManagedUser(user);
144
145                 // Should be found!
146                 assert (managedUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
147
148                 // Delete it
149                 this.getEntityManager().remove(managedUser);
150
151                 // Trace message
152                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteUser: EXIT!", this.getClass().getSimpleName())); //NOI18N
153         }
154
155         @Override
156         public User linkUser (final User user) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
157                 // Trace message
158                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkUser: user={0} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
159
160                 // user should not be null
161                 if (null == user) {
162                         // Abort here
163                         throw new NullPointerException("user is null"); //NOI18N
164                 } else if (user.getUserId() instanceof Long) {
165                         // Id is set
166                         throw new IllegalArgumentException("user.userId is not null"); //NOI18N
167                 } else if (user.getUserContact() == null) {
168                         // Throw NPE again
169                         throw new NullPointerException("user.userContact is null"); //NOI18N
170                 } else if (user.getUserContact().getContactId() == null) {
171                         // Throw NPE again
172                         throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
173                 } else if (user.getUserContact().getContactId() < 1) {
174                         // Not valid id number
175                         throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N
176                 } else if (user.getUserAccountStatus() == null) {
177                         // Throw NPE again
178                         throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
179                 } else if (this.userBean.ifUserNameExists(user.getUserName())) {
180                         // Name already found
181                         throw new UserNameAlreadyRegisteredException(user.getUserName());
182                 }
183
184                 // Try to find the contact
185                 Contact managedContact = this.getEntityManager().find(user.getUserContact().getClass(), user.getUserContact().getContactId());
186
187                 // Should be found!
188                 assert (managedContact instanceof Contact) : MessageFormat.format("Contact with id {0} not found, but should be.", user.getUserContact().getContactId()); //NOI18N
189
190                 // Set detached object in rexcruiter instance
191                 user.setUserContact(managedContact);
192
193                 // Set timestamp
194                 user.setUserCreated(new GregorianCalendar());
195
196                 // Perist it
197                 this.getEntityManager().persist(user);
198
199                 // Flush it to get updated instance back
200                 this.getEntityManager().flush();
201
202                 // Log trace message
203                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkUser: user={1} - EXIT!", this.getClass().getSimpleName(), user)); //NOI18N
204
205                 // Return updated instanc
206                 return user;
207         }
208
209         @Override
210         public User lockUserAccount (final User user, final String userLockReason, final String baseUrl) throws UserStatusLockedException, UserStatusUnconfirmedException, UserNotFoundException {
211                 // Trace message
212                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount: user={1},userLockReason={2},baseUrl={3} - CALLED!", this.getClass().getSimpleName(), user, userLockReason, baseUrl)); //NOI18N
213
214                 // user should not be null
215                 if (null == user) {
216                         // Abort here
217                         throw new NullPointerException("user is null"); //NOI18N
218                 } else if (user.getUserId() == null) {
219                         // Id is set
220                         throw new NullPointerException("user.userId is null"); //NOI18N
221                 } else if (user.getUserId() < 1) {
222                         // Id is set
223                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is null", user.getUserId())); //NOI18N
224                 } else if (user.getUserContact() == null) {
225                         // Throw NPE again
226                         throw new NullPointerException("user.userContact is null"); //NOI18N
227                 } else if (user.getUserContact().getContactId() == null) {
228                         // Throw NPE again
229                         throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
230                 } else if (user.getUserContact().getContactId() < 1) {
231                         // Not valid id number
232                         throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N
233                 } else if (user.getUserAccountStatus() == null) {
234                         // Throw NPE again
235                         throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
236                 } else if (!this.userBean.ifUserExists(user)) {
237                         // Name already found
238                         throw new UserNotFoundException(user);
239                 } else if (user.getUserAccountStatus() == UserAccountStatus.LOCKED) {
240                         // Account is locked
241                         throw new UserStatusLockedException(user);
242                 } else if (user.getUserAccountStatus() == UserAccountStatus.UNCONFIRMED) {
243                         // Account is unconfirmed
244                         throw new UserStatusUnconfirmedException(user);
245                 } else if (null == userLockReason) {
246                         // Throw NPE again
247                         throw new NullPointerException("userLockReason is null"); //NOI18N
248                 } else if (userLockReason.isEmpty()) {
249                         // Is empty
250                         throw new IllegalArgumentException("userLockReason is empty"); //NOI18N
251                 }
252
253                 // Remove contact instance as this is not updated
254                 user.setUserContact(null);
255
256                 // Set as locked, set timestamp and lock reason
257                 user.setUserAccountStatus(UserAccountStatus.LOCKED);
258                 user.setUserLastLocked(new GregorianCalendar());
259                 user.setUserLastLockedReason(userLockReason);
260
261                 // Update user
262                 User managedUser = this.userBean.updateUserData(user);
263
264                 // @TODO Create user lock history entry
265
266                 // Send out email
267                 // @TODO externalize subject line
268                 this.sendEmail("User account locked", "user_account_locked", managedUser, baseUrl, null); //NOI18N
269
270                 // Trace message
271                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount: managedUser={1} - EXIT!", this.getClass().getSimpleName(), managedUser)); //NOI18N
272
273                 // Return detached (and updated) user
274                 return managedUser;
275         }
276
277         @Override
278         public User unlockUserAccount (final User user, final String baseUrl) throws UserStatusConfirmedException, UserStatusUnconfirmedException, UserNotFoundException {
279                 // Trace message
280                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount: user={1},baseUrl={2} - CALLED!", this.getClass().getSimpleName(), user, baseUrl)); //NOI18N
281
282                 // user should not be null
283                 if (null == user) {
284                         // Abort here
285                         throw new NullPointerException("user is null"); //NOI18N
286                 } else if (user.getUserId() == null) {
287                         // Id is set
288                         throw new NullPointerException("user.userId is null"); //NOI18N
289                 } else if (user.getUserId() < 1) {
290                         // Id is set
291                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is null", user.getUserId())); //NOI18N
292                 } else if (user.getUserContact() == null) {
293                         // Throw NPE again
294                         throw new NullPointerException("user.userContact is null"); //NOI18N
295                 } else if (user.getUserContact().getContactId() == null) {
296                         // Throw NPE again
297                         throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
298                 } else if (user.getUserContact().getContactId() < 1) {
299                         // Not valid id number
300                         throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N
301                 } else if (user.getUserAccountStatus() == null) {
302                         // Throw NPE again
303                         throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
304                 } else if (!this.userBean.ifUserExists(user)) {
305                         // Name already found
306                         throw new UserNotFoundException(user);
307                 } else if (user.getUserAccountStatus() == UserAccountStatus.CONFIRMED) {
308                         // Account is confirmed
309                         throw new UserStatusConfirmedException(user);
310                 } else if (user.getUserAccountStatus() == UserAccountStatus.UNCONFIRMED) {
311                         // Account is unconfirmed
312                         throw new UserStatusUnconfirmedException(user);
313                 }
314
315                 // Remove contact instance as this is not updated
316                 user.setUserContact(null);
317
318                 // Unlock account
319                 user.setUserAccountStatus(UserAccountStatus.CONFIRMED);
320
321                 // Update user
322                 User managedUser = this.userBean.updateUserData(user);
323
324                 // @TODO Create user lock history entry
325
326                 // Send out email
327                 // @TODO externalize subject line
328                 this.sendEmail("User account unlocked", "user_account_unlocked", managedUser, baseUrl, null); //NOI18N
329
330                 // Trace message
331                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount: managedUser={1} - EXIT!", this.getClass().getSimpleName(), managedUser)); //NOI18N
332
333                 // Return changed account
334                 return managedUser;
335         }
336
337 }