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