com.trender.user.component.UserForm.java Source code

Java tutorial

Introduction

Here is the source code for com.trender.user.component.UserForm.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 com.trender.user.component;

import com.trender.entity.Role;
import com.trender.entity.User;
import com.trender.service.RoleService;
import com.trender.service.UserService;
import com.trender.service.exception.ServiceException;
import com.trender.user.IndexPageView;
import com.trender.user.hash.Hash;
import com.vaadin.data.Validator;
import com.vaadin.data.validator.EmailValidator;
import com.vaadin.data.validator.NullValidator;
import com.vaadin.data.validator.RegexpValidator;
import com.vaadin.data.validator.StringLengthValidator;
import com.vaadin.shared.ui.MarginInfo;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Component;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Notification;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.TextField;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;

/**
 *
 * @author Vladimir
 */
public class UserForm extends FormLayout {

    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;

    private final String regexpValue = "^[A-Za-z?--??]+$";
    private Button performButton, clearButton;
    private TextField emailField, surnameField, nameField;
    private PasswordField passwordField;
    private HorizontalLayout horizontalLayout;

    public UserForm() {
        super();
    }

    public Component loginForm() {
        init("");

        addComponent(horizontalLayout);
        setMargin(new MarginInfo(false, false, true, true));

        performButton.addClickListener(new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                UserForm.this.singIn();
            }
        });

        clearButton.addClickListener(new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                UserForm.this.clearForm(false);
            }
        });

        return this;
    }

    public Component regForm() {
        init("???");

        surnameField = new TextField("?");
        surnameField.addStyleName("v-textfield-borderless");
        surnameField.addValidator(new StringLengthValidator(
                "  , "
                        + "?  2 ?, ?? 45",
                2, 45, false));
        surnameField.addValidator(new NullValidator("? ", false));
        surnameField.addValidator(new RegexpValidator(regexpValue, "   "
                + " ??  ? ?"));
        surnameField.setInputPrompt("?");
        surnameField.setRequired(true);

        nameField = new TextField("?");
        nameField.addStyleName("v-textfield-borderless");
        nameField.addValidator(new StringLengthValidator("  ?, ? "
                + " 2 ?, ?? 45", 2, 45, false));
        nameField.addValidator(new RegexpValidator(regexpValue, "   "
                + " ??  ? ?"));
        nameField.addValidator(new NullValidator("? ", false));
        nameField.setInputPrompt("?");
        nameField.setRequired(true);

        addComponent(surnameField);
        addComponent(nameField);
        addComponent(horizontalLayout);
        setMargin(new MarginInfo(false, false, true, true));

        performButton.addClickListener(new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                UserForm.this.singUp();
            }
        });

        clearButton.addClickListener(new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                UserForm.this.clearForm(true);
            }
        });

        return this;
    }

    public Component ProfilForm() {
        addStyleName("light");
        setWidth(90, Unit.PERCENTAGE);

        Label title = new Label();
        title.setStyleName("h2");
        title.setValue("");

        passwordField = new PasswordField(" ");
        passwordField.setInputPrompt("12345678");
        passwordField.setWidth(80, Unit.PERCENTAGE);

        surnameField = new TextField("?");
        surnameField.addValidator(new StringLengthValidator(
                "  , "
                        + "?  2 ?, ?? 45",
                2, 45, false));
        surnameField.addValidator(new RegexpValidator(regexpValue, "   "
                + " ??  ? ?"));
        surnameField.setInputPrompt("?");
        surnameField.setWidth(80, Unit.PERCENTAGE);

        nameField = new TextField("?");
        nameField.addValidator(new StringLengthValidator("  ?, ? "
                + " 2 ?, ?? 45", 2, 45, false));
        nameField.addValidator(new RegexpValidator(regexpValue, "   "
                + " ??  ? ?"));
        nameField.setInputPrompt("?");
        nameField.setWidth(80, Unit.PERCENTAGE);

        performButton = new Button("");
        performButton.setStyleName("v-button-friendly");

        clearButton = new Button("?");
        clearButton.setStyleName("v-button-quiet");

        horizontalLayout = new HorizontalLayout();
        horizontalLayout.addComponent(performButton);
        horizontalLayout.addComponent(clearButton);
        horizontalLayout.setSpacing(true);
        horizontalLayout.setDefaultComponentAlignment(Alignment.MIDDLE_LEFT);
        horizontalLayout.setMargin(new MarginInfo(true, false, false, true));

        addComponent(title);
        addComponent(passwordField);
        addComponent(surnameField);
        addComponent(nameField);
        addComponent(horizontalLayout);

        return this;
    }

    private void init(String valueButtonPerform) {
        setStyleName("v-formlayout-contentcell");

        emailField = new TextField(" Email ?");
        emailField.addStyleName("v-textfield-borderless");
        emailField.addValidator(new EmailValidator(
                "  ? ? "));
        emailField.setRequired(true);
        emailField.setInputPrompt("user@domen.com");

        passwordField = new PasswordField(" ");
        passwordField.setInputPrompt("12345678");
        passwordField.setStyleName("v-textfield-borderless");
        passwordField.setRequired(true);

        performButton = new Button(valueButtonPerform);
        performButton.setStyleName("v-button-friendly");

        clearButton = new Button("?");
        clearButton.setStyleName("v-button-quiet");

        horizontalLayout = new HorizontalLayout();
        horizontalLayout.addComponent(performButton);
        horizontalLayout.addComponent(clearButton);
        horizontalLayout.setSpacing(true);

        addComponent(emailField);
        addComponent(passwordField);

        ////        clearButton.addClickListener(new Button.ClickListener() {
        ////            @Override
        ////            public void buttonClick(Button.ClickEvent event) {
        ////                UserForm.this.clearForm(false);
        ////            }
        ////        });
        ////
        ////        performButton.addClickListener(new Button.ClickListener() {
        ////            @Override
        ////            public void buttonClick(Button.ClickEvent event) {
        ////                UserForm.this.chengProfil();
        ////            }
        ////        });
    }

    private void clearForm(boolean flag) {
        emailField.setValue("");
        passwordField.setValue("");
        if (flag) {
            surnameField.setValue("");
            nameField.setValue("");
        }
    }

    private void singIn() {
        try {
            emailField.validate();
            passwordField.validate();
            if (passwordField.isValid() && emailField.isValid()) {
                // ?   ? 
                Notification.show("ok", Notification.Type.ERROR_MESSAGE);
            }
        } catch (Validator.InvalidValueException e) {
            Notification.show(" ? ? !",
                    Notification.Type.ERROR_MESSAGE);
        }
    }

    private void singUp() {
        try {
            emailField.validate();
            passwordField.validate();
            surnameField.validate();
            nameField.validate();
            if (emailField.isValid() && passwordField.isValid() && surnameField.isValid() && nameField.isValid()) {

                User user = new User();
                user.setEmail(emailField.getValue());
                user.setFirstName(surnameField.getValue());
                user.setPassword(Hash.getPassword(passwordField.getValue()));
                user.setSecondName(nameField.getValue());

                Set<Role> role = new HashSet<>();
                role.add(roleService.getById(2L));
                user.setRoles(role);

                userService.create(user);
                Notification.show(
                        " ? ??!   ?   !",
                        Notification.Type.TRAY_NOTIFICATION);
                loginForm();
            }
        } catch (Validator.InvalidValueException e) {
            Notification.show(" ? ? !",
                    Notification.Type.ERROR_MESSAGE);
        } catch (ServiceException ex) {
            Notification.show(" ??   !",
                    Notification.Type.TRAY_NOTIFICATION);
            Logger.getLogger(IndexPageView.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    // TODO
    private void chengProfil() {
        try {
            passwordField.validate();
            surnameField.validate();
            nameField.validate();
            if (passwordField.isValid() && surnameField.isValid() && nameField.isValid()) {

                User user = new User();
                user.setFirstName(surnameField.getValue());
                user.setPassword(Hash.getPassword(passwordField.getValue()));
                user.setSecondName(nameField.getValue());

                Set<Role> role = new HashSet<>();
                role.add(roleService.getById(2L));
                user.setRoles(role);

                userService.create(user);
                Notification.show(
                        " ? ??!   ?   !",
                        Notification.Type.TRAY_NOTIFICATION);
                loginForm();
            }
        } catch (Validator.InvalidValueException e) {
            Notification.show(" ? ? !",
                    Notification.Type.ERROR_MESSAGE);
        } catch (ServiceException ex) {
            Notification.show(" ??   !",
                    Notification.Type.TRAY_NOTIFICATION);
            Logger.getLogger(IndexPageView.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}