/*
* To change this template, choose Tools | Templates and open the template in
* the editor.
*/
package com.evasion.plugin.common;
import com.evasion.AbstractEJBModule;
import com.evasion.ejb.local.*;
import com.evasion.exception.EvasionException;
import com.evasion.module.common.ICommonModule;
import com.evasion.module.common.entity.AccountDTO;
import com.evasion.module.common.entity.IEventData;
import com.evasion.plugin.common.entity.Account;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.ejb.*;
import javax.interceptor.AroundInvoke;
import javax.interceptor.InvocationContext;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author glon-56610
*/
@Stateful
@Remote(value = ICommonModule.class)
public class CommonModule extends AbstractEJBModule implements ICommonModule {
/**
* LOGGER
*/
private static final Logger LOGGER = LoggerFactory.getLogger(
CommonModule.class);
/**
* Objet de persistence.
*/
@PersistenceContext(unitName = Constante.PERSISTENCE_UNIT, type = PersistenceContextType.EXTENDED)
private transient EntityManager em;
@EJB
private ParametreManagerLocal paramEJB;
@EJB
private MailManagerLocal mailEJB;
@EJB
PersonManagerLocal personEJB;
private AccountManager getAccountManager() {
return new AccountManager(em);
}
@PostConstruct
@TransactionAttribute(TransactionAttributeType.REQUIRED)
@Override
public void preLoad() {
try {
getAccountManager().checkOrUpdateAdminAuth();
} catch (EvasionException ex) {
LOGGER.error("Erreur dans la phase de création du compte administrateur.", ex);
}
}
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
@Override
public List<? extends IEventData> getEventSince(Date date, String code) {
return getEventManager().getEventSince(date, code);
}
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
@Override
public List<? extends IEventData> getEventforPluginSince(Date date, String code, String plugin) {
return getEventManager().getEventforPluginSince(date, code, plugin);
}
@TransactionAttribute(TransactionAttributeType.REQUIRED)
@Override
public void addEvent(String plugin, String entityName, String entityId, String code) throws EvasionException{
getEventManager().addEvent(plugin, entityName, entityId, code, this.getPrinciaplUserName());
}
@Override
public AccountDTO createAccount(AccountDTO extAccount) throws EvasionException {
Account account = new Account();
account.mergeFromDTO(extAccount);
personEJB.createPerson(account.getPerson()); // @TODO à transférer dans l'accountManager une fois supprimé le mode EJB.
getAccountManager().createAccount(account);
return Account.toDTO(account);
}
@Override
public void deleteAccount(AccountDTO u) {
getAccountManager().deleteAccount(u.getId());
}
@Override
public AccountDTO updateAccount(AccountDTO u) {
Account account = getAccountManager().findAccountById(u.getId());
account.mergeFromDTO(u);
return Account.toDTO(getAccountManager().updateAccount(account));
}
@Override
public List<AccountDTO> listAllAccount() {
List<Account> accounts = getAccountManager().findAllAccount();
List<AccountDTO> result = new ArrayList<AccountDTO>(accounts.size());
for (Account account : accounts) {
result.add(Account.toDTO(account));
}
return result;
}
@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);
}
}
}
protected void aroundCreateAccount(InvocationContext ctx) {
if (ctx.getMethod().getName().equals("createAccount")) {
AccountDTO account = (AccountDTO) ctx.getParameters()[0];
if (isSendEmailOnCreateAccount()) {
LOGGER.debug("demande d'envoi de mail :" + account);
Map<String, Object> properties = new HashMap<String, Object>();
properties.put("account", account);
if (account.getEmail() == null) {
LOGGER.error("Can not send email confirmation; for user {}", account.getUsername());
} else {
mailEJB.sendEmailWithTemplate(account.getEmail(), Constante.SEND_EMAIL_ACCOUNT_CREATION, properties);
}
}
}
}
private boolean isSendEmailOnCreateAccount() {
return StringUtils.equalsIgnoreCase(paramEJB.getProperty(Constante.SEND_EMAIL_ACCOUNT_CREATION), Boolean.TRUE.toString());
}
@Override
public AccountDTO findAccountByUsername(String username) {
return Account.toDTO(getAccountManager().findAccountByUserName(username));
}
private EventManager getEventManager() {
return new EventManager(em);
}
}