45bbdf6fd2f6201892a6bf24762454393fc4f025
[cdm-vaadin.git] / src / main / java / eu / etaxonomy / cdm / vaadin / view / AccountRegistrationViewBean.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.vaadin.spring.events.EventBus;
14
15 import com.vaadin.data.validator.RegexpValidator;
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.TextField;
25 import com.vaadin.ui.VerticalLayout;
26 import com.vaadin.ui.themes.ValoTheme;
27
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=AccountRegistrationViewBean.NAME)
38 public class AccountRegistrationViewBean
39 extends AbstractView<AccountRegistrationView,AccountRegistrationPresenter>
40 implements AccountRegistrationView, View {
41
42 private static final long serialVersionUID = 2066153054170511405L;
43
44 @Autowired
45 private EventBus.UIEventBus uiEventBus;
46
47 public static final String NAME = "accountCreation";
48
49 private Label header = new Label();
50
51 private TextField emailAddress = new TextField("Email address");
52
53 private TextField userName = new TextField("User name");
54
55 private PasswordField password1Field = new PasswordField ("Password");
56
57 private PasswordField password2Field = new PasswordField("Repeat password");
58
59 private TextField prefix = new TextField("Name prefix");
60
61 private TextField familyName = new TextField("Family name");
62
63 private TextField givenName = new TextField("Given name");
64
65 private Button registerButton = new Button("Register user account");
66
67 private Label messageLabel = new Label();
68
69 @Override
70 protected void initContent() {
71 VerticalLayout root = new VerticalLayout();
72
73 FormLayout formLayout = new FormLayout();
74 formLayout.addComponents(header, userName, password1Field, password2Field, prefix, givenName, familyName, registerButton, messageLabel);
75 formLayout.setComponentAlignment(messageLabel, Alignment.MIDDLE_CENTER);
76 formLayout.setMargin(true);
77 formLayout.setSizeUndefined();
78
79 header.setValue("Register a user account");
80 header.setStyleName(ValoTheme.LABEL_H3);
81 registerButton.setStyleName(ValoTheme.BUTTON_PRIMARY);
82 registerButton.setEnabled(false);
83 registerButton.setWidth(100, Unit.PERCENTAGE);
84 registerButton.addClickListener(e -> {
85 getViewEventBus().publish(this, new UserAccountEvent(UserAccountEvent.UserAccountAction.REGISTER_ACCOUNT, e));
86 });
87 messageLabel.setCaptionAsHtml(true);
88 messageLabel.setVisible(false);
89
90 setCompositionRoot(root);
91 root.addComponent(formLayout);
92 root.setComponentAlignment(formLayout, Alignment.MIDDLE_CENTER);
93
94 root.setSizeFull();
95
96 userName.setRequired(true);
97 password1Field.setRequired(true);
98 password2Field.setRequired(true);
99
100 userName.addValidator(new RegexpValidator("[A-Za-z0-9_\\.\\-]{3,}", true, "User names may contain the characters \"A-Z a-z 0-9 _ . -\", and must be at least 3 characters long."));
101 password1Field.addValidator(new PasswordsPolicyValidator());
102 password2Field.addValidator(new PasswordsMatchValidator("The passwords are not identical.", password1Field, password2Field));
103 password1Field.addValueChangeListener(e -> updateResetButtonState());
104 password2Field.addValueChangeListener(e -> updateResetButtonState());
105
106 // value will be set by presenter and user must not change it here
107 emailAddress.setEnabled(false);
108 emailAddress.setReadOnly(true);
109
110
111 prefix.setInputPrompt("Dr., Prof, ...");
112 }
113
114 private void updateResetButtonState() {
115 registerButton.setEnabled(StringUtils.isNoneBlank(password1Field.getValue()) && password1Field.getErrorMessage() == null && password2Field.getErrorMessage() == null);
116 }
117
118 @Override
119 // TODO pull up to AbstractView and let AbstractView implement View?
120 public void enter(ViewChangeEvent event) {
121 getPresenter().onViewEnter();
122 }
123
124 @Override
125 public void showErrorMessage(String text) {
126 messageLabel.setValue(text);
127 messageLabel.setStyleName(ValoTheme.LABEL_FAILURE);
128 messageLabel.setVisible(true);
129 disableForm();
130 }
131
132 @Override
133 public void showSuccessMessage(String text) {
134 messageLabel.setValue(text);
135 messageLabel.setStyleName(ValoTheme.LABEL_SUCCESS);
136 messageLabel.setVisible(true);
137 disableForm();
138 }
139
140 public void disableForm() {
141 password1Field.setEnabled(false);
142 password2Field.setEnabled(false);
143 registerButton.setEnabled(false);
144 }
145
146 @Override
147 public TextField getUserName() {
148 return userName;
149 }
150
151 @Override
152 public PasswordField getPassword1Field() {
153 return password1Field;
154 }
155
156 @Override
157 public TextField getEmailAddress() {
158 return emailAddress;
159 }
160
161 @Override
162 public TextField getPrefix() {
163 return prefix;
164 }
165
166 @Override
167 public TextField getFamilyName() {
168 return familyName;
169 }
170
171 @Override
172 public TextField getGivenName() {
173 return givenName;
174 }
175
176
177 }