2 * Copyright (C) 2016 Cho-Time GmbH
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.model.register.UserRegistrationSessionBeanRemote;
30 * An administrative user EJB
32 * @author Roland Haeder<rhaeder@cho-time.de>
34 @Stateless (name = "user", description = "A bean handling the user data")
35 public class JobsAdminUserSessionBean extends BaseJobsDatabaseBean implements AdminUserSessionBeanRemote {
40 private static final long serialVersionUID = 542_145_347_916L;
46 private UserRegistrationSessionBeanRemote registerBean;
52 private UserSessionBeanRemote userBean;
57 public JobsAdminUserSessionBean () {
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
65 // user should not be null
68 throw new NullPointerException("user is null"); //NOI18N
69 } else if (user.getUserId() != null) {
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.registerBean.isUserNameRegistered(user)) {
77 throw new UserNameAlreadyRegisteredException(user);
78 } else if (this.registerBean.isEmailAddressRegistered(user)) {
80 throw new EmailAddressAlreadyRegisteredException(user);
83 // Set created timestamp
84 user.setUserCreated(new GregorianCalendar());
85 user.getUserContact().setContactCreated(new GregorianCalendar());
87 // Update cellphone, land-line and fax instance
88 this.setAllContactPhoneEntriesCreated(user.getUserContact());
91 this.getEntityManager().persist(user);
93 // Flush to get id back
94 this.getEntityManager().flush();
97 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addUser: user={1},user.userId={2} - EXIT!", this.getClass().getSimpleName(), user, user.getUserId())); //NOI18N
104 public User linkUser (final User user) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
106 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkUser: user={0} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
108 // user should not be null
111 throw new NullPointerException("user is null"); //NOI18N
112 } else if (user.getUserId() instanceof Long) {
114 throw new IllegalArgumentException("user.userId is not null"); //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.ifUserNameExists(user.getUserName())) {
128 // Name already found
129 throw new UserNameAlreadyRegisteredException(user.getUserName());
132 // Try to find the contact
133 Contact foundContact = this.getEntityManager().find(user.getUserContact().getClass(), user.getUserContact().getContactId());
135 // Set detached object in rexcruiter instance
136 user.setUserContact(foundContact);
139 user.setUserCreated(new GregorianCalendar());
142 this.getEntityManager().persist(user);
144 // Flush it to get updated instance back
145 this.getEntityManager().flush();
148 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkUser: user={1} - EXIT!", this.getClass().getSimpleName(), user)); //NOI18N
150 // Return updated instanc