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