OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 151 | Rev 174 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
18 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.config.ComptaPropsConfiguration;
17
import org.openconcerto.erp.core.common.ui.PreviewFrame;
156 ilm 18
import org.openconcerto.erp.core.sales.invoice.report.VenteFactureXmlSheet;
19
import org.openconcerto.erp.core.sales.quote.report.PaypalStamper;
21 ilm 20
import org.openconcerto.erp.generationDoc.element.TypeModeleSQLElement;
156 ilm 21
import org.openconcerto.erp.preferences.PayPalPreferencePanel;
61 ilm 22
import org.openconcerto.erp.storage.CloudStorageEngine;
25 ilm 23
import org.openconcerto.erp.storage.StorageEngine;
24
import org.openconcerto.erp.storage.StorageEngines;
19 ilm 25
import org.openconcerto.openoffice.OOUtils;
18 ilm 26
import org.jopendocument.link.Component;
27
import org.openconcerto.sql.Configuration;
28
import org.openconcerto.sql.element.SQLElement;
29
import org.openconcerto.sql.model.SQLBase;
30
import org.openconcerto.sql.model.SQLRow;
144 ilm 31
import org.openconcerto.sql.model.SQLRowAccessor;
156 ilm 32
import org.openconcerto.sql.preferences.SQLPreferences;
18 ilm 33
import org.openconcerto.utils.ExceptionHandler;
144 ilm 34
import org.openconcerto.utils.cc.ITransformer;
18 ilm 35
 
156 ilm 36
import java.awt.GraphicsEnvironment;
132 ilm 37
import java.awt.print.PrinterJob;
25 ilm 38
import java.io.BufferedInputStream;
18 ilm 39
import java.io.File;
25 ilm 40
import java.io.FileInputStream;
156 ilm 41
import java.io.FileNotFoundException;
19 ilm 42
import java.io.IOException;
156 ilm 43
import java.io.InputStream;
44
import java.io.InputStreamReader;
45
import java.io.OutputStream;
18 ilm 46
import java.lang.Thread.UncaughtExceptionHandler;
156 ilm 47
import java.net.HttpURLConnection;
48
import java.net.URL;
49
import java.net.URLConnection;
50
import java.net.URLEncoder;
51
import java.nio.charset.StandardCharsets;
132 ilm 52
import java.util.Arrays;
18 ilm 53
import java.util.HashMap;
25 ilm 54
import java.util.List;
18 ilm 55
import java.util.Map;
156 ilm 56
import java.util.StringJoiner;
25 ilm 57
import java.util.concurrent.Callable;
58
import java.util.concurrent.ExecutionException;
18 ilm 59
import java.util.concurrent.ExecutorService;
60
import java.util.concurrent.Future;
61
import java.util.concurrent.LinkedBlockingQueue;
132 ilm 62
import java.util.concurrent.ThreadFactory;
18 ilm 63
import java.util.concurrent.ThreadPoolExecutor;
64
import java.util.concurrent.TimeUnit;
65
 
132 ilm 66
import javax.swing.JOptionPane;
18 ilm 67
 
68
import org.jopendocument.model.OpenDocument;
69
 
70
public abstract class SheetXml {
71
 
144 ilm 72
    private ITransformer<List<SQLRowAccessor>, List<SQLRowAccessor>> postProcess = null;
73
 
74
    public ITransformer<List<SQLRowAccessor>, List<SQLRowAccessor>> getPostProcess() {
75
        return postProcess;
76
    }
77
 
93 ilm 78
    // INSTEAD USE THE PREF IN L.O. CALC --> Formules, toujours recalculer
79
    private boolean refreshFormulasRequired = false;
80
 
25 ilm 81
    // return null to keep default value
82
    public interface StorageDirs {
83
        public File getDocumentOutputDirectory(SheetXml sheet);
84
 
85
        public File getPDFOutputDirectory(SheetXml sheet);
86
 
87
        public String getStoragePath(SheetXml sheet);
88
    }
89
 
144 ilm 90
    /**
91
     * Post process line
92
     *
93
     * @param postProcess
94
     */
95
    public void setPostProcess(ITransformer<List<SQLRowAccessor>, List<SQLRowAccessor>> postProcess) {
96
        this.postProcess = postProcess;
97
    }
98
 
25 ilm 99
    private static StorageDirs STORAGE_DIRS;
100
 
101
    // allow to redirect all documents
102
    public static void setStorageDirs(StorageDirs d) {
103
        STORAGE_DIRS = d;
104
    }
105
 
106
    public static final String DEFAULT_PROPERTY_NAME = "Default";
18 ilm 107
    protected SQLElement elt;
108
 
109
    // nom de l'imprimante à utiliser
110
    protected String printer;
111
 
112
    // id
113
    protected SQLRow row;
114
 
19 ilm 115
    // Language du document
116
    protected SQLRow rowLanguage;
117
 
18 ilm 118
    protected static final SQLBase base = ((ComptaPropsConfiguration) Configuration.getInstance()).getSQLBaseSociete();
119
 
120
    // single threaded and kill its thread after 3 seconds (to allow the program to exit)
132 ilm 121
    protected static final ExecutorService runnableQueue = new ThreadPoolExecutor(0, 1, 3L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), new ThreadFactory() {
18 ilm 122
 
132 ilm 123
        @Override
124
        public Thread newThread(Runnable r) {
125
            final Thread res = new Thread(r);
126
            res.setUncaughtExceptionHandler(DEFAULT_HANDLER);
127
 
128
            return res;
129
        }
130
    });
