de.berlios.jhelpdesk.dao.jpa.UserDAOJpa.java Source code

Java tutorial

Introduction

Here is the source code for de.berlios.jhelpdesk.dao.jpa.UserDAOJpa.java

Source

/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright: (C) 2006 jHelpdesk Developers Team
*/
package de.berlios.jhelpdesk.dao.jpa;

import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.jpa.JpaCallback;
import org.springframework.orm.jpa.JpaTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import de.berlios.jhelpdesk.dao.UserDAO;
import de.berlios.jhelpdesk.model.Role;
import de.berlios.jhelpdesk.model.User;

/**
 *
 * @author jjhop
 */
@Repository
@Transactional(readOnly = true)
public class UserDAOJpa implements UserDAO {

    private final JpaTemplate jpaTemplate;

    @Autowired
    public UserDAOJpa(EntityManagerFactory emf) {
        this.jpaTemplate = new JpaTemplate(emf);
    }

    public List<User> getAllUsers() {
        return this.jpaTemplate.findByNamedQuery("User.allOrderByLastName");
    }

    public List<User> getByRole(Role role) {
        return this.jpaTemplate.findByNamedQuery("User.allByRoleOrderByLastName", role.toInt());
    }

    public User getById(Long id) {
        return this.jpaTemplate.find(User.class, id);
    }

    public User getByLoginFetchFilters(String login) {
        List<User> users = this.jpaTemplate.findByNamedQuery("User.byLoginFetchFilters", login);
        return users.isEmpty() ? null : users.get(0);
    }

    public User getByLogin(String login) {
        List<User> users = this.jpaTemplate.findByNamedQuery("User.byLogin", login);
        return users.isEmpty() ? null : users.get(0);
    }

    public User getByEmail(String email) {
        List<User> users = this.jpaTemplate.findByNamedQuery("User.byEmail", email);
        return users.isEmpty() ? null : users.get(0);
    }

    public boolean authenticate(String login, String passw) {
        List<User> users = this.jpaTemplate.findByNamedQuery("User.byLoginAndHashedPassoword", login, passw);
        return users.isEmpty() ? false : true;
    }

    @Transactional(readOnly = false)
    public void loginUser(String login, Date date) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Transactional(readOnly = false)
    public void saveOrUpdate(User user) {
        if (user.getUserId() == null) {
            this.jpaTemplate.persist(user);
        } else {
            this.jpaTemplate.merge(user);
        }
    }

    @Transactional(readOnly = false)
    public void updatePasswordAndSalt(final User user, final String password) {
        this.jpaTemplate.execute(new JpaCallback<Object>() {
            public Object doInJpa(EntityManager em) throws PersistenceException {
                User u = em.find(User.class, user.getUserId());
                if (u != null) {
                    u.setPassword(password); // TODO: zbada czy to ma sens (vide moppee)
                    em.merge(u);
                }
                return null;
            }
        });
    }

    public void refresh(User user) {
        this.jpaTemplate.refresh(user);
    }

}