controller.HibUserController.java Source code

Java tutorial

Introduction

Here is the source code for controller.HibUserController.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 controller;

import hib.session.SklepHibernateUtil;
import hib_beans.Uzytkownik;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.List;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import org.hibernate.HibernateException;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

/**
 *
 * @author HP
 */
public class HibUserController {

    ArrayList<Uzytkownik> users = new ArrayList<>();
    private Uzytkownik u = new Uzytkownik();

    public Uzytkownik getU() {
        return u;
    }

    public void setU(Uzytkownik u) {
        this.u = u;
    }

    public static String sha256(String base) {
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-256");
            byte[] hash = digest.digest(base.getBytes("UTF-8"));
            StringBuffer hexString = new StringBuffer();

            for (int i = 0; i < hash.length; i++) {
                String hex = Integer.toHexString(0xff & hash[i]);
                if (hex.length() == 1)
                    hexString.append('0');
                hexString.append(hex);
            }

            return hexString.toString();
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    public String register() {
        SessionFactory sf = SklepHibernateUtil.getSessionFactory();
        Session session = sf.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            SQLQuery insertQuery = session.createSQLQuery(
                    "INSERT INTO Uzytkownik(userid, password, imie, nazwisko, email, wiek)VALUES(?,?,?,?,?,?)");
            insertQuery.setParameter(0, this.u.getUserid());
            String pass = sha256(this.u.getPassword());
            insertQuery.setParameter(1, pass);
            insertQuery.setParameter(2, this.u.getImie());
            insertQuery.setParameter(3, this.u.getNazwisko());
            insertQuery.setParameter(4, this.u.getEmail());
            insertQuery.setParameter(5, this.u.getWiek());
            insertQuery.executeUpdate();
            tx.commit();
            tx = session.beginTransaction();
            insertQuery = session.createSQLQuery("INSERT INTO Grupy(userid, groupid)VALUES(?,?)");
            insertQuery.setParameter(0, this.u.getUserid());
            insertQuery.setParameter(1, "klient");
            insertQuery.executeUpdate();
            tx.commit();
        } catch (HibernateException e) {
            if (tx != null) {
                tx.rollback();
            }
            e.printStackTrace();
        } finally {
            session.close();
        }
        return "/faces/klient/welcome.xhtml";
    }

    public HibUserController() {
    }

    public List<Uzytkownik> getUsers() {

        SessionFactory sf = SklepHibernateUtil.getSessionFactory();
        Session session = sf.openSession();
        users = (ArrayList<Uzytkownik>) session.createQuery("from Uzytkownik").list();
        session.close();
        return users;
    }

    public String logout() {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        session.invalidate();
        return "/index.xhtml";
    }
}