131
 
18 ilm 132
    protected static UncaughtExceptionHandler DEFAULT_HANDLER = new UncaughtExceptionHandler() {
133
        @Override
134
        public void uncaughtException(Thread t, Throwable e) {
19 ilm 135
            ExceptionHandler.handle("Erreur de generation", e);
18 ilm 136
        }
137
    };
138
 
25 ilm 139
    public final SQLElement getElement() {
140
        return this.elt;
18 ilm 141
    }
142
 
25 ilm 143
    /**
144
     * Show, print and export the document to PDF. This method is asynchronous, but is executed in a
145
     * single threaded queue shared with createDocument
132 ilm 146
     */
25 ilm 147
    public Future<SheetXml> showPrintAndExportAsynchronous(final boolean showDocument, final boolean printDocument, final boolean exportToPDF) {
148
        final Callable<SheetXml> c = new Callable<SheetXml>() {
149
            @Override
150
            public SheetXml call() throws Exception {
151
                showPrintAndExport(showDocument, printDocument, exportToPDF);
152
                return SheetXml.this;
153
            }
154
        };
155
        return runnableQueue.submit(c);
18 ilm 156
 
25 ilm 157
    }
158
 
132 ilm 159
    public static Future<?> submitInQueue(final Runnable r) {
160
 
161
        return runnableQueue.submit(r);
94 ilm 162
    }
163
 
61 ilm 164
    public void showPrintAndExport(final boolean showDocument, final boolean printDocument, boolean exportToPDF) {
65 ilm 165
        showPrintAndExport(showDocument, printDocument, exportToPDF, Boolean.getBoolean("org.openconcerto.oo.useODSViewer"), false);
61 ilm 166
    }
167
 
25 ilm 168
    /**
169
     * Show, print and export the document to PDF. This method is synchronous
132 ilm 170
     */
65 ilm 171
    public void showPrintAndExport(final boolean showDocument, final boolean printDocument, boolean exportToPDF, boolean useODSViewer, boolean exportPDFSynch) {
25 ilm 172
 
173
        final File generatedFile = getGeneratedFile();
174
        final File pdfFile = getGeneratedPDFFile();
175
        if (generatedFile == null || !generatedFile.exists()) {
132 ilm 176
            JOptionPane.showMessageDialog(null, "Fichier généré manquant : " + generatedFile);
18 ilm 177
            return;
178
        }
179
 
180
        try {
61 ilm 181
            if (!useODSViewer) {
25 ilm 182
                final Component doc = ComptaPropsConfiguration.getOOConnexion().loadDocument(generatedFile, !showDocument);
18 ilm 183
 
25 ilm 184
                if (printDocument) {
18 ilm 185
                    Map<String, Object> map = new HashMap<String, Object>();
186
                    map.put("Name", this.printer);
187
                    doc.printDocument(map);
188
                }
25 ilm 189
                if (exportToPDF) {
190
                    doc.saveToPDF(pdfFile).get();
191
                }
73 ilm 192
                if (!showDocument) {
193
                    doc.close();
194
                }
18 ilm 195
            } else {
25 ilm 196
                final OpenDocument doc = new OpenDocument(generatedFile);
18 ilm 197
 
25 ilm 198
                if (showDocument) {
18 ilm 199
                    showPreviewDocument();
200
                }
25 ilm 201
                if (printDocument) {
18 ilm 202
                    // Print !
28 ilm 203
                    DefaultNXDocumentPrinter printer = new DefaultNXDocumentPrinter();
132 ilm 204
                    printer.print(Arrays.asList(doc));
18 ilm 205
                }
61 ilm 206
 
207
                // FIXME Profiler pour utiliser moins de ram --> ex : demande trop de mémoire pour
208
                // faire
209
                // un grand livre KD
25 ilm 210
                if (exportToPDF) {
18 ilm 211
 
25 ilm 212
                    Thread t = new Thread(new Runnable() {
213
 
214
                        @Override
215
                        public void run() {
67 ilm 216
                            createPDF(generatedFile, pdfFile, doc, getStoragePath());
65 ilm 217
 
67 ilm 218
                        }
25 ilm 219
 
65 ilm 220
                    }, "convert and upload to pdf");
221
 
222
                    t.setDaemon(true);
25 ilm 223
                    t.start();
65 ilm 224
                    if (exportPDFSynch) {
225
                        t.join();
226
                    }
25 ilm 227
                }
18 ilm 228
            }
229
 
230
        } catch (Exception e) {
156 ilm 231
            ExceptionHandler.handle("Impossible de charger le document OpenOffice " + pdfFile.getAbsolutePath() + "(viewer : " + useODSViewer + ")", e);
18 ilm 232
        }
233
    }
