OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 142 | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * 
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
 * 
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
 * language governing permissions and limitations under the License.
 * 
 * When distributing the software, include this License Header Notice in each file.
 */
 
 package org.openconcerto.erp.core.finance.accounting.report;

import static org.openconcerto.task.config.ComptaBasePropsConfiguration.getStreamStatic;

import org.openconcerto.utils.ExceptionHandler;

import java.awt.Color;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

import com.ibm.icu.text.SimpleDateFormat;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter;

public abstract class PdfGenerator {

    private int offsetX;
    private int offsetY;
    private int templateOffsetX;
    private int templateOffsetY;
    private PdfContentByte cb;
    private Document document;
    private BaseFont bf;
    private BaseFont bfb;
    private int width;
    private Map<String, String> map;
    private String fileNameIn;
    private String fileNameOut;
    private File directoryOut;
    private File fOut;

    public static void main(String[] args) {
        new PdfGenerator_2033B().generateFrom(null);
    }

    PdfGenerator(String fileNameIn, String fileNameOut, String directoryOut) {
        this.fileNameIn = "/Configuration/Template/PDF/" + fileNameIn;
        this.fileNameOut = fileNameOut;
        this.directoryOut = new File(directoryOut, String.valueOf(Calendar.getInstance().get(Calendar.YEAR)));
    }

    public void generateFrom(Map<String, String> m) {
        try {
            if (m == null) {
                System.err.println(this + " : filling with defaults");
            }
            this.map = m;
            init();
            this.cb.beginText();
            generate();
            this.cb.endText();
            this.document.close();
        } catch (FileNotFoundException e) {
            ExceptionHandler.handle("Impossible de générer le fichier. \n" + e, e);
        }
    }

    private void init() throws FileNotFoundException {

        // we create a reader for a certain document
        PdfReader reader = null;
        PdfWriter writer = null;
        try {
            reader = new PdfReader(getStreamStatic(this.fileNameIn));

            final Rectangle psize = reader.getPageSize(1);
            psize.setRight(psize.getRight() - this.templateOffsetX);
            psize.setTop(psize.getTop() - this.templateOffsetY);

            this.width = (int) psize.getWidth();

            int margin = 32;
            this.document = new Document(psize, margin, margin, margin, margin);
            if (!this.directoryOut.exists()) {
                this.directoryOut.mkdirs();
            }
            final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd-HHmmss");
            final String computedFileName = sdf.format(new Date()) + "-" + this.fileNameOut;

            fOut = new File(this.directoryOut, computedFileName);
            System.err.println("Creation du fichier " + fOut.getCanonicalPath());
            writer = PdfWriter.getInstance(this.document, new FileOutputStream(fOut));
            this.document.open();
            this.cb = writer.getDirectContent();

            this.document.newPage();

            PdfImportedPage page1 = writer.getImportedPage(reader, 1);

            this.cb.addTemplate(page1, -this.templateOffsetX, -this.templateOffsetY);

            this.bf = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.EMBEDDED);
            this.bfb = BaseFont.createFont(BaseFont.TIMES_BOLD, BaseFont.CP1252, BaseFont.EMBEDDED);

        } catch (FileNotFoundException fE) {
            throw fE;
        } catch (IOException | DocumentException e) {
            throw new IllegalStateException(e);
        } finally {
            if (reader != null) {
                reader.close();
            }
        }
    }

    public abstract void generate();

    public File getGeneratedFile() {
        return this.fOut;
    }

    protected void setOffset(int i, int j) {
        this.offsetX = i;
        this.offsetY = j;
    }

    protected void setTemplateOffset(int i, int j) {
        this.templateOffsetX = i;
        this.templateOffsetY = j;
    }

    protected void addSplittedText(String code, String string, int fromx, int y, double deltax) {
        int x = fromx - this.offsetX - this.templateOffsetX;
        y = y - this.offsetY - this.templateOffsetY;
        boolean error = false;
        String s = string;
        if (this.map != null) {
            s = this.map.get(code);
        }
        if (s == null) {
            s = code;
            error = true;
            this.cb.setColorFill(Color.RED);
        }

        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            String sub = String.valueOf(c);
            this.cb.showTextAligned(PdfContentByte.ALIGN_LEFT, sub, x, y, 0);
            x += deltax;
        }

        if (error) {
            this.cb.setColorStroke(Color.BLACK);
        }

    }

    protected void setFontRoman(int i) {
        this.cb.setFontAndSize(this.bf, i);

    }

    protected void setFontBold(int i) {
        this.cb.setFontAndSize(this.bfb, i);
    }

    protected void addText(String code, String string, int i, int j) {
        addText(code, string, i, j, 0);
    }

    protected void addText(String code, String string, int i, int j, int k) {
        addText(PdfContentByte.ALIGN_LEFT, code, string, i, j, k);

    }

    protected void addTextRight(String code, String string, int i, int j) {
        int a = PdfContentByte.ALIGN_RIGHT;
        int k = 0;

        if (this.map == null)
            this.cb.showTextAligned(a, string, i, j, k);
        else {
            boolean error = false;
            String s = this.map.get(code);
            if (s == null) {
                s = code;
                error = true;
            } else {
                if (s.equalsIgnoreCase("-0.0")) {
                    s = "0.0";
                }
                s = insertCurrencySpaces(s);
            }
            this.cb.showTextAligned(a, s, i, j, k);
            if (error) {
                this.cb.setColorStroke(Color.BLACK);
            }
        }

    }

    private final void addText(int a, String code, String string, int i, int j, int k) {
        if (this.map == null)
            this.cb.showTextAligned(a, string, i, j, k);
        else {
            boolean error = false;
            String s = this.map.get(code);
            if (s == null) {
                s = code;
                error = true;
            }
            this.cb.showTextAligned(a, s, i, j, k);
            if (error) {
                this.cb.setColorStroke(Color.BLACK);
            }
        }
    }

    protected int getWidth() {
        return this.width;
    }

    protected static String insertCurrencySpaces(String string) {
        final StringBuilder s = new StringBuilder();
        for (int i = string.length() - 1; i >= 0; i--) {
            s.insert(0, string.charAt(i));
            if ((i - string.length()) % 3 == 0) {
                s.insert(0, " ");
            }
        }
        return s.toString().trim();
    }

}