]> git.mxchange.org Git - addressbook-war.git/blob - src/java/org/mxchange/addressbook/beans/email_address/AddressbookEmailChangeWebSessionBean.java
Continued with fixes/rewrites:
[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                 // Check if the email address is already enqueued
126                 if (this.isEmailAddressQueued(this.getEmailAddress())) {
127                         // Yes, then abort here
128                         return "login_email_already_added"; //NOI18N
129                 }
130
131                 // Create change object, to save EJB calls, the hash is not generated here
132                 ChangeableEmailAddress emailChange = new EmailAddressChange(user, this.getEmailAddress());
133
134                 // Call EJB
135                 this.emailBean.enqueueEmailAddressForChange(emailChange);
136
137                 // Unset all so the user is forced to re-enter it
138                 this.clear();
139
140                 // All fine
141                 return "login_email_change_queued"; //NOI18N
142         }
143
144         @Override
145         public String getEmailAddress () {
146                 return this.emailAddress;
147         }
148
149         @Override
150         public void setEmailAddress (final String emailAddress) {
151                 this.emailAddress = emailAddress;
152         }
153
154         @Override
155         public String getEmailAddressRepeat () {
156                 return this.emailAddressRepeat;
157         }
158
159         @Override
160         public void setEmailAddressRepeat (final String emailAddressRepeat) {
161                 this.emailAddressRepeat = emailAddressRepeat;
162         }
163
164         @Override
165         public boolean isRequiredChangeEmailAddressSet () {
166                 return ((this.getEmailAddress() != null) &&
167                                 (this.getEmailAddressRepeat() != null));
168         }
169
170         /**
171          * Clears email address fields so the user has to re-enter them
172          */
173         private void clear () {
174                 // Clear fields
175                 this.setEmailAddress(null);
176                 this.setEmailAddressRepeat(null);
177         }
178
179         /**
180          * Checks if given email address has already been queued. First a local list
181          * is being checked, if not found, the EJB is called. Only if found, the
182          * result is "cached" in the list.
183          * <p>
184          * @param emailAddress Email address to verify
185          * <p>
186          * @return Whether the email address in field emailAddress is already queued
187          */
188         private boolean isEmailAddressQueued (final String emailAddress) {
189                 // It should be there
190                 assert (emailAddress != null) : "emailAddress should not be null"; //NOI18N
191                 assert (!emailAddress.trim().isEmpty()) : "emailAddress should not be empty"; //NOI18N
192
193                 // Check list
194                 if (this.emailAddresses.contains(emailAddress)) {
195                         // Okay, found it
196                         return true;
197                 }
198
199                 // Check EJB
200                 boolean isQueued = this.emailBean.isEmailAddressEnqueued(emailAddress);
201
202                 // Is it there?
203                 if (isQueued) {
204                         // Add to list
205                         this.emailAddresses.add(emailAddress);
206                 }
207
208                 // Return status
209                 return isQueued;
210         }
211
212 }