All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.evasion.plugin.account.AccountManager Maven / Gradle / Ivy

The newest version!
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.evasion.plugin.account;

import com.evasion.ejb.local.AccountManagerLocal;
import com.evasion.ejb.local.MailManagerLocal;
import com.evasion.ejb.local.ParametreManagerLocal;
import com.evasion.ejb.local.PersonManagerLocal;
import com.evasion.ejb.local.UserAuthServiceLocal;
import com.evasion.ejb.remote.AccountManagerRemote;
import com.evasion.entity.Account;
import com.evasion.exception.PersistenceViolationException;
import com.evasion.plugin.account.dao.AccountDAOImpl;
import com.evasion.plugin.person.PersonEJB;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.interceptor.AroundInvoke;
import javax.interceptor.InvocationContext;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author sebastien.glon
 */
@Stateless
@Local(value = AccountManagerLocal.class)
@Remote(value = AccountManagerRemote.class)
public class AccountManager implements AccountManagerLocal, AccountManagerRemote {

    /** LOGGER  */
    private static final Logger LOGGER = LoggerFactory.getLogger(
            AccountManager.class);
    private AccountDAOImpl accountDAO;
    @EJB
    private MailManagerLocal mailEJB;
    @EJB
    private ParametreManagerLocal paramEJB;
    @EJB
    private UserAuthServiceLocal userEJB;
    @PersistenceContext(unitName = "EvasionPU")
    private EntityManager em;

    protected AccountManager(EntityManager em) {
        this.em = em;
    }

    public AccountManager() {
    }

    @SuppressWarnings("PMD.UnusedPrivateMethod")
    @edu.umd.cs.findbugs.annotations.SuppressWarnings("UPM_UNCALLED_PRIVATE_METHOD")
    @PostConstruct
    private void postConstruct() {
        accountDAO = new AccountDAOImpl();
        accountDAO.setEntityManager(em);
    }

    @AroundInvoke
    public Object sendConfirmationEmail(InvocationContext ctx) throws Exception {
        String businessMethodName = ctx.getMethod().getName();
        Object[] data = ctx.getParameters();
        LOGGER.debug("Around Invode methodName {}, data {}", businessMethodName, data);
        boolean success = true;
        try {
            return ctx.proceed();
        } catch (Exception ex) {
            success = false;
            throw ex;
        } finally {
            if (success) {
                aroundCreateAccount(ctx);
            }
        }
    }

    private void aroundCreateAccount(InvocationContext ctx) {
        if (ctx.getMethod().getName().equals("createAccount")) {
            Account account = (Account) ctx.getParameters()[0];
            if (StringUtils.equalsIgnoreCase(paramEJB.getProperty(Constante.SEND_EMAIL_ACCOUNT_CREATION), Boolean.TRUE.toString())) {
                LOGGER.debug("demande d'envoi de mail :" + account);
                Map properties = new HashMap();
                properties.put("account", account);
                mailEJB.sendEmailWithTemplate(account.getUser().getUsername(), Constante.SEND_EMAIL_ACCOUNT_CREATION, properties);
            }
        }
    }

    @EJB
    PersonManagerLocal personEJB;
    /**
     * {@inheritDoc }
     */
    public Account createAccount(Account account) throws PersistenceViolationException {
        personEJB.createPerson(account.getPerson());
        userEJB.createUser(account.getUser());
        account.getUser().setEmail(account.getPerson().getEmail());
        LOGGER.debug("Create an account for user: {}", account.getUser().getUsername());
        try {
            em.persist(account);
            em.flush();
        } catch (Exception e) {
            LOGGER.error("Erreur dans la validation du compte utilisateur", e);
            throw new PersistenceViolationException("Erreur dans la validation du compte utilisateur", e.fillInStackTrace());
        }
        return account;
    }

    /**
     * {@inheritDoc }
     */
    public void deleteAccount(Account u) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    /**
     * {@inheritDoc }
     */
    public Account updateAccount(Account u) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    /**
     * {@inheritDoc }
     */
    public List listAllAccount() {
        return accountDAO.findAll();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy