Package dao.jpa

Source Code of dao.jpa.UserJpaDao

package dao.jpa;

import dao.DataAccessException;
import dao.UserDao;
import models.entities.Contact;
import models.entities.User;
import models.entities.UserRole;
import play.Logger;
import play.db.jpa.JPA;
import utils.Utilities;

import javax.persistence.TypedQuery;
import java.util.ArrayList;
import java.util.List;

/**
* Created by kiryl on 17.08.2014.
*/
public class UserJpaDao extends BaseJpaDao<User, Integer> implements UserDao {
    @Override
    public User findByLoginAndPassword(String login, String password) {
        TypedQuery<User> query = JPA.em()
                .createQuery("SELECT u FROM User u WHERE u.login = :login AND u.shaPassword = :shaPassword", User.class);
        query.setParameter("login", login);
        query.setParameter("shaPassword", Utilities.getSha512(password));
        try {
            User user = query.getSingleResult();
            Logger.debug("FIND User login " + user.getLogin() + " and pass " + user.getPassword());
            return user;
        } catch (Exception e) {
            Logger.error("find user by Login And Password failed for login " + login + " and password " + password, e);
            return null;
        }
    }

    @Override
    public User findByAuthToken(String authToken) {
        if (authToken == null) {
            return null;
        }
        TypedQuery<User> query = JPA.em().createQuery("SELECT u FROM User u WHERE u.authToken = :authToken", User.class);
        query.setParameter("authToken", authToken);
        try {
            return query.getSingleResult();
        } catch (Exception e) {
            Logger.error("findByAuthToken failed, token: " + authToken, e);
            return null;
        }
    }

    @Override
    public List<User> findByRole(UserRole role) {
        if (role == null) {
            return new ArrayList<User>();
        }
        TypedQuery<User> query = JPA.em().createQuery("SELECT u FROM User u WHERE u.role = :role", User.class);
        query.setParameter("role", role);
        try {
            return query.getResultList();
        } catch (Exception e) {
            Logger.error("findByRole failed, role: " + role.getName(), e);
            return null;
        }
    }

    public List<User> quickSearch(Integer batchNumber, Integer batchSize, String role, String term) throws DataAccessException {
        try {
            Logger.info("Quick contact search term:" + term);
            String query = "SELECT u FROM User u WHERE u.role.name = :role AND u.contact.lastName LIKE :term";
            TypedQuery<User> typedQuery = JPA.em().createQuery(query, User.class);
            typedQuery.setParameter("role", role);
            typedQuery.setParameter("term", term + "%");

            return getBatch(batchNumber, batchSize, typedQuery);
        } catch (Throwable throwable) {
            Logger.error("failed to get batch " + batchNumber + " of size " + batchSize);
            throw new DataAccessException("failed to get batch " + batchNumber + " of size " + batchSize, throwable);
        }
    }

    public Integer quickSearchResultsCount(String role, String term) throws DataAccessException {
        try {
            String query = "SELECT u FROM User u WHERE u.role.name = :role AND u.contact.lastName LIKE :term";
            TypedQuery<User> typedQuery = JPA.em().createQuery(query, User.class);
            typedQuery.setParameter("role", role);
            typedQuery.setParameter("term", term + "%");

            return typedQuery.getResultList().size();
        } catch (Throwable throwable) {
            Logger.error("failed to get quick user search results count");
            throw new DataAccessException("failed to get quick user search results count", throwable);
        }
    }

}
TOP

Related Classes of dao.jpa.UserJpaDao

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.