]> git.mxchange.org Git - addressbook-mailer-ejb.git/blob - src/java/org/mxchange/jusercore/model/user/AddressbookAdminUserSessionBean.java
9352fe244e56dbbfcc2fe5485f56feee50ab3578
[addressbook-mailer-ejb.git] / src / java / org / mxchange / jusercore / model / user / AddressbookAdminUserSessionBean.java
1 /*
2  * Copyright (C) 2016 Cho-Time GmbH
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.jusercore.model.user;
18
19 import java.text.MessageFormat;
20 import java.util.GregorianCalendar;
21 import javax.ejb.EJB;
22 import javax.ejb.Stateless;
23 import org.mxchange.addressbook.database.BaseAddressbookDatabaseBean;
24 import org.mxchange.jcontacts.contact.Contact;
25 import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
26 import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
27 import org.mxchange.jusercore.model.register.UserRegistrationSessionBeanRemote;
28
29 /**
30  * An administrative user EJB
31  * <p>
32  * @author Roland Haeder<rhaeder@cho-time.de>
33  */
34 @Stateless (name = "user", description = "A bean handling the user data")
35 public class AddressbookAdminUserSessionBean extends BaseAddressbookDatabaseBean implements AdminUserSessionBeanRemote {
36
37         /**
38          * Serial number
39          */
40         private static final long serialVersionUID = 542_145_347_916L;
41
42         /**
43          * Registration EJB
44          */
45         @EJB
46         private UserRegistrationSessionBeanRemote registerBean;
47
48         /**
49          * Regular user bean
50          */
51         @EJB
52         private UserSessionBeanRemote userBean;
53
54         /**
55          * Default constructor
56          */
57         public AddressbookAdminUserSessionBean () {
58         }
59
60         @Override
61         public User addUser (final User user) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
62                 // Trace message
63                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addUser: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
64
65                 // user should not be null
66                 if (null == user) {
67                         // Abort here
68                         throw new NullPointerException("user is null"); //NOI18N
69                 } else if (user.getUserId() != null) {
70                         // Not allowed here
71                         throw new IllegalStateException(MessageFormat.format("user.userId must be null, is: {0}", user.getUserId())); //NOI18N
72                 }
73
74                 // Check if user is registered
75                 if (this.registerBean.isUserNameRegistered(user)) {
76                         // Abort here
77                         throw new UserNameAlreadyRegisteredException(user);
78                 } else if (this.registerBean.isEmailAddressRegistered(user)) {
79                         // Abort here
80                         throw new EmailAddressAlreadyRegisteredException(user);
81                 }
82
83                 // Set created timestamp
84                 user.setUserCreated(new GregorianCalendar());
85                 user.getUserContact().setContactCreated(new GregorianCalendar());
86
87                 // Update cellphone, land-line and fax instance
88                 this.setAllContactPhoneEntriesCreated(user.getUserContact());
89
90                 // Persist it
91                 this.getEntityManager().persist(user);
92
93                 // Flush to get id back
94                 this.getEntityManager().flush();
95
96                 // Trace message
97                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addUser: user={1},user.userId={2} - EXIT!", this.getClass().getSimpleName(), user, user.getUserId())); //NOI18N
98
99                 // Return it
100                 return user;
101         }
102
103         @Override
104         public User linkUser (final User user) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
105                 // Trace message
106                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkUser: user={0} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
107
108                 // user should not be null
109                 if (null == user) {
110                         // Abort here
111                         throw new NullPointerException("user is null"); //NOI18N
112                 } else if (user.getUserId() instanceof Long) {
113                         // Id is set
114                         throw new IllegalArgumentException("user.userId is not null"); //NOI18N
115                 } else if (user.getUserContact() == null) {
116                         // Throw NPE again
117                         throw new NullPointerException("user.userContact is null"); //NOI18N
118                 } else if (user.getUserContact().getContactId() == null) {
119                         // Throw NPE again
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) {
125                         // Throw NPE again
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());
130                 }
131
132                 // Try to find the contact
133                 Contact foundContact = this.getEntityManager().find(user.getUserContact().getClass(), user.getUserContact().getContactId());
134
135                 // Set detached object in rexcruiter instance
136                 user.setUserContact(foundContact);
137
138                 // Set timestamp
139                 user.setUserCreated(new GregorianCalendar());
140
141                 // Perist it
142                 this.getEntityManager().persist(user);
143
144                 // Flush it to get updated instance back
145                 this.getEntityManager().flush();
146
147                 // Log trace message
148                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkUser: user={1} - EXIT!", this.getClass().getSimpleName(), user)); //NOI18N
149
150                 // Return updated instanc
151                 return user;
152         }
153
154 }