mupomat.view.PodrskaLozinka.java Source code

Java tutorial

Introduction

Here is the source code for mupomat.view.PodrskaLozinka.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 mupomat.view;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import mupomat.controller.ObradaKorisnik;
import mupomat.model.Korisnik;
import mupomat.utility.Kontrola;
import org.apache.commons.lang.RandomStringUtils;

/**
 *
 * @author SrKy
 */
public class PodrskaLozinka extends javax.swing.JFrame {

    private Pattern pattern;
    private Matcher matcher;
    private ObradaKorisnik obradaKorisnik;
    public Korisnik korisnik;
    private String generiranaLozinka;
    private ImageIcon ikona;

    /**
     * Creates new form PodrskaLozinka
     */
    public PodrskaLozinka() {
        initComponents();
        setTitle("MUPomat: Zaboravljena lozinka");
        ikona = new ImageIcon(getClass().getResource("/mupomat/pictures/balloonerror.png"));
        centrirajZaslon();
        dodajIkonu();
        obradaKorisnik = new ObradaKorisnik();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        txtEmail = new javax.swing.JTextField();
        btnPosalji = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Upiite e-mail koji ste koristili pri registraciji i poslat emo Vam lozinku.");

        txtEmail.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        btnPosalji.setText("Poalji zahtjev");
        btnPosalji.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btnPosalji.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPosaljiActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel1)
                                .addGroup(layout.createSequentialGroup()
                                        .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, 255,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(btnPosalji)))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap().addComponent(jLabel1).addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(btnPosalji))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnPosaljiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPosaljiActionPerformed

        if (!kontrolaEmail()) {
            return;
        }

        generirajLozinku();
        posaljiEmail();
    }//GEN-LAST:event_btnPosaljiActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnPosalji;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField txtEmail;

    // End of variables declaration//GEN-END:variables
    private void centrirajZaslon() {
        Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (int) ((dimension.getWidth() - this.getWidth()) / 2);
        int y = (int) ((dimension.getHeight() - this.getHeight()) / 2);
        this.setLocation(x, y);
        this.setResizable(false);
    }

    private void dodajIkonu() {
        this.setIconImage(new ImageIcon(getClass().getResource("/mupomat" + "/pictures" + "/m.png")).getImage());
    }

    private void posaljiEmail() {

        final String username = "mupomat@gmail.com";
        final String password = "lesnibokysr187";

        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "587");

        Session session = Session.getInstance(props, new javax.mail.Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
        });

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("mupomat@gmail.com"));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(txtEmail.getText().trim()));
            message.setSubject("MUPomat podrka");

            message.setText("Nova lozinka: " + generiranaLozinka
                    + "\nMolimo Vas da nakon prijave promjenite lozinku radi sigurnosti.\nHvala.");

            Transport.send(message);
            this.dispose();
            JOptionPane.showMessageDialog(rootPane, "Lozinka je uspjeno poslana na Vau e-mail adresu!",
                    "MUPomat: Podrka", JOptionPane.INFORMATION_MESSAGE);

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }

    }

    private void generirajLozinku() {

        generiranaLozinka = RandomStringUtils.random(6, true, true);
        System.out.println("random = " + generiranaLozinka);

        obradaKorisnik.promjeniPostojecuLozinku(generiranaLozinka, txtEmail.getText());
    }

    private boolean kontrolaEmail() {
        pattern = Pattern.compile(Kontrola.EMAIL);
        matcher = pattern.matcher(txtEmail.getText().trim());

        Kontrola.resetirajPoljeGreske(txtEmail);
        if (!Kontrola.kontrolaPraznoPolje(txtEmail.getText(), txtEmail, "Niste unijeli email!", ikona)) {
            Kontrola.oznaciPoljeGreske(txtEmail);
            return false;
        }
        if (!matcher.matches()) {
            JOptionPane.showMessageDialog(rootPane,
                    "Niste unijeli dobar format za email!\nPrimjer: korisnik@gmail.com", "Greka",
                    JOptionPane.ERROR_MESSAGE);
            Kontrola.oznaciPoljeGreske(txtEmail);
            return false;
        }
        if (obradaKorisnik.provjeraEmaila(txtEmail.getText().trim())) {
            JOptionPane.showMessageDialog(rootPane, "Unijeli ste nepostojei e-mail!", "Greka",
                    JOptionPane.ERROR_MESSAGE);
            Kontrola.oznaciPoljeGreske(txtEmail);
            return false;
        }

        return true;
    }
}