dhbw.clippinggorilla.userinterface.windows.PasswordRecoveryWindow.java Source code

Java tutorial

Introduction

Here is the source code for dhbw.clippinggorilla.userinterface.windows.PasswordRecoveryWindow.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package dhbw.clippinggorilla.userinterface.windows;

import com.vaadin.event.ShortcutAction;
import com.vaadin.icons.VaadinIcons;
import com.vaadin.server.AbstractErrorMessage;
import com.vaadin.server.ErrorMessage;
import com.vaadin.server.Sizeable;
import com.vaadin.server.UserError;
import com.vaadin.shared.Registration;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Component;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.ProgressBar;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import com.vaadin.ui.themes.ValoTheme;
import dhbw.clippinggorilla.external.database.exceptions.PasswordChangeException;
import dhbw.clippinggorilla.external.database.exceptions.UserNotFoundException;
import dhbw.clippinggorilla.utilities.language.Language;
import dhbw.clippinggorilla.utilities.language.Word;
import dhbw.clippinggorilla.objects.user.User;
import dhbw.clippinggorilla.objects.user.UserUtils;
import dhbw.clippinggorilla.utilities.ui.VaadinUtils;
import java.util.HashMap;

/**
 * This is the window to recover your password
 * @author frank
 */
public class PasswordRecoveryWindow extends Window {

    public static Window create() {
        return new PasswordRecoveryWindow();
    }

    private final VerticalLayout windowLayout;
    private final FormLayout forms;
    private final GridLayout footer;

    private final TextField emailOrUsername;
    private final Button send;
    private final Registration sendListener;
    private final Button cancel;

    public PasswordRecoveryWindow() {
        setModal(true);
        setResizable(false);
        setDraggable(false);
        setCaption(Language.get(Word.RESET_PW));
        addCloseShortcut(ShortcutAction.KeyCode.ENTER, null);

        windowLayout = new VerticalLayout();
        windowLayout.setMargin(false);
        windowLayout.setSizeUndefined();

        forms = new FormLayout();
        forms.setMargin(true);
        forms.setSizeUndefined();

        send = new Button(Language.get(Word.SEND_RESET_CODE));

        emailOrUsername = new TextField(Language.get(Word.USERNAME_OR_EMAIL));
        emailOrUsername.setMaxLength(256);
        emailOrUsername.focus();
        forms.addComponent(emailOrUsername);

        footer = new GridLayout(3, 1);
        footer.setSpacing(true);
        footer.addStyleName(ValoTheme.WINDOW_BOTTOM_TOOLBAR);
        footer.setWidth(100.0f, Sizeable.Unit.PERCENTAGE);

        Label placeholder = new Label();

        cancel = new Button(Language.get(Word.CANCEL));
        cancel.setIcon(VaadinIcons.CLOSE);
        cancel.addClickListener(ce -> {
            close();
        });
        cancel.setClickShortcut(ShortcutAction.KeyCode.ESCAPE, null);

        send.setIcon(VaadinIcons.CHECK);
        send.addStyleName(ValoTheme.BUTTON_PRIMARY);
        sendListener = send.addClickListener(ce -> changeWindow());
        send.setClickShortcut(ShortcutAction.KeyCode.ENTER, null);

        footer.setSizeUndefined();
        footer.setWidth("100%");
        footer.addComponents(placeholder, cancel, send);
        footer.setColumnExpandRatio(0, 1);//ExpandRatio(placeholder, 1);
        footer.setComponentAlignment(cancel, Alignment.MIDDLE_CENTER);
        footer.setComponentAlignment(send, Alignment.MIDDLE_CENTER);

        windowLayout.addComponents(forms, footer);
        windowLayout.setWidth("450px");

        setContent(windowLayout);
        //setWidth("450px");
    }

    private final HashMap<Component, Boolean> errors = new HashMap<>();

