ref #9877, ref #9498 LoginDialog Tab for registration request ready and account regis...
[cdm-vaadin.git] / src / main / java / eu / etaxonomy / cdm / vaadin / view / LoginViewBean.java
1 /**
2 * Copyright (C) 2017 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.vaadin.spring.events.EventScope;
13
14 import com.vaadin.data.validator.EmailValidator;
15 import com.vaadin.data.validator.StringLengthValidator;
16 import com.vaadin.event.ShortcutAction.KeyCode;
17 import com.vaadin.navigator.View;
18 import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
19 import com.vaadin.spring.annotation.SpringView;
20 import com.vaadin.ui.Alignment;
21 import com.vaadin.ui.Button.ClickEvent;
22 import com.vaadin.ui.VerticalLayout;
23 import com.vaadin.ui.themes.ValoTheme;
24
25 import eu.etaxonomy.cdm.vaadin.component.LoginDialog;
26 import eu.etaxonomy.cdm.vaadin.event.AuthenticationAttemptEvent;
27 import eu.etaxonomy.cdm.vaadin.event.UserAccountEvent;
28 import eu.etaxonomy.vaadin.mvp.AbstractView;
29 import eu.etaxonomy.vaadin.ui.navigation.NavigationEvent;
30
31 /**
32 * @author a.kohlbecker
33 * @since Apr 25, 2017
34 *
35 */
36 @SpringView(name=LoginViewBean.NAME)
37 public class LoginViewBean extends AbstractView<LoginPresenter> implements LoginView, View {
38
39 private static final long serialVersionUID = 8527714663738364972L;
40
41 public static final String NAME = "login";
42
43 private LoginDialog loginDialog = new LoginDialog();
44
45 public LoginViewBean() {
46 super();
47 }
48
49 @Override
50 protected void initContent() {
51 VerticalLayout root = new VerticalLayout();
52 root.setSizeFull();
53 root.addComponent(loginDialog);
54 root.setMargin(true);
55 root.setComponentAlignment(loginDialog, Alignment.MIDDLE_CENTER);
56 setCompositionRoot(root);
57
58 // --- login tab
59 loginDialog.getLoginButton().addClickListener(e -> handleLoginClick(e));
60 loginDialog.getLoginButton().setClickShortcut(KeyCode.ENTER);
61 // NOTE: null viewName will be replaced by the default view name in NavigationManagerBean
62 loginDialog.getCancelLoginButton().addClickListener(e -> getViewEventBus().publish(EventScope.UI, this, new NavigationEvent(null)));
63
64 // --- Register tab
65 loginDialog.getRegisterButton().addClickListener(e -> {
66 getViewEventBus().publish(this, new UserAccountEvent(UserAccountEvent.UserAccountAction.REGISTER_ACCOUNT, e));
67 }
68 );
69 loginDialog.getCancelRegistrationButton().addClickListener(e -> getViewEventBus().publish(EventScope.UI, this, new NavigationEvent(null)));
70 loginDialog.getEmail().addValidator(new EmailValidator("The enterd E-mail address is not valid."));
71 loginDialog.getEmail().addValueChangeListener(e -> updateRegisterButtonState());
72 // further validators added in the presenter
73
74 // --- Password reset tab
75 StringLengthValidator nameOrEmailValidator = new StringLengthValidator("Please enter your username or email address.");
76 loginDialog.getUserNameOrEmail().addValidator(nameOrEmailValidator);
77 loginDialog.getUserNameOrEmail().addTextChangeListener(e -> {
78 String text = e.getText();
79 loginDialog.getSendOnetimeLogin().setEnabled(text != null && text.length() > 1);
80 });
81 loginDialog.getSendOnetimeLogin().addClickListener(e -> {
82 getViewEventBus().publish(this, new UserAccountEvent(UserAccountEvent.UserAccountAction.REQUEST_PASSWORD_RESET,e));
83 });
84 }
85
86 private void updateRegisterButtonState() {
87 loginDialog.getRegisterButton().setEnabled(StringUtils.isNoneBlank(loginDialog.getEmail().getValue()) && loginDialog.getEmail().getErrorMessage() == null);
88 }
89
90 private void handleLoginClick(ClickEvent e) {
91 getViewEventBus().publish(EventScope.UI, this, new AuthenticationAttemptEvent(e, loginDialog.getUserName().getValue()));
92 }
93
94 @Override
95 public LoginDialog getLoginDialog(){
96 return loginDialog;
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 loginDialog.getLoginMessageLabel().setVisible(true);
108 loginDialog.getLoginMessageLabel().setStyleName(ValoTheme.BUTTON_TINY + " " + ValoTheme.LABEL_FAILURE);
109 loginDialog.getLoginMessageLabel().setValue(text);
110 }
111
112 @Override
113 public void clearMessage(){
114 loginDialog.getLoginMessageLabel().setVisible(false);
115 loginDialog.getLoginMessageLabel().setStyleName("");
116 loginDialog.getLoginMessageLabel().setValue("");
117 }
118 }