2 * Copyright (C) 2016 Roland Haeder
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 Haeder<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() != null) {
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 User linkUser (final User user) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
115 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkUser: user={0} - 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() instanceof Long) {
123 throw new IllegalArgumentException("user.userId is not null"); //NOI18N
124 } else if (user.getUserContact() == null) {
126 throw new NullPointerException("user.userContact is null"); //NOI18N
127 } else if (user.getUserContact().getContactId() == null) {
129 throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
130 } else if (user.getUserContact().getContactId() < 1) {
131 // Not valid id number
132 throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N
133 } else if (user.getUserAccountStatus() == null) {
135 throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
136 } else if (this.userBean.ifUserNameExists(user.getUserName())) {
137 // Name already found
138 throw new UserNameAlreadyRegisteredException(user.getUserName());
141 // Try to find the contact
142 Contact foundContact = this.getEntityManager().find(user.getUserContact().getClass(), user.getUserContact().getContactId());
144 // Set detached object in rexcruiter instance
145 user.setUserContact(foundContact);
148 user.setUserCreated(new GregorianCalendar());
151 this.getEntityManager().persist(user);
153 // Flush it to get updated instance back
154 this.getEntityManager().flush();
157 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkUser: user={1} - EXIT!", this.getClass().getSimpleName(), user)); //NOI18N
159 // Return updated instanc
164 public User lockUserAccount (final User user, final String userLockReason, final String baseUrl) throws UserStatusLockedException, UserStatusUnconfirmedException, UserNotFoundException {
166 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount: user={1},userLockReason={2},baseUrl={3} - CALLED!", this.getClass().getSimpleName(), user, userLockReason, baseUrl)); //NOI18N
168 // user should not be null
171 throw new NullPointerException("user is null"); //NOI18N
172 } else if (user.getUserId() == null) {
174 throw new NullPointerException("user.userId is null"); //NOI18N
175 } else if (user.getUserId() < 1) {
177 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is null", user.getUserId())); //NOI18N
178 } else if (user.getUserContact() == null) {
180 throw new NullPointerException("user.userContact is null"); //NOI18N
181 } else if (user.getUserContact().getContactId() == null) {
183 throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
184 } else if (user.getUserContact().getContactId() < 1) {
185 // Not valid id number
186 throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N
187 } else if (user.getUserAccountStatus() == null) {
189 throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
190 } else if (!this.userBean.ifUserExists(user)) {
191 // Name already found
192 throw new UserNotFoundException(user);
193 } else if (user.getUserAccountStatus() == UserAccountStatus.LOCKED) {
195 throw new UserStatusLockedException(user);
196 } else if (user.getUserAccountStatus() == UserAccountStatus.UNCONFIRMED) {
197 // Account is unconfirmed
198 throw new UserStatusUnconfirmedException(user);
199 } else if (null == userLockReason) {
201 throw new NullPointerException("userLockReason is null"); //NOI18N
202 } else if (userLockReason.isEmpty()) {
204 throw new IllegalArgumentException("userLockReason is empty"); //NOI18N
207 // Remove contact instance as this is not updated
208 user.setUserContact(null);
211 User foundUser = this.getEntityManager().find(user.getClass(), user.getUserId());
214 assert (foundUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
217 foundUser.copyAll(user);
220 foundUser.setUserAccountStatus(UserAccountStatus.LOCKED);
221 foundUser.setUserLastLocked(new GregorianCalendar());
222 foundUser.setUserLastLockedReason(userLockReason);
225 User detachedUser = this.getEntityManager().merge(foundUser);
228 assert (detachedUser instanceof User) : MessageFormat.format("User with id {0} not merged, but should be.", user.getUserId()); //NOI18N
231 User updatedUser = this.userBean.updateUserData(user);
233 // @TODO Create user lock history entry
235 Address emailAddress;
238 // Create email address and set
239 emailAddress = new InternetAddress(updatedUser.getUserContact().getContactEmailAddress());
240 } catch (final AddressException ex) {
242 throw new EJBException(ex);
246 // @TODO externalize subject line
247 this.sendEmail("Account locked", "account_locked", emailAddress, updatedUser, baseUrl); //NOI18N
250 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount: detachedUser={1} - EXIT!", this.getClass().getSimpleName(), detachedUser)); //NOI18N
252 // Return detached (and updated) user
257 public User unlockUserAccount (final User user, final String baseUrl) throws UserStatusConfirmedException, UserStatusUnconfirmedException, UserNotFoundException {
259 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount: user={1},baseUrl={2} - CALLED!", this.getClass().getSimpleName(), user, baseUrl)); //NOI18N
261 // user should not be null
264 throw new NullPointerException("user is null"); //NOI18N
265 } else if (user.getUserId() == null) {
267 throw new NullPointerException("user.userId is null"); //NOI18N
268 } else if (user.getUserId() < 1) {
270 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is null", user.getUserId())); //NOI18N
271 } else if (user.getUserContact() == null) {
273 throw new NullPointerException("user.userContact is null"); //NOI18N
274 } else if (user.getUserContact().getContactId() == null) {
276 throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
277 } else if (user.getUserContact().getContactId() < 1) {
278 // Not valid id number
279 throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N
280 } else if (user.getUserAccountStatus() == null) {
282 throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
283 } else if (!this.userBean.ifUserExists(user)) {
284 // Name already found
285 throw new UserNotFoundException(user);
286 } else if (user.getUserAccountStatus() == UserAccountStatus.CONFIRMED) {
287 // Account is confirmed
288 throw new UserStatusConfirmedException(user);
289 } else if (user.getUserAccountStatus() == UserAccountStatus.UNCONFIRMED) {
290 // Account is unconfirmed
291 throw new UserStatusUnconfirmedException(user);
294 // Remove contact instance as this is not updated
295 user.setUserContact(null);
298 User foundUser = this.getEntityManager().find(user.getClass(), user.getUserId());
301 assert (foundUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
304 foundUser.copyAll(user);
307 foundUser.setUserAccountStatus(UserAccountStatus.CONFIRMED);
310 User detachedUser = this.getEntityManager().merge(foundUser);
313 assert (detachedUser instanceof User) : MessageFormat.format("User with id {0} not merged, but should be.", user.getUserId()); //NOI18N
316 User updatedUser = this.userBean.updateUserData(user);
318 // @TODO Create user lock history entry
320 Address emailAddress;
323 // Create email address and set
324 emailAddress = new InternetAddress(updatedUser.getUserContact().getContactEmailAddress());
325 } catch (final AddressException ex) {
327 throw new EJBException(ex);
331 // @TODO externalize subject line
332 this.sendEmail("Account unlocked", "account_unlocked", emailAddress, updatedUser, baseUrl); //NOI18N
335 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount: detachedUser={1} - EXIT!", this.getClass().getSimpleName(), detachedUser)); //NOI18N
337 // Return changed account