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