2 * Copyright (C) 2016 - 2020 Free Software Foundation
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.Date;
22 import javax.ejb.Stateless;
23 import org.mxchange.jcontacts.model.contact.Contact;
24 import org.mxchange.jjobs.enterprise.BaseJobsEnterpriseBean;
25 import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
26 import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
27 import org.mxchange.jusercore.exceptions.UserNotFoundException;
28 import org.mxchange.jusercore.exceptions.UserStatusConfirmedException;
29 import org.mxchange.jusercore.exceptions.UserStatusLockedException;
30 import org.mxchange.jusercore.exceptions.UserStatusUnconfirmedException;
31 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
34 * An administrative user EJB
36 * @author Roland Häder<roland@mxchange.org>
38 @Stateless (name = "adminUser", description = "A bean handling the user data")
39 public class JobsAdminUserSessionBean extends BaseJobsEnterpriseBean implements AdminUserSessionBeanRemote {
44 private static final long serialVersionUID = 542_145_347_916L;
50 private UserSessionBeanRemote userBean;
55 public JobsAdminUserSessionBean () {
56 // Call super constructor
57 super("jms/jjobs-queue-factory", "jms/jjobs-email-queue"); //NOI18N
61 public User addUser (final User user) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
63 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addUser: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
68 throw new NullPointerException("user is null"); //NOI18N
69 } else if (user.getUserId() instanceof Long) {
71 throw new IllegalStateException(MessageFormat.format("user.userId must be null, is: {0}", user.getUserId())); //NOI18N
74 // Check if user is registered
75 if (this.userBean.isUserNameRegistered(user)) {
77 throw new UserNameAlreadyRegisteredException(user);
78 } else if (this.userBean.isEmailAddressRegistered(user)) {
80 throw new EmailAddressAlreadyRegisteredException(user);
83 // Set created timestamp
84 user.setUserCreated(new Date());
85 user.getUserContact().setContactCreated(new Date());
87 // Update mobile, land-line and fax instance
88 this.setAllPhoneEntriesCreated(user.getUserContact());
91 this.getEntityManager().persist(user);
94 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addUser: user={1},user.userId={2} - EXIT!", this.getClass().getSimpleName(), user, user.getUserId())); //NOI18N
101 public void deleteUser (final User user, final String userDeleteReason) throws UserNotFoundException {
103 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteUser: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
105 // Validate parameters
108 throw new NullPointerException("user is null"); //NOI18N
109 } else if (user.getUserId() == null) {
111 throw new NullPointerException("user.userId is null"); //NOI18N
112 } else if (user.getUserId() < 1) {
113 // Not valid id number
114 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", user.getUserId())); //NOI18N
115 } else if (user.getUserContact() == null) {
117 throw new NullPointerException("user.userContact is null"); //NOI18N
118 } else if (user.getUserContact().getContactId() == null) {
120 throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
121 } else if (user.getUserContact().getContactId() < 1) {
122 // Not valid id number
123 throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N
124 } else if (user.getUserAccountStatus() == null) {
126 throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
127 } else if (!this.userBean.ifUserExists(user)) {
128 // Name already found
129 throw new UserNotFoundException(user);
132 // Get a managed instance
133 final User managedUser = this.createManaged(user);
136 assert (managedUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
139 this.getEntityManager().remove(managedUser);
142 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteUser: EXIT!", this.getClass().getSimpleName())); //NOI18N
146 public User linkUser (final User user) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
148 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkUser: user={0} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
150 // user should not be null
153 throw new NullPointerException("user is null"); //NOI18N
154 } else if (user.getUserId() instanceof Long) {
156 throw new IllegalArgumentException("user.userId is not null"); //NOI18N
157 } else if (user.getUserContact() == null) {
159 throw new NullPointerException("user.userContact is null"); //NOI18N
160 } else if (user.getUserContact().getContactId() == null) {
162 throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
163 } else if (user.getUserContact().getContactId() < 1) {
164 // Not valid id number
165 throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N
166 } else if (user.getUserAccountStatus() == null) {
168 throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
169 } else if (this.userBean.ifUserNameExists(user.getUserName())) {
170 // Name already found
171 throw new UserNameAlreadyRegisteredException(user.getUserName());
174 // Try to find the contact
175 final Contact foundContact = this.getEntityManager().find(user.getUserContact().getClass(), user.getUserContact().getContactId());
177 // Set detached object in rexcruiter instance
178 user.setUserContact(foundContact);
181 user.setUserCreated(new Date());
184 this.getEntityManager().persist(user);
187 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkUser: user={1} - EXIT!", this.getClass().getSimpleName(), user)); //NOI18N
189 // Return updated instanc
194 public User lockUserAccount (final User user, final String userLockReason, final String baseUrl) throws UserStatusLockedException, UserStatusUnconfirmedException, UserNotFoundException {
196 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount: user={1},userLockReason={2},baseUrl={3} - CALLED!", this.getClass().getSimpleName(), user, userLockReason, baseUrl)); //NOI18N
198 // user should not be null
201 throw new NullPointerException("user is null"); //NOI18N
202 } else if (user.getUserId() == null) {
204 throw new NullPointerException("user.userId is null"); //NOI18N
205 } else if (user.getUserId() < 1) {
207 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is null", user.getUserId())); //NOI18N
208 } else if (user.getUserContact() == null) {
210 throw new NullPointerException("user.userContact is null"); //NOI18N
211 } else if (user.getUserContact().getContactId() == null) {
213 throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
214 } else if (user.getUserContact().getContactId() < 1) {
215 // Not valid id number
216 throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N
217 } else if (user.getUserAccountStatus() == null) {
219 throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
220 } else if (!this.userBean.ifUserExists(user)) {
221 // Name already found
222 throw new UserNotFoundException(user);
223 } else if (user.getUserAccountStatus() == UserAccountStatus.LOCKED) {
225 throw new UserStatusLockedException(user);
226 } else if (user.getUserAccountStatus() == UserAccountStatus.UNCONFIRMED) {
227 // Account is unconfirmed
228 throw new UserStatusUnconfirmedException(user);
229 } else if (null == userLockReason) {
231 throw new NullPointerException("userLockReason is null"); //NOI18N
232 } else if (userLockReason.isEmpty()) {
234 throw new IllegalArgumentException("userLockReason is empty"); //NOI18N
237 // Remove contact instance as this is not updated
238 user.setUserContact(null);
240 // Set as locked, set timestamp and lock reason
241 user.setUserAccountStatus(UserAccountStatus.LOCKED);
242 user.setUserLastLocked(new Date());
243 user.setUserLastLockedReason(userLockReason);
246 final User managedUser = this.userBean.updateUserData(user);
248 // @TODO Create user lock history entry
250 // @TODO externalize subject line
251 this.sendEmail("User account locked", "user_account_locked", managedUser, baseUrl, null); //NOI18N
254 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount: managedUser={1} - EXIT!", this.getClass().getSimpleName(), managedUser)); //NOI18N
256 // Return detached (and updated) user
261 public User unlockUserAccount (final User user, final String baseUrl) throws UserStatusConfirmedException, UserStatusUnconfirmedException, UserNotFoundException {
263 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount: user={1},baseUrl={2} - CALLED!", this.getClass().getSimpleName(), user, baseUrl)); //NOI18N
265 // user should not be null
268 throw new NullPointerException("user is null"); //NOI18N
269 } else if (user.getUserId() == null) {
271 throw new NullPointerException("user.userId is null"); //NOI18N
272 } else if (user.getUserId() < 1) {
274 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is null", user.getUserId())); //NOI18N
275 } else if (user.getUserContact() == null) {
277 throw new NullPointerException("user.userContact is null"); //NOI18N
278 } else if (user.getUserContact().getContactId() == null) {
280 throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
281 } else if (user.getUserContact().getContactId() < 1) {
282 // Not valid id number
283 throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N
284 } else if (user.getUserAccountStatus() == null) {
286 throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
287 } else if (!this.userBean.ifUserExists(user)) {
288 // Name already found
289 throw new UserNotFoundException(user);
290 } else if (user.getUserAccountStatus() == UserAccountStatus.CONFIRMED) {
291 // Account is confirmed
292 throw new UserStatusConfirmedException(user);
293 } else if (user.getUserAccountStatus() == UserAccountStatus.UNCONFIRMED) {
294 // Account is unconfirmed
295 throw new UserStatusUnconfirmedException(user);
298 // Remove contact instance as this is not updated
299 user.setUserContact(null);
302 user.setUserAccountStatus(UserAccountStatus.CONFIRMED);
305 final User managedUser = this.userBean.updateUserData(user);
307 // @TODO Create user lock history entry
309 // @TODO externalize subject line
310 this.sendEmail("User account unlocked", "user_account_unlocked", managedUser, baseUrl, null); //NOI18N
313 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount: managedUser={1} - EXIT!", this.getClass().getSimpleName(), managedUser)); //NOI18N
315 // Return changed account