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