br.eti.danielcamargo.backend.common.core.business.UsuarioService.java Source code

Java tutorial

Introduction

Here is the source code for br.eti.danielcamargo.backend.common.core.business.UsuarioService.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 br.eti.danielcamargo.backend.common.core.business;

import br.eti.danielcamargo.backend.common.core.persistence.Usuario;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Daniel
 */
@Service
public class UsuarioService implements UserDetailsService {

    @PersistenceContext
    private EntityManager em;

    @Autowired
    private MailService mailService;

    @Transactional
    public void createFacebook(Usuario usuario) {
        usuario.setFacebook(true);
        usuario.setConfirmado(true);
        usuario.setChaveConfirmacao(UUID.randomUUID().toString());
        em.persist(usuario);
    }

    @Transactional
    public void create(Usuario usuario, String urlConfirmacao) {
        usuario.setChaveConfirmacao(UUID.randomUUID().toString());

        em.persist(usuario);

        StringBuilder chaveConfirmacao = new StringBuilder();
        chaveConfirmacao.append(usuario.getId());
        chaveConfirmacao.append(":");
        chaveConfirmacao.append(usuario.getChaveConfirmacao());

        Map<String, Object> params = new HashMap<>();
        params.put("titulo", "Bem Vindo!");
        params.put("chave", chaveConfirmacao);
        params.put("urlConfirmacao", urlConfirmacao);

        mailService.enviarEmail("/emails/common", "confirmacao-email.html", params, "Confirmao de Registro",
                usuario.getEmail());
    }

    public Usuario get(Long id) {
        TypedQuery<Usuario> query = em.createNamedQuery(Usuario.GET_USUARIO_BY_ID, Usuario.class);
        query.setParameter("id", id);
        List<Usuario> result = query.getResultList();
        return result.isEmpty() ? null : result.get(0);
    }

    public Usuario get(String email) {
        TypedQuery<Usuario> query = em.createNamedQuery(Usuario.GET_USUARIO_BY_EMAIL, Usuario.class);
        query.setParameter("email", email);

        List<Usuario> result = query.getResultList();
        return result.isEmpty() ? null : result.get(0);
    }

    @Override
    public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
        Usuario usuario = get(email);
        if (usuario == null) {
            throw new UsernameNotFoundException(email);
        }
        return usuario;
    }

    @Transactional
    public void confirmar(Usuario usuario) {
        usuario.setConfirmado(Boolean.TRUE);
        em.merge(usuario);
    }

}