]> git.mxchange.org Git - jfinancials-mailer-ejb.git/blob - src/java/org/mxchange/jusercore/model/user/AddressbookAdminUserSessionBean.java
Continued a bit:
[jfinancials-mailer-ejb.git] / src / java / org / mxchange / jusercore / model / user / AddressbookAdminUserSessionBean.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.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 Häder<roland@mxchange.org>
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 void deleteUser (final User user, final String userDeleteReason) throws UserNotFoundException {
114                 // Trace message
115                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteUser: user={1} - 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() == null) {
122                         // Id is set
123                         throw new NullPointerException("user.userId is null"); //NOI18N
124                 } else if (user.getUserId() < 1) {
125                         // Not valid id number
126                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", user.getUserId())); //NOI18N
127                 } else if (user.getUserContact() == null) {
128                         // Throw NPE again
129                         throw new NullPointerException("user.userContact is null"); //NOI18N
130                 } else if (user.getUserContact().getContactId() == null) {
131                         // Throw NPE again
132                         throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
133                 } else if (user.getUserContact().getContactId() < 1) {
134                         // Not valid id number
135                         throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N
136                 } else if (user.getUserAccountStatus() == null) {
137                         // Throw NPE again
138                         throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
139                 } else if (!this.userBean.ifUserExists(user)) {
140                         // Name already found
141                         throw new UserNotFoundException(user);
142                 }
143
144                 // Get a managed instance
145                 User managedUser = this.getManagedUser(user);
146
147                 // Should be found!
148                 assert (managedUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
149
150                 // Delete it
151                 this.getEntityManager().remove(managedUser);
152
153                 // Trace message
154                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteUser: EXIT!", this.getClass().getSimpleName())); //NOI18N
155         }
156
157         @Override
158         public User linkUser (final User user) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
159                 // Trace message
160                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkUser: user={0} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
161
162                 // user should not be null
163                 if (null == user) {
164                         // Abort here
165                         throw new NullPointerException("user is null"); //NOI18N
166                 } else if (user.getUserId() instanceof Long) {
167                         // Id is set
168                         throw new IllegalArgumentException("user.userId is not null"); //NOI18N
169                 } else if (user.getUserContact() == null) {
170                         // Throw NPE again
171                         throw new NullPointerException("user.userContact is null"); //NOI18N
172                 } else if (user.getUserContact().getContactId() == null) {
173                         // Throw NPE again
174                         throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
175                 } else if (user.getUserContact().getContactId() < 1) {
176                         // Not valid id number
177                         throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N
178                 } else if (user.getUserAccountStatus() == null) {
179                         // Throw NPE again
180                         throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
181                 } else if (this.userBean.ifUserNameExists(user.getUserName())) {
182                         // Name already found
183                         throw new UserNameAlreadyRegisteredException(user.getUserName());
184                 }
185
186                 // Try to find the contact
187                 Contact foundContact = this.getEntityManager().find(user.getUserContact().getClass(), user.getUserContact().getContactId());
188
189                 // Set detached object in rexcruiter instance
190                 user.setUserContact(foundContact);
191
192                 // Set timestamp
193                 user.setUserCreated(new GregorianCalendar());
194
195                 // Perist it
196                 this.getEntityManager().persist(user);
197
198                 // Flush it to get updated instance back
199                 this.getEntityManager().flush();
200
201                 // Log trace message
202                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkUser: user={1} - EXIT!", this.getClass().getSimpleName(), user)); //NOI18N
203
204                 // Return updated instanc
205                 return user;
206         }
207
208         @Override
209         public User lockUserAccount (final User user, final String userLockReason, final String baseUrl) throws UserStatusLockedException, UserStatusUnconfirmedException, UserNotFoundException {
210                 // Trace message
211                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount: user={1},userLockReason={2},baseUrl={3} - CALLED!", this.getClass().getSimpleName(), user, userLockReason, baseUrl)); //NOI18N
212
213                 // user should not be null
214                 if (null == user) {
215                         // Abort here
216                         throw new NullPointerException("user is null"); //NOI18N
217                 } else if (user.getUserId() == null) {
218                         // Id is set
219                         throw new NullPointerException("user.userId is null"); //NOI18N
220                 } else if (user.getUserId() < 1) {
221                         // Id is set
222                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is null", user.getUserId())); //NOI18N
223                 } else if (user.getUserContact() == null) {
224                         // Throw NPE again
225                         throw new NullPointerException("user.userContact is null"); //NOI18N
226                 } else if (user.getUserContact().getContactId() == null) {
227                         // Throw NPE again
228                         throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
229                 } else if (user.getUserContact().getContactId() < 1) {
230                         // Not valid id number
231                         throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N
232                 } else if (user.getUserAccountStatus() == null) {
233                         // Throw NPE again
234                         throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
235                 } else if (!this.userBean.ifUserExists(user)) {
236                         // Name already found
237                         throw new UserNotFoundException(user);
238                 } else if (user.getUserAccountStatus() == UserAccountStatus.LOCKED) {
239                         // Account is locked
240                         throw new UserStatusLockedException(user);
241                 } else if (user.getUserAccountStatus() == UserAccountStatus.UNCONFIRMED) {
242                         // Account is unconfirmed
243                         throw new UserStatusUnconfirmedException(user);
244                 } else if (null == userLockReason) {
245                         // Throw NPE again
246                         throw new NullPointerException("userLockReason is null"); //NOI18N
247                 } else if (userLockReason.isEmpty()) {
248                         // Is empty
249                         throw new IllegalArgumentException("userLockReason is empty"); //NOI18N
250                 }
251
252                 // Remove contact instance as this is not updated
253                 user.setUserContact(null);
254
255                 // Set as locked, set timestamp and lock reason
256                 user.setUserAccountStatus(UserAccountStatus.LOCKED);
257                 user.setUserLastLocked(new GregorianCalendar());
258                 user.setUserLastLockedReason(userLockReason);
259
260                 // Update user
261                 User updatedUser = this.userBean.updateUserData(user);
262
263                 // @TODO Create user lock history entry
264                 // Init variable
265                 Address emailAddress;
266
267                 try {
268                         // Create email address and set
269                         emailAddress = new InternetAddress(updatedUser.getUserContact().getContactEmailAddress());
270                 } catch (final AddressException ex) {
271                         // Throw again
272                         throw new EJBException(ex);
273                 }
274
275                 // Send out email
276                 // @TODO externalize subject line
277                 this.sendEmail("Account locked", "account_locked", emailAddress, updatedUser, baseUrl); //NOI18N
278
279                 // Trace message
280                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount: updatedUser={1} - EXIT!", this.getClass().getSimpleName(), updatedUser)); //NOI18N
281
282                 // Return detached (and updated) user
283                 return updatedUser;
284         }
285
286         @Override
287         public User unlockUserAccount (final User user, final String baseUrl) throws UserStatusConfirmedException, UserStatusUnconfirmedException, UserNotFoundException {
288                 // Trace message
289                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount: user={1},baseUrl={2} - CALLED!", this.getClass().getSimpleName(), user, baseUrl)); //NOI18N
290
291                 // user should not be null
292                 if (null == user) {
293                         // Abort here
294                         throw new NullPointerException("user is null"); //NOI18N
295                 } else if (user.getUserId() == null) {
296                         // Id is set
297                         throw new NullPointerException("user.userId is null"); //NOI18N
298                 } else if (user.getUserId() < 1) {
299                         // Id is set
300                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is null", user.getUserId())); //NOI18N
301                 } else if (user.getUserContact() == null) {
302                         // Throw NPE again
303                         throw new NullPointerException("user.userContact is null"); //NOI18N
304                 } else if (user.getUserContact().getContactId() == null) {
305                         // Throw NPE again
306                         throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
307                 } else if (user.getUserContact().getContactId() < 1) {
308                         // Not valid id number
309                         throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N
310                 } else if (user.getUserAccountStatus() == null) {
311                         // Throw NPE again
312                         throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
313                 } else if (!this.userBean.ifUserExists(user)) {
314                         // Name already found
315                         throw new UserNotFoundException(user);
316                 } else if (user.getUserAccountStatus() == UserAccountStatus.CONFIRMED) {
317                         // Account is confirmed
318                         throw new UserStatusConfirmedException(user);
319                 } else if (user.getUserAccountStatus() == UserAccountStatus.UNCONFIRMED) {
320                         // Account is unconfirmed
321                         throw new UserStatusUnconfirmedException(user);
322                 }
323
324                 // Remove contact instance as this is not updated
325                 user.setUserContact(null);
326
327                 // Unlock account
328                 user.setUserAccountStatus(UserAccountStatus.CONFIRMED);
329
330                 // Update user
331                 User updatedUser = this.userBean.updateUserData(user);
332
333                 // @TODO Create user lock history entry
334                 // Init variable
335                 Address emailAddress;
336
337                 try {
338                         // Create email address and set
339                         emailAddress = new InternetAddress(updatedUser.getUserContact().getContactEmailAddress());
340                 } catch (final AddressException ex) {
341                         // Throw again
342                         throw new EJBException(ex);
343                 }
344
345                 // Send out email
346                 // @TODO externalize subject line
347                 this.sendEmail("Account unlocked", "account_unlocked", emailAddress, updatedUser, baseUrl); //NOI18N
348
349                 // Trace message
350                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount: updatedUser={1} - EXIT!", this.getClass().getSimpleName(), updatedUser)); //NOI18N
351
352                 // Return changed account
353                 return updatedUser;
354         }
355
356 }