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