package fr.gouv.finances.dgfip.xemelios.batch.chaineimport.mailer;
-import fr.gouv.finances.dgfip.xemelios.batch.Batch;
-import fr.gouv.finances.dgfip.xemelios.batch.BatchRunner;
-import fr.gouv.finances.dgfip.xemelios.batch.chaineimport.importeur.Importer;
-import fr.gouv.finances.dgfip.xemelios.data.impl.pool.PoolManager;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
-import java.nio.CharBuffer;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
-import java.util.Enumeration;
import java.util.GregorianCalendar;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
+
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.apache.log4j.Logger;
+import fr.gouv.finances.dgfip.xemelios.batch.Batch;
+import fr.gouv.finances.dgfip.xemelios.batch.BatchRunner;
+import fr.gouv.finances.dgfip.xemelios.batch.chaineimport.importeur.Importer;
+import fr.gouv.finances.dgfip.xemelios.data.impl.pool.PoolManager;
+
/**
- *
+ *
* @author cmarchand
*/
public class Mailer extends Batch {
- private static final Logger logger = Logger.getLogger(Mailer.class);
- public static final String BATCH_NAME = "mailer";
- public static final int DEFAULT_OUVERTURE = 365;
- public static final int DEFAULT_RAPPEL = 31;
- public static final String PARAM_PERIODE_OUVERTURE = "periode.ouverture";
- public static final String PARAM_PERIODE_RAPPEL = "periode.rappel";
- public static final String PARAM_EMAIL_SUBJECT = "email.subject";
- public static final String PARAM_MAIL_FERMETURE = "mail.fermeture";
- public static final String PARAM_MAIL_RAPPEL = "mail.rappel";
- public static final String PARAM_MAIL_DECHARGEMENT = "mail.dechargement";
- public static final String PARAM_SMTP_SERVER = "smtp.server";
- public static final String PARAM_SMTP_PORT = "smtp.port";
- public static final String PARAM_MAIL_FROM = "mail.from";
- public static final String PARAM_BOUNCE_ADDRESS = "bounce.address";
- public static final String PARAM_SMTP_LOGIN = "smtp.login";
- public static final String PARAM_SMTP_PASSWORD = "smtp.password";
- public static final String PARAM_IMPORT_FULL = "full.import.scheme";
- public static final String PARAM_IMPORT_PURGE = "purge.import.scheme";
- public static final String PARAM_NO_MAIL_USERS = "no.mail.users";
- public static final String PARAM_SMTP_TLS = "smtp.tls";
- private int periodeOuverture = DEFAULT_OUVERTURE;
- private int periodeRappel = DEFAULT_RAPPEL;
- private String emailSubject;
- private String mailFermeture;
- private String mailRappel;
- private String mailDechargement;
- private String smtpServer;
- private int smtpPort = 25;
- private String mailFrom;
- private String bounceAddress;
- private String smtpUser;
- private String smtpPassword;
- private String fullImportScheme;
- private String purgeImportScheme;
- private HashSet<String> avoidMailUsers;
- private boolean tls = false;
- private ExecutorService pool;
+ private static final Logger logger = Logger.getLogger(Mailer.class);
+ public static final String BATCH_NAME = "mailer";
+ public static final int DEFAULT_OUVERTURE = 365;
+ public static final int DEFAULT_RAPPEL = 31;
+ public static final String PARAM_PERIODE_OUVERTURE = "periode.ouverture";
+ public static final String PARAM_PERIODE_RAPPEL = "periode.rappel";
+ public static final String PARAM_EMAIL_SUBJECT = "email.subject";
+ public static final String PARAM_MAIL_FERMETURE = "mail.fermeture";
+ public static final String PARAM_MAIL_RAPPEL = "mail.rappel";
+ public static final String PARAM_MAIL_DECHARGEMENT = "mail.dechargement";
+ public static final String PARAM_SMTP_SERVER = "smtp.server";
+ public static final String PARAM_SMTP_PORT = "smtp.port";
+ public static final String PARAM_MAIL_FROM = "mail.from";
+ public static final String PARAM_BOUNCE_ADDRESS = "bounce.address";
+ public static final String PARAM_SMTP_LOGIN = "smtp.login";
+ public static final String PARAM_SMTP_PASSWORD = "smtp.password";
+ public static final String PARAM_IMPORT_FULL = "full.import.scheme";
+ public static final String PARAM_IMPORT_PURGE = "purge.import.scheme";
+ public static final String PARAM_NO_MAIL_USERS = "no.mail.users";
+ public static final String PARAM_SMTP_TLS = "smtp.tls";
+ private int periodeOuverture = DEFAULT_OUVERTURE;
+ private int periodeRappel = DEFAULT_RAPPEL;
+ private String emailSubject;
+ private String mailFermeture;
+ private String mailRappel;
+ private String mailDechargement;
+ private String smtpServer;
+ private int smtpPort = 25;
+ private String mailFrom;
+ private String bounceAddress;
+ private String smtpUser;
+ private String smtpPassword;
+ private String fullImportScheme;
+ private String purgeImportScheme;
+ private final HashSet<String> avoidMailUsers;
+ private boolean tls = false;
+ private ExecutorService pool;
+
+ private final Hashtable<String, StringBuilder> models;
+
+ public Mailer(final String[] args) {
+ super(args);
+ this.models = new Hashtable<String, StringBuilder>();
+ this.avoidMailUsers = new HashSet<String>();
+ }
- private Hashtable<String,StringBuilder> models;
+ @Override
+ protected void initialize() throws Exception {
+ super.initialize();
+ String sTmp = this.getProps().getProperty(PARAM_PERIODE_OUVERTURE);
+ try {
+ this.periodeOuverture = Integer.parseInt(sTmp);
+ } catch (final Exception ex) {
+ }
+ sTmp = this.getProps().getProperty(PARAM_PERIODE_RAPPEL);
+ try {
+ this.periodeRappel = Integer.parseInt(sTmp);
+ } catch (final Exception ex) {
+ }
+ this.emailSubject = this.getProps().getProperty(PARAM_EMAIL_SUBJECT);
+ this.mailFermeture = this.getProps().getProperty(PARAM_MAIL_FERMETURE);
+ this.mailRappel = this.getProps().getProperty(PARAM_MAIL_RAPPEL);
+ this.mailDechargement = this.getProps().getProperty(PARAM_MAIL_DECHARGEMENT);
+ this.smtpServer = this.getProps().getProperty(PARAM_SMTP_SERVER);
+ sTmp = this.getProps().getProperty(PARAM_SMTP_PORT);
+ try {
+ this.smtpPort = Integer.parseInt(sTmp);
+ } catch (final Exception ex) {
+ }
+ this.mailFrom = this.getProps().getProperty(PARAM_MAIL_FROM);
+ this.bounceAddress = this.getProps().getProperty(PARAM_BOUNCE_ADDRESS);
+ this.smtpUser = this.getProps().getProperty(PARAM_SMTP_LOGIN);
+ this.smtpPassword = this.getProps().getProperty(PARAM_SMTP_PASSWORD);
+ this.fullImportScheme = this.getProps().getProperty(PARAM_IMPORT_FULL);
+ this.purgeImportScheme = this.getProps().getProperty(PARAM_IMPORT_PURGE);
+ sTmp = this.getProps().getProperty(PARAM_NO_MAIL_USERS);
+ if (sTmp != null) {
+ final String[] users = sTmp.split(",");
+ for (final String user : users) {
+ this.avoidMailUsers.add(user.trim());
+ }
+ }
+ sTmp = this.getProps().getProperty(PARAM_SMTP_TLS);
+ if (sTmp != null) {
+ try {
+ this.tls = Boolean.valueOf(sTmp);
+ } catch (final Exception ex) {
+ }
+ }
+ if (this.smtpServer == null) {
+ throw new java.lang.IllegalArgumentException("Le paramètre " + PARAM_SMTP_SERVER + " est obligatoire");
+ }
+ if (this.mailFrom == null) {
+ throw new IllegalArgumentException("Le paramètre " + PARAM_MAIL_FROM + " est obligatoire");
+ }
+ if (this.emailSubject == null) {
+ throw new IllegalArgumentException("Le paramètre " + PARAM_EMAIL_SUBJECT + " est obligatoire");
+ }
+ if (this.mailFermeture == null) {
+ throw new IllegalArgumentException("Le paramètre " + PARAM_MAIL_FERMETURE + " est obligatoire");
+ }
+ if (this.mailRappel == null) {
+ throw new IllegalArgumentException("Le paramètre " + PARAM_MAIL_RAPPEL + " est obligatoire");
+ }
+ if (this.mailDechargement == null) {
+ throw new IllegalArgumentException("Le paramètre " + PARAM_MAIL_DECHARGEMENT + " est obligatoire");
+ }
+ if (this.fullImportScheme == null) {
+ throw new IllegalArgumentException("Le paramètre " + PARAM_IMPORT_FULL + " est obligatoire");
+ }
+ if (this.purgeImportScheme == null) {
+ throw new IllegalArgumentException("Le paramètre " + PARAM_IMPORT_PURGE + " est obligatoire");
+ }
+ }
- public Mailer(String[] args) {
- super(args);
- models = new Hashtable<String, StringBuilder>();
- avoidMailUsers = new HashSet<String>();
- }
+ @Override
+ protected void doProcess() throws Exception {
+ Connection con = null;
+ try {
+ con = PoolManager.getInstance().getConnection();
+ this.pool = Executors.newSingleThreadExecutor();
+ // select
+ // I1.import_id
+ // from
+ // IMPORT_JOBS I1
+ // where
+ // I1.DATE_MESSAGE IS NULL
+ // AND I1.DATE_DEMANDE<?
+ // AND I1.IMPORT_SCHEME=?
+ // and not exists(
+ // select null
+ // from IMPORT_JOBS I2
+ // where
+ // I2.ARCHIVE_NAME=I1.ARCHIVE_NAME
+ // AND I2.DATE_DEMANDE>I1.DATE_DEMANDE
+ // AND I2.IMPORT_SCHEME=?
+ // )
- @Override
- protected void initialize() throws Exception {
- super.initialize();
- String sTmp = getProps().getProperty(PARAM_PERIODE_OUVERTURE);
- try { periodeOuverture = Integer.parseInt(sTmp); } catch(Exception ex) {}
- sTmp = getProps().getProperty(PARAM_PERIODE_RAPPEL);
- try { periodeRappel = Integer.parseInt(sTmp); } catch(Exception ex) {}
- emailSubject = getProps().getProperty(PARAM_EMAIL_SUBJECT);
- mailFermeture = getProps().getProperty(PARAM_MAIL_FERMETURE);
- mailRappel = getProps().getProperty(PARAM_MAIL_RAPPEL);
- mailDechargement = getProps().getProperty(PARAM_MAIL_DECHARGEMENT);
- smtpServer = getProps().getProperty(PARAM_SMTP_SERVER);
- sTmp = getProps().getProperty(PARAM_SMTP_PORT);
- try { smtpPort = Integer.parseInt(sTmp); } catch(Exception ex) {}
- mailFrom = getProps().getProperty(PARAM_MAIL_FROM);
- bounceAddress = getProps().getProperty(PARAM_BOUNCE_ADDRESS);
- smtpUser = getProps().getProperty(PARAM_SMTP_LOGIN);
- smtpPassword = getProps().getProperty(PARAM_SMTP_PASSWORD);
- fullImportScheme = getProps().getProperty(PARAM_IMPORT_FULL);
- purgeImportScheme = getProps().getProperty(PARAM_IMPORT_PURGE);
- sTmp = getProps().getProperty(PARAM_NO_MAIL_USERS);
- if(sTmp!=null) {
- String[] users = sTmp.split(",");
- for(String user:users)
- avoidMailUsers.add(user.trim());
- }
- sTmp = getProps().getProperty(PARAM_SMTP_TLS);
- if(sTmp!=null)
- try {
- tls = Boolean.valueOf(sTmp);
- } catch(Exception ex) {}
- if(smtpServer==null) throw new java.lang.IllegalArgumentException("Le paramètre "+PARAM_SMTP_SERVER+" est obligatoire");
- if(mailFrom==null) throw new IllegalArgumentException("Le paramètre "+PARAM_MAIL_FROM+" est obligatoire");
- if(emailSubject==null) throw new IllegalArgumentException("Le paramètre "+PARAM_EMAIL_SUBJECT+" est obligatoire");
- if(mailFermeture==null) throw new IllegalArgumentException("Le paramètre "+PARAM_MAIL_FERMETURE+" est obligatoire");
- if(mailRappel==null) throw new IllegalArgumentException("Le paramètre "+PARAM_MAIL_RAPPEL+" est obligatoire");
- if(mailDechargement==null) throw new IllegalArgumentException("Le paramètre "+PARAM_MAIL_DECHARGEMENT+" est obligatoire");
- if(fullImportScheme==null) throw new IllegalArgumentException("Le paramètre "+PARAM_IMPORT_FULL+" est obligatoire");
- if(purgeImportScheme==null) throw new IllegalArgumentException("Le paramètre "+PARAM_IMPORT_PURGE+" est obligatoire");
- }
-
- @Override
- protected void doProcess() throws Exception {
- Connection con = null;
- try {
- con = PoolManager.getInstance().getConnection();
- pool = Executors.newSingleThreadExecutor();
-//select
-// I1.import_id
-//from
-// IMPORT_JOBS I1
-//where
-// I1.DATE_MESSAGE IS NULL
-// AND I1.DATE_DEMANDE<?
-// AND I1.IMPORT_SCHEME=?
-// and not exists(
-// select null
-// from IMPORT_JOBS I2
-// where
-// I2.ARCHIVE_NAME=I1.ARCHIVE_NAME
-// AND I2.DATE_DEMANDE>I1.DATE_DEMANDE
-// AND I2.IMPORT_SCHEME=?
-//)
+ PreparedStatement ps = con
+ .prepareStatement("select I1.import_id from IMPORT_JOBS I1 where I1.DATE_MESSAGE IS NULL AND I1.DATE_DEMANDE<? AND I1.IMPORT_SCHEME=? and not exists(select null from IMPORT_JOBS I2 where I2.ARCHIVE_NAME=I1.ARCHIVE_NAME AND I2.DATE_DEMANDE>I1.DATE_DEMANDE AND I2.IMPORT_SCHEME=?)");
+ final Date now = new Date();
+ final Calendar cal = new GregorianCalendar();
+ cal.setTime(now);
+ cal.add(Calendar.DAY_OF_MONTH, -this.periodeOuverture);
+ ps.setDate(1, new java.sql.Date(cal.getTime().getTime()));
+ ps.setString(2, this.fullImportScheme);
+ ps.setString(3, this.purgeImportScheme);
+ ResultSet rs = ps.executeQuery();
+ while (rs.next()) {
+ final MailerImpl mi = new MailerImpl(rs.getLong(1), ACTION.FERMETURE);
+ this.pool.submit(mi);
+ }
+ // les rappels
+ ps = con.prepareStatement("select I1.import_id from IMPORT_JOBS I1 where I1.DATE_MESSAGE<? AND I1.DATE_RAPPEL IS NULL AND I1.IMPORT_SCHEME=? and not exists(select null from IMPORT_JOBS I2 where I2.ARCHIVE_NAME=I1.ARCHIVE_NAME AND I2.DATE_DEMANDE>I1.DATE_DEMANDE AND I2.IMPORT_SCHEME=?)");
+ final Calendar message = new GregorianCalendar();
+ message.setTime(now);
+ message.add(Calendar.DAY_OF_MONTH, -this.periodeRappel);
+ ps.setDate(1, new java.sql.Date(message.getTime().getTime()));
+ ps.setString(2, this.fullImportScheme);
+ ps.setString(3, this.purgeImportScheme);
+ rs = ps.executeQuery();
+ while (rs.next()) {
+ final MailerImpl mi = new MailerImpl(rs.getLong(1), ACTION.RAPPEL);
+ this.pool.submit(mi);
+ }
+ // les purges
+ ps = con.prepareStatement("select IMPORT_ID from IMPORT_JOBS where IMPORT_SCHEME=? and IMPORT_STATUS=? and DATE_CONFIRM IS NULL");
+ ps.setString(1, this.purgeImportScheme);
+ ps.setInt(2, Importer.IMPORT_JOB_STATUS_TERMINATED);
+ rs = ps.executeQuery();
+ while (rs.next()) {
+ final MailerImpl mi = new MailerImpl(rs.getLong(1), ACTION.DECHARGEMENT);
+ this.pool.submit(mi);
+ }
+ } catch (final Exception ex) {
+ logger.error("doProcess()", ex);
+ } finally {
+ if (con != null) {
+ PoolManager.getInstance().releaseConnection(con);
+ }
+ }
+ }
- PreparedStatement ps = con.prepareStatement("select I1.import_id from IMPORT_JOBS I1 where I1.DATE_MESSAGE IS NULL AND I1.DATE_DEMANDE<? AND I1.IMPORT_SCHEME=? and not exists(select null from IMPORT_JOBS I2 where I2.ARCHIVE_NAME=I1.ARCHIVE_NAME AND I2.DATE_DEMANDE>I1.DATE_DEMANDE AND I2.IMPORT_SCHEME=?)");
- Date now = new Date();
- Calendar cal = new GregorianCalendar();
- cal.setTime(now);
- cal.add(Calendar.DAY_OF_MONTH, -periodeOuverture);
- ps.setDate(1,new java.sql.Date(cal.getTime().getTime()));
- ps.setString(2, fullImportScheme);
- ps.setString(3, purgeImportScheme);
- ResultSet rs = ps.executeQuery();
- while(rs.next()) {
- MailerImpl mi = new MailerImpl(rs.getLong(1), ACTION.FERMETURE);
- pool.submit(mi);
- }
- // les rappels
- ps = con.prepareStatement("select I1.import_id from IMPORT_JOBS I1 where I1.DATE_MESSAGE<? AND I1.DATE_RAPPEL IS NULL AND I1.IMPORT_SCHEME=? and not exists(select null from IMPORT_JOBS I2 where I2.ARCHIVE_NAME=I1.ARCHIVE_NAME AND I2.DATE_DEMANDE>I1.DATE_DEMANDE AND I2.IMPORT_SCHEME=?)");
- Calendar message = new GregorianCalendar();
- message.setTime(now);
- message.add(Calendar.DAY_OF_MONTH, -periodeRappel);
- ps.setDate(1,new java.sql.Date(message.getTime().getTime()));
- ps.setString(2, fullImportScheme);
- ps.setString(3, purgeImportScheme);
- rs = ps.executeQuery();
- while(rs.next()) {
- MailerImpl mi = new MailerImpl(rs.getLong(1), ACTION.RAPPEL);
- pool.submit(mi);
- }
- // les purges
- ps = con.prepareStatement("select IMPORT_ID from IMPORT_JOBS where IMPORT_SCHEME=? and IMPORT_STATUS=? and DATE_CONFIRM IS NULL");
- ps.setString(1, purgeImportScheme);
- ps.setInt(2, Importer.IMPORT_JOB_STATUS_TERMINATED);
- rs = ps.executeQuery();
- while(rs.next()) {
- MailerImpl mi = new MailerImpl(rs.getLong(1), ACTION.DECHARGEMENT);
- pool.submit(mi);
- }
- } catch(Exception ex) {
- logger.error("doProcess()",ex);
- } finally {
- if(con!=null)
- PoolManager.getInstance().releaseConnection(con);
- }
- }
+ @Override
+ public String getResumeTraitement() {
+ return "En cours";
+ }
- @Override
- public String getResumeTraitement() {
- return "En cours";
- }
+ @Override
+ public String typeTraitementRefCode() {
+ return BATCH_NAME;
+ }
- @Override
- public String typeTraitementRefCode() {
- return BATCH_NAME;
- }
+ @Override
+ public String getInformations() {
+ return "";
+ }
- @Override
- public String getInformations() {
- return "";
- }
+ @Override
+ protected String getBatchVersion() {
+ return "1.0";
+ }
- @Override
- protected String getBatchVersion() {
- return "1.0";
- }
+ private class MailerImpl extends RunnableBatch {
+ private final long importId;
+ private final ACTION action;
+ private final SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
- private class MailerImpl extends RunnableBatch {
- private long importId;
- private ACTION action;
- private SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
+ MailerImpl(final long importId, final ACTION action) {
+ super();
+ this.importId = importId;
+ this.action = action;
+ }
- MailerImpl(long importId, ACTION action) {
- super();
- this.importId = importId;
- this.action = action;
- }
+ @Override
+ public String getInformations() {
+ return "";
+ }
- @Override
- public String getInformations() {
- return "";
- }
+ @Override
+ public void run() {
+ Connection con = null;
+ try {
+ con = PoolManager.getInstance().getConnection();
+ PreparedStatement ps = con.prepareStatement("SELECT ARCHIVE_NAME,EXERCICE,USER_LOGIN,DATE_DEMANDE,IMPORT_STATUS,DATE_MESSAGE,DATE_RAPPEL,DATE_CONFIRM FROM IMPORT_JOBS WHERE IMPORT_ID=?");
+ ps.setLong(1, this.importId);
+ final ResultSet rs = ps.executeQuery();
+ rs.next();
+ int i = 1;
+ final String archiveName = rs.getString(i++);
+ final String exercice = rs.getString(i++);
+ final String userLogin = rs.getString(i++);
+ final java.sql.Timestamp dateDemande = rs.getTimestamp(i++);
+ final int importStatus = rs.getInt(i++);
+ java.sql.Date dateMessage = rs.getDate(i++);
+ if (rs.wasNull()) {
+ dateMessage = null;
+ }
+ java.sql.Date dateRappel = rs.getDate(i++);
+ if (rs.wasNull()) {
+ dateRappel = null;
+ }
+ java.sql.Date dateConfirm = rs.getDate(i++);
+ if (rs.wasNull()) {
+ dateConfirm = null;
+ }
+ String mailFileName = null;
+ if (!Mailer.this.avoidMailUsers.contains(userLogin)) {
+ switch (this.action) {
+ case FERMETURE: {
+ mailFileName = Mailer.this.mailFermeture;
+ break;
+ }
+ case RAPPEL: {
+ mailFileName = Mailer.this.mailRappel;
+ break;
+ }
+ default:
+ mailFileName = Mailer.this.mailDechargement;
+ }
+ final StringBuilder sb = Mailer.this.getMailModel(mailFileName);
+ stringBuilderReplace(sb, "${ARCHIVE_NAME}", archiveName);
+ stringBuilderReplace(sb, "${EXERCICE}", exercice);
+ stringBuilderReplace(sb, "${USER_LOGIN}", userLogin);
+ stringBuilderReplace(sb, "${DATE_DEMANDE}", this.sdf.format(dateDemande));
+ stringBuilderReplace(sb, "${IMPORT_STATUS}", getImportStatusText(importStatus));
+ stringBuilderReplace(sb, "${DATE_MESSAGE}", dateMessage != null ? this.sdf.format(dateMessage) : "xx/xx/xxxx");
+ stringBuilderReplace(sb, "${DATE_RAPPEL}", dateRappel != null ? this.sdf.format(dateRappel) : "xx/xx/xxxx");
+ stringBuilderReplace(sb, "${DATE_CONFIRM}", dateConfirm != null ? this.sdf.format(dateConfirm) : "xx/xx/xxxx");
+ final HtmlEmail email = new HtmlEmail();
+ email.setHtmlMsg(sb.toString());
+ email.setHostName(Mailer.this.smtpServer);
+ email.setSmtpPort(Mailer.this.smtpPort);
+ if (Mailer.this.tls) {
+ email.setTLS(true);
+ }
+ if (Mailer.this.smtpUser != null && Mailer.this.smtpPassword != null) {
+ email.setAuthentication(Mailer.this.smtpUser, Mailer.this.smtpPassword);
+ }
+ email.setFrom(Mailer.this.mailFrom);
+ email.addTo(userLogin);
+ email.setSubject(Mailer.this.emailSubject);
+ if (Mailer.this.bounceAddress != null) {
+ email.setBounceAddress(Mailer.this.bounceAddress);
+ }
+ try {
+ email.send();
+ } catch (final EmailException emEx) {
+ logger.error("send mail error", emEx);
+ }
+ }
+ String update = "UPDATE IMPORT_JOBS set ";
+ switch (this.action) {
+ case FERMETURE:
+ update = update + "DATE_MESSAGE";
+ break;
+ case RAPPEL:
+ update = update + "DATE_RAPPEL";
+ break;
+ default:
+ update = update + "DATE_COMFIRM";
+ }
+ update = update + "=? WHERE IMPORT_ID=?";
+ ps = con.prepareStatement(update);
+ ps.setDate(1, new java.sql.Date(System.currentTimeMillis()));
+ ps.setLong(2, this.importId);
+ ps.executeUpdate();
+ } catch (final Exception ex) {
+ logger.error("impl<" + this.importId + ">", ex);
+ } finally {
+ if (con != null) {
+ PoolManager.getInstance().releaseConnection(con);
+ }
+ }
+ }
+ }
- @Override
- public void run() {
- Connection con = null;
- try {
- con = PoolManager.getInstance().getConnection();
- PreparedStatement ps = con.prepareStatement("SELECT ARCHIVE_NAME,EXERCICE,USER_LOGIN,DATE_DEMANDE,IMPORT_STATUS,DATE_MESSAGE,DATE_RAPPEL,DATE_CONFIRM FROM IMPORT_JOBS WHERE IMPORT_ID=?");
- ps.setLong(1,importId);
- ResultSet rs = ps.executeQuery();
- rs.next();
- int i =1;
- String archiveName = rs.getString(i++);
- String exercice = rs.getString(i++);
- String userLogin = rs.getString(i++);
- java.sql.Timestamp dateDemande = rs.getTimestamp(i++);
- int importStatus = rs.getInt(i++);
- java.sql.Date dateMessage = rs.getDate(i++);
- if(rs.wasNull()) dateMessage = null;
- java.sql.Date dateRappel = rs.getDate(i++);
- if(rs.wasNull()) dateRappel = null;
- java.sql.Date dateConfirm = rs.getDate(i++);
- if(rs.wasNull()) dateConfirm = null;
- String mailFileName = null;
- if(!avoidMailUsers.contains(userLogin)) {
- switch(action) {
- case FERMETURE: { mailFileName = mailFermeture; break; }
- case RAPPEL: { mailFileName = mailRappel; break; }
- default : mailFileName = mailDechargement;
- }
- StringBuilder sb = getMailModel(mailFileName);
- stringBuilderReplace(sb, "${ARCHIVE_NAME}", archiveName);
- stringBuilderReplace(sb, "${EXERCICE}", exercice);
- stringBuilderReplace(sb, "${USER_LOGIN}", userLogin);
- stringBuilderReplace(sb, "${DATE_DEMANDE}", sdf.format(dateDemande));
- stringBuilderReplace(sb, "${IMPORT_STATUS}", getImportStatusText(importStatus));
- stringBuilderReplace(sb, "${DATE_MESSAGE}", dateMessage!=null ? sdf.format(dateMessage) : "xx/xx/xxxx");
- stringBuilderReplace(sb, "${DATE_RAPPEL}", dateRappel!=null ? sdf.format(dateRappel) : "xx/xx/xxxx");
- stringBuilderReplace(sb, "${DATE_CONFIRM}", dateConfirm!=null ? sdf.format(dateConfirm) : "xx/xx/xxxx");
- HtmlEmail email = new HtmlEmail();
- email.setHtmlMsg(sb.toString());
- email.setHostName(smtpServer);
- email.setSmtpPort(smtpPort);
- if(tls) email.setTLS(true);
- if(smtpUser!=null && smtpPassword!=null)
- email.setAuthentication(smtpUser, smtpPassword);
- email.setFrom(mailFrom);
- email.addTo(userLogin);
- email.setSubject(emailSubject);
- if(bounceAddress!=null)
- email.setBounceAddress(bounceAddress);
- try {
- email.send();
- } catch(EmailException emEx) {
- logger.error("send mail error",emEx);
- }
- }
- String update = "UPDATE IMPORT_JOBS set ";
- switch(action) {
- case FERMETURE: update=update+"DATE_MESSAGE"; break;
- case RAPPEL: update=update+"DATE_RAPPEL"; break;
- default: update=update+"DATE_COMFIRM";
- }
- update = update+"=? WHERE IMPORT_ID=?";
- ps = con.prepareStatement(update);
- ps.setDate(1,new java.sql.Date(System.currentTimeMillis()));
- ps.setLong(2,importId);
- ps.executeUpdate();
- } catch(Exception ex) {
- logger.error("impl<"+importId+">",ex);
- } finally {
- if(con!=null) PoolManager.getInstance().releaseConnection(con);
- }
- }
- }
+ private StringBuilder getMailModel(final String fileName) {
+ StringBuilder ret = this.models.get(fileName);
+ if (ret == null) {
+ try {
- private StringBuilder getMailModel(String fileName) {
- StringBuilder ret = models.get(fileName);
- if(ret==null) {
- try {
+ final File f = new File(fileName.replace("%basedir%", BatchRunner.getCanonicalBaseDir()));
+ final FileInputStream fis = new FileInputStream(f);
+ final InputStreamReader isr = new InputStreamReader(fis);
+ ret = new StringBuilder();
+ final char[] buff = new char[2048];
+ int read = 0;
+ while ((read = isr.read(buff)) > 0) {
+ ret.append(buff, 0, read);
+ }
+ fis.close();
+ this.models.put(fileName, ret);
+ } catch (final Exception ex) {
+ logger.error("lecture de " + fileName, ex);
+ }
+ }
+ return ret;
+ }
- File f = new File(fileName.replace("%basedir%", BatchRunner.getCanonicalBaseDir()));
- FileInputStream fis = new FileInputStream(f);
- InputStreamReader isr = new InputStreamReader(fis);
- ret = new StringBuilder();
- char[] buff = new char[2048];
- int read = 0;
- while((read=isr.read(buff)) > 0) {
- ret.append(buff, 0, read);
- }
- fis.close();
- models.put(fileName, ret);
- } catch(Exception ex) {
- logger.error("lecture de "+fileName, ex);
- }
- }
- return ret;
- }
+ private enum ACTION {
+ FERMETURE, RAPPEL, DECHARGEMENT
+ };
- private enum ACTION {
- FERMETURE, RAPPEL, DECHARGEMENT
- };
+ public static void stringBuilderReplace(final StringBuilder builder, final String strWhat, final String strBy) {
+ stringBuilderReplace(builder, strWhat, strBy, Integer.MAX_VALUE);
+ }
- public static void stringBuilderReplace(StringBuilder builder, String strWhat, String strBy) {
- stringBuilderReplace(builder, strWhat, strBy, Integer.MAX_VALUE);
- }
+ public static void stringBuilderReplace(final StringBuilder builder, final String strWhat, final String strBy, final int maxOccurences) {
+ int pos = 0, occur = 0;
+ while (occur < maxOccurences && (pos = builder.indexOf(strWhat)) > 0) {
+ builder.replace(pos, pos + strWhat.length(), strBy != null ? strBy : "");
+ occur++;
+ }
+ }
- public static void stringBuilderReplace(StringBuilder builder, String strWhat, String strBy, int maxOccurences) {
- int pos = 0, occur = 0;
- while (occur < maxOccurences && (pos = builder.indexOf(strWhat)) > 0) {
- builder.replace(pos, pos + strWhat.length(), strBy != null ? strBy : "");
- occur++;
- }
- }
- private static String getImportStatusText(int status) {
- switch(status) {
- case Importer.IMPORT_JOB_STATUS_ASKED: return "Demandé";
- case Importer.IMPORT_JOB_STATUS_CANCELED: return "Annulé";
- case Importer.IMPORT_JOB_STATUS_ERROR: return "Erreur";
- case Importer.IMPORT_JOB_STATUS_RUNNING: return "En cours";
- default: return "Terminé";
- }
- }
+ private static String getImportStatusText(final int status) {
+ switch (status) {
+ case Importer.IMPORT_JOB_STATUS_ASKED:
+ return "Demandé";
+ case Importer.IMPORT_JOB_STATUS_CANCELED:
+ return "Annulé";
+ case Importer.IMPORT_JOB_STATUS_ERROR:
+ return "Erreur";
+ case Importer.IMPORT_JOB_STATUS_RUNNING:
+ return "En cours";
+ default:
+ return "Terminé";
+ }
+ }
}