18 |
ilm |
1 |
/*
|
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
|
|
3 |
*
|
185 |
ilm |
4 |
* Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
|
18 |
ilm |
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 static org.openconcerto.task.config.ComptaBasePropsConfiguration.getStreamStatic;
|
156 |
ilm |
17 |
|
18 |
ilm |
18 |
import org.openconcerto.erp.config.ComptaPropsConfiguration;
|
|
|
19 |
import org.openconcerto.erp.core.common.ui.PreviewFrame;
|
|
|
20 |
import org.openconcerto.erp.preferences.TemplateNXProps;
|
|
|
21 |
import org.openconcerto.openoffice.ODPackage;
|
|
|
22 |
import org.jopendocument.link.Component;
|
|
|
23 |
import org.openconcerto.openoffice.spreadsheet.MutableCell;
|
|
|
24 |
import org.openconcerto.openoffice.spreadsheet.Sheet;
|
|
|
25 |
import org.openconcerto.openoffice.spreadsheet.SpreadSheet;
|
|
|
26 |
import org.openconcerto.sql.Configuration;
|
|
|
27 |
import org.openconcerto.utils.ExceptionHandler;
|
|
|
28 |
import org.openconcerto.utils.StreamUtils;
|
|
|
29 |
|
|
|
30 |
import java.awt.Point;
|
|
|
31 |
import java.io.File;
|
185 |
ilm |
32 |
import java.io.FileInputStream;
|
18 |
ilm |
33 |
import java.io.FileNotFoundException;
|
|
|
34 |
import java.io.IOException;
|
|
|
35 |
import java.io.InputStream;
|
|
|
36 |
import java.util.ArrayList;
|
132 |
ilm |
37 |
import java.util.Arrays;
|
18 |
ilm |
38 |
import java.util.HashMap;
|
|
|
39 |
import java.util.Iterator;
|
|
|
40 |
import java.util.List;
|
|
|
41 |
import java.util.Map;
|
|
|
42 |
|
|
|
43 |
import javax.swing.JOptionPane;
|
|
|
44 |
import javax.swing.SwingUtilities;
|
|
|
45 |
|
|
|
46 |
import org.jopendocument.model.OpenDocument;
|
|
|
47 |
|
|
|
48 |
public abstract class SpreadSheetGenerator implements Runnable {
|
|
|
49 |
|
|
|
50 |
private Map mCell, mapStyleRow;
|
|
|
51 |
|
|
|
52 |
static private String modelDir;
|
|
|
53 |
private String modele, destFileName;
|
|
|
54 |
private File destDirOO, destDirPDF;
|
|
|
55 |
protected int nbPage, nbRowsPerPage;
|
|
|
56 |
private boolean visu, impression;
|
|
|
57 |
private String printer;
|
|
|
58 |
private boolean exportPDF;
|
|
|
59 |
private Map mapReplaceText;
|
185 |
ilm |
60 |
private InputStream fODSP = null;
|
18 |
ilm |
61 |
|
|
|
62 |
protected static final String defaultLocationTemplate = "/Configuration/Template/Default/";
|
|
|
63 |
|
|
|
64 |
static {
|
|
|
65 |
String loc = TemplateNXProps.getInstance().getStringProperty("LocationTemplate");
|
|
|
66 |
if (loc.trim().length() == 0) {
|
|
|
67 |
loc = defaultLocationTemplate;
|
|
|
68 |
}
|
|
|
69 |
setModelDir(loc);
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
public static void setModelDir(String loc) {
|
|
|
73 |
modelDir = loc;
|
|
|
74 |
System.err.println("Repertoire des template : " + modelDir);
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
protected void searchStyle(Sheet sheet, Map<String, Map<Integer, String>> mapStyleDef, int colEnd, int rowEnd) {
|
|
|
78 |
|
|
|
79 |
// on parcourt chaque ligne de la feuille pour recuperer les styles
|
|
|
80 |
int columnCount = (colEnd == -1) ? sheet.getColumnCount() : (colEnd + 1);
|
185 |
ilm |
81 |
int endColumnSearch = Math.min(100, columnCount);
|
|
|
82 |
System.err.println("End column search : " + endColumnSearch);
|
18 |
ilm |
83 |
|
|
|
84 |
int rowCount = (rowEnd > 0) ? rowEnd : sheet.getRowCount();
|
185 |
ilm |
85 |
int endRowSearch = Math.min(100, rowCount);
|
|
|
86 |
for (int i = 0; i < endRowSearch; i++) {
|
18 |
ilm |
87 |
int x = 0;
|
|
|
88 |
Map<Integer, String> mapCellStyle = new HashMap<Integer, String>();
|
|
|
89 |
String style = "";
|
|
|
90 |
|
185 |
ilm |
91 |
for (int j = 0; j < endColumnSearch; j++) {
|
18 |
ilm |
92 |
|
|
|
93 |
if (sheet.isCellValid(j, i)) {
|
|
|
94 |
|
|
|
95 |
MutableCell c = sheet.getCellAt(j, i);
|
|
|
96 |
String cellStyle = c.getStyleName();
|
|
|
97 |
|
|
|
98 |
if (mapStyleDef.containsKey(c.getValue().toString())) {
|
|
|
99 |
style = c.getValue().toString();
|
|
|
100 |
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
mapCellStyle.put(new Integer(x), cellStyle);
|
|
|
104 |
if (style.trim().length() != 0) {
|
|
|
105 |
c.clearValue();
|
|
|
106 |
}
|
|
|
107 |
}
|
|
|
108 |
x++;
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
if (style.length() > 0) {
|
|
|
112 |
mapStyleDef.put(style, mapCellStyle);
|
|
|
113 |
|
|
|
114 |
}
|
|
|
115 |
}
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
protected void fill(Sheet sheet, Map<String, Map<Integer, String>> mapStyleDef) {
|
|
|
119 |
|
|
|
120 |
for (Iterator i = this.mCell.keySet().iterator(); i.hasNext();) {
|
|
|
121 |
|
|
|
122 |
Object o = i.next();
|
|
|
123 |
if (this.mCell.get(o) != null) {
|
|
|
124 |
// System.err.println(o + " ---> " + m.get(o).toString());
|
|
|
125 |
final Point p = sheet.resolveHint(o.toString());
|
|
|
126 |
Object value = this.mCell.get(o);
|
|
|
127 |
boolean cellValid = false;
|
|
|
128 |
try {
|
|
|
129 |
cellValid = sheet.isCellValid(p.x, p.y);
|
|
|
130 |
} catch (IndexOutOfBoundsException e) {
|
|
|
131 |
e.printStackTrace();
|
|
|
132 |
}
|
|
|
133 |
if (cellValid) {
|
|
|
134 |
MutableCell cell = sheet.getCellAt(p.x, p.y);
|
|
|
135 |
// on replace les retours a la ligne
|
|
|
136 |
|
93 |
ilm |
137 |
if (value != null && value.toString().indexOf('\n') >= 0) {
|
|
|
138 |
final String stringValue = value.toString();
|
18 |
ilm |
139 |
String firstPart = stringValue.substring(0, stringValue.indexOf('\n'));
|
|
|
140 |
String secondPart = stringValue.substring(stringValue.indexOf('\n') + 1, stringValue.length());
|
|
|
141 |
secondPart = secondPart.replace('\n', ',');
|
|
|
142 |
cell.setValue(firstPart);
|
|
|
143 |
|
|
|
144 |
try {
|
|
|
145 |
MutableCell cellSec = sheet.getCellAt(p.x, p.y + 1);
|
|
|
146 |
cellSec.setValue(secondPart);
|
|
|
147 |
} catch (Exception e) {
|
|
|
148 |
e.printStackTrace();
|
|
|
149 |
SwingUtilities.invokeLater(new Runnable() {
|
|
|
150 |
public void run() {
|
|
|
151 |
JOptionPane.showMessageDialog(null, "Impossible d'accéder à la cellule " + p.x + ";" + (p.y + 1), "Erreur accés cellule", JOptionPane.INFORMATION_MESSAGE);
|
|
|
152 |
}
|
|
|
153 |
});
|
|
|
154 |
}
|
|
|
155 |
} else {
|
|
|
156 |
cell.setValue(((value == null) ? "" : value));
|
|
|
157 |
|
|
|
158 |
Object sty = this.mapStyleRow.get(new Integer(p.y + 1));
|
|
|
159 |
if (sty != null) {
|
|
|
160 |
Map<Integer, String> styleToApply = mapStyleDef.get(sty.toString());
|
|
|
161 |
if (styleToApply != null) {
|
|
|
162 |
|
|
|
163 |
if (styleToApply.get(new Integer(p.x)) != null) {
|
|
|
164 |
cell.setStyleName(styleToApply.get(new Integer(p.x)));
|
|
|
165 |
}
|
|
|
166 |
}
|
|
|
167 |
}
|
|
|
168 |
}
|
|
|
169 |
}
|
|
|
170 |
}
|
|
|
171 |
}
|
|
|
172 |
|
|
|
173 |
}
|
|
|
174 |
|
|
|
175 |
protected void replace(Sheet sheet) {
|
|
|
176 |
for (Iterator i = this.mapReplaceText.keySet().iterator(); i.hasNext();) {
|
|
|
177 |
Object o = i.next();
|
|
|
178 |
if (this.mapReplaceText.get(o) != null) {
|
|
|
179 |
|
|
|
180 |
Object value = this.mapReplaceText.get(o);
|
|
|
181 |
MutableCell cell = sheet.getCellAt(o.toString());
|
|
|
182 |
|
|
|
183 |
cell.replaceBy("_", ((value == null) ? "" : value.toString()));
|
|
|
184 |
}
|
|
|
185 |
}
|
|
|
186 |
}
|
|
|
187 |
|
|
|
188 |
protected SpreadSheet loadTemplate() throws IOException {
|
185 |
ilm |
189 |
|
|
|
190 |
InputStream f = TemplateManager.getInstance().getTemplate(modele.replaceAll(".ods", ""));
|
18 |
ilm |
191 |
if (f == null) {
|
185 |
ilm |
192 |
f = getStreamStatic(modelDir + File.separator + this.modele);
|
|
|
193 |
fODSP = getStreamStatic(modelDir + File.separator + this.modele + "p");
|
18 |
ilm |
194 |
if (f == null) {
|
185 |
ilm |
195 |
f = getStreamStatic(defaultLocationTemplate + File.separator + this.modele);
|
|
|
196 |
fODSP = getStreamStatic(defaultLocationTemplate + File.separator + this.modele + "p");
|
|
|
197 |
if (f == null) {
|
|
|
198 |
ExceptionHandler.handle("Modele " + this.modele + " introuvable. Impossible de générer le document.");
|
|
|
199 |
System.err.println("Modele introuvable : " + (defaultLocationTemplate + File.separator + this.modele));
|
|
|
200 |
fODSP = null;
|
|
|
201 |
return null;
|
|
|
202 |
}
|
18 |
ilm |
203 |
}
|
185 |
ilm |
204 |
} else {
|
|
|
205 |
fODSP = TemplateManager.getInstance().getTemplatePrintConfiguration(modele.replaceAll(".ods", ""), null, null);
|
18 |
ilm |
206 |
}
|
185 |
ilm |
207 |
|
25 |
ilm |
208 |
final SpreadSheet res = new ODPackage(f).getSpreadSheet();
|
18 |
ilm |
209 |
f.close();
|
|
|
210 |
return res;
|
|
|
211 |
}
|
|
|
212 |
|
|
|
213 |
protected File save(SpreadSheet ssheet) throws IOException {
|
|
|
214 |
File fDest = new File(this.destDirOO, this.destFileName + ".ods");
|
|
|
215 |
|
|
|
216 |
int i = 0;
|
|
|
217 |
String destName = this.destFileName;
|
|
|
218 |
|
|
|
219 |
File oldPath = new File(this.destDirOO, "Historique");
|
|
|
220 |
oldPath.mkdirs();
|
|
|
221 |
while (fDest.exists()) {
|
|
|
222 |
destName = this.destFileName + "_" + i;
|
|
|
223 |
fDest = new File(oldPath, destName + ".ods");
|
|
|
224 |
i++;
|
|
|
225 |
}
|
|
|
226 |
File fTmp = new File(this.destDirOO, this.destFileName + ".ods");
|
|
|
227 |
fTmp.renameTo(fDest);
|
|
|
228 |
|
|
|
229 |
fDest = new File(this.destDirOO, this.destFileName + ".ods");
|
185 |
ilm |
230 |
if (this.fODSP != null) {
|
|
|
231 |
// Copie de l'odsp
|
|
|
232 |
File odspOut = new File(this.destDirOO, this.destFileName + ".odsp");
|
|
|
233 |
StreamUtils.copy(this.fODSP, odspOut);
|
18 |
ilm |
234 |
}
|
185 |
ilm |
235 |
|
18 |
ilm |
236 |
try {
|
|
|
237 |
ssheet.saveAs(fDest);
|
|
|
238 |
} catch (FileNotFoundException e) {
|
|
|
239 |
final File file = fDest;
|
|
|
240 |
SwingUtilities.invokeLater(new Runnable() {
|
|
|
241 |
public void run() {
|
|
|
242 |
try {
|
|
|
243 |
ExceptionHandler.handle("Le fichier " + ((file == null) ? "" : file.getCanonicalPath()) + " n'a pu être créé. \n Vérifiez qu'il n'est pas déjà ouvert.");
|
|
|
244 |
} catch (IOException e) {
|
|
|
245 |
e.printStackTrace();
|
|
|
246 |
}
|
|
|
247 |
}
|
|
|
248 |
});
|
|
|
249 |
|
|
|
250 |
e.printStackTrace();
|
|
|
251 |
}
|
|
|
252 |
return fDest;
|
|
|
253 |
}
|
|
|
254 |
|
|
|
255 |
public SpreadSheetGenerator(SheetInterface sheet, String destFileName, boolean impr, boolean visu) {
|
|
|
256 |
this(sheet, destFileName, impr, visu, true);
|
|
|
257 |
}
|
|
|
258 |
|
|
|
259 |
public SpreadSheetGenerator(SheetInterface sheet, String destFileName, boolean impr, boolean visu, boolean exportPDF) {
|
|
|
260 |
|
|
|
261 |
this.modele = sheet.modele;
|
|
|
262 |
this.mCell = sheet.mCell;
|
25 |
ilm |
263 |
this.destDirOO = sheet.getDocumentOutputDirectory();
|
18 |
ilm |
264 |
this.destDirOO.mkdirs();
|
25 |
ilm |
265 |
this.destDirPDF = sheet.getPDFOutputDirectory();
|
18 |
ilm |
266 |
this.destDirPDF.mkdirs();
|
|
|
267 |
this.nbPage = sheet.nbPage;
|
|
|
268 |
this.nbRowsPerPage = sheet.nbRowsPerPage;
|
|
|
269 |
this.destFileName = destFileName;
|
|
|
270 |
this.mapStyleRow = sheet.mapStyleRow;
|
|
|
271 |
this.mapReplaceText = sheet.mapReplace;
|
|
|
272 |
this.visu = visu;
|
|
|
273 |
this.impression = impr;
|
|
|
274 |
this.printer = sheet.getPrinter();
|
|
|
275 |
this.exportPDF = exportPDF;
|
|
|
276 |
}
|
|
|
277 |
|
|
|
278 |
protected abstract File generateWithStyle() throws IOException;
|
|
|
279 |
|
|
|
280 |
public void run() {
|
|
|
281 |
File f = null;
|
|
|
282 |
try {
|
|
|
283 |
|
|
|
284 |
f = generateWithStyle();
|
25 |
ilm |
285 |
final File pdfFileToCreate = new File(this.destDirPDF.getAbsolutePath(), this.destFileName + ".pdf");
|
18 |
ilm |
286 |
try {
|
|
|
287 |
|
|
|
288 |
if (!Boolean.getBoolean("org.openconcerto.oo.useODSViewer")) {
|
|
|
289 |
|
|
|
290 |
final Component doc = ComptaPropsConfiguration.getOOConnexion().loadDocument(f, !this.visu);
|
|
|
291 |
if (this.exportPDF) {
|
25 |
ilm |
292 |
|
|
|
293 |
doc.saveToPDF(pdfFileToCreate);
|
18 |
ilm |
294 |
}
|
|
|
295 |
|
|
|
296 |
if (this.impression) {
|
|
|
297 |
Map<String, Object> map = new HashMap<String, Object>();
|
|
|
298 |
map.put("Name", this.printer);
|
|
|
299 |
doc.printDocument(map);
|
|
|
300 |
|
|
|
301 |
}
|
|
|
302 |
doc.close();
|
|
|
303 |
} else {
|
|
|
304 |
final OpenDocument doc = new OpenDocument(f);
|
|
|
305 |
if (this.visu) {
|
|
|
306 |
PreviewFrame.show(f);
|
|
|
307 |
}
|
|
|
308 |
|
25 |
ilm |
309 |
SheetUtils.convert2PDF(doc, pdfFileToCreate);
|
18 |
ilm |
310 |
if (this.impression) {
|
|
|
311 |
// Print !
|
|
|
312 |
DefaultNXDocumentPrinter printer = new DefaultNXDocumentPrinter();
|
132 |
ilm |
313 |
printer.print(Arrays.asList(doc));
|
18 |
ilm |
314 |
}
|
|
|
315 |
|
|
|
316 |
}
|
|
|
317 |
} catch (Exception e) {
|
|
|
318 |
e.printStackTrace();
|
|
|
319 |
ExceptionHandler.handle("Impossible de charger le document OpenOffice", e);
|
|
|
320 |
}
|
|
|
321 |
|
|
|
322 |
// }
|
|
|
323 |
} catch (IOException e) {
|
|
|
324 |
e.printStackTrace();
|
|
|
325 |
System.err.println("Fichier déjà ouvert!");
|
|
|
326 |
|
|
|
327 |
final File ff = f;
|
|
|
328 |
SwingUtilities.invokeLater(new Runnable() {
|
|
|
329 |
public void run() {
|
|
|
330 |
ExceptionHandler.handle("Le fichier " + ((ff == null) ? "" : ff.getAbsolutePath()) + " n'a pu être créé. \n Vérifiez qu'il n'est pas déjà ouvert.");
|
|
|
331 |
}
|
|
|
332 |
});
|
|
|
333 |
}
|
|
|
334 |
|
|
|
335 |
fireGenerateEnd();
|
|
|
336 |
}
|
|
|
337 |
|
|
|
338 |
private final List<SpreadSheetGeneratorListener> listeners = new ArrayList<SpreadSheetGeneratorListener>();
|
|
|
339 |
|
|
|
340 |
private void fireGenerateEnd() {
|
|
|
341 |
for (SpreadSheetGeneratorListener listener : listeners) {
|
|
|
342 |
listener.taskEnd();
|
|
|
343 |
}
|
|
|
344 |
}
|
|
|
345 |
|
|
|
346 |
public void addGenerateListener(SpreadSheetGeneratorListener l) {
|
|
|
347 |
this.listeners.add(l);
|
|
|
348 |
}
|
|
|
349 |
}
|