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