]> git.mxchange.org Git - addressbook-mailer-ejb.git/blob - src/java/org/mxchange/jusercore/model/user/AddressbookAdminUserSessionBean.java
Continued with unlocking users: (please cherry-pick)
[addressbook-mailer-ejb.git] / src / java / org / mxchange / jusercore / model / user / AddressbookAdminUserSessionBean.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.addressbook.database.BaseAddressbookDatabaseBean;
28 import org.mxchange.jcontacts.contact.Contact;
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<rhaeder@cho-time.de>
42  */
43 @Stateless (name = "adminUser", description = "A bean handling the user data")
44 public class AddressbookAdminUserSessionBean extends BaseAddressbookDatabaseBean 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 AddressbookAdminUserSessionBean () {
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() != null) {
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 foundContact = this.getEntityManager().find(user.getUserContact().getClass(), user.getUserContact().getContactId());
143
144                 // Set detached object in rexcruiter instance
145                 user.setUserContact(foundContact);
146
147                 // Set timestamp
148                 user.setUserCreated(new GregorianCalendar());
149
150                 // Perist it
151                 this.getEntityManager().persist(user);
152
153                 // Flush it to get updated instance back
154                 this.getEntityManager().flush();
155
156                 // Log trace message
157                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkUser: user={1} - EXIT!", this.getClass().getSimpleName(), user)); //NOI18N
158
159                 // Return updated instanc
160                 return user;
161         }
162
163         @Override
164         public void lockUserAccount (final User user, final String userLockReason, final String baseUrl) throws UserStatusLockedException, UserStatusUnconfirmedException, UserNotFoundException {
165                 // Trace message
166                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount: user={1},userLockReason={2},baseUrl={3} - CALLED!", this.getClass().getSimpleName(), user, userLockReason, baseUrl)); //NOI18N
167
168                 // user should not be null
169                 if (null == user) {
170                         // Abort here
171                         throw new NullPointerException("user is null"); //NOI18N
172                 } else if (user.getUserId() instanceof Long) {
173                         // Id is set
174                         throw new IllegalArgumentException("user.userId is not null"); //NOI18N
175                 } else if (user.getUserContact() == null) {
176                         // Throw NPE again
177                         throw new NullPointerException("user.userContact is null"); //NOI18N
178                 } else if (user.getUserContact().getContactId() == null) {
179                         // Throw NPE again
180                         throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
181                 } else if (user.getUserContact().getContactId() < 1) {
182                         // Not valid id number
183                         throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N
184                 } else if (user.getUserAccountStatus() == null) {
185                         // Throw NPE again
186                         throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
187                 } else if (user.getUserAccountStatus() == UserAccountStatus.LOCKED) {
188                         // Account is locked
189                         throw new UserStatusLockedException(user);
190                 } else if (user.getUserAccountStatus() == UserAccountStatus.UNCONFIRMED) {
191                         // Account is locked
192                         throw new UserStatusUnconfirmedException(user);
193                 } else if (!this.userBean.ifUserExists(user)) {
194                         // Name already found
195                         throw new UserNotFoundException(user);
196                 } else if (null == userLockReason) {
197                         // Throw NPE again
198                         throw new NullPointerException("userLockReason is null"); //NOI18N
199                 } else if (userLockReason.isEmpty()) {
200                         // Is empty
201                         throw new IllegalArgumentException("userLockReason is empty"); //NOI18N
202                 }
203
204                 // Remove contact instance as this is not updated
205                 user.setUserContact(null);
206
207                 // Find the instance
208                 User foundUser = this.getEntityManager().find(user.getClass(), user.getUserId());
209
210                 // Should be found!
211                 assert (foundUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
212
213                 // Merge user
214                 User detachedUser = this.getEntityManager().merge(foundUser);
215
216                 // Should be found!
217                 assert (detachedUser instanceof User) : MessageFormat.format("User with id {0} not merged, but should be.", user.getUserId()); //NOI18N
218
219                 // Copy all data
220                 detachedUser.copyAll(user);
221
222                 // Set as locked
223                 detachedUser.setUserAccountStatus(UserAccountStatus.LOCKED);
224                 detachedUser.setUserLastLocked(new GregorianCalendar());
225                 detachedUser.setUserLastLockedReason(userLockReason);
226
227                 // Update user
228                 User updatedUser = this.userBean.updateUserData(user);
229
230                 // @TODO Create user lock history entry
231                 // Init variable
232                 Address emailAddress;
233
234                 try {
235                         // Create email address and set
236                         emailAddress = new InternetAddress(updatedUser.getUserContact().getContactEmailAddress());
237                 } catch (final AddressException ex) {
238                         // Throw again
239                         throw new EJBException(ex);
240                 }
241
242                 // Send out email
243                 // @TODO externalize subject line
244                 this.sendEmail("Account locked", "account_locked", emailAddress, updatedUser, baseUrl); //NOI18N
245
246                 // Trace message
247                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount - EXIT!", this.getClass().getSimpleName())); //NOI18N
248         }
249
250         @Override
251         public void unlockUserAccount (final User user, final String baseUrl) throws UserStatusConfirmedException, UserStatusUnconfirmedException, UserNotFoundException {
252                 // Trace message
253                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount: user={1},baseUrl={2} - CALLED!", this.getClass().getSimpleName(), user, baseUrl)); //NOI18N
254
255                 // user should not be null
256                 if (null == user) {
257                         // Abort here
258                         throw new NullPointerException("user is null"); //NOI18N
259                 } else if (user.getUserId() instanceof Long) {
260                         // Id is set
261                         throw new IllegalArgumentException("user.userId is not null"); //NOI18N
262                 } else if (user.getUserContact() == null) {
263                         // Throw NPE again
264                         throw new NullPointerException("user.userContact is null"); //NOI18N
265                 } else if (user.getUserContact().getContactId() == null) {
266                         // Throw NPE again
267                         throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
268                 } else if (user.getUserContact().getContactId() < 1) {
269                         // Not valid id number
270                         throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N
271                 } else if (user.getUserAccountStatus() == null) {
272                         // Throw NPE again
273                         throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
274                 } else if (user.getUserAccountStatus() == UserAccountStatus.CONFIRMED) {
275                         // Account is locked
276                         throw new UserStatusConfirmedException(user);
277                 } else if (user.getUserAccountStatus() == UserAccountStatus.UNCONFIRMED) {
278                         // Account is locked
279                         throw new UserStatusUnconfirmedException(user);
280                 } else if (!this.userBean.ifUserExists(user)) {
281                         // Name already found
282                         throw new UserNotFoundException(user);
283                 }
284
285                 // Remove contact instance as this is not updated
286                 user.setUserContact(null);
287
288                 // Find the instance
289                 User foundUser = this.getEntityManager().find(user.getClass(), user.getUserId());
290
291                 // Should be found!
292                 assert (foundUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
293
294                 // Merge user
295                 User detachedUser = this.getEntityManager().merge(foundUser);
296
297                 // Should be found!
298                 assert (detachedUser instanceof User) : MessageFormat.format("User with id {0} not merged, but should be.", user.getUserId()); //NOI18N
299
300                 // Copy all data
301                 detachedUser.copyAll(user);
302
303                 // Unlock account
304                 detachedUser.setUserAccountStatus(UserAccountStatus.CONFIRMED);
305
306                 // Update user
307                 User updatedUser = this.userBean.updateUserData(user);
308
309                 // @TODO Create user lock history entry
310                 // Init variable
311                 Address emailAddress;
312
313                 try {
314                         // Create email address and set
315                         emailAddress = new InternetAddress(updatedUser.getUserContact().getContactEmailAddress());
316                 } catch (final AddressException ex) {
317                         // Throw again
318                         throw new EJBException(ex);
319                 }
320
321                 // Send out email
322                 // @TODO externalize subject line
323                 this.sendEmail("Account unlocked", "account_unlocked", emailAddress, updatedUser, baseUrl); //NOI18N
324
325                 // Trace message
326                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount - EXIT!", this.getClass().getSimpleName())); //NOI18N
327         }
328
329 }