OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Blame | 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.generationDoc;

import org.openconcerto.erp.core.edm.Attachment;
import org.openconcerto.erp.core.edm.AttachmentUtils;
import org.openconcerto.sql.model.SQLRow;
import org.openconcerto.sql.model.SQLRowAccessor;
import org.openconcerto.sql.model.SQLRowValues;
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
import org.openconcerto.sql.model.SQLTable;
import org.openconcerto.sql.model.Where;
import org.openconcerto.sql.model.graph.Path;
import org.openconcerto.utils.FileUtils;
import org.openconcerto.utils.FillMode;
import org.openconcerto.utils.ImageUtils;
import org.openconcerto.utils.StringUtils;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.imageio.ImageIO;

import org.jdom2.Element;

public class OOXMLTableImage {

    private float x = 0;
    private float y = 0;
    private final float width, height;
    private int dpi = 300;

    private final int rowCount;
    // #ffffff
    private String bgColor = "#ffffff";

    public final static String EDM_URL = "edm://miniature.png";
    // C://Users/Toto.jpg ou edm://miniature.png (prendre nom miniature)
    private final String url;
    // ID_AFFAIRE.ID_CLIENT
    private String fieldPathEDM;
    private String edmProvider;
    // top,center,bottom
    private String vAlign = "center";
    // left, center, right
    private String hAlign = "center";

    private OOXMLElement xmlField;
    private final SQLRowAccessor row;

    public OOXMLTableImage(OOXMLElement xmlField, Element e, SQLRowAccessor row) {
        this.xmlField = xmlField;
        this.row = row;
        this.url = e.getAttributeValue("url");
        this.fieldPathEDM = e.getAttributeValue("fieldPathEDM");
        this.edmProvider = e.getAttributeValue("edmProvider");
        this.width = Float.valueOf(e.getAttributeValue("width"));
        this.height = Float.valueOf(e.getAttributeValue("height"));
        this.rowCount = Integer.valueOf(e.getAttributeValue("rowCount"));
        if (e.getAttributeValue("x") != null && !e.getAttributeValue("x").isEmpty()) {
            this.x = Integer.valueOf(e.getAttributeValue("x"));
        }
        if (e.getAttributeValue("y") != null && !e.getAttributeValue("y").isEmpty()) {
            this.y = Integer.valueOf(e.getAttributeValue("y"));
        }
    }

    private byte[] img = null;
    private boolean cachedImg = false;

    public byte[] getImgBytes() throws IOException {

        if (this.cachedImg) {
            return this.img;
        } else {

            if (this.url.startsWith("edm://")) {
                Path p = new Path(this.row.getTable());
                final SQLRow r = this.row.asRow();
                final SQLRow rowToEdm;
                String sourceTableName = r.getTable().getName();
                int sourceID = r.getID();
                if (this.fieldPathEDM == null || this.fieldPathEDM.isEmpty() || this.fieldPathEDM.equalsIgnoreCase(".")) {
                    rowToEdm = r;
                } else if (this.fieldPathEDM != null && this.fieldPathEDM.equalsIgnoreCase("EDM_PROVIDER")) {
                    SpreadSheetCellValueProvider provider = SpreadSheetCellValueProviderManager.get(this.edmProvider);
                    rowToEdm = (SQLRow) provider.getValue(new SpreadSheetCellValueContext(r));
                    if (rowToEdm == null) {
                        return null;
                    } else {
                        sourceTableName = rowToEdm.getTable().getName();
                        sourceID = rowToEdm.getID();
                    }

                } else {
                    List<String> fields = StringUtils.fastSplit(this.fieldPathEDM, '.');
                    if (fields.size() == 1) {
                        sourceTableName = r.getTable().getForeignTable(fields.get(0)).getName();
                        sourceID = r.getForeignID(fields.get(0));
                    } else {
                        for (String f : fields) {
                            p = p.add(p.getLast().getField(f));
                        }
                        rowToEdm = r.getDistantRow(p);
                        sourceTableName = rowToEdm.getTable().getName();
                        sourceID = rowToEdm.getID();
                    }
                }
                final SQLTable tableAttachment = this.row.getTable().getTable("ATTACHMENT");
                SQLRowValues rowVals = new SQLRowValues(tableAttachment);
                rowVals.putNulls(tableAttachment.getFieldsName());

                SQLRowValuesListFetcher fetcher = SQLRowValuesListFetcher.create(rowVals);
                Where where = new Where(tableAttachment.getField("SOURCE_TABLE"), "=", sourceTableName);
                where = where.and(new Where(tableAttachment.getField("SOURCE_ID"), "=", sourceID));
                where = where.and(new Where(tableAttachment.getField("NAME"), "=", this.url.replaceAll("edm://", "")));
                final List<SQLRowValues> rAttachments = fetcher.fetch(where);
                if (rAttachments.isEmpty()) {
                    this.cachedImg = true;
                    return null;
                } else {
                    final SQLRowValues rowValsAttachment = rAttachments.get(0);
                    final Attachment a = new Attachment(rowValsAttachment);
                    final AttachmentUtils u = new AttachmentUtils();
                    final File f = u.getFile(a);
                    BufferedImage unscaledImage = ImageIO.read(f);

                    // 1 pouce = 25.4 mmm
                    float scale = this.dpi / 25.4f;
                    int maxWidth = (int) (this.width * scale);
                    int maxHeight = (int) (this.height * scale);
                    final BufferedImage im = ImageUtils.createQualityResizedImage(unscaledImage, maxWidth, maxHeight, false, new FillMode.ZoomOut(Color.WHITE), true);
                    ByteArrayOutputStream output = new ByteArrayOutputStream();
                    ImageIO.write(im, "PNG", output);
                    output.close();
                    this.img = output.toByteArray();
                    this.cachedImg = true;
                    return this.img;
                }

            } else

            {
                this.img = FileUtils.readBytes(new File(this.url));
                this.cachedImg = true;
                return this.img;
            }
        }
    }

    public String getBgColor() {
        return this.bgColor;
    }

    public int getDpi() {
        return this.dpi;
    }

    public float getX() {
        return this.x;
    }

    public float getY() {
        return this.y;
    }

    public float getWidth() {
        return this.width;
    }

    public float getHeight() {
        return this.height;
    }

    public int getRowCount() {
        return this.rowCount;
    }
}