234
 
73 ilm 235
    MetaDataSheet meta;
236
 
237
    /**
238
     * MetaData à inclure lors de la génération
239
     *
240
     * @param meta
241
     */
242
    public void setMetaGeneration(MetaDataSheet meta) {
243
        this.meta = meta;
244
    }
245
 
246
    /**
247
     * MetaData à inclure lors de la génération
248
     *
249
     * @param meta
250
     */
251
 
252
    public MetaDataSheet getMetaGeneration() {
253
        return this.meta;
254
    }
255
 
156 ilm 256
    public void createPDF(final File generatedFile, final File pdfFile, final OpenDocument doc, String storagePath) {
80 ilm 257
        if (pdfFile == null) {
258
            throw new IllegalArgumentException("null PDF file");
259
        }
67 ilm 260
        try {
156 ilm 261
            if (VenteFactureXmlSheet.TEMPLATE_ID.equals(getDefaultTemplateId())) {
262
                final SQLPreferences prefs = SQLPreferences.getMemCached(getElement().getTable().getDBRoot());
263
                if (prefs.getBoolean(PayPalPreferencePanel.PAYPAL_INVOICE, false)) {
264
                    try {
265
                        final File inFile = File.createTempFile("oc_", pdfFile.getName());
266
                        SheetUtils.convert2PDF(doc, inFile);
267
                        PaypalStamper s = new PaypalStamper();
268
                        int x = prefs.getInt(PayPalPreferencePanel.PAYPAL_INVOICE_X, 0);
269
                        int y = prefs.getInt(PayPalPreferencePanel.PAYPAL_INVOICE_Y, 0);
270
 
271
                        // Reference
272
                        String ref = getSQLRow().getString("NUMERO");
273
                        // Montant : ex : 10.55
274
                        long cents = getSQLRow().getLong("NET_A_PAYER");
275
                        String amount = cents / 100 + "." + cents % 100;
276
                        // Devise
277
                        // TODO : autres devises
278
                        String currency = "EUR";
279
                        // POST
280
                        final URL url = new URL("https://cloud.openconcerto.org/payment");
281
                        final URLConnection con = url.openConnection();
282
                        final HttpURLConnection http = (HttpURLConnection) con;
283
                        http.setRequestMethod("POST");
284
                        http.setDoOutput(true);
285
                        http.setDefaultUseCaches(false);
286
 
287
                        String hyperlink = null;
288
                        // x-www-form-urlencoded
289
                        final Map<String, String> arguments = new HashMap<>();
290
                        arguments.put("pI", prefs.get(PayPalPreferencePanel.PAYPAL_CLIENTID, ""));
291
                        arguments.put("pS", prefs.get(PayPalPreferencePanel.PAYPAL_SECRET, ""));
292
                        arguments.put("ref", ref);
293
                        arguments.put("amount", amount);
294
                        arguments.put("currency", currency);
295
                        arguments.put("type", "paypal");
296
                        final StringJoiner sj = new StringJoiner("&");
297
                        for (Map.Entry<String, String> entry : arguments.entrySet()) {
298
                            sj.add(URLEncoder.encode(entry.getKey(), "UTF-8") + "=" + URLEncoder.encode(entry.getValue(), "UTF-8"));
299
                        }
300
                        final String postData = sj.toString();
301
                        System.err.println("SheetXml.createPDF() " + postData);
302
                        byte[] out = postData.getBytes(StandardCharsets.UTF_8);
303
                        int length = out.length;
304
                        http.setFixedLengthStreamingMode(length);
305
                        http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
306
                        http.connect();
307
                        try (OutputStream os = http.getOutputStream()) {
308
                            os.write(out);
309
                        }
310
                        if (http.getResponseCode() != 401) {
311
 
312
                            InputStream is = http.getInputStream();
313
                            InputStreamReader isr = new InputStreamReader(is);
314
 
315
                            int numCharsRead;
316
                            char[] charArray = new char[1024];
317
                            StringBuilder sb = new StringBuilder();
318
                            while ((numCharsRead = isr.read(charArray)) > 0) {
319
                                sb.append(charArray, 0, numCharsRead);
320
                            }
321
                            //
322
                            hyperlink = sb.toString();
323
                        }
324
                        s.addLink(inFile, pdfFile, x, y, hyperlink);
325
                    } catch (Exception e) {
326
                        e.printStackTrace();
327
                        SheetUtils.convert2PDF(doc, pdfFile);
328
                    }
329
 
330
                } else {
331
                    SheetUtils.convert2PDF(doc, pdfFile);
332
                }
333
            } else {
334
                SheetUtils.convert2PDF(doc, pdfFile);
335
            }
336
 
67 ilm 337
        } catch (Throwable e) {
80 ilm 338
            ExceptionHandler.handle("Impossible de créer le PDF " + pdfFile.getAbsolutePath(), e);
67 ilm 339
        }
80 ilm 340
        if (!pdfFile.canRead()) {
341
            ExceptionHandler.handle("Le fichier PDF " + pdfFile.getAbsolutePath() + " ne peut être lu.");
342
        }
67 ilm 343
        List<StorageEngine> engines = StorageEngines.getInstance().getActiveEngines();
344
        for (StorageEngine storageEngine : engines) {
345
            if (storageEngine.isConfigured() && storageEngine.allowAutoStorage()) {
80 ilm 346
                final String path = storagePath;
67 ilm 347
                try {
348
                    storageEngine.connect();
349
                    final BufferedInputStream inStream = new BufferedInputStream(new FileInputStream(pdfFile));
350
                    storageEngine.store(inStream, path, pdfFile.getName(), true);
351
                    inStream.close();
352
                    storageEngine.disconnect();
353
                } catch (IOException e) {
80 ilm 354
                    ExceptionHandler.handle("Impossible de sauvegarder le PDF " + pdfFile.getAbsolutePath() + " vers " + path + "(" + storageEngine + ")", e);
67 ilm 355
                }
356
                if (storageEngine instanceof CloudStorageEngine) {
357
                    try {
358
                        storageEngine.connect();
359
                        final BufferedInputStream inStream = new BufferedInputStream(new FileInputStream(generatedFile));
360
                        storageEngine.store(inStream, path, generatedFile.getName(), true);
361
                        inStream.close();
362
                        storageEngine.disconnect();
363
                    } catch (IOException e) {
80 ilm 364
                        ExceptionHandler.handle("Impossible de sauvegarder le fichier généré " + generatedFile.getAbsolutePath() + " vers " + path + "(" + storageEngine + ")", e);
67 ilm 365
                    }
366
                }
367
            }
368
        }
369
    }
