2 * Copyright (C) 2021 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
9 package eu
.etaxonomy
.cdm
.vaadin
.view
;
11 import org
.apache
.commons
.lang3
.StringUtils
;
12 import org
.springframework
.beans
.factory
.annotation
.Autowired
;
13 import org
.springframework
.core
.env
.Environment
;
14 import org
.vaadin
.spring
.events
.EventBus
;
16 import com
.vaadin
.navigator
.View
;
17 import com
.vaadin
.navigator
.ViewChangeListener
.ViewChangeEvent
;
18 import com
.vaadin
.spring
.annotation
.SpringView
;
19 import com
.vaadin
.ui
.Alignment
;
20 import com
.vaadin
.ui
.Button
;
21 import com
.vaadin
.ui
.FormLayout
;
22 import com
.vaadin
.ui
.Label
;
23 import com
.vaadin
.ui
.PasswordField
;
24 import com
.vaadin
.ui
.VerticalLayout
;
25 import com
.vaadin
.ui
.themes
.ValoTheme
;
27 import eu
.etaxonomy
.cdm
.api
.config
.CdmConfigurationKeys
;
28 import eu
.etaxonomy
.cdm
.vaadin
.data
.validator
.PasswordsMatchValidator
;
29 import eu
.etaxonomy
.cdm
.vaadin
.data
.validator
.PasswordsPolicyValidator
;
30 import eu
.etaxonomy
.cdm
.vaadin
.event
.UserAccountEvent
;
31 import eu
.etaxonomy
.vaadin
.mvp
.AbstractView
;
34 * @author a.kohlbecker
37 @SpringView(name
=PasswordResetViewBean
.NAME
)
38 public class PasswordResetViewBean
extends AbstractView
<PasswordResetPresenter
> implements PasswordResetView
, View
{
40 private static final long serialVersionUID
= -1857662031516326300L;
43 protected EventBus
.UIEventBus uiEventBus
;
46 protected Environment env
;
48 public static final String NAME
= "passwordReset";
50 private Label header
= new Label();
52 private PasswordField password1Field
= new PasswordField ("New password");
54 private PasswordField password2Field
= new PasswordField("Repeat password");
56 private Button resetButton
= new Button("Set new password");
58 private Label messageLabel
= new Label();
60 private String userName
;
63 protected void initContent() {
64 VerticalLayout root
= new VerticalLayout();
66 FormLayout formLayout
= new FormLayout();
67 formLayout
.addComponents(header
, password1Field
, password2Field
, resetButton
, messageLabel
);
68 formLayout
.setComponentAlignment(messageLabel
, Alignment
.MIDDLE_CENTER
);
69 formLayout
.setMargin(true);
70 formLayout
.setSizeUndefined();
72 header
.setValue("Reset your password ...");
73 header
.setStyleName(ValoTheme
.LABEL_H3
);
74 resetButton
.setStyleName(ValoTheme
.BUTTON_PRIMARY
);
75 resetButton
.setEnabled(false);
76 resetButton
.setWidth(100, Unit
.PERCENTAGE
);
77 resetButton
.addClickListener(e
-> {
78 getViewEventBus().publish(this, new UserAccountEvent(UserAccountEvent
.UserAccountAction
.RESET_PASSWORD
,e
));
80 messageLabel
.setCaptionAsHtml(true);
81 messageLabel
.setVisible(false);
83 setCompositionRoot(root
);
84 root
.addComponent(formLayout
);
85 root
.setComponentAlignment(formLayout
, Alignment
.MIDDLE_CENTER
);
89 password1Field
.addValidator(new PasswordsPolicyValidator());
90 password2Field
.addValidator(new PasswordsMatchValidator("The passwords are not identical.", password1Field
, password2Field
));
91 password1Field
.addValueChangeListener(e
-> updateResetButtonState());
92 password2Field
.addValueChangeListener(e
-> updateResetButtonState());
95 private void updateResetButtonState() {
96 resetButton
.setEnabled(StringUtils
.isNoneBlank(password1Field
.getValue()) && password1Field
.getErrorMessage() == null && password2Field
.getErrorMessage() == null);
100 // TODO pull up to AbstractView and let AbstractView implement View?
101 public void enter(ViewChangeEvent event
) {
102 getPresenter().onViewEnter();
106 public void showErrorMessage(String text
) {
107 messageLabel
.setValue(text
);
108 messageLabel
.setStyleName(ValoTheme
.LABEL_FAILURE
);
109 messageLabel
.setVisible(true);
114 public void showSuccessMessage(String text
) {
115 messageLabel
.setValue(text
);
116 messageLabel
.setStyleName(ValoTheme
.LABEL_SUCCESS
);
117 messageLabel
.setVisible(true);
121 public void disableForm() {
122 password1Field
.setEnabled(false);
123 password2Field
.setEnabled(false);
124 resetButton
.setEnabled(false);
127 public String
getUserName() {
132 public void setUserName(String userName
) {
133 this.userName
= userName
;
134 String dataSourceID
= env
.getProperty(CdmConfigurationKeys
.CDM_DATA_SOURCE_ID
);
135 header
.setValue("Reset your password for " + userName
+ " at " + dataSourceID
);
139 public PasswordField
getPassword1Field() {
140 return password1Field
;