cleanup
[cdm-vaadin.git] / src / main / java / eu / etaxonomy / cdm / vaadin / view / PasswordResetViewBean.java
1 /**
2 * Copyright (C) 2021 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
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.
8 */
9 package eu.etaxonomy.cdm.vaadin.view;
10
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;
15
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;
26
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;
32
33 /**
34 * @author a.kohlbecker
35 * @since Nov 11, 2021
36 */
37 @SpringView(name=PasswordResetViewBean.NAME)
38 public class PasswordResetViewBean extends AbstractView<PasswordResetPresenter> implements PasswordResetView, View {
39
40 private static final long serialVersionUID = -1857662031516326300L;
41
42 @Autowired
43 protected EventBus.UIEventBus uiEventBus;
44
45 @Autowired
46 protected Environment env;
47
48 public static final String NAME = "passwordReset";
49
50 private Label header = new Label();
51
52 private PasswordField password1Field = new PasswordField ("New password");
53
54 private PasswordField password2Field = new PasswordField("Repeat password");
55
56 private Button resetButton = new Button("Set new password");
57
58 private Label messageLabel = new Label();
59
60 private String userName;
61
62 @Override
63 protected void initContent() {
64 VerticalLayout root = new VerticalLayout();
65
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();
71
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));
79 });
80 messageLabel.setCaptionAsHtml(true);
81 messageLabel.setVisible(false);
82
83 setCompositionRoot(root);
84 root.addComponent(formLayout);
85 root.setComponentAlignment(formLayout, Alignment.MIDDLE_CENTER);
86
87 root.setSizeFull();
88
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());
93 }
94
95 private void updateResetButtonState() {
96 resetButton.setEnabled(StringUtils.isNoneBlank(password1Field.getValue()) && password1Field.getErrorMessage() == null && password2Field.getErrorMessage() == null);
97 }
98
99 @Override
100 // TODO pull up to AbstractView and let AbstractView implement View?
101 public void enter(ViewChangeEvent event) {
102 getPresenter().onViewEnter();
103 }
104
105 @Override
106 public void showErrorMessage(String text) {
107 messageLabel.setValue(text);
108 messageLabel.setStyleName(ValoTheme.LABEL_FAILURE);
109 messageLabel.setVisible(true);
110 disableForm();
111 }
112
113 @Override
114 public void showSuccessMessage(String text) {
115 messageLabel.setValue(text);
116 messageLabel.setStyleName(ValoTheme.LABEL_SUCCESS);
117 messageLabel.setVisible(true);
118 disableForm();
119 }
120
121 public void disableForm() {
122 password1Field.setEnabled(false);
123 password2Field.setEnabled(false);
124 resetButton.setEnabled(false);
125 }
126
127 public String getUserName() {
128 return userName;
129 }
130
131 @Override
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);
136 }
137
138 @Override
139 public PasswordField getPassword1Field() {
140 return password1Field;
141 }
142 }