package fr.gouv.finances.dgfip.xemelios.batch.pdf;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+
+import org.apache.log4j.Logger;
+
import fr.gouv.finances.cp.xemelios.data.impl.MySqlDataLayer;
import fr.gouv.finances.dgfip.xemelios.auth.XemeliosUser;
import fr.gouv.finances.dgfip.xemelios.batch.Batch;
import fr.gouv.finances.dgfip.xemelios.data.DataImpl;
import fr.gouv.finances.dgfip.xemelios.data.DataLayerManager;
import fr.gouv.finances.dgfip.xemelios.data.impl.pool.PoolManager;
-import java.sql.Connection;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-import org.apache.log4j.Logger;
/**
- *
+ *
* @author cmarchand
*/
public class PdfCreator extends Batch {
- private static final Logger logger = Logger.getLogger(PdfCreator.class);
- public static final String PROP_DOCUMENTS_DEF_DIR = "documents-def.dir";
-// public static final String PROP_DATALAYER_NAME = "datalayer.name";
- public static final String PROP_DOCUMENT_ID = "document.id";
- public static final Integer STATUS_NONE = null;
- public static final Integer STATUS_IDLE = 0;
- public static final Integer STATUS_RUNNING = 1;
- public static final Integer STATUS_TERMINATED = 2;
- public static final Integer STATUS_ERROR = 3;
-
- private static ExecutorService runner = Executors.newFixedThreadPool(3);
-
- private String documentId;
-
- public PdfCreator(String args[]) {
- super(args);
- }
-
- @Override
- protected void initialize() throws Exception {
- super.initialize();
- documentId = getProps().getProperty(PROP_DOCUMENT_ID);
- }
-
- @Override
- protected void doProcess() throws Exception {
- logger.debug("running PdfCreator for document "+documentId);
- XemeliosUser user = new User();
- Connection con = null;
- try {
- EtatModel em = BatchRunner.getInstance().getDocuments().getDocumentById("cg-etat").getEtatById("BalanceGenerale");
- con = PoolManager.getInstance().getConnection();
- DataImpl impl = DataLayerManager.getImplementation();
- if(impl instanceof MySqlDataLayer) {
- MySqlDataLayer msImpl = (MySqlDataLayer)impl;
- if("compteGestionEtat".equals(documentId)) {
- String select = "SELECT COLLECTIVITE, BUDGET, EXERCICE FROM PDF_CREATION WHERE DOCUMENT_ID=? AND ETAT_ID=? AND CREATION_STATUS IS NULL";
- PreparedStatement ps = con.prepareStatement(select);
- ps.setString(1,documentId);
- ps.setString(2,"BalanceGenerale");
- ResultSet rs = ps.executeQuery();
- while(rs.next()) {
- String collectivite = rs.getString(1);
- String budget = rs.getString(2);
- int exercice = rs.getInt(3);
- PdfCreatorImpl process = new PdfCreatorImpl(documentId, "BalanceGenerale", collectivite, budget, exercice, user, msImpl, em);
- runner.submit(process);
- }
-
- }
- } else {
- throw new RuntimeException("PdfCreator only runs with MysqlDataLayer");
- }
- } catch(Exception ex) {
- logger.error("in doProcess",ex);
- } finally {
- if(con!=null) {
- PoolManager.getInstance().releaseConnection(con);
- con = null;
- }
- }
- }
-
- @Override
- public String getResumeTraitement() {
- return "pdf.creator";
- }
-
- @Override
- public String typeTraitementRefCode() {
- return getResumeTraitement()+"<"+documentId+">";
- }
-
- @Override
- public String getInformations() {
- StringBuilder sb = new StringBuilder();
- if(isStarted()) {
-// sb.append(typeTraitementRefCode()).append(" - ").append(pool.getRunningThreadCount()).append(" running thread(s)\n");
-// for(RunnableBatch rb: pool.getThreads()) {
-// sb.append("\t").append(rb.getName()).append(": ").append(rb.isAlive()?"running":"idle").append("\n");
-// if(rb.isAlive()) sb.append(rb.getInformations());
-// }
- } else {
- sb.append(typeTraitementRefCode()).append(" - not started\n");
- }
- return sb.toString();
- }
-
- @Override
- protected String getBatchVersion() {
- return "1.0";
- }
-
- private class User implements XemeliosUser {
- @Override
- public String getId() {
- return "PdfCreatorUser";
- }
- @Override
- public String getDisplayName() {
- return getId();
- }
- @Override
- public boolean hasRole(String role) {
- return true;
- }
- @Override
- public boolean hasDocument(String document) {
- return true;
- }
- @Override
- public boolean hasCollectivite(String collectivite, DocumentModel dm) {
- return true;
- }
- }
-
- protected class PdfCreatorImpl extends RunnableBatch {
- private String documentId;
- private String etatId;
- private String collectivite;
- private String budget;
- private int exercice;
- private XemeliosUser user;
- private MySqlDataLayer msLayer;
- private EtatModel em;
-
- public PdfCreatorImpl(String documentId, String etatId, String collectivite, String budget, int exercice, XemeliosUser user, MySqlDataLayer msLayer, EtatModel em) {
- super();
- this.documentId=documentId;
- this.etatId=etatId;
- this.collectivite=collectivite;
- this.budget=budget;
- this.exercice=exercice;
- this.msLayer = msLayer;
- this.user=user;
- this.em=em;
- }
-
- @Override
- public void run() {
- logger.info("starting "+documentId+"."+etatId+" "+collectivite+" "+budget+" "+exercice);
- super.run();
- if("compteGestionEtat".equals(documentId) && "BalanceGenerale".equals(etatId)) {
- generateCgEtatBalance();
- } else {
- getNotifier().notifyThreadStop(this);
- throw new RuntimeException("PdfCreator do not know how to PDF's "+documentId);
- }
- getNotifier().notifyThreadStop(this);
- }
-
- protected void generateCgEtatBalance() {
- PdfCreatorCgEtatBalance generator = new PdfCreatorCgEtatBalance(em,collectivite, budget, exercice,user);
- generator.run();
- }
- @Override
- public String getInformations() {
- StringBuilder sb = new StringBuilder();
- sb.append("\t").append(getName()).append(":");
- if(isAlive()) {
- sb.append("PdfCreator<"+documentId+"> running.");
- } else {
- if(isStarted()) {
- sb.append("PdfCreator<"+documentId+"> fully terminated.");
- } else {
- sb.append("PdfCreator<"+documentId+"> idle");
- }
- }
- sb.append("\n");
- return sb.toString();
- }
- }
+ private static final Logger logger = Logger.getLogger(PdfCreator.class);
+ public static final String PROP_DOCUMENTS_DEF_DIR = "documents-def.dir";
+ // public static final String PROP_DATALAYER_NAME = "datalayer.name";
+ public static final String PROP_DOCUMENT_ID = "document.id";
+ public static final Integer STATUS_NONE = null;
+ public static final Integer STATUS_IDLE = 0;
+ public static final Integer STATUS_RUNNING = 1;
+ public static final Integer STATUS_TERMINATED = 2;
+ public static final Integer STATUS_ERROR = 3;
+
+ private static ExecutorService runner = Executors.newFixedThreadPool(3);
+
+ private String documentId;
+
+ public PdfCreator(final String args[]) {
+ super(args);
+ }
+
+ @Override
+ protected void initialize() throws Exception {
+ super.initialize();
+ this.documentId = this.getProps().getProperty(PROP_DOCUMENT_ID);
+ }
+
+ @Override
+ protected void doProcess() throws Exception {
+ logger.debug("running PdfCreator for document " + this.documentId);
+ final XemeliosUser user = new User();
+ Connection con = null;
+ try {
+ final EtatModel em = BatchRunner.getInstance().getDocuments().getDocumentById("cg-etat").getEtatById("BalanceGenerale");
+ con = PoolManager.getInstance().getConnection();
+ final DataImpl impl = DataLayerManager.getImplementation();
+ if (impl instanceof MySqlDataLayer) {
+ final MySqlDataLayer msImpl = (MySqlDataLayer) impl;
+ if ("compteGestionEtat".equals(this.documentId)) {
+ final String select = "SELECT COLLECTIVITE, BUDGET, EXERCICE FROM PDF_CREATION WHERE DOCUMENT_ID=? AND ETAT_ID=? AND CREATION_STATUS IS NULL";
+ final PreparedStatement ps = con.prepareStatement(select);
+ ps.setString(1, this.documentId);
+ ps.setString(2, "BalanceGenerale");
+ final ResultSet rs = ps.executeQuery();
+ while (rs.next()) {
+ final String collectivite = rs.getString(1);
+ final String budget = rs.getString(2);
+ final int exercice = rs.getInt(3);
+ final PdfCreatorImpl process = new PdfCreatorImpl(this.documentId, "BalanceGenerale", collectivite, budget, exercice, user, msImpl, em);
+ runner.submit(process);
+ }
+
+ }
+ } else {
+ throw new RuntimeException("PdfCreator only runs with MysqlDataLayer");
+ }
+ } catch (final Exception ex) {
+ logger.error("in doProcess", ex);
+ } finally {
+ if (con != null) {
+ PoolManager.getInstance().releaseConnection(con);
+ con = null;
+ }
+ }
+ }
+
+ @Override
+ public String getResumeTraitement() {
+ return "pdf.creator";
+ }
+
+ @Override
+ public String typeTraitementRefCode() {
+ return this.getResumeTraitement() + "<" + this.documentId + ">";
+ }
+
+ @Override
+ public String getInformations() {
+ final StringBuilder sb = new StringBuilder();
+ if (this.isStarted()) {
+ // sb.append(typeTraitementRefCode()).append(" - ").append(pool.getRunningThreadCount()).append(" running thread(s)\n");
+ // for(RunnableBatch rb: pool.getThreads()) {
+ // sb.append("\t").append(rb.getName()).append(": ").append(rb.isAlive()?"running":"idle").append("\n");
+ // if(rb.isAlive()) sb.append(rb.getInformations());
+ // }
+ } else {
+ sb.append(this.typeTraitementRefCode()).append(" - not started\n");
+ }
+ return sb.toString();
+ }
+
+ @Override
+ protected String getBatchVersion() {
+ return "1.0";
+ }
+
+ private class User implements XemeliosUser {
+ @Override
+ public String getId() {
+ return "PdfCreatorUser";
+ }
+
+ @Override
+ public String getDisplayName() {
+ return this.getId();
+ }
+
+ @Override
+ public boolean hasRole(final String role) {
+ return true;
+ }
+
+ @Override
+ public boolean hasDocument(final String document) {
+ return true;
+ }
+
+ @Override
+ public boolean hasCollectivite(final String collectivite, final DocumentModel dm) {
+ return true;
+ }
+ }
+
+ protected class PdfCreatorImpl extends RunnableBatch {
+ private final String documentId;
+ private final String etatId;
+ private final String collectivite;
+ private final String budget;
+ private final int exercice;
+ private final XemeliosUser user;
+ private final EtatModel em;
+
+ public PdfCreatorImpl(final String documentId, final String etatId, final String collectivite, final String budget, final int exercice, final XemeliosUser user, final MySqlDataLayer msLayer, final EtatModel em) {
+ super();
+ this.documentId = documentId;
+ this.etatId = etatId;
+ this.collectivite = collectivite;
+ this.budget = budget;
+ this.exercice = exercice;
+ this.user = user;
+ this.em = em;
+ }
+
+ @Override
+ public void run() {
+ logger.info("starting " + this.documentId + "." + this.etatId + " " + this.collectivite + " " + this.budget + " " + this.exercice);
+ super.run();
+ if ("compteGestionEtat".equals(this.documentId) && "BalanceGenerale".equals(this.etatId)) {
+ this.generateCgEtatBalance();
+ } else {
+ this.getNotifier().notifyThreadStop(this);
+ throw new RuntimeException("PdfCreator do not know how to PDF's " + this.documentId);
+ }
+ this.getNotifier().notifyThreadStop(this);
+ }
+
+ protected void generateCgEtatBalance() {
+ final PdfCreatorCgEtatBalance generator = new PdfCreatorCgEtatBalance(this.em, this.collectivite, this.budget, this.exercice, this.user);
+ generator.run();
+ }
+
+ @Override
+ public String getInformations() {
+ final StringBuilder sb = new StringBuilder();
+ sb.append("\t").append(this.getName()).append(":");
+ if (this.isAlive()) {
+ sb.append("PdfCreator<" + this.documentId + "> running.");
+ } else {
+ if (PdfCreator.this.isStarted()) {
+ sb.append("PdfCreator<" + this.documentId + "> fully terminated.");
+ } else {
+ sb.append("PdfCreator<" + this.documentId + "> idle");
+ }
+ }
+ sb.append("\n");
+ return sb.toString();
+ }
+ }
}