]> git.mxchange.org Git - addressbook-war.git/blob - src/java/org/mxchange/addressbook/beans/email_address/AddressbookEmailChangeWebSessionBean.java
Please cherry-pick:
[addressbook-war.git] / src / java / org / mxchange / addressbook / beans / email_address / AddressbookEmailChangeWebSessionBean.java
1 /*
2  * Copyright (C) 2016 Roland Häder
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.addressbook.beans.email_address;
18
19 import java.text.MessageFormat;
20 import java.util.List;
21 import java.util.Objects;
22 import javax.annotation.PostConstruct;
23 import javax.enterprise.context.SessionScoped;
24 import javax.faces.view.facelets.FaceletException;
25 import javax.inject.Inject;
26 import javax.inject.Named;
27 import javax.naming.Context;
28 import javax.naming.InitialContext;
29 import javax.naming.NamingException;
30 import org.mxchange.addressbook.beans.BaseAddressbookController;
31 import org.mxchange.addressbook.beans.features.AddressbookFeaturesWebApplicationController;
32 import org.mxchange.addressbook.beans.login.AddressbookUserLoginWebSessionController;
33 import org.mxchange.jcontacts.contact.Contact;
34 import org.mxchange.jcoreee.utils.FacesUtils;
35 import org.mxchange.jusercore.exceptions.UserPasswordMismatchException;
36 import org.mxchange.jusercore.model.email_address.ChangeableEmailAddress;
37 import org.mxchange.jusercore.model.email_address.EmailAddressChange;
38 import org.mxchange.jusercore.model.email_address.UserEmailChangeSessionBeanRemote;
39 import org.mxchange.jusercore.model.user.User;
40
41 /**
42  * A web session bean for changing email addresses
43  * <p>
44  * @author Roland Häder<roland@mxchange.org>
45  */
46 @Named ("emailChangeController")
47 @SessionScoped
48 public class AddressbookEmailChangeWebSessionBean extends BaseAddressbookController implements AddressbookEmailChangeWebSessionController {
49
50         /**
51          * Serial number
52          */
53         private static final long serialVersionUID = 186_078_724_659_153L;
54
55         /**
56          * Email address 1 (changing)
57          */
58         private String emailAddress;
59
60         /**
61          * Email address 2 (repeat in changing)
62          */
63         private String emailAddressRepeat;
64
65         /**
66          * Local list of already queued email addresses
67          */
68         private List<String> emailAddresses;
69
70         /**
71          * Remote email change bean
72          */
73         private UserEmailChangeSessionBeanRemote emailChangeBean;
74
75         /**
76          * Features controller
77          */
78         @Inject
79         private AddressbookFeaturesWebApplicationController featureController;
80
81         /**
82          * Login controller (bean)
83          */
84         @Inject
85         private AddressbookUserLoginWebSessionController userLoginController;
86
87         /**
88          * Default constructor
89          */
90         public AddressbookEmailChangeWebSessionBean () {
91         }
92
93         @Override
94         public String doUserChangeEmailAddress () {
95                 // This method shall only be called if the user is logged-in
96                 if (!this.userLoginController.isUserLoggedIn()) {
97                         // Not logged-in
98                         throw new IllegalStateException("User is not logged-in"); //NOI18N
99                 } else if (!this.featureController.isFeatureEnabled("user_change_email_address")) { //NOI18N
100                         // Editing is not allowed
101                         throw new IllegalStateException("User tried to change email address"); //NOI18N
102                 } else if (!this.isRequiredChangeEmailAddressSet()) {
103                         // Not all required fields are set
104                         throw new FaceletException("Not all required fields are set."); //NOI18N
105                 } else if (!Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat())) {
106                         // Email address 1+2 mismatch
107                         this.showFacesMessage("form_user_change_email_address:emailAddressRepeat", "ERROR_USER_EMAIL_ADDRESSES_MISMATCH"); //NOI18N
108                         return ""; //NOI18N
109                 } else if (!this.userLoginController.ifCurrentPasswordMatches()) {
110                         // Password not matching
111                         this.showFacesMessage("form_login_user_change_email_address:currentPassword", new UserPasswordMismatchException(this.userLoginController.getLoggedInUser())); //NOI18N
112                         return ""; //NOI18N
113                 }
114
115                 // Get user instance
116                 User user = this.userLoginController.getLoggedInUser();
117
118                 // It should be there, so run some tests on it
119                 assert (user instanceof User) : "Instance userLoginController.loggedInUser is null"; //NOI18N
120                 assert (user.getUserId() instanceof Long) : "Instance userLoginController.loggedInUser.userId is null"; //NOI18N
121                 assert (user.getUserId() > 0) : MessageFormat.format("userLoginController.loggedInUser.userId={0} is invalid", user.getUserId()); //NOI18N
122                 assert (user.getUserContact() instanceof Contact) : "Instance userLoginController.loggedInUser.userContact is null"; //NOI18N
123                 assert (user.getUserContact().getContactId() instanceof Long) : "Instance userLoginController.userContact.contactId is null"; //NOI18N
124                 assert (user.getUserContact().getContactId() > 0) : MessageFormat.format("Instance userLoginController.userContact.contactId={0} is invalid", user.getUserContact().getContactId()); //NOI18N
125
126                 // Check if the email address is already enqueued
127                 if (this.isEmailAddressQueued(this.getEmailAddress())) {
128                         // Clear both email addresses
129                         this.setEmailAddress(null);
130                         this.setEmailAddressRepeat(null);
131
132                         // Yes, then abort here
133                         this.showFacesMessage("form_user_change_email_address:emailAddress", "ERROR_USER_CHANGE_EMAIL_ADDRESS_ALREADY_QUEUED"); //NOI18N
134                         return ""; //NOI18N
135                 }
136
137                 // Create change object, to save EJB calls, the hash is not generated here
138                 ChangeableEmailAddress emailChange = new EmailAddressChange(user, this.getEmailAddress());
139
140                 // Get base URL
141                 String baseUrl = FacesUtils.generateBaseUrl();
142
143                 // Call EJB
144                 this.emailChangeBean.enqueueEmailAddressForChange(emailChange, baseUrl);
145
146                 // Unset all so the user is forced to re-enter it
147                 this.clear();
148
149                 // All fine
150                 return "user_login_email_change_queued"; //NOI18N
151         }
152
153         @Override
154         public String getEmailAddress () {
155                 return this.emailAddress;
156         }
157
158         @Override
159         public void setEmailAddress (final String emailAddress) {
160                 this.emailAddress = emailAddress;
161         }
162
163         @Override
164         public String getEmailAddressRepeat () {
165                 return this.emailAddressRepeat;
166         }
167
168         @Override
169         public void setEmailAddressRepeat (final String emailAddressRepeat) {
170                 this.emailAddressRepeat = emailAddressRepeat;
171         }
172
173         /**
174          * Post-construction
175          */
176         @PostConstruct
177         public void init () {
178                 // Try it
179                 try {
180                         // Get initial context
181                         Context context = new InitialContext();
182
183                         // Try to lookup
184                         this.emailChangeBean = (UserEmailChangeSessionBeanRemote) context.lookup("java:global/addressbook-ejb/userEmailChange!org.mxchange.jusercore.model.email_address.EmailChangeSessionBeanRemote"); //NOI18N
185                 } catch (final NamingException e) {
186                         // Throw again
187                         throw new FaceletException(e);
188                 }
189
190                 // Init list
191                 this.emailAddresses = this.emailChangeBean.allQueuedAddresses();
192         }
193
194         @Override
195         public boolean isRequiredChangeEmailAddressSet () {
196                 return ((this.getEmailAddress() != null) &&
197                                 (this.getEmailAddressRepeat() != null));
198         }
199
200         /**
201          * Clears email address fields so the user has to re-enter them
202          */
203         private void clear () {
204                 // Clear fields
205                 this.setEmailAddress(null);
206                 this.setEmailAddressRepeat(null);
207         }
208
209         /**
210          * Checks if given email address has already been queued. First a local list
211          * is being checked, if not found, the EJB is called. Only if found, the
212          * result is "cached" in the list.
213          * <p>
214          * @param emailAddress Email address to verify
215          * <p>
216          * @return Whether the email address in field emailAddress is already queued
217          */
218         private boolean isEmailAddressQueued (final String emailAddress) {
219                 // It should be there
220                 assert (emailAddress != null) : "emailAddress should not be null"; //NOI18N
221                 assert (!emailAddress.trim().isEmpty()) : "emailAddress should not be empty"; //NOI18N
222
223                 // Check list
224                 if (this.emailAddresses.contains(emailAddress)) {
225                         // Okay, found it
226                         return true;
227                 }
228
229                 // Check EJB
230                 boolean isQueued = this.emailChangeBean.isEmailAddressEnqueued(emailAddress);
231
232                 // Is it there?
233                 if (isQueued) {
234                         // Add to list
235                         this.emailAddresses.add(emailAddress);
236                 }
237
238                 // Return status
239                 return isQueued;
240         }
241
242 }