    public void changeWindow() {
        String errorMsg;
        if (emailOrUsername.getValue().contains("@")) {
            errorMsg = UserUtils.checkEmailOnFormalCorrectness(emailOrUsername.getValue().toLowerCase());
        } else {
            errorMsg = UserUtils.checkUsernameOnFormalCorrectness(emailOrUsername.getValue());
        }

        if (errorMsg.isEmpty()) {
            VaadinUtils.middleInfoNotification(Language.get(Word.IF_USER_EXIST_CHECK_MAIL));
            try {
                String stringEmailOrUsername = emailOrUsername.getValue();
                if (stringEmailOrUsername.contains("@")) {
                    stringEmailOrUsername = stringEmailOrUsername.toLowerCase();
                }
                String token = UserUtils.initiateForgottenPasswordRecovery(stringEmailOrUsername);
                forms.removeAllComponents();

                TextField resetCode = new TextField(Language.get(Word.RESET_CODE));
                resetCode.setMaxLength(6);
                resetCode.addValueChangeListener(e -> {
                    if (resetCode.getValue().length() > 5) {
                        resetCode.setComponentError(null);
                        setError(resetCode, false);
                    } else {
                        resetCode.setComponentError(new UserError(Language.get(Word.RESET_CODE_SIX_CHARS),
                                AbstractErrorMessage.ContentMode.HTML, ErrorMessage.ErrorLevel.INFORMATION));
                        VaadinUtils.infoNotification(Language.get(Word.RESET_CODE_SIX_CHARS));
                        setError(resetCode, true);
                    }
                });

                ProgressBar strength = new ProgressBar();

                PasswordField pw1 = new PasswordField(Language.get(Word.PASSWORD));
                pw1.setMaxLength(51);
                pw1.addValueChangeListener(e -> {
                    String pwErrorMsg = UserUtils.checkPassword(e.getValue());
                    strength.setValue(UserUtils.calculatePasswordStrength(e.getValue()));
                    if (!pwErrorMsg.isEmpty()) {
                        pw1.setComponentError(new UserError(pwErrorMsg, AbstractErrorMessage.ContentMode.HTML,
                                ErrorMessage.ErrorLevel.ERROR));
                        VaadinUtils.infoNotification(pwErrorMsg);
                        setError(pw1, true);
                    } else {
                        pw1.setComponentError(null);
                        setError(pw1, false);
                    }
                });

                strength.setWidth("184px");
                strength.setHeight("1px");

                PasswordField pw2 = new PasswordField(Language.get(Word.PASSWORD_AGAIN));
                pw2.setMaxLength(51);
                pw2.addValueChangeListener(e -> {
                    String pwErrorMsg = UserUtils.checkSecondPassword(pw1.getValue(), e.getValue());
                    if (!pwErrorMsg.isEmpty()) {
                        pw2.setComponentError(new UserError(pwErrorMsg, AbstractErrorMessage.ContentMode.HTML,
                                ErrorMessage.ErrorLevel.ERROR));
                        VaadinUtils.infoNotification(pwErrorMsg);
                        setError(pw2, true);
                    } else {
                        pw2.setComponentError(null);
                        setError(pw2, false);
                    }
                });

                send.setCaption(Language.get(Word.RESET));
                send.setEnabled(false);
                sendListener.remove();
                send.addClickListener(ce -> {
                    try {
                        UserUtils.executeForgottenPasswordRecovery(emailOrUsername.getValue(), resetCode.getValue(),
                                token, pw1.getValue(), pw2.getValue());
                        VaadinUtils.infoNotification(Language.get(Word.RESET_SUCCESSFUL));
                        close();
                    } catch (PasswordChangeException ex) {
                        VaadinUtils.infoNotification(Language.get(Word.RESET_FAILED));
                    }
                });
                forms.addComponents(resetCode, pw1, strength, pw2);
                center();
            } catch (UserNotFoundException e) {
                VaadinUtils.errorNotification(Language.get(Word.RESET_FAILED));
            }
        } else {
            VaadinUtils.infoNotification(errorMsg);
        }
    }

    public void setError(Component toBeChecked, boolean error) {
        errors.put(toBeChecked, error);
        if (errors.size() == 3 && errors.values().stream().allMatch(v -> v == false)) {
            send.setEnabled(true);
        } else {
            send.setEnabled(false);
        }
    }

}