370
 
25 ilm 371
    public abstract String getDefaultTemplateId();
21 ilm 372
 
25 ilm 373
    /**
374
     * Path of the directory used for storage. Ex: Devis/2010
132 ilm 375
     */
25 ilm 376
    public final String getStoragePath() {
377
        final String res = STORAGE_DIRS == null ? null : STORAGE_DIRS.getStoragePath(this);
378
        if (res != null)
379
            return res;
380
        else
381
            return this.getStoragePathP();
382
    }
383
 
384
    public final File getDocumentOutputDirectory() {
385
        final File res = STORAGE_DIRS == null ? null : STORAGE_DIRS.getDocumentOutputDirectory(this);
386
        if (res != null)
387
            return res;
388
        else
389
            return this.getDocumentOutputDirectoryP();
390
    }
391
 
392
    public final File getPDFOutputDirectory() {
393
        final File res = STORAGE_DIRS == null ? null : STORAGE_DIRS.getPDFOutputDirectory(this);
394
        if (res != null)
395
            return res;
396
        else
397
            return this.getPDFOutputDirectoryP();
398
    }
399
 
400
    protected abstract String getStoragePathP();
401
 
402
    protected abstract File getDocumentOutputDirectoryP();
403
 
404
    protected abstract File getPDFOutputDirectoryP();
