/*
* 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.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 com.evasion.plugin.security.UserAuthService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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 final AccountDAOImpl accountDAO;
@EJB
private MailManagerLocal mailEJB;
@EJB
private ParametreManagerLocal paramEJB;
@PersistenceContext(unitName = "EvasionPU")
private EntityManager em;
protected AccountManager(EntityManager em) {
accountDAO = new AccountDAOImpl();
accountDAO.setEntityManager(em);
this.em = em;
}
public AccountManager() {
accountDAO = new AccountDAOImpl();
}
@AroundInvoke
public Object sendConfirmationEmail(InvocationContext ctx) throws Exception {
//String beanClassName = ctx.getClass().getName();
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<String, Object> properties = new HashMap<String, Object>();
properties.put("account", account);
mailEJB.sendEmailWithTemplate(account.getUser().getUsername(), Constante.SEND_EMAIL_ACCOUNT_CREATION, properties);
}
}
}
/**
* {@inheritDoc }
*/
public Account createAccount(Account account) throws PersistenceViolationException {
(new PersonEJB(em)).createPerson(account.getPerson());
(new UserAuthService(em)).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) {
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<Account> listAllAccount() {
return accountDAO.findAll();
}
}