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;
49 @EJB (lookup = "java:global/jjobs-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote")
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
72 } else if (null == user.getUserContact()) {
74 throw new NullPointerException("user.contact is null"); //NOI18N
75 } else if (user.getUserContact().getContactId() != null) {
77 throw new IllegalStateException(MessageFormat.format("user.userContact.contactId must be null, is: {0}", user.getUserId())); //NOI18N
80 // Check if user is registered
81 if (this.userBean.isUserNameRegistered(user)) {
83 throw new UserNameAlreadyRegisteredException(user);
84 } else if (this.userBean.isEmailAddressRegistered(user)) {
86 throw new EmailAddressAlreadyRegisteredException(user);
89 // Set created timestamp
90 user.setUserEntryCreated(new Date());
91 user.getUserContact().setContactEntryCreated(new Date());
93 // Update mobile, land-line and fax instance
94 this.setAllPhoneEntriesCreated(user.getUserContact());
97 this.getEntityManager().persist(user);
100 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addUser: user={1},user.userId={2} - EXIT!", this.getClass().getSimpleName(), user, user.getUserId())); //NOI18N
107 public void deleteUser (final User user, final String userDeleteReason) throws UserNotFoundException {
109 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteUser: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
111 // Validate parameters
114 throw new NullPointerException("user is null"); //NOI18N
115 } else if (user.getUserId() == null) {
117 throw new NullPointerException("user.userId is null"); //NOI18N
118 } else if (user.getUserId() < 1) {
119 // Not valid id number
120 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", user.getUserId())); //NOI18N
121 } else if (user.getUserContact() == null) {
123 throw new NullPointerException("user.userContact is null"); //NOI18N
124 } else if (user.getUserContact().getContactId() == null) {
126 throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
127 } else if (user.getUserContact().getContactId() < 1) {
128 // Not valid id number
129 throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N
130 } else if (user.getUserAccountStatus() == null) {
132 throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
133 } else if (!this.userBean.ifUserExists(user)) {
134 // Name already found
135 throw new UserNotFoundException(user);
138 // Get a managed instance
139 final User managedUser = this.createManaged(user);
142 assert (managedUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
145 this.getEntityManager().remove(managedUser);
148 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteUser: EXIT!", this.getClass().getSimpleName())); //NOI18N
152 public User linkUser (final User user) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
154 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkUser: user={0} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
156 // user should not be null
159 throw new NullPointerException("user is null"); //NOI18N
160 } else if (user.getUserId() instanceof Long) {
162 throw new IllegalArgumentException("user.userId is not null"); //NOI18N
163 } else if (user.getUserContact() == null) {
165 throw new NullPointerException("user.userContact is null"); //NOI18N
166 } else if (user.getUserContact().getContactId() == null) {
168 throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
169 } else if (user.getUserContact().getContactId() < 1) {
170 // Not valid id number
171 throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N
172 } else if (user.getUserAccountStatus() == null) {
174 throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
175 } else if (this.userBean.ifUserNameExists(user.getUserName())) {
176 // Name already found
177 throw new UserNameAlreadyRegisteredException(user.getUserName());
180 // Try to find the contact
181 final Contact foundContact = this.getEntityManager().find(user.getUserContact().getClass(), user.getUserContact().getContactId());
183 // Set detached object in rexcruiter instance
184 user.setUserContact(foundContact);
187 user.setUserEntryCreated(new Date());
190 this.getEntityManager().persist(user);
193 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkUser: user={1} - EXIT!", this.getClass().getSimpleName(), user)); //NOI18N
195 // Return updated instanc
200 public User lockUserAccount (final User user, final String userLockReason, final String baseUrl) throws UserStatusLockedException, UserStatusUnconfirmedException, UserNotFoundException {
202 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount: user={1},userLockReason={2},baseUrl={3} - CALLED!", this.getClass().getSimpleName(), user, userLockReason, baseUrl)); //NOI18N
204 // user should not be null
207 throw new NullPointerException("user is null"); //NOI18N
208 } else if (user.getUserId() == null) {
210 throw new NullPointerException("user.userId is null"); //NOI18N
211 } else if (user.getUserId() < 1) {
213 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is null", user.getUserId())); //NOI18N
214 } else if (user.getUserContact() == null) {
216 throw new NullPointerException("user.userContact is null"); //NOI18N
217 } else if (user.getUserContact().getContactId() == null) {
219 throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
220 } else if (user.getUserContact().getContactId() < 1) {
221 // Not valid id number
222 throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N
223 } else if (user.getUserAccountStatus() == null) {
225 throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
226 } else if (!this.userBean.ifUserExists(user)) {
227 // Name already found
228 throw new UserNotFoundException(user);
229 } else if (user.getUserAccountStatus() == UserAccountStatus.LOCKED) {
231 throw new UserStatusLockedException(user);
232 } else if (user.getUserAccountStatus() == UserAccountStatus.UNCONFIRMED) {
233 // Account is unconfirmed
234 throw new UserStatusUnconfirmedException(user);
235 } else if (null == userLockReason) {
237 throw new NullPointerException("userLockReason is null"); //NOI18N
238 } else if (userLockReason.isEmpty()) {
240 throw new IllegalArgumentException("userLockReason is empty"); //NOI18N
243 // Remove contact instance as this is not updated
244 user.setUserContact(null);
246 // Set as locked, set timestamp and lock reason
247 user.setUserAccountStatus(UserAccountStatus.LOCKED);
248 user.setUserLastLocked(new Date());
249 user.setUserLastLockedReason(userLockReason);
252 final User managedUser = this.userBean.updateUserData(user);
254 // @TODO Create user lock history entry
256 // @TODO externalize subject line
257 this.sendEmail("User account locked", "user_account_locked", managedUser, baseUrl, null); //NOI18N
260 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount: managedUser={1} - EXIT!", this.getClass().getSimpleName(), managedUser)); //NOI18N
262 // Return detached (and updated) user
267 public User unlockUserAccount (final User user, final String baseUrl) throws UserStatusConfirmedException, UserStatusUnconfirmedException, UserNotFoundException {
269 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount: user={1},baseUrl={2} - CALLED!", this.getClass().getSimpleName(), user, baseUrl)); //NOI18N
271 // user should not be null
274 throw new NullPointerException("user is null"); //NOI18N
275 } else if (user.getUserId() == null) {
277 throw new NullPointerException("user.userId is null"); //NOI18N
278 } else if (user.getUserId() < 1) {
280 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is null", user.getUserId())); //NOI18N
281 } else if (user.getUserContact() == null) {
283 throw new NullPointerException("user.userContact is null"); //NOI18N
284 } else if (user.getUserContact().getContactId() == null) {
286 throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
287 } else if (user.getUserContact().getContactId() < 1) {
288 // Not valid id number
289 throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N
290 } else if (user.getUserAccountStatus() == null) {
292 throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
293 } else if (!this.userBean.ifUserExists(user)) {
294 // Name already found
295 throw new UserNotFoundException(user);
296 } else if (user.getUserAccountStatus() == UserAccountStatus.CONFIRMED) {
297 // Account is confirmed
298 throw new UserStatusConfirmedException(user);
299 } else if (user.getUserAccountStatus() == UserAccountStatus.UNCONFIRMED) {
300 // Account is unconfirmed
301 throw new UserStatusUnconfirmedException(user);
304 // Remove contact instance as this is not updated
305 user.setUserContact(null);
308 user.setUserAccountStatus(UserAccountStatus.CONFIRMED);
311 final User managedUser = this.userBean.updateUserData(user);
313 // @TODO Create user lock history entry
315 // @TODO externalize subject line
316 this.sendEmail("User account unlocked", "user_account_unlocked", managedUser, baseUrl, null); //NOI18N
319 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount: managedUser={1} - EXIT!", this.getClass().getSimpleName(), managedUser)); //NOI18N
321 // Return changed account