405
 
406
    /**
407
     * Name of the generated document (without extension), do not rely on this name.
408
     *
409
     * Use getGeneratedFile().getName() to get the generated file name.
132 ilm 410
     */
25 ilm 411
    public abstract String getName();
412
 
413
    /**
414
     * @return the template id for this template (ex: "sales.quote")
132 ilm 415
     */
25 ilm 416
    public String getTemplateId() {
417
        if (this.row != null && this.row.getTable().getFieldsName().contains("ID_MODELE")) {
151 ilm 418
            if (row.getObject("ID_MODELE") == null || row.isForeignEmpty("ID_MODELE")) {
21 ilm 419
                TypeModeleSQLElement typeModele = Configuration.getInstance().getDirectory().getElement(TypeModeleSQLElement.class);
25 ilm 420
                String modele = typeModele.getTemplateMapping().get(this.row.getTable().getName());
21 ilm 421
                if (modele == null) {
422
                    System.err.println("No default modele in table TYPE_MODELE for table " + this.row.getTable().getName());
423
                    Thread.dumpStack();
25 ilm 424
                    return getDefaultTemplateId();
21 ilm 425
                } else {
426
                    return modele;
427
                }
428
            } else {
90 ilm 429
                SQLRow rowModele = this.row.getForeignRow("ID_MODELE");
21 ilm 430
                return rowModele.getString("NOM");
431
            }
432
        }
25 ilm 433
        return getDefaultTemplateId();
21 ilm 434
    }
435
 
25 ilm 436
    public abstract Future<SheetXml> createDocumentAsynchronous();
18 ilm 437
 
25 ilm 438
    public void createDocument() throws InterruptedException, ExecutionException {
439
        createDocumentAsynchronous().get();
18 ilm 440
    }
441
 
25 ilm 442
    /**
443
     * get the File that is, or must be generated.
444
     *
445
     * @return a file (not null)
132 ilm 446
     */
25 ilm 447
    public abstract File getGeneratedFile();
18 ilm 448
 
25 ilm 449
    public File getGeneratedPDFFile() {
450
        return SheetUtils.getFileWithExtension(getGeneratedFile(), ".pdf");
18 ilm 451
    }
452
 
19 ilm 453
    public SQLRow getRowLanguage() {
454
        return this.rowLanguage;
455
    }
456
 
457
    public String getReference() {
458
        return "";
459
    }
460
 
18 ilm 461
    /**
25 ilm 462
     * Creates the document if needed and returns the generated file (OpenDocument)
132 ilm 463
     */
25 ilm 464
    public File getOrCreateDocumentFile() throws Exception {
465
        File f = getGeneratedFile();
466
        if (!f.exists()) {
467
            return createDocumentAsynchronous().get().getGeneratedFile();
18 ilm 468
        } else {
25 ilm 469
            return f;
18 ilm 470
        }
471
    }
472
 
25 ilm 473
    /**
41 ilm 474
     * Creates the document if needed and returns the generated file (OpenDocument)
475
     *
476
     * @param createRecent true for recreate the pdf document if older than ods
477
     * @return
478
     * @throws Exception
479
     *
132 ilm 480
     */
41 ilm 481
    public File getOrCreatePDFDocumentFile(boolean createRecent) throws Exception {
61 ilm 482
        return getOrCreatePDFDocumentFile(createRecent, Boolean.getBoolean("org.openconcerto.oo.useODSViewer"));
483
    }
484
 
485
    public File getOrCreatePDFDocumentFile(boolean createRecent, boolean useODSViewer) throws Exception {
41 ilm 486
        File f = getGeneratedPDFFile();
487
        if (!f.exists()) {
488
            getOrCreateDocumentFile();
65 ilm 489
            showPrintAndExport(false, false, true, useODSViewer, true);
41 ilm 490
            return f;
491
        } else {
492
            File fODS = getOrCreateDocumentFile();
493
            if (fODS.lastModified() > f.lastModified()) {
65 ilm 494
                showPrintAndExport(false, false, true, useODSViewer, true);
41 ilm 495
            }
496
            return f;
497
        }
498
    }
499
 
