<commit_before>/*
 * 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 stois.login;

import Controller.PersonsJpaController;
import Controller.UsersJpaController;
import entity.Persons;
import entity.Users;
import java.awt.Component;
import javax.persistence.EntityManagerFactory;
import java.security.NoSuchAlgorithmException;
import java.io.UnsupportedEncodingException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import stois.Passport;

/**
 *
 * @author Milhouse
 */
public class Loginform extends javax.swing.JFrame {

    /**
     * Creates new form Loginform
     */
    private EntityManagerFactory emf = null;

    public Loginform(EntityManagerFactory emf) {
        initComponents();
        this.emf = emf;
    }

    /**
     * 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();
        jLabel2 = new javax.swing.JLabel();
        txtBoxUsername = new javax.swing.JTextField();
        txtBoxPasswd = new javax.swing.JPasswordField();
        btnLogin = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Login to StoIS");

        jLabel1.setText("Username");

        jLabel2.setText("Password");

        btnLogin.setText("Login");
        btnLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoginActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(210, Short.MAX_VALUE)
                .addComponent(btnLogin)
                .addGap(133, 133, 133))
            .addGroup(layout.createSequentialGroup()
                .addGap(62, 62, 62)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addGap(62, 62, 62)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtBoxUsername, javax.swing.GroupLayout.DEFAULT_SIZE, 85, Short.MAX_VALUE)
                    .addComponent(txtBoxPasswd))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(47, 47, 47)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtBoxUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtBoxPasswd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(12, 12, 12)
                .addComponent(btnLogin)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

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

    private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoginActionPerformed
        char[] pass = txtBoxPasswd.getPassword();
        String username = txtBoxUsername.getText();

        UsersJpaController ujc = new UsersJpaController(emf);
        Users u = ujc.findUsersByUserName(username);
        if (u == null) {
            System.err.println("User not found");
        } else {
            try {
                // Compute the new DIGEST
                Passport pp = new Passport();
                if (pp.isItSamePassword(pass, u.getPasswordsalt(), u.getPassword2())) {
                    int userId = u.getUsersid();
                    PersonsJpaController pjc = new PersonsJpaController(emf);
                    List<Persons> personas = pjc.findPersonsEntities();
//                    switch(role)
//                                    Loginform lm = new Loginform(emf);
//        lm.setVisible(true);
                } else {
                    Component frame = new JFrame();
                    JOptionPane.showMessageDialog(frame,
                            "Incorret password or login",
                            "Inane error",
                            JOptionPane.ERROR_MESSAGE);
                }
            } catch (NoSuchAlgorithmException ex) {<commit_msg>pridani kontroly zda se funguje JPA USer<commit_after>/*
 * 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 stois.login;

import Controller.PersonsJpaController;
import Controller.UsersJpaController;
import entity.Persons;
import entity.Users;
import java.awt.Component;
import javax.persistence.EntityManagerFactory;
import java.security.NoSuchAlgorithmException;
import java.io.UnsupportedEncodingException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import stois.Passport;

/**
 *
 * @author Milhouse
 */
public class Loginform extends javax.swing.JFrame {

    /**
     * Creates new form Loginform
     */
    private EntityManagerFactory emf = null;

    public Loginform(EntityManagerFactory emf) {
        initComponents();
        this.emf = emf;
    }

    /**
     * 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();
        jLabel2 = new javax.swing.JLabel();
        txtBoxUsername = new javax.swing.JTextField();
        txtBoxPasswd = new javax.swing.JPasswordField();
        btnLogin = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Login to StoIS");

        jLabel1.setText("Username");

        jLabel2.setText("Password");

        btnLogin.setText("Login");
        btnLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoginActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(210, Short.MAX_VALUE)
                .addComponent(btnLogin)
                .addGap(133, 133, 133))
            .addGroup(layout.createSequentialGroup()
                .addGap(62, 62, 62)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addGap(62, 62, 62)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtBoxUsername, javax.swing.GroupLayout.DEFAULT_SIZE, 85, Short.MAX_VALUE)
                    .addComponent(txtBoxPasswd))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(47, 47, 47)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtBoxUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtBoxPasswd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(12, 12, 12)
                .addComponent(btnLogin)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

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

    private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoginActionPerformed
        char[] pass = <PASSWORD>Box<PASSWORD>wd.getPassword();
        String username = txtBoxUsername.getText();

        UsersJpaController ujc = new UsersJpaController(emf);
        Users u= null;
        try {
        u = ujc.findUsersByUserName(username);    
        } catch (Exception e) {
        System.err.println("Connection to database and searching in database faild");
        }
        
        if (u == null) {
            System.err.println("User not found");
        } else {
            try {
                // Compute the new DIGEST
                Passport pp = new Passport();
                if (pp.isItSamePassword(pass, u.getPasswordsalt(), u.getPassword2())) {
                    int userId = u.getUsersid();
                    PersonsJpaController pjc = new PersonsJpaController(emf);
                    List<Persons> personas = pjc.findPersonsEntities();
//                    switch(role)
//                                    Loginform lm = new Loginform(emf);
//        lm.setVisible(true);
                } else {
                    Component frame = new JFrame();
                    JOptionPane.showMessageDialog(frame,
                            "Incorret password or login",
                            "Inane error",
                            JOptionPane.ERROR_MESSAGE);
                }
            } catch (NoSuchAlgorithmException ex) {
{'fraction_non_alphanumeric': 0.09791235334713595, 'fraction_numerical': 0.00914423740510697, 'mean_word_length': 2.2841359773371104, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1661059', 'n_tokens_mistral': 3193, 'n_tokens_neox': 3126, 'n_words': 588}