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