cs544.blog.dao.UserDAOHibernate.java Source code

Java tutorial

Introduction

Here is the source code for cs544.blog.dao.UserDAOHibernate.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 cs544.blog.dao;

import cs544.blog.entities.Post;
import java.util.List;
import cs544.blog.entities.User;

import java.util.ArrayList;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Transactional(propagation = Propagation.REQUIRED)
public class UserDAOHibernate implements IUserDAO {

    private SessionFactory sessionFactory;
    //  List<User> userList = new ArrayList<User>();

    public UserDAOHibernate() {

    }

    public UserDAOHibernate(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    @Override
    public void saveUser(User user) {
        Session session = sessionFactory.getCurrentSession();
        session.persist(user);

    }

    @Override
    public User getUser(String username) {
        System.out.println("username:" + username);
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery("from User u where u.username=:username");
        query.setParameter("username", username);

        List<User> users = query.list();
        if (users.isEmpty())
            return null;
        return users.get(0);

    }

    @Override
    public void updateUser(User user) {
        Session session = sessionFactory.getCurrentSession();
        // session.update(user);
        User userExisted = loadUser(user.getId());
        //        Session session = sessionFactory.getCurrentSession();
        if (userExisted != null) {
            session.delete(userExisted);
            session.persist(user);
        }
        //saveUser(user);
    }

    @Override
    public User loadUser(long userId) {
        Session session = sessionFactory.getCurrentSession();
        return (User) session.get(User.class, userId);
        //        userList = session.createQuery("From User").list();
        //        for (User user : userList) {
        //            if (user.getUserId() == userId) {
        //                return user;
        //            }
        //        }
        //        return null;
    }

    @Override
    public List<User> getUserList() {

        Session session = sessionFactory.getCurrentSession();
        return session.createQuery("from User").list();

    }

    @Override
    public List<Post> getAllPosts() {
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery("select p from User u join u.postList p");
        List<Post> postList = query.list();
        return postList;
    }

    @Override
    public List<Post> getUserPosts(long userId) {
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery("select distinct u.postList from User u where u.id=:userId");
        query.setParameter("userId", userId);
        List<Post> postList = query.list();
        return postList;
    }

    @Override
    public void deleteUser(long userId) {
        Session session = sessionFactory.getCurrentSession();
        session.delete(session.get(User.class, userId));

    }

    @Override
    public boolean isUserExist(String username) {
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery("select u.username from User u where u.username= :username");
        query.setParameter("username", username);
        String username1 = (String) query.uniqueResult();
        if (username1.equals(username)) {
            return true;
        }
        return false;
    }

    public boolean isAuthenticUser(String username, String password) {
        Session session = sessionFactory.getCurrentSession();
        if (isUserExist(username)) {
            Query query = session.createQuery("select u.password from User u where u.password = :password");
            query.setParameter("password", password);
            String password1 = (String) query.uniqueResult();
            if (password1.equals(password)) {
                return true;
            }
        }
        return false;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

}