bc8.movies.dao.UserDaoImpl.java Source code

Java tutorial

Introduction

Here is the source code for bc8.movies.dao.UserDaoImpl.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 bc8.movies.dao;

import bc8.movies.entities.Movie;
import bc8.movies.entities.User;
import bc8.movies.utils.Constants;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.springframework.stereotype.Repository;

/**
 *
 * @author Catalin
 */
@Repository
public class UserDaoImpl {

    @PersistenceContext
    EntityManager em;

    public void addUser(User user) {
        em.persist(user);
    }

    public User getUser(User user) {

        Query query = em.createQuery(
                "select user from User user where user.username= :username and user.password=:password");
        query.setParameter("username", user.getUsername());
        query.setParameter("password", user.getPassword());

        List<User> users = query.getResultList();

        if (!users.isEmpty()) {
            return users.get(0);
        } else {
            return null;
        }
    }

    public boolean checkUsername(User user) {
        Query query = em.createQuery("select user from User user where user.username = :username");

        query.setParameter("username", user.getUsername());
        if (query.getResultList().isEmpty()) {
            return false;
        } else {
            return true;
        }
    }

    public void updateUser(User user) {
        em.merge(user);
    }

    public void rateMovie(User user, Movie movie, int rating) {
        String id = String.format("%05d%05d", user.getId(), movie.getId());
        StringBuilder sb = new StringBuilder();
        sb.append("insert into ").append(Constants.DB_TABLE_USER_MOVIE)
                .append(" (id, userId, movieId, rating) values (").append(id).append(", ").append(user.getId())
                .append(", ").append(movie.getId()).append(", ").append(rating)
                .append(") on duplicate key update rating=values(rating)");

        Query query = em.createNativeQuery(sb.toString());

        query.executeUpdate();
    }

    public void favoriteMovie(User user, Movie movie, boolean favorited) {
        int favorite = (favorited) ? 1 : 0;

        String id = String.format("%05d%05d", user.getId(), movie.getId());
        StringBuilder sb = new StringBuilder();
        sb.append("insert into ").append(Constants.DB_TABLE_USER_MOVIE)
                .append(" (id, userId, movieId, favorite) values ('").append(id).append("', ").append(user.getId())
                .append(", ").append(movie.getId()).append(", ").append(favorite)
                .append(") on duplicate key update favorite=values(favorite);");

        Query query = em.createNativeQuery(sb.toString());

        query.executeUpdate();
    }

    public boolean isFavorite(User user, Movie movie) {
        int favorite = Constants.DB_MOVIE_FAVORITE;
        boolean toReturn = false;

        StringBuilder sb = new StringBuilder();
        sb.append("select * from ").append(Constants.DB_TABLE_USER_MOVIE).append(" where userId=")
                .append(user.getId()).append(" and movieId=").append(movie.getId());

        Query query = em.createNativeQuery(sb.toString());

        try {
            Object[] myArr = ((Object[]) query.getSingleResult());
            String value = myArr[4].toString();
            if (value.equals("1")) {
                toReturn = true;
            }
        } catch (NoResultException e) {
        }
        return toReturn;
    }

    public List<Movie> getFavoriteMovies(User user) {
        Query query = em.createQuery(
                "select movie from Movie movie where movie.id in (select ref.movieId from UserMovieRef ref where ref.userId="
                        + user.getId() + ")");

        return query.getResultList();
    }
}