]> git.mxchange.org Git - jjobs-ejb.git/blob - src/java/org/mxchange/jusercore/model/user/JobsAdminUserSessionBean.java
7d102f3b49795f12906a5b8b7bb58bf4b26aea7c
[jjobs-ejb.git] / src / java / org / mxchange / jusercore / model / user / JobsAdminUserSessionBean.java
1 /*
2  * Copyright (C) 2016 - 2020 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_347_916L;
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                 }
73
74                 // Check if user is registered
75                 if (this.userBean.isUserNameRegistered(user)) {
76                         // Abort here
77                         throw new UserNameAlreadyRegisteredException(user);
78                 } else if (this.userBean.isEmailAddressRegistered(user)) {
79                         // Abort here
80                         throw new EmailAddressAlreadyRegisteredException(user);
81                 }
82
83                 // Set created timestamp
84                 user.setUserCreated(new Date());
85                 user.getUserContact().setContactCreated(new Date());
86
87                 // Update mobile, land-line and fax instance
88                 this.setAllPhoneEntriesCreated(user.getUserContact());
89
90                 // Persist it
91                 this.getEntityManager().persist(user);
92
93                 // Trace message
94                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addUser: user={1},user.userId={2} - EXIT!", this.getClass().getSimpleName(), user, user.getUserId())); //NOI18N
95
96                 // Return it
97                 return user;
98         }
99
100         @Override
101         public void deleteUser (final User user, final String userDeleteReason) throws UserNotFoundException {
102                 // Trace message
103                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteUser: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
104
105                 // Validate parameters
106                 if (null == user) {
107                         // Abort here
108                         throw new NullPointerException("user is null"); //NOI18N
109                 } else if (user.getUserId() == null) {
110                         // Id is set
111                         throw new NullPointerException("user.userId is null"); //NOI18N
112                 } else if (user.getUserId() < 1) {
113                         // Not valid id number
114                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", user.getUserId())); //NOI18N
115                 } else if (user.getUserContact() == null) {
116                         // Throw NPE again
117                         throw new NullPointerException("user.userContact is null"); //NOI18N
118                 } else if (user.getUserContact().getContactId() == null) {
119                         // Throw NPE again
120                         throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
121                 } else if (user.getUserContact().getContactId() < 1) {
122                         // Not valid id number
123                         throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N
124                 } else if (user.getUserAccountStatus() == null) {
125                         // Throw NPE again
126                         throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
127                 } else if (!this.userBean.ifUserExists(user)) {
128                         // Name already found
129                         throw new UserNotFoundException(user);
130                 }
131
132                 // Get a managed instance
133                 final User managedUser = this.createManaged(user);
134
135                 // Should be found!
136                 assert (managedUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
137
138                 // Delete it
139                 this.getEntityManager().remove(managedUser);
140
141                 // Trace message
142                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteUser: EXIT!", this.getClass().getSimpleName())); //NOI18N
143         }
144
145         @Override
146         public User linkUser (final User user) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
147                 // Trace message
148                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkUser: user={0} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
149
150                 // user should not be null
151                 if (null == user) {
152                         // Abort here
153                         throw new NullPointerException("user is null"); //NOI18N
154                 } else if (user.getUserId() instanceof Long) {
155                         // Id is set
156                         throw new IllegalArgumentException("user.userId is not null"); //NOI18N
157                 } else if (user.getUserContact() == null) {
158                         // Throw NPE again
159                         throw new NullPointerException("user.userContact is null"); //NOI18N
160                 } else if (user.getUserContact().getContactId() == null) {
161                         // Throw NPE again
162                         throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
163                 } else if (user.getUserContact().getContactId() < 1) {
164                         // Not valid id number
165                         throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N
166                 } else if (user.getUserAccountStatus() == null) {
167                         // Throw NPE again
168                         throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
169                 } else if (this.userBean.ifUserNameExists(user.getUserName())) {
170                         // Name already found
171                         throw new UserNameAlreadyRegisteredException(user.getUserName());
172                 }
173
174                 // Try to find the contact
175                 final Contact foundContact = this.getEntityManager().find(user.getUserContact().getClass(), user.getUserContact().getContactId());
176
177                 // Set detached object in rexcruiter instance
178                 user.setUserContact(foundContact);
179
180                 // Set timestamp
181                 user.setUserCreated(new Date());
182
183                 // Perist it
184                 this.getEntityManager().persist(user);
185
186                 // Log trace message
187                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkUser: user={1} - EXIT!", this.getClass().getSimpleName(), user)); //NOI18N
188
189                 // Return updated instanc
190                 return user;
191         }
192
193         @Override
194         public User lockUserAccount (final User user, final String userLockReason, final String baseUrl) throws UserStatusLockedException, UserStatusUnconfirmedException, UserNotFoundException {
195                 // Trace message
196                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount: user={1},userLockReason={2},baseUrl={3} - CALLED!", this.getClass().getSimpleName(), user, userLockReason, baseUrl)); //NOI18N
197
198                 // user should not be null
199                 if (null == user) {
200                         // Abort here
201                         throw new NullPointerException("user is null"); //NOI18N
202                 } else if (user.getUserId() == null) {
203                         // Id is set
204                         throw new NullPointerException("user.userId is null"); //NOI18N
205                 } else if (user.getUserId() < 1) {
206                         // Id is set
207                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is null", user.getUserId())); //NOI18N
208                 } else if (user.getUserContact() == null) {
209                         // Throw NPE again
210                         throw new NullPointerException("user.userContact is null"); //NOI18N
211                 } else if (user.getUserContact().getContactId() == null) {
212                         // Throw NPE again
213                         throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
214                 } else if (user.getUserContact().getContactId() < 1) {
215                         // Not valid id number
216                         throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N
217                 } else if (user.getUserAccountStatus() == null) {
218                         // Throw NPE again
219                         throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
220                 } else if (!this.userBean.ifUserExists(user)) {
221                         // Name already found
222                         throw new UserNotFoundException(user);
223                 } else if (user.getUserAccountStatus() == UserAccountStatus.LOCKED) {
224                         // Account is locked
225                         throw new UserStatusLockedException(user);
226                 } else if (user.getUserAccountStatus() == UserAccountStatus.UNCONFIRMED) {
227                         // Account is unconfirmed
228                         throw new UserStatusUnconfirmedException(user);
229                 } else if (null == userLockReason) {
230                         // Throw NPE again
231                         throw new NullPointerException("userLockReason is null"); //NOI18N
232                 } else if (userLockReason.isEmpty()) {
233                         // Is empty
234                         throw new IllegalArgumentException("userLockReason is empty"); //NOI18N
235                 }
236
237                 // Remove contact instance as this is not updated
238                 user.setUserContact(null);
239
240                 // Set as locked, set timestamp and lock reason
241                 user.setUserAccountStatus(UserAccountStatus.LOCKED);
242                 user.setUserLastLocked(new Date());
243                 user.setUserLastLockedReason(userLockReason);
244
245                 // Update user
246                 final User managedUser = this.userBean.updateUserData(user);
247
248                 // @TODO Create user lock history entry
249                 // Send out email
250                 // @TODO externalize subject line
251                 this.sendEmail("User account locked", "user_account_locked", managedUser, baseUrl, null); //NOI18N
252
253                 // Trace message
254                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount: managedUser={1} - EXIT!", this.getClass().getSimpleName(), managedUser)); //NOI18N
255
256                 // Return detached (and updated) user
257                 return managedUser;
258         }
259
260         @Override
261         public User unlockUserAccount (final User user, final String baseUrl) throws UserStatusConfirmedException, UserStatusUnconfirmedException, UserNotFoundException {
262                 // Trace message
263                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount: user={1},baseUrl={2} - CALLED!", this.getClass().getSimpleName(), user, baseUrl)); //NOI18N
264
265                 // user should not be null
266                 if (null == user) {
267                         // Abort here
268                         throw new NullPointerException("user is null"); //NOI18N
269                 } else if (user.getUserId() == null) {
270                         // Id is set
271                         throw new NullPointerException("user.userId is null"); //NOI18N
272                 } else if (user.getUserId() < 1) {
273                         // Id is set
274                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is null", user.getUserId())); //NOI18N
275                 } else if (user.getUserContact() == null) {
276                         // Throw NPE again
277                         throw new NullPointerException("user.userContact is null"); //NOI18N
278                 } else if (user.getUserContact().getContactId() == null) {
279                         // Throw NPE again
280                         throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
281                 } else if (user.getUserContact().getContactId() < 1) {
282                         // Not valid id number
283                         throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N
284                 } else if (user.getUserAccountStatus() == null) {
285                         // Throw NPE again
286                         throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
287                 } else if (!this.userBean.ifUserExists(user)) {
288                         // Name already found
289                         throw new UserNotFoundException(user);
290                 } else if (user.getUserAccountStatus() == UserAccountStatus.CONFIRMED) {
291                         // Account is confirmed
292                         throw new UserStatusConfirmedException(user);
293                 } else if (user.getUserAccountStatus() == UserAccountStatus.UNCONFIRMED) {
294                         // Account is unconfirmed
295                         throw new UserStatusUnconfirmedException(user);
296                 }
297
298                 // Remove contact instance as this is not updated
299                 user.setUserContact(null);
300
301                 // Unlock account
302                 user.setUserAccountStatus(UserAccountStatus.CONFIRMED);
303
304                 // Update user
305                 final User managedUser = this.userBean.updateUserData(user);
306
307                 // @TODO Create user lock history entry
308                 // Send out email
309                 // @TODO externalize subject line
310                 this.sendEmail("User account unlocked", "user_account_unlocked", managedUser, baseUrl, null); //NOI18N
311
312                 // Trace message
313                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount: managedUser={1} - EXIT!", this.getClass().getSimpleName(), managedUser)); //NOI18N
314
315                 // Return changed account
316                 return managedUser;
317         }
318
319 }