2 * Copyright (C) 2016 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.jcontacts.contact.Contact;
28 import org.mxchange.jjobs.database.BaseJobsDatabaseBean;
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 JobsAdminUserSessionBean extends BaseJobsDatabaseBean implements AdminUserSessionBeanRemote {
49 private static final long serialVersionUID = 542_145_347_916L;
55 private UserRegistrationSessionBeanRemote registerBean;
61 private UserSessionBeanRemote userBean;
66 public JobsAdminUserSessionBean () {
70 public User addUser (final User user) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
72 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addUser: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
74 // user should not be null
77 throw new NullPointerException("user is null"); //NOI18N
78 } else if (user.getUserId() instanceof Long) {
80 throw new IllegalStateException(MessageFormat.format("user.userId must be null, is: {0}", user.getUserId())); //NOI18N
83 // Check if user is registered
84 if (this.registerBean.isUserNameRegistered(user)) {
86 throw new UserNameAlreadyRegisteredException(user);
87 } else if (this.registerBean.isEmailAddressRegistered(user)) {
89 throw new EmailAddressAlreadyRegisteredException(user);
92 // Set created timestamp
93 user.setUserCreated(new GregorianCalendar());
94 user.getUserContact().setContactCreated(new GregorianCalendar());
96 // Update cellphone, land-line and fax instance
97 this.setAllContactPhoneEntriesCreated(user.getUserContact());
100 this.getEntityManager().persist(user);
102 // Flush to get id back
103 this.getEntityManager().flush();
106 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addUser: user={1},user.userId={2} - EXIT!", this.getClass().getSimpleName(), user, user.getUserId())); //NOI18N
113 public void deleteUser (final User user, final String userDeleteReason) throws UserNotFoundException {
115 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteUser: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
117 // user should not be null
120 throw new NullPointerException("user is null"); //NOI18N
121 } else if (user.getUserId() == null) {
123 throw new NullPointerException("user.userId is null"); //NOI18N
124 } else if (user.getUserId() < 1) {
125 // Not valid id number
126 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", user.getUserId())); //NOI18N
127 } else if (user.getUserContact() == null) {
129 throw new NullPointerException("user.userContact is null"); //NOI18N
130 } else if (user.getUserContact().getContactId() == null) {
132 throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
133 } else if (user.getUserContact().getContactId() < 1) {
134 // Not valid id number
135 throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N
136 } else if (user.getUserAccountStatus() == null) {
138 throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
139 } else if (!this.userBean.ifUserExists(user)) {
140 // Name already found
141 throw new UserNotFoundException(user);
144 // Get a managed instance
145 User managedUser = this.getManagedUser(user);
148 assert (managedUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
151 this.getEntityManager().remove(managedUser);
154 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteUser: EXIT!", this.getClass().getSimpleName())); //NOI18N
158 public User linkUser (final User user) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
160 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkUser: user={0} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
162 // user should not be null
165 throw new NullPointerException("user is null"); //NOI18N
166 } else if (user.getUserId() instanceof Long) {
168 throw new IllegalArgumentException("user.userId is not null"); //NOI18N
169 } else if (user.getUserContact() == null) {
171 throw new NullPointerException("user.userContact is null"); //NOI18N
172 } else if (user.getUserContact().getContactId() == null) {
174 throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
175 } else if (user.getUserContact().getContactId() < 1) {
176 // Not valid id number
177 throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N
178 } else if (user.getUserAccountStatus() == null) {
180 throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
181 } else if (this.userBean.ifUserNameExists(user.getUserName())) {
182 // Name already found
183 throw new UserNameAlreadyRegisteredException(user.getUserName());
186 // Try to find the contact
187 Contact managedContact = this.getEntityManager().find(user.getUserContact().getClass(), user.getUserContact().getContactId());
190 assert (managedContact instanceof Contact) : MessageFormat.format("Contact with id {0} not found, but should be.", user.getUserContact().getContactId()); //NOI18N
192 // Set detached object in rexcruiter instance
193 user.setUserContact(managedContact);
196 user.setUserCreated(new GregorianCalendar());
199 this.getEntityManager().persist(user);
201 // Flush it to get updated instance back
202 this.getEntityManager().flush();
205 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkUser: user={1} - EXIT!", this.getClass().getSimpleName(), user)); //NOI18N
207 // Return updated instanc
212 public User lockUserAccount (final User user, final String userLockReason, final String baseUrl) throws UserStatusLockedException, UserStatusUnconfirmedException, UserNotFoundException {
214 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount: user={1},userLockReason={2},baseUrl={3} - CALLED!", this.getClass().getSimpleName(), user, userLockReason, baseUrl)); //NOI18N
216 // user should not be null
219 throw new NullPointerException("user is null"); //NOI18N
220 } else if (user.getUserId() == null) {
222 throw new NullPointerException("user.userId is null"); //NOI18N
223 } else if (user.getUserId() < 1) {
225 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is null", user.getUserId())); //NOI18N
226 } else if (user.getUserContact() == null) {
228 throw new NullPointerException("user.userContact is null"); //NOI18N
229 } else if (user.getUserContact().getContactId() == null) {
231 throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
232 } else if (user.getUserContact().getContactId() < 1) {
233 // Not valid id number
234 throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N
235 } else if (user.getUserAccountStatus() == null) {
237 throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
238 } else if (!this.userBean.ifUserExists(user)) {
239 // Name already found
240 throw new UserNotFoundException(user);
241 } else if (user.getUserAccountStatus() == UserAccountStatus.LOCKED) {
243 throw new UserStatusLockedException(user);
244 } else if (user.getUserAccountStatus() == UserAccountStatus.UNCONFIRMED) {
245 // Account is unconfirmed
246 throw new UserStatusUnconfirmedException(user);
247 } else if (null == userLockReason) {
249 throw new NullPointerException("userLockReason is null"); //NOI18N
250 } else if (userLockReason.isEmpty()) {
252 throw new IllegalArgumentException("userLockReason is empty"); //NOI18N
255 // Remove contact instance as this is not updated
256 user.setUserContact(null);
258 // Set as locked, set timestamp and lock reason
259 user.setUserAccountStatus(UserAccountStatus.LOCKED);
260 user.setUserLastLocked(new GregorianCalendar());
261 user.setUserLastLockedReason(userLockReason);
264 User managedUser = this.userBean.updateUserData(user);
267 Address emailAddress;
270 // Create email address and set
271 emailAddress = new InternetAddress(managedUser.getUserContact().getContactEmailAddress());
272 } catch (final AddressException ex) {
274 throw new EJBException(ex);
278 // @TODO externalize subject line
279 this.sendEmail("Account locked", "account_locked", emailAddress, managedUser, baseUrl); //NOI18N
282 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount: managedUser={1} - EXIT!", this.getClass().getSimpleName(), managedUser)); //NOI18N
284 // Return detached (and updated) user
289 public User unlockUserAccount (final User user, final String baseUrl) throws UserStatusConfirmedException, UserStatusUnconfirmedException, UserNotFoundException {
291 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount: user={1},baseUrl={2} - CALLED!", this.getClass().getSimpleName(), user, baseUrl)); //NOI18N
293 // user should not be null
296 throw new NullPointerException("user is null"); //NOI18N
297 } else if (user.getUserId() == null) {
299 throw new NullPointerException("user.userId is null"); //NOI18N
300 } else if (user.getUserId() < 1) {
302 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is null", user.getUserId())); //NOI18N
303 } else if (user.getUserContact() == null) {
305 throw new NullPointerException("user.userContact is null"); //NOI18N
306 } else if (user.getUserContact().getContactId() == null) {
308 throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
309 } else if (user.getUserContact().getContactId() < 1) {
310 // Not valid id number
311 throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N
312 } else if (user.getUserAccountStatus() == null) {
314 throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
315 } else if (!this.userBean.ifUserExists(user)) {
316 // Name already found
317 throw new UserNotFoundException(user);
318 } else if (user.getUserAccountStatus() == UserAccountStatus.CONFIRMED) {
319 // Account is confirmed
320 throw new UserStatusConfirmedException(user);
321 } else if (user.getUserAccountStatus() == UserAccountStatus.UNCONFIRMED) {
322 // Account is unconfirmed
323 throw new UserStatusUnconfirmedException(user);
326 // Remove contact instance as this is not updated
327 user.setUserContact(null);
330 user.setUserAccountStatus(UserAccountStatus.CONFIRMED);
333 User managedUser = this.userBean.updateUserData(user);
336 Address emailAddress;
339 // Create email address and set
340 emailAddress = new InternetAddress(managedUser.getUserContact().getContactEmailAddress());
341 } catch (final AddressException ex) {
343 throw new EJBException(ex);
347 // @TODO externalize subject line
348 this.sendEmail("Account unlocked", "account_unlocked", emailAddress, managedUser, baseUrl); //NOI18N
351 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount: managedUser={1} - EXIT!", this.getClass().getSimpleName(), managedUser)); //NOI18N
353 // Return changed account