2 * Copyright (C) 2016, 2017 Roland Häder
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.
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.
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/>.
17 package org.mxchange.jusercore.model.user;
19 import java.text.MessageFormat;
20 import java.util.GregorianCalendar;
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;
39 * An administrative user EJB
41 * @author Roland Häder<roland@mxchange.org>
43 @Stateless (name = "adminUser", description = "A bean handling the user data")
44 public class AddressbookAdminUserSessionBean extends BaseAddressbookDatabaseBean implements AdminUserSessionBeanRemote {
49 private static final long serialVersionUID = 542_145_347_916L;
55 private UserRegistrationSessionBeanRemote registerBean;
61 private UserSessionBeanRemote userBean;
66 public AddressbookAdminUserSessionBean () {
67 // Call super constructor
72 public User addUser (final User user) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
74 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addUser: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
76 // user should not be null
79 throw new NullPointerException("user is null"); //NOI18N
80 } else if (user.getUserId() instanceof Long) {
82 throw new IllegalStateException(MessageFormat.format("user.userId must be null, is: {0}", user.getUserId())); //NOI18N
85 // Check if user is registered
86 if (this.registerBean.isUserNameRegistered(user)) {
88 throw new UserNameAlreadyRegisteredException(user);
89 } else if (this.registerBean.isEmailAddressRegistered(user)) {
91 throw new EmailAddressAlreadyRegisteredException(user);
94 // Set created timestamp
95 user.setUserCreated(new GregorianCalendar());
96 user.getUserContact().setContactCreated(new GregorianCalendar());
98 // Update cellphone, land-line and fax instance
99 this.setAllContactPhoneEntriesCreated(user.getUserContact());
102 this.getEntityManager().persist(user);
104 // Flush to get id back
105 this.getEntityManager().flush();
108 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addUser: user={1},user.userId={2} - EXIT!", this.getClass().getSimpleName(), user, user.getUserId())); //NOI18N
115 public void deleteUser (final User user, final String userDeleteReason) throws UserNotFoundException {
117 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteUser: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
119 // user should not be null
122 throw new NullPointerException("user is null"); //NOI18N
123 } else if (user.getUserId() == null) {
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) {
131 throw new NullPointerException("user.userContact is null"); //NOI18N
132 } else if (user.getUserContact().getContactId() == null) {
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) {
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);
146 // Get a managed instance
147 User managedUser = this.getManagedUser(user);
150 assert (managedUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
153 this.getEntityManager().remove(managedUser);
156 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteUser: EXIT!", this.getClass().getSimpleName())); //NOI18N
160 public User linkUser (final User user) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
162 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkUser: user={0} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
164 // user should not be null
167 throw new NullPointerException("user is null"); //NOI18N
168 } else if (user.getUserId() instanceof Long) {
170 throw new IllegalArgumentException("user.userId is not null"); //NOI18N
171 } else if (user.getUserContact() == null) {
173 throw new NullPointerException("user.userContact is null"); //NOI18N
174 } else if (user.getUserContact().getContactId() == null) {
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) {
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());
188 // Try to find the contact
189 Contact managedContact = this.getEntityManager().find(user.getUserContact().getClass(), user.getUserContact().getContactId());
192 assert (managedContact instanceof Contact) : MessageFormat.format("Contact with id {0} not found, but should be.", user.getUserContact().getContactId()); //NOI18N
194 // Set detached object in rexcruiter instance
195 user.setUserContact(managedContact);
198 user.setUserCreated(new GregorianCalendar());
201 this.getEntityManager().persist(user);
203 // Flush it to get updated instance back
204 this.getEntityManager().flush();
207 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkUser: user={1} - EXIT!", this.getClass().getSimpleName(), user)); //NOI18N
209 // Return updated instanc
214 public User lockUserAccount (final User user, final String userLockReason, final String baseUrl) throws UserStatusLockedException, UserStatusUnconfirmedException, UserNotFoundException {
216 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount: user={1},userLockReason={2},baseUrl={3} - CALLED!", this.getClass().getSimpleName(), user, userLockReason, baseUrl)); //NOI18N
218 // user should not be null
221 throw new NullPointerException("user is null"); //NOI18N
222 } else if (user.getUserId() == null) {
224 throw new NullPointerException("user.userId is null"); //NOI18N
225 } else if (user.getUserId() < 1) {
227 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is null", user.getUserId())); //NOI18N
228 } else if (user.getUserContact() == null) {
230 throw new NullPointerException("user.userContact is null"); //NOI18N
231 } else if (user.getUserContact().getContactId() == null) {
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) {
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) {
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) {
251 throw new NullPointerException("userLockReason is null"); //NOI18N
252 } else if (userLockReason.isEmpty()) {
254 throw new IllegalArgumentException("userLockReason is empty"); //NOI18N
257 // Remove contact instance as this is not updated
258 user.setUserContact(null);
260 // Set as locked, set timestamp and lock reason
261 user.setUserAccountStatus(UserAccountStatus.LOCKED);
262 user.setUserLastLocked(new GregorianCalendar());
263 user.setUserLastLockedReason(userLockReason);
266 User managedUser = this.userBean.updateUserData(user);
268 // @TODO Create user lock history entry
270 Address emailAddress;
273 // Create email address and set
274 emailAddress = new InternetAddress(managedUser.getUserContact().getContactEmailAddress());
275 } catch (final AddressException ex) {
277 throw new EJBException(ex);
281 // @TODO externalize subject line
282 this.sendEmail("Account locked", "account_locked", emailAddress, managedUser, baseUrl); //NOI18N
285 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount: managedUser={1} - EXIT!", this.getClass().getSimpleName(), managedUser)); //NOI18N
287 // Return detached (and updated) user
292 public User unlockUserAccount (final User user, final String baseUrl) throws UserStatusConfirmedException, UserStatusUnconfirmedException, UserNotFoundException {
294 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount: user={1},baseUrl={2} - CALLED!", this.getClass().getSimpleName(), user, baseUrl)); //NOI18N
296 // user should not be null
299 throw new NullPointerException("user is null"); //NOI18N
300 } else if (user.getUserId() == null) {
302 throw new NullPointerException("user.userId is null"); //NOI18N
303 } else if (user.getUserId() < 1) {
305 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is null", user.getUserId())); //NOI18N
306 } else if (user.getUserContact() == null) {
308 throw new NullPointerException("user.userContact is null"); //NOI18N
309 } else if (user.getUserContact().getContactId() == null) {
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) {
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);
329 // Remove contact instance as this is not updated
330 user.setUserContact(null);
333 user.setUserAccountStatus(UserAccountStatus.CONFIRMED);
336 User managedUser = this.userBean.updateUserData(user);
338 // @TODO Create user lock history entry
340 Address emailAddress;
343 // Create email address and set
344 emailAddress = new InternetAddress(managedUser.getUserContact().getContactEmailAddress());
345 } catch (final AddressException ex) {
347 throw new EJBException(ex);
351 // @TODO externalize subject line
352 this.sendEmail("Account unlocked", "account_unlocked", emailAddress, managedUser, baseUrl); //NOI18N
355 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount: managedUser={1} - EXIT!", this.getClass().getSimpleName(), managedUser)); //NOI18N
357 // Return changed account