package fr.gouv.finances.dgfip.xemelios.batch.suivi;
-import fr.gouv.finances.dgfip.xemelios.data.DataLayerManager;
-import fr.gouv.finances.dgfip.xemelios.data.impl.pool.PoolManager;
import java.io.ByteArrayInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
+
import org.apache.log4j.Logger;
+import fr.gouv.finances.dgfip.xemelios.data.DataLayerManager;
+import fr.gouv.finances.dgfip.xemelios.data.impl.pool.PoolManager;
+
/**
- *
+ *
* @author cmarchand
*/
public class Traitement {
- private static final Logger logger = Logger.getLogger(Traitement.class);
- public static final transient String SEVERITY_INFO = "I";
- public static final transient String SEVERITY_WARN = "EN";
- public static final transient String SEVERITY_ERROR = "EB";
- private long id;
- /**
- * Class lancée pour ce traitement
- */
- private String className;
- /**
- * Nom donné au traitement, issu du fichier de configuration
- */
- private String batchName;
- private Timestamp start;
- private Timestamp end;
- private String severity;
- private String resume;
- private List<Message> messages;
-
- public Traitement(Class batch, String batchName) {
- super();
- this.className = batch.getName();
- this.batchName = batchName;
- this.start = new Timestamp(System.currentTimeMillis());
- this.severity = SEVERITY_INFO;
- messages = new ArrayList<Message>();
- createInDatabase();
- }
- public long getStart() { return start.getTime(); }
- public void setEnd(long end) {
- this.end = new Timestamp(end);
- updateEnd();
- }
- public void setResume(String resume) {
- this.resume = resume;
- updateResume();
- }
- public void setSeverity(String severity) {
- this.severity = severity;
- updateSeverity();
- }
- public Message addMessage(String severity, String message) {
- Message msg = new Message(this, severity, message);
- messages.add(msg);
- __updateSeverity(severity);
- return msg;
- }
- /**
- * Stocke les données binaires sur ce traitement. Attention,
- * ;enregistre les données telles quelle. Il faut donc les
- * GZipper avant d'appeler cette méthode
- * @param data Les données GZippées
- */
- public void setBinaryData(byte[] data) {
- Connection con = null;
- try {
- con = PoolManager.getInstance().getConnection();
- PreparedStatement ps = con.prepareStatement("update TRAITEMENT set TRT_BINARY_DATA=? where TRT_ID=?");
- ps.setBinaryStream(1, new ByteArrayInputStream(data), data.length);
- ps.setLong(2, id);
- ps.executeUpdate();
- } catch(Throwable ex) {
- logger.error("Traitement.setBinaryData(byte[])",ex);
- } finally {
- if(con!=null) PoolManager.getInstance().releaseConnection(con);
- }
- }
- public long getId() {
- return id;
- }
- protected void createInDatabase() {
- Connection con = null;
- try {
- con = PoolManager.getInstance().getConnection();
- if("mysql".equals(DataLayerManager.getImplementation().getLayerName())) {
- String sql = "insert into TRAITEMENT(TRT_ID,TRT_CLASS,TRT_DESC,TRT_DATE_DEBUT,TRT_CODE_GRAVITE) VALUES(?,?,?,?,?)";
- con.createStatement().executeUpdate("LOCK TABLES TRAITEMENT WRITE");
- ResultSet rs = con.createStatement().executeQuery("SELECT MAX(TRT_ID)+1 FROM TRAITEMENT");
- if(rs.next()) id = rs.getLong(1);
- else id = 1;
- rs.close();
- PreparedStatement ps = con.prepareStatement(sql);
- ps.setLong(1,id);
- ps.setString(2,className);
- ps.setString(3, batchName);
- ps.setTimestamp(4, start);
- ps.setString(5,severity);
- ps.execute();
- ps.close();
- con.createStatement().executeUpdate("UNLOCK TABLES");
- } else if("oracle".equals(DataLayerManager.getImplementation().getLayerName())) {
- ResultSet rs = con.createStatement().executeQuery("select SQ_TRAITEMENT.NEXTVAL from dual");
- if(rs.next())
- id = rs.getLong(1);
- else
- id = 1;
-
- String sql = "insert into TRAITEMENT(TRT_ID,TRT_CLASS,TRT_DESC,TRT_CODE_GRAVITE) VALUES(?,?,?,?)";
-
- logger.debug(sql + " --> with id : " + id);
-
- PreparedStatement ps = con.prepareStatement(sql);
- ps.setLong(1,id);
- ps.setString(2, className);
- ps.setString(3, batchName);
- ps.setString(4,severity);
- ps.execute();
- ps.close();
- }
- } catch(Exception ex) {
- logger.error("Traitement.createInDatabase()",ex);
- } finally {
- if(con!=null) PoolManager.getInstance().releaseConnection(con);
- }
- }
- protected void updateEnd() {
- Connection con = null;
- try {
- con = PoolManager.getInstance().getConnection();
- PreparedStatement ps = con.prepareStatement("update TRAITEMENT set TRT_DATE_FIN=? where TRT_ID=?");
- ps.setTimestamp(1, end);
- ps.setLong(2,id);
- ps.executeUpdate();
- } catch(Exception ex) {
- logger.error("Traitement.updateEnd()",ex);
- } finally {
- if(con!=null) PoolManager.getInstance().releaseConnection(con);
- }
- }
- protected void updateResume() {
- Connection con = null;
- try {
- con = PoolManager.getInstance().getConnection();
- PreparedStatement ps = con.prepareStatement("update TRAITEMENT set TRT_RESUME=? where TRT_ID=?");
- ps.setString(1, resume);
- ps.setLong(2,id);
- ps.executeUpdate();
- } catch(Exception ex) {
- logger.error("Traitement.updateResume()",ex);
- } finally {
- if(con!=null) PoolManager.getInstance().releaseConnection(con);
- }
- }
- protected void updateSeverity() {
- Connection con = null;
- try {
- con = PoolManager.getInstance().getConnection();
- PreparedStatement ps = con.prepareStatement("update TRAITEMENT set TRT_CODE_GRAVITE=? where TRT_ID=?");
- ps.setString(1, severity);
- ps.setLong(2,id);
- ps.executeUpdate();
- } catch(Exception ex) {
- logger.error("Traitement.updateSeverity()",ex);
- } finally {
- if(con!=null) PoolManager.getInstance().releaseConnection(con);
- }
- }
- void __updateSeverity(String newSeverity) {
- int localRank = getSeverityRank(severity);
- int newRank = getSeverityRank(newSeverity);
- if(newRank>localRank) {
- setSeverity(newSeverity);
- }
- }
- private static int getSeverityRank(String sev) {
- if(sev.equals(SEVERITY_INFO)) return 1;
- else if(sev.equals(SEVERITY_WARN)) return 2;
- else return 3;
- }
+ private static final Logger logger = Logger.getLogger(Traitement.class);
+ public static final transient String SEVERITY_INFO = "I";
+ public static final transient String SEVERITY_WARN = "EN";
+ public static final transient String SEVERITY_ERROR = "EB";
+ private long id;
+ /**
+ * Class lancée pour ce traitement
+ */
+ private final String className;
+ /**
+ * Nom donné au traitement, issu du fichier de configuration
+ */
+ private final String batchName;
+ private final Timestamp start;
+ private Timestamp end;
+ private String severity;
+ private String resume;
+ private final List<Message> messages;
+
+ public Traitement(final Class batch, final String batchName) {
+ super();
+ this.className = batch.getName();
+ this.batchName = batchName;
+ this.start = new Timestamp(System.currentTimeMillis());
+ this.severity = SEVERITY_INFO;
+ this.messages = new ArrayList<Message>();
+ this.createInDatabase();
+ }
+
+ public long getStart() {
+ return this.start.getTime();
+ }
+
+ public void setEnd(final long end) {
+ this.end = new Timestamp(end);
+ this.updateEnd();
+ }
+
+ public void setResume(final String resume) {
+ this.resume = resume;
+ this.updateResume();
+ }
+
+ public void setSeverity(final String severity) {
+ this.severity = severity;
+ this.updateSeverity();
+ }
+
+ public Message addMessage(final String severity, final String message) {
+ final Message msg = new Message(this, severity, message);
+ this.messages.add(msg);
+ this.__updateSeverity(severity);
+ return msg;
+ }
+
+ /**
+ * Stocke les données binaires sur ce traitement. Attention, ;enregistre les données telles quelle. Il faut donc les GZipper avant d'appeler cette méthode
+ *
+ * @param data
+ * Les données GZippées
+ */
+ public void setBinaryData(final byte[] data) {
+ Connection con = null;
+ try {
+ con = PoolManager.getInstance().getConnection();
+ final PreparedStatement ps = con.prepareStatement("update TRAITEMENT set TRT_BINARY_DATA=? where TRT_ID=?");
+ ps.setBinaryStream(1, new ByteArrayInputStream(data), data.length);
+ ps.setLong(2, this.id);
+ ps.executeUpdate();
+ } catch (final Throwable ex) {
+ logger.error("Traitement.setBinaryData(byte[])", ex);
+ } finally {
+ if (con != null) {
+ PoolManager.getInstance().releaseConnection(con);
+ }
+ }
+ }
+
+ public long getId() {
+ return this.id;
+ }
+
+ protected void createInDatabase() {
+ Connection con = null;
+ try {
+ con = PoolManager.getInstance().getConnection();
+ if ("mysql".equals(DataLayerManager.getImplementation().getLayerName())) {
+ final String sql = "insert into TRAITEMENT(TRT_ID,TRT_CLASS,TRT_DESC,TRT_DATE_DEBUT,TRT_CODE_GRAVITE) VALUES(?,?,?,?,?)";
+ con.createStatement().executeUpdate("LOCK TABLES TRAITEMENT WRITE");
+ final ResultSet rs = con.createStatement().executeQuery("SELECT MAX(TRT_ID)+1 FROM TRAITEMENT");
+ if (rs.next()) {
+ this.id = rs.getLong(1);
+ } else {
+ this.id = 1;
+ }
+ rs.close();
+ final PreparedStatement ps = con.prepareStatement(sql);
+ ps.setLong(1, this.id);
+ ps.setString(2, this.className);
+ ps.setString(3, this.batchName);
+ ps.setTimestamp(4, this.start);
+ ps.setString(5, this.severity);
+ ps.execute();
+ ps.close();
+ con.createStatement().executeUpdate("UNLOCK TABLES");
+ } else if ("oracle".equals(DataLayerManager.getImplementation().getLayerName())) {
+ final ResultSet rs = con.createStatement().executeQuery("select SQ_TRAITEMENT.NEXTVAL from dual");
+ if (rs.next()) {
+ this.id = rs.getLong(1);
+ } else {
+ this.id = 1;
+ }
+
+ final String sql = "insert into TRAITEMENT(TRT_ID,TRT_CLASS,TRT_DESC,TRT_CODE_GRAVITE) VALUES(?,?,?,?)";
+
+ logger.debug(sql + " --> with id : " + this.id);
+
+ final PreparedStatement ps = con.prepareStatement(sql);
+ ps.setLong(1, this.id);
+ ps.setString(2, this.className);
+ ps.setString(3, this.batchName);
+ ps.setString(4, this.severity);
+ ps.execute();
+ ps.close();
+ }
+ } catch (final Exception ex) {
+ logger.error("Traitement.createInDatabase()", ex);
+ } finally {
+ if (con != null) {
+ PoolManager.getInstance().releaseConnection(con);
+ }
+ }
+ }
+
+ protected void updateEnd() {
+ Connection con = null;
+ try {
+ con = PoolManager.getInstance().getConnection();
+ final PreparedStatement ps = con.prepareStatement("update TRAITEMENT set TRT_DATE_FIN=? where TRT_ID=?");
+ ps.setTimestamp(1, this.end);
+ ps.setLong(2, this.id);
+ ps.executeUpdate();
+ } catch (final Exception ex) {
+ logger.error("Traitement.updateEnd()", ex);
+ } finally {
+ if (con != null) {
+ PoolManager.getInstance().releaseConnection(con);
+ }
+ }
+ }
+
+ protected void updateResume() {
+ Connection con = null;
+ try {
+ con = PoolManager.getInstance().getConnection();
+ final PreparedStatement ps = con.prepareStatement("update TRAITEMENT set TRT_RESUME=? where TRT_ID=?");
+ ps.setString(1, this.resume);
+ ps.setLong(2, this.id);
+ ps.executeUpdate();
+ } catch (final Exception ex) {
+ logger.error("Traitement.updateResume()", ex);
+ } finally {
+ if (con != null) {
+ PoolManager.getInstance().releaseConnection(con);
+ }
+ }
+ }
+
+ protected void updateSeverity() {
+ Connection con = null;
+ try {
+ con = PoolManager.getInstance().getConnection();
+ final PreparedStatement ps = con.prepareStatement("update TRAITEMENT set TRT_CODE_GRAVITE=? where TRT_ID=?");
+ ps.setString(1, this.severity);
+ ps.setLong(2, this.id);
+ ps.executeUpdate();
+ } catch (final Exception ex) {
+ logger.error("Traitement.updateSeverity()", ex);
+ } finally {
+ if (con != null) {
+ PoolManager.getInstance().releaseConnection(con);
+ }
+ }
+ }
+
+ void __updateSeverity(final String newSeverity) {
+ final int localRank = getSeverityRank(this.severity);
+ final int newRank = getSeverityRank(newSeverity);
+ if (newRank > localRank) {
+ this.setSeverity(newSeverity);
+ }
+ }
+
+ private static int getSeverityRank(final String sev) {
+ if (sev.equals(SEVERITY_INFO)) {
+ return 1;
+ } else if (sev.equals(SEVERITY_WARN)) {
+ return 2;
+ } else {
+ return 3;
+ }
+ }
}