OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
180 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.erp.generationDoc;
15
 
16
import org.openconcerto.erp.core.edm.Attachment;
17
import org.openconcerto.erp.core.edm.AttachmentUtils;
18
import org.openconcerto.sql.model.SQLRow;
19
import org.openconcerto.sql.model.SQLRowAccessor;
20
import org.openconcerto.sql.model.SQLRowValues;
21
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
22
import org.openconcerto.sql.model.SQLTable;
23
import org.openconcerto.sql.model.Where;
24
import org.openconcerto.sql.model.graph.Path;
25
import org.openconcerto.utils.FileUtils;
26
import org.openconcerto.utils.FillMode;
27
import org.openconcerto.utils.ImageUtils;
28
import org.openconcerto.utils.StringUtils;
29
 
30
import java.awt.Color;
31
import java.awt.image.BufferedImage;
32
import java.io.ByteArrayOutputStream;
33
import java.io.File;
34
import java.io.IOException;
35
import java.util.List;
36
 
37
import javax.imageio.ImageIO;
38
 
39
import org.jdom2.Element;
40
 
41
public class OOXMLTableImage {
42
 
43
    private float x = 0;
44
    private float y = 0;
45
    private final float width, height;
46
    private int dpi = 300;
47
 
48
    private final int rowCount;
49
    // #ffffff
50
    private String bgColor = "#ffffff";
51
 
52
    public final static String EDM_URL = "edm://miniature.png";
53
    // C://Users/Toto.jpg ou edm://miniature.png (prendre nom miniature)
54
    private final String url;
55
    // ID_AFFAIRE.ID_CLIENT
56
    private String fieldPathEDM;
57
    private String edmProvider;
58
    // top,center,bottom
59
    private String vAlign = "center";
60
    // left, center, right
61
    private String hAlign = "center";
62
 
63
    private OOXMLElement xmlField;
64
    private final SQLRowAccessor row;
65
 
66
    public OOXMLTableImage(OOXMLElement xmlField, Element e, SQLRowAccessor row) {
67
        this.xmlField = xmlField;
68
        this.row = row;
69
        this.url = e.getAttributeValue("url");
70
        this.fieldPathEDM = e.getAttributeValue("fieldPathEDM");
71
        this.edmProvider = e.getAttributeValue("edmProvider");
72
        this.width = Float.valueOf(e.getAttributeValue("width"));
73
        this.height = Float.valueOf(e.getAttributeValue("height"));
74
        this.rowCount = Integer.valueOf(e.getAttributeValue("rowCount"));
75
        if (e.getAttributeValue("x") != null && !e.getAttributeValue("x").isEmpty()) {
76
            this.x = Integer.valueOf(e.getAttributeValue("x"));
77
        }
78
        if (e.getAttributeValue("y") != null && !e.getAttributeValue("y").isEmpty()) {
79
            this.y = Integer.valueOf(e.getAttributeValue("y"));
80
        }
81
    }
82
 
83
    private byte[] img = null;
84
    private boolean cachedImg = false;
85
 
86
    public byte[] getImgBytes() throws IOException {
87
 
88
        if (this.cachedImg) {
89
            return this.img;
90
        } else {
91
 
92
            if (this.url.startsWith("edm://")) {
93
                Path p = new Path(this.row.getTable());
94
                final SQLRow r = this.row.asRow();
95
                final SQLRow rowToEdm;
96
                String sourceTableName = r.getTable().getName();
97
                int sourceID = r.getID();
98
                if (this.fieldPathEDM == null || this.fieldPathEDM.isEmpty() || this.fieldPathEDM.equalsIgnoreCase(".")) {
99
                    rowToEdm = r;
100
                } else if (this.fieldPathEDM != null && this.fieldPathEDM.equalsIgnoreCase("EDM_PROVIDER")) {
101
                    SpreadSheetCellValueProvider provider = SpreadSheetCellValueProviderManager.get(this.edmProvider);
102
                    rowToEdm = (SQLRow) provider.getValue(new SpreadSheetCellValueContext(r));
103
                    if (rowToEdm == null) {
104
                        return null;
105
                    } else {
106
                        sourceTableName = rowToEdm.getTable().getName();
107
                        sourceID = rowToEdm.getID();
108
                    }
109
 
110
                } else {
111
                    List<String> fields = StringUtils.fastSplit(this.fieldPathEDM, '.');
112
                    if (fields.size() == 1) {
113
                        sourceTableName = r.getTable().getForeignTable(fields.get(0)).getName();
114
                        sourceID = r.getForeignID(fields.get(0));
115
                    } else {
116
                        for (String f : fields) {
117
                            p = p.add(p.getLast().getField(f));
118
                        }
119
                        rowToEdm = r.getDistantRow(p);
120
                        sourceTableName = rowToEdm.getTable().getName();
121
                        sourceID = rowToEdm.getID();
122
                    }
123
                }
124
                final SQLTable tableAttachment = this.row.getTable().getTable("ATTACHMENT");
125
                SQLRowValues rowVals = new SQLRowValues(tableAttachment);
126
                rowVals.putNulls(tableAttachment.getFieldsName());
127
 
128
                SQLRowValuesListFetcher fetcher = SQLRowValuesListFetcher.create(rowVals);
129
                Where where = new Where(tableAttachment.getField("SOURCE_TABLE"), "=", sourceTableName);
130
                where = where.and(new Where(tableAttachment.getField("SOURCE_ID"), "=", sourceID));
131
                where = where.and(new Where(tableAttachment.getField("NAME"), "=", this.url.replaceAll("edm://", "")));
132
                final List<SQLRowValues> rAttachments = fetcher.fetch(where);
133
                if (rAttachments.isEmpty()) {
134
                    this.cachedImg = true;
135
                    return null;
136
                } else {
137
                    final SQLRowValues rowValsAttachment = rAttachments.get(0);
138
                    final Attachment a = new Attachment(rowValsAttachment);
139
                    final AttachmentUtils u = new AttachmentUtils();
140
                    final File f = u.getFile(a);
141
                    BufferedImage unscaledImage = ImageIO.read(f);
142
 
143
                    // 1 pouce = 25.4 mmm
144
                    float scale = this.dpi / 25.4f;
145
                    int maxWidth = (int) (this.width * scale);
146
                    int maxHeight = (int) (this.height * scale);
147
                    final BufferedImage im = ImageUtils.createQualityResizedImage(unscaledImage, maxWidth, maxHeight, false, new FillMode.ZoomOut(Color.WHITE), true);
148
                    ByteArrayOutputStream output = new ByteArrayOutputStream();
149
                    ImageIO.write(im, "PNG", output);
150
                    output.close();
151
                    this.img = output.toByteArray();
152
                    this.cachedImg = true;
153
                    return this.img;
154
                }
155
 
156
            } else
157
 
158
            {
159
                this.img = FileUtils.readBytes(new File(this.url));
160
                this.cachedImg = true;
161
                return this.img;
162
            }
163
        }
164
    }
165
 
166
    public String getBgColor() {
167
        return this.bgColor;
168
    }
169
 
170
    public int getDpi() {
171
        return this.dpi;
172
    }
173
 
174
    public float getX() {
175
        return this.x;
176
    }
177
 
178
    public float getY() {
179
        return this.y;
180
    }
181
 
182
    public float getWidth() {
183
        return this.width;
184
    }
185
 
186
    public float getHeight() {
187
        return this.height;
188
    }
189
 
190
    public int getRowCount() {
191
        return this.rowCount;
192
    }
193
}