238 lines
9.3 KiB
Plaintext
Executable File
238 lines
9.3 KiB
Plaintext
Executable File
package com.fp.armas.mail;
|
|
|
|
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.Schedule;
|
|
import javax.ejb.Stateless;
|
|
import javax.persistence.EntityManager;
|
|
import javax.persistence.EntityManagerFactory;
|
|
import javax.persistence.Persistence;
|
|
|
|
import org.jboss.logging.Logger;
|
|
|
|
import com.fp.common.logger.APPLogger;
|
|
import com.fp.mail.Mail;
|
|
import com.fp.persistence.commondb.data.SessionData;
|
|
import com.fp.persistence.commondb.data.ThreadFacade;
|
|
import com.fp.persistence.pgeneral.gene.TgeneParameters;
|
|
|
|
@Stateless(name="scheduleNotificacionCliente")
|
|
public class ScheduleNotificacionCliente {
|
|
private final Logger LOG = Logger.getLogger(ScheduleNotificacionCliente.class.getName());
|
|
|
|
|
|
private EntityManagerFactory emf;
|
|
@PostConstruct
|
|
private final void crearManager() {
|
|
try {
|
|
if (emf == null) {
|
|
Map<String, Object> configOverrides = new HashMap<String, Object>();
|
|
configOverrides.put("hibernate.ejb.cfgfile", "hibernateFlipMapping.cfg.xml");
|
|
emf = Persistence.createEntityManagerFactory("local1" , configOverrides);
|
|
}
|
|
SessionData sessionData = new SessionData();
|
|
sessionData.setCompany(1);
|
|
sessionData.setIsEjb(Boolean.FALSE);
|
|
ThreadFacade.setSaveRequest(sessionData);
|
|
} catch (Exception e) {
|
|
APPLogger.getLogger().error(e);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* Envío de correo MAIL. Con un mes antes de caducar el permiso enviar un correo al usuario informándole
|
|
*/
|
|
@SuppressWarnings("deprecation")
|
|
@Schedule(dayOfWeek="Sun" ,hour="*", minute="*", persistent=false)
|
|
public void automaticaNotificacion(){
|
|
LOG.info("-----------------------NOTIFICACION MENSAJE DE ALERTA UNA VEZ CADUCADO EL PERMISO DE ARMAS.****");
|
|
try {
|
|
EntityManager entityManager=emf.createEntityManager();
|
|
|
|
Date fechaLimite = new Date();
|
|
fechaLimite.setMonth(fechaLimite.getDay()-8);
|
|
LOG.info("fecha"+ fechaLimite);
|
|
Date fechaActual = new Date();
|
|
fechaActual.setMonth(fechaLimite.getMonth()+1);
|
|
StringBuilder sql = new StringBuilder(
|
|
"select "
|
|
+ " dh.personcode,"
|
|
|
|
+ " (select cat.description from TgeneCatalogDetail cat "
|
|
+ " where cat.pk.catalog = (select tr.tipoautorizacion from TarmTramite tr WHERE tr.pk=dh.ctramite) "
|
|
+ " and cat.pk.catalogcode='TIPOAUTORIZACION') as TIPOAUTORIZACION,"
|
|
|
|
+ " (select cat.description from TgeneCatalogDetail cat "
|
|
+ " where cat.pk.catalog= (select tr.usoactividad from TarmTramite tr WHERE tr.pk=dh.ctramite)"
|
|
+ " and cat.pk.catalogcode='USOACTIVIDAD') as USOACTIVIDAD "
|
|
|
|
+ " from TarmDocumentoHabilitante dh where dh.ctramite in ( "
|
|
+ " select tr.pk from TarmTramite tr where tr.tipoautorizacioncodigo='TIPOAUTORIZACION' and tr.tipoautorizacion in (6,1)) "
|
|
+ " and dh.fechaexpiracion is not null and dh.estado ='APR'"
|
|
+ " and dh.fechaexpiracion > :fechaLimite and dh.fechaexpiracion < :fechaActual"
|
|
|
|
);
|
|
|
|
LOG.info("Consulta");
|
|
List<Object[]>listPersonas = entityManager.createQuery(sql.toString(),Object[].class)
|
|
.setParameter("fechaLimite", fechaLimite)
|
|
.setParameter("fechaActual", fechaActual)
|
|
.getResultList();
|
|
LOG.info("Consulta 2:: "+listPersonas);
|
|
if(listPersonas!= null){
|
|
LOG.info("Tamanio listPersonas "+listPersonas.size());
|
|
//Datos para el envio de correo
|
|
StringBuilder sqlParametros = new StringBuilder("select t from TgeneParameters t"
|
|
+ " where t.pk.code in :codigosmail ");
|
|
List<String>listaCodigos = new ArrayList<>();
|
|
listaCodigos.add("ASUNTO.NOTIFICACION.SCHEDULE");
|
|
listaCodigos.add("CUERPO.NOTIFICACION.SCHEDULE");
|
|
listaCodigos.add("CORRDES.NOTIFICACION.SCHEDULE");
|
|
listaCodigos.add("CORRCOP.NOTIFICACION.SCHEDULE");
|
|
|
|
listaCodigos.add("MAIL.FROM");
|
|
listaCodigos.add("MAIL.SMTP.SERVER");
|
|
listaCodigos.add("MAIL.SMTP.PORT");
|
|
listaCodigos.add("MAIL.SMTP.USER");
|
|
listaCodigos.add("MAIL.SMTP.PASSWORD");
|
|
listaCodigos.add("MAIL.SMTP.AUTH");
|
|
listaCodigos.add("MAIL.SMTPS");
|
|
|
|
|
|
|
|
List<TgeneParameters>listaParametrosMail = entityManager.createQuery(sqlParametros.toString(),TgeneParameters.class)
|
|
.setParameter("codigosmail", listaCodigos)
|
|
.getResultList();
|
|
|
|
String asunto=valorTexto(listaParametrosMail, "ASUNTO.NOTIFICACION.SCHEDULE");
|
|
String cuerpo=valorTexto(listaParametrosMail, "CUERPO.NOTIFICACION.SCHEDULE");;
|
|
String correodesde=valorTexto(listaParametrosMail, "CORRDES.NOTIFICACION.SCHEDULE");
|
|
String correoCC[]=valorTexto(listaParametrosMail, "CORRCOP.NOTIFICACION.SCHEDULE").split(",");
|
|
|
|
String MAIL_FROM=valorTexto(listaParametrosMail, "MAIL.FROM");
|
|
String MAIL_SMTP_SERVER=valorTexto(listaParametrosMail, "MAIL.SMTP.SERVER");
|
|
int MAIL_SMTP_PORT=valorNumber(listaParametrosMail, "MAIL.SMTP.PORT");
|
|
String MAIL_SMTP_USER=valorTexto(listaParametrosMail, "MAIL.SMTP.USER");
|
|
String MAIL_SMTP_PASSWORD=valorTexto(listaParametrosMail, "MAIL.SMTP.PASSWORD");
|
|
Boolean MAIL_SMTP_AUTH=Boolean.valueOf(valorTexto(listaParametrosMail, "MAIL.SMTP.AUTH"));
|
|
Boolean MAIL_SMTPS=Boolean.valueOf(valorTexto(listaParametrosMail, "MAIL.SMTPS"));
|
|
|
|
for (Object[] item : listPersonas) {
|
|
StringBuilder sqlInformacion = new StringBuilder(
|
|
"select p.address from TcustPersonAddress p where p.pk.personcode=:personcode "
|
|
+ " and p.address is not null "
|
|
+ " and p.addresstypecatalog=3");
|
|
List<String> listaDirecciones =entityManager.createQuery(sqlInformacion.toString(),String.class)
|
|
.setParameter("personcode", (Integer)item[0])
|
|
.getResultList();
|
|
LOG.info("ListaDirecciones "+listaDirecciones);
|
|
if(listaDirecciones!= null && listaDirecciones.size()>0){
|
|
LOG.info("Tamanio listaDirecciones "+listaDirecciones.size());
|
|
List<String> remover = new ArrayList<String>();
|
|
for (String itemcorreo : listaDirecciones) {
|
|
if(!itemcorreo.contains("@")){
|
|
remover.add(itemcorreo);
|
|
}
|
|
}
|
|
listaDirecciones.removeAll(remover);
|
|
if(listaDirecciones!= null && listaDirecciones.size()>0){
|
|
LOG.info("Envio "+listaDirecciones.size());
|
|
cuerpo = cuerpo +" "+(item[1]!=null?item[1].toString():"")+" "+(item[2]!=null?item[2].toString():"");
|
|
for (String itemMail : listaDirecciones) {
|
|
envioCorreoCambio(MAIL_FROM, MAIL_SMTP_SERVER,MAIL_SMTP_PORT,
|
|
MAIL_SMTP_USER,MAIL_SMTP_PASSWORD,MAIL_SMTP_AUTH,MAIL_SMTPS,
|
|
itemMail,asunto,cuerpo,correoCC[0],correodesde);
|
|
}
|
|
//String [] arrayCorreos= new String[listaDirecciones.size()];
|
|
//arrayCorreos=listaDirecciones.toArray(arrayCorreos);
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
} catch(Exception e){
|
|
LOG.info("Error: "+e.getMessage());
|
|
e.printStackTrace();
|
|
} catch (Throwable e) {
|
|
LOG.info("Error: "+e.getMessage());
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
private String valorTexto(List<TgeneParameters>listaParametrosMail, String codigo){
|
|
for (TgeneParameters item : listaParametrosMail) {
|
|
if(codigo.trim().equals(item.getPk().getCode().trim())){
|
|
return item.getTextvalue();
|
|
}
|
|
}
|
|
return "";
|
|
}
|
|
|
|
private int valorNumber(List<TgeneParameters>listaParametrosMail, String codigo){
|
|
for (TgeneParameters item : listaParametrosMail) {
|
|
if(codigo.trim().equals(item.getPk().getCode().trim())){
|
|
return item.getNumbervalue().intValue();
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
/**
|
|
* Envía el correo de cambio de email
|
|
* @throws Exception
|
|
*/
|
|
private void envioCorreoCambio(String MAIL_FROM, String MAIL_SMTP_SERVER,int MAIL_SMTP_PORT,
|
|
String MAIL_SMTP_USER,String MAIL_SMTP_PASSWORD,Boolean MAIL_SMTP_AUTH,Boolean MAIL_SMTPS,
|
|
String correos,String asunto,String cuerpo,String correoCC,String correodesde) {
|
|
try {
|
|
Mail m = new Mail();
|
|
m.config(MAIL_FROM,MAIL_SMTP_SERVER,MAIL_SMTP_PORT, MAIL_SMTP_USER, MAIL_SMTP_PASSWORD, MAIL_SMTP_AUTH, MAIL_SMTPS);
|
|
m.setTo(correos);
|
|
m.setCc(correoCC);
|
|
m.setSubject(asunto);
|
|
m.setCont(cuerpo);
|
|
m.setContentMessageType("text/html");
|
|
m.send();
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
/*
|
|
private void envioMail(String[] correos,String asunto,String cuerpo,String correoCC[],String correodesde) throws Exception{
|
|
try {
|
|
LOG.info("INGREA A ENVIAR MAIL");
|
|
LOG.info("correos: "+correos);
|
|
LOG.info("asunto: "+asunto);
|
|
LOG.info("correoCC: "+correoCC);
|
|
LOG.info("correodesde: "+correodesde);
|
|
EnvioCorreoDTO envioCorreo = new EnvioCorreoDTO();
|
|
envioCorreo.setCorreoDesde(correodesde);
|
|
envioCorreo.setCorreoPara(correos);//new String[]{correos}
|
|
envioCorreo.setCorreoCC(correoCC);
|
|
envioCorreo.setAsunto(asunto);
|
|
envioCorreo.setMensajeHTML(cuerpo);
|
|
EnvioMail envioMail = EnvioMailFactory.getInstancia();
|
|
LOG.info("envioMail.envioMail(envioCorreo)");
|
|
envioMail.envioMail(envioCorreo);
|
|
LOG.info("FIN Envio de mail");
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
throw new Exception(e);
|
|
}catch (Throwable e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
*/
|
|
|
|
public EntityManagerFactory getEmf() {
|
|
return emf;
|
|
}
|
|
}
|