500
    /**
25 ilm 501
     * Open the document with the native application
502
     *
503
     * @param synchronous
132 ilm 504
     */
25 ilm 505
    public void openDocument(boolean synchronous) {
506
        Runnable r = new Runnable() {
507
 
508
            @Override
509
            public void run() {
510
                File f;
511
                try {
512
                    f = getOrCreateDocumentFile();
156 ilm 513
                    if (f != null && f.exists()) {
514
                        OOUtils.open(f);
515
                    } else {
516
                        if (!GraphicsEnvironment.isHeadless()) {
517
                            if (f != null) {
518
                                JOptionPane.showMessageDialog(null, "Le fichier " + f.getAbsolutePath() + " est manquant");
519
                            } else {
520
                                JOptionPane.showMessageDialog(null, "Fichier manquant");
521
                            }
522
                        } else {
523
                            if (f != null) {
524
                                throw new FileNotFoundException(f.getAbsolutePath() + " missing");
525
                            } else {
526
                                throw new NullPointerException("null document");
527
                            }
528
                        }
529
                    }
25 ilm 530
                } catch (Exception e) {
531
                    ExceptionHandler.handle("Impossible d'ouvrir le document.", e);
532
                }
18 ilm 533
            }
25 ilm 534
        };
535
        if (synchronous) {
536
            r.run();
18 ilm 537
        } else {
25 ilm 538
            Thread thread = new Thread(r, "openDocument: " + getGeneratedFile().getAbsolutePath());
539
            thread.setDaemon(true);
540
            thread.start();
18 ilm 541
        }
542
 
543
    }
544
 
25 ilm 545
    public void showPreviewDocument() throws Exception {
546
        File f = null;
547
        f = getOrCreateDocumentFile();
18 ilm 548
        PreviewFrame.show(f);
549
    }
550
 
132 ilm 551
    public void printDocument() {
552
        printDocument(null);
18 ilm 553
    }
554
 
132 ilm 555
    public void printDocument(PrinterJob job) {
18 ilm 556
 
557
        try {
25 ilm 558
            final File f = getOrCreateDocumentFile();
559
 
18 ilm 560
            if (!Boolean.getBoolean("org.openconcerto.oo.useODSViewer")) {
561
                final Component doc = ComptaPropsConfiguration.getOOConnexion().loadDocument(f, true);
149 ilm 562
                doc.printDocument(job);
18 ilm 563
                doc.close();
564
            } else {
565
                // Load the spreadsheet.
566
                final OpenDocument doc = new OpenDocument(f);
567
                // Print !
568
                DefaultNXDocumentPrinter printer = new DefaultNXDocumentPrinter();
132 ilm 569
                printer.print(Arrays.asList(doc), job);
18 ilm 570
            }
571
 
572
        } catch (Exception e) {
132 ilm 573
            ExceptionHandler.handle("Impossible d'imprimer le document OpenOffice", e);
18 ilm 574
            e.printStackTrace();
575
        }
576
    }
577
 
578
    public SQLRow getSQLRow() {
579
        return this.row;
580
    }
581
 
582
    /**
583
     * Remplace tous les caracteres non alphanumeriques (seul le _ est autorisé) par un -. Cela
584
     * permet d'avoir toujours un nom de fichier valide.
585
     *
586
     * @param fileName nom du fichier à créer ex:FACTURE_2007/03/001
587
     * @return un nom fichier valide ex:FACTURE_2007-03-001
588
     */
83 ilm 589
    public static String getValidFileName(String fileName) {
25 ilm 590
        final StringBuffer result = new StringBuffer(fileName.length());
18 ilm 591
        for (int i = 0; i < fileName.length(); i++) {
25 ilm 592
            char ch = fileName.charAt(i);
18 ilm 593
 
594
            // Si c'est un caractere alphanumerique
25 ilm 595
            if (Character.isLetterOrDigit(ch) || (ch == '_') || (ch == ' ')) {
596
                result.append(ch);
18 ilm 597
            } else {
598
                result.append('-');
599
            }
600
        }
601
        return result.toString();
602
    }
603
 
604
    public String getPrinter() {
605
        return this.printer;
606
    }
607
 
93 ilm 608
    public void setRefreshFormulasRequired(boolean refreshFormulasRequired) {
609
        this.refreshFormulasRequired = refreshFormulasRequired;
610
    }
611
 
612
    public boolean isRefreshFormulasRequired() {
613
            return refreshFormulasRequired;
614
    }
615
 
18 ilm 616
}