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.Stateless;
23 import org.mxchange.jcontacts.contact.Contact;
24 import org.mxchange.jjobs.database.BaseJobsDatabaseBean;
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;
32 import org.mxchange.juserlogincore.model.user.register.UserRegistrationSessionBeanRemote;
35 * An administrative user EJB
37 * @author Roland Häder<roland@mxchange.org>
39 @Stateless (name = "adminUser", description = "A bean handling the user data")
40 public class JobsAdminUserSessionBean extends BaseJobsDatabaseBean implements AdminUserSessionBeanRemote {
45 private static final long serialVersionUID = 542_145_347_916L;
51 private UserRegistrationSessionBeanRemote registerBean;
57 private UserSessionBeanRemote userBean;
62 public JobsAdminUserSessionBean () {
63 // Call super constructor
64 super("jms/jjobs-queue-factory", "jms/jjobs-email-queue"); //NOI18N
68 public User addUser (final User user) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
70 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addUser: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
72 // user should not be null
75 throw new NullPointerException("user is null"); //NOI18N
76 } else if (user.getUserId() instanceof Long) {
78 throw new IllegalStateException(MessageFormat.format("user.userId must be null, is: {0}", user.getUserId())); //NOI18N
81 // Check if user is registered
82 if (this.registerBean.isUserNameRegistered(user)) {
84 throw new UserNameAlreadyRegisteredException(user);
85 } else if (this.registerBean.isEmailAddressRegistered(user)) {
87 throw new EmailAddressAlreadyRegisteredException(user);
90 // Set created timestamp
91 user.setUserCreated(new GregorianCalendar());
92 user.getUserContact().setContactCreated(new GregorianCalendar());
94 // Update mobile, land-line and fax instance
95 this.setAllContactPhoneEntriesCreated(user.getUserContact());
98 this.getEntityManager().persist(user);
100 // Flush to get id back
101 this.getEntityManager().flush();
104 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addUser: user={1},user.userId={2} - EXIT!", this.getClass().getSimpleName(), user, user.getUserId())); //NOI18N
111 public void deleteUser (final User user, final String userDeleteReason) throws UserNotFoundException {
113 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteUser: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
115 // user should not be null
118 throw new NullPointerException("user is null"); //NOI18N
119 } else if (user.getUserId() == null) {
121 throw new NullPointerException("user.userId is null"); //NOI18N
122 } else if (user.getUserId() < 1) {
123 // Not valid id number
124 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", user.getUserId())); //NOI18N
125 } else if (user.getUserContact() == null) {
127 throw new NullPointerException("user.userContact is null"); //NOI18N
128 } else if (user.getUserContact().getContactId() == null) {
130 throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
131 } else if (user.getUserContact().getContactId() < 1) {
132 // Not valid id number
133 throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N
134 } else if (user.getUserAccountStatus() == null) {
136 throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
137 } else if (!this.userBean.ifUserExists(user)) {
138 // Name already found
139 throw new UserNotFoundException(user);
142 // Get a managed instance
143 final User managedUser = this.getManagedUser(user);
146 assert (managedUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
149 this.getEntityManager().remove(managedUser);
152 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteUser: EXIT!", this.getClass().getSimpleName())); //NOI18N
156 public User linkUser (final User user) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
158 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkUser: user={0} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
160 // user should not be null
163 throw new NullPointerException("user is null"); //NOI18N
164 } else if (user.getUserId() instanceof Long) {
166 throw new IllegalArgumentException("user.userId is not null"); //NOI18N
167 } else if (user.getUserContact() == null) {
169 throw new NullPointerException("user.userContact is null"); //NOI18N
170 } else if (user.getUserContact().getContactId() == null) {
172 throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
173 } else if (user.getUserContact().getContactId() < 1) {
174 // Not valid id number
175 throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N
176 } else if (user.getUserAccountStatus() == null) {
178 throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
179 } else if (this.userBean.ifUserNameExists(user.getUserName())) {
180 // Name already found
181 throw new UserNameAlreadyRegisteredException(user.getUserName());
184 // Try to find the contact
185 final Contact foundContact = this.getEntityManager().find(user.getUserContact().getClass(), user.getUserContact().getContactId());
187 // Set detached object in rexcruiter instance
188 user.setUserContact(foundContact);
191 user.setUserCreated(new GregorianCalendar());
194 this.getEntityManager().persist(user);
196 // Flush it to get updated instance back
197 this.getEntityManager().flush();
200 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkUser: user={1} - EXIT!", this.getClass().getSimpleName(), user)); //NOI18N
202 // Return updated instanc
207 public User lockUserAccount (final User user, final String userLockReason, final String baseUrl) throws UserStatusLockedException, UserStatusUnconfirmedException, UserNotFoundException {
209 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount: user={1},userLockReason={2},baseUrl={3} - CALLED!", this.getClass().getSimpleName(), user, userLockReason, baseUrl)); //NOI18N
211 // user should not be null
214 throw new NullPointerException("user is null"); //NOI18N
215 } else if (user.getUserId() == null) {
217 throw new NullPointerException("user.userId is null"); //NOI18N
218 } else if (user.getUserId() < 1) {
220 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is null", user.getUserId())); //NOI18N
221 } else if (user.getUserContact() == null) {
223 throw new NullPointerException("user.userContact is null"); //NOI18N
224 } else if (user.getUserContact().getContactId() == null) {
226 throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
227 } else if (user.getUserContact().getContactId() < 1) {
228 // Not valid id number
229 throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N
230 } else if (user.getUserAccountStatus() == null) {
232 throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
233 } else if (!this.userBean.ifUserExists(user)) {
234 // Name already found
235 throw new UserNotFoundException(user);
236 } else if (user.getUserAccountStatus() == UserAccountStatus.LOCKED) {
238 throw new UserStatusLockedException(user);
239 } else if (user.getUserAccountStatus() == UserAccountStatus.UNCONFIRMED) {
240 // Account is unconfirmed
241 throw new UserStatusUnconfirmedException(user);
242 } else if (null == userLockReason) {
244 throw new NullPointerException("userLockReason is null"); //NOI18N
245 } else if (userLockReason.isEmpty()) {
247 throw new IllegalArgumentException("userLockReason is empty"); //NOI18N
250 // Remove contact instance as this is not updated
251 user.setUserContact(null);
253 // Set as locked, set timestamp and lock reason
254 user.setUserAccountStatus(UserAccountStatus.LOCKED);
255 user.setUserLastLocked(new GregorianCalendar());
256 user.setUserLastLockedReason(userLockReason);
259 final User managedUser = this.userBean.updateUserData(user);
261 // @TODO Create user lock history entry
263 // @TODO externalize subject line
264 this.sendEmail("User account locked", "user_account_locked", managedUser, baseUrl, null); //NOI18N
267 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount: managedUser={1} - EXIT!", this.getClass().getSimpleName(), managedUser)); //NOI18N
269 // Return detached (and updated) user
274 public User unlockUserAccount (final User user, final String baseUrl) throws UserStatusConfirmedException, UserStatusUnconfirmedException, UserNotFoundException {
276 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount: user={1},baseUrl={2} - CALLED!", this.getClass().getSimpleName(), user, baseUrl)); //NOI18N
278 // user should not be null
281 throw new NullPointerException("user is null"); //NOI18N
282 } else if (user.getUserId() == null) {
284 throw new NullPointerException("user.userId is null"); //NOI18N
285 } else if (user.getUserId() < 1) {
287 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is null", user.getUserId())); //NOI18N
288 } else if (user.getUserContact() == null) {
290 throw new NullPointerException("user.userContact is null"); //NOI18N
291 } else if (user.getUserContact().getContactId() == null) {
293 throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
294 } else if (user.getUserContact().getContactId() < 1) {
295 // Not valid id number
296 throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N
297 } else if (user.getUserAccountStatus() == null) {
299 throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
300 } else if (!this.userBean.ifUserExists(user)) {
301 // Name already found
302 throw new UserNotFoundException(user);
303 } else if (user.getUserAccountStatus() == UserAccountStatus.CONFIRMED) {
304 // Account is confirmed
305 throw new UserStatusConfirmedException(user);
306 } else if (user.getUserAccountStatus() == UserAccountStatus.UNCONFIRMED) {
307 // Account is unconfirmed
308 throw new UserStatusUnconfirmedException(user);
311 // Remove contact instance as this is not updated
312 user.setUserContact(null);
315 user.setUserAccountStatus(UserAccountStatus.CONFIRMED);
318 final User managedUser = this.userBean.updateUserData(user);
320 // @TODO Create user lock history entry
322 // @TODO externalize subject line
323 this.sendEmail("User account unlocked", "user_account_unlocked", managedUser, baseUrl, null); //NOI18N
326 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount: managedUser={1} - EXIT!", this.getClass().getSimpleName(), managedUser)); //NOI18N
328 // Return changed account