com.cerebro.provevaadin.database.Configurazione.java Source code

Java tutorial

Introduction

Here is the source code for com.cerebro.provevaadin.database.Configurazione.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.cerebro.provevaadin.database;

import com.vaadin.data.Validator;
import com.vaadin.data.util.sqlcontainer.connection.JDBCConnectionPool;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.Button;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.Notification;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Window;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author matteo
 */
public class Configurazione extends FormLayout {

    public Configurazione() {

        System.out.println("Componente per la personalizzazione");

        this.setMargin(true);

        TextField db_host = new TextField("Database Host Name");
        db_host.isRequired();
        TextField db_port = new TextField("Database Port");
        db_port.isRequired();
        TextField db_user = new TextField("Username");
        db_user.isRequired();
        PasswordField db_pwd = new PasswordField("Password");
        db_pwd.isRequired();
        PasswordField pwd_conf = new PasswordField("Conferma Password");
        pwd_conf.setImmediate(true);

        Properties props = new Properties();
        InputStream config = VaadinServlet.getCurrent().getServletContext()
                .getResourceAsStream("/WEB-INF/config.properties");
        if (config != null) {
            System.out.println("Carico file di configurazione");
            try {
                props.load(config);
            } catch (IOException ex) {
                Logger.getLogger(MySQLServer.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        db_host.setValue(props.getProperty("db_host"));
        db_user.setValue(props.getProperty("db_user"));

        Button salva = new Button("Salva");
        salva.addClickListener((Button.ClickEvent event) -> {
            System.out.println("Salvo i parametri");
            if (db_host.isValid() && db_port.isValid() && db_user.isValid() && db_pwd.isValid()
                    && pwd_conf.getValue().equals(db_pwd.getValue())) {
                String dbHost = "jdbc:mysql://" + db_host.getValue() + ":" + db_port.getValue()
                        + "/grizzly?useSSL=false&serverTimezone=UTC";
                props.setProperty("db_host", dbHost);
                props.setProperty("db_user", db_user.getValue());
                props.setProperty("db_pwd", db_pwd.getValue());
                String webInfPath = VaadinServlet.getCurrent().getServletConfig().getServletContext()
                        .getRealPath("WEB-INF");
                File f = new File(webInfPath + "/config.properties");
                try {
                    OutputStream o = new FileOutputStream(f);
                    try {
                        props.store(o, "Prova");
                    } catch (IOException ex) {
                        Logger.getLogger(Configurazione.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } catch (FileNotFoundException ex) {
                    Logger.getLogger(Configurazione.class.getName()).log(Level.SEVERE, null, ex);
                }
                Notification.show("Parametri salvati");
            } else {
                Notification.show("Ricontrolla i parametri");
            }
        });

        Button test = new Button("Test per i parametri salvati");
        test.addClickListener((Button.ClickEvent event) -> {
            System.out.println("Testo i parametri");
            MySQLServer mysql = new MySQLServer();
            JDBCConnectionPool conn = mysql.getJDBCConnPool();
            if (!mysql.testJDBCConnPool(conn)) {
                Notification.show("Errore nella connessione al database");
            } else {
                Notification.show("Connessione avvenuta con successo");
            }
        });

        this.addComponents(db_host, db_port, db_user, db_pwd, pwd_conf, salva, test);

    }

}