OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 18 | Rev 57 | 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 static org.openconcerto.task.config.ComptaBasePropsConfiguration.getStream;
17
import org.openconcerto.erp.config.ComptaPropsConfiguration;
18
import org.openconcerto.erp.config.Gestion;
19
import org.openconcerto.erp.preferences.PrinterNXProps;
20
import org.openconcerto.erp.preferences.TemplateNXProps;
21
import org.openconcerto.map.model.Ville;
22
import org.openconcerto.odtemplate.Template;
23
import org.openconcerto.odtemplate.engine.OGNLDataModel;
24
import org.jopendocument.link.Component;
25
import org.jopendocument.link.OOConnexion;
26
import org.openconcerto.sql.model.SQLRow;
27
import org.openconcerto.utils.ExceptionHandler;
28
import org.openconcerto.utils.FileUtils;
29
import org.openconcerto.utils.Tuple2;
30
 
31
import java.io.BufferedInputStream;
32
import java.io.File;
33
import java.io.FileNotFoundException;
34
import java.io.IOException;
35
import java.io.InputStream;
36
import java.text.DateFormat;
37
import java.text.SimpleDateFormat;
38
import java.util.HashMap;
39
import java.util.Map;
40
 
41
import javax.swing.JOptionPane;
42
import javax.swing.SwingUtilities;
43
 
44
public abstract class AbstractJOOReportsSheet {
45
    private static final String defaultLocationTemplate = SpreadSheetGenerator.defaultLocationTemplate;
46
    protected static final DateFormat dateFormat = new SimpleDateFormat("dd MMMM yyyy");
47
    protected static final DateFormat dateFormat2 = new SimpleDateFormat("dd/MM/yy");
48
    protected static final DateFormat yearFormat = new SimpleDateFormat("yyyy");
49
    private String year;
50
    protected String locationTemplate = TemplateNXProps.getInstance().getStringProperty("LocationTemplate");
25 ilm 51
    protected String templateId;
18 ilm 52
    private String printer;
53
    protected boolean askOverwriting = false;
54
 
55
    /**
56
     * @return une Map contenant les valeurs à remplacer dans la template
57
     */
58
    abstract protected Map createMap();
59
 
60
    abstract public String getFileName();
61
 
25 ilm 62
    protected void init(String year, String templateId, String attributePrinter) {
18 ilm 63
        this.year = year;
25 ilm 64
        this.templateId = templateId;
18 ilm 65
        this.printer = PrinterNXProps.getInstance().getStringProperty(attributePrinter);
66
    }
67
 
68
    public final void generate(boolean print, boolean show, String printer) {
69
        generate(print, show, printer, false);
70
    }
71
 
72
    /**
73
     * Genere le document OO, le pdf, et ouvre le document OO
74
     *
75
     * @param print
76
     * @param show
77
     * @param printer
78
     */
79
    public void generate(boolean print, boolean show, String printer, boolean overwrite) {
80
 
81
        if (this.locationTemplate.trim().length() == 0) {
82
            this.locationTemplate = defaultLocationTemplate;
83
        }
84
        try {
85
 
86
            String fileName = getFileName();
25 ilm 87
            final InputStream fileTemplate = TemplateManager.getInstance().getTemplate(this.templateId);
88
            File outputDir = DocumentLocalStorageManager.getInstance().getDocumentOutputDirectory(this.templateId);
89
            File fileOutOO = getDocumentFile();
18 ilm 90
            if (fileOutOO.exists() && overwrite) {
91
                if (this.askOverwriting) {
92
                    int answer = JOptionPane.showConfirmDialog(null, "Voulez vous écraser le document ?", "Remplacement d'un document", JOptionPane.YES_NO_OPTION);
93
                    if (answer == JOptionPane.YES_OPTION) {
25 ilm 94
                        SheetUtils.convertToOldFile(fileName, outputDir, fileOutOO, ".odt");
18 ilm 95
                    }
96
                } else {
25 ilm 97
                    SheetUtils.convertToOldFile(fileName, outputDir, fileOutOO, ".odt");
18 ilm 98
                }
99
            }
100
 
101
            if (!fileOutOO.exists()) {
102
                fileOutOO.getParentFile().mkdirs();
25 ilm 103
                Template template = new Template(new BufferedInputStream(fileTemplate));
18 ilm 104
 
105
                // creation du document
106
                final Map createMap = createMap();
107
                OGNLDataModel model = new OGNLDataModel(createMap);
108
 
109
                model.putAll(createMap);
110
                template.createDocument(model).saveAs(fileOutOO);
25 ilm 111
 
18 ilm 112
            }
113
 
114
            // ouverture de OO
115
            if (show || print) {
116
 
117
                try {
118
                    final OOConnexion ooConnexion = ComptaPropsConfiguration.getOOConnexion();
119
                    if (ooConnexion == null) {
120
                        return;
121
                    }
122
                    final Component doc = ooConnexion.loadDocument(fileOutOO, !show);
123
 
25 ilm 124
                    if (this.savePDF()) {
125
                        File pdfOutputDir = DocumentLocalStorageManager.getInstance().getPDFOutputDirectory(templateId);
126
                        doc.saveToPDF(new File(pdfOutputDir, fileName + ".pdf"), "writer_pdf_Export");
127
                    }
18 ilm 128
                    if (print) {
129
                        Map<String, Object> map = new HashMap<String, Object>();
130
                        map.put("Name", printer);
131
                        doc.printDocument(map);
132
                    }
133
                    doc.close();
134
 
135
                } catch (Exception e) {
136
                    e.printStackTrace();
137
                    ExceptionHandler.handle("Impossible de charger le document OpenOffice", e);
138
                }
139
 
140
            }
141
        } catch (FileNotFoundException e) {
142
            e.printStackTrace();
143
            ExceptionHandler.handle("Impossible de trouver le modéle.", e);
144
        } catch (IOException e) {
145
            e.printStackTrace();
146
        } catch (org.openconcerto.odtemplate.TemplateException e) {
147
            e.printStackTrace();
148
        }
149
    }
150
 
151
    protected boolean savePDF() {
152
        return false;
153
    }
154
 
155
    public void showDocument() {
25 ilm 156
        File fileOutOO = getDocumentFile();
18 ilm 157
        if (fileOutOO.exists()) {
158
            try {
159
                final OOConnexion ooConnexion = ComptaPropsConfiguration.getOOConnexion();
160
                if (ooConnexion == null) {
161
                    return;
162
                }
163
                ooConnexion.loadDocument(fileOutOO, false);
164
 
165
            } catch (Exception e) {
166
                e.printStackTrace();
167
                ExceptionHandler.handle("Impossible de charger le document OpenOffice", e);
168
            }
169
 
170
        } else {
171
            generate(false, true, "");
172
        }
173
    }
174
 
25 ilm 175
    private File getDocumentFile() {
176
        File outputDir = DocumentLocalStorageManager.getInstance().getDocumentOutputDirectory(templateId);
177
        return new File(outputDir, getFileName() + ".odt");
178
    }
179
 
18 ilm 180
    public void printDocument() {
25 ilm 181
        File fileOutOO = getDocumentFile();
18 ilm 182
        if (fileOutOO.exists()) {
183
 
184
            try {
185
                final OOConnexion ooConnexion = ComptaPropsConfiguration.getOOConnexion();
186
                if (ooConnexion == null) {
187
                    return;
188
                }
189
                final Component doc = ooConnexion.loadDocument(fileOutOO, true);
190
 
191
                Map<String, Object> map = new HashMap<String, Object>();
192
                map.put("Name", printer);
193
                doc.printDocument(map);
194
                doc.close();
195
 
196
            } catch (Exception e) {
197
                e.printStackTrace();
198
                ExceptionHandler.handle("Impossible de charger le document OpenOffice", e);
199
            }
200
 
201
        } else {
202
            generate(true, false, this.printer);
203
        }
204
    }
205
 
206
    public void fastPrintDocument() {
207
 
25 ilm 208
        final File f = getDocumentFile();
18 ilm 209
 
210
        if (!f.exists()) {
211
            generate(true, false, this.printer);
212
        } else {
213
 
214
            try {
215
                final OOConnexion ooConnexion = ComptaPropsConfiguration.getOOConnexion();
216
                if (ooConnexion == null) {
217
                    return;
218
                }
219
                final Component doc = ooConnexion.loadDocument(f, true);
220
 
221
                Map<String, Object> map = new HashMap<String, Object>();
222
                map.put("Name", this.printer);
223
                Map<String, Object> map2 = new HashMap<String, Object>();
224
                map2.put("CopyCount", 1);
225
                doc.printDocument(map, map2);
226
                doc.close();
227
 
228
            } catch (Exception e) {
229
 
230
                ExceptionHandler.handle("Impossible de charger le document OpentOffice", e);
231
                e.printStackTrace();
232
            }
233
        }
234
    }
235
 
236
    protected String getInitiales(SQLRow row) {
237
        String init = "";
238
        if (row != null) {
239
            final String stringPrenom = row.getString("PRENOM");
240
            if (stringPrenom != null && stringPrenom.trim().length() != 0) {
241
                init += stringPrenom.trim().charAt(0);
242
            }
243
            final String stringNom = row.getString("NOM");
244
            if (stringNom != null && stringNom.trim().length() != 0) {
245
                init += stringNom.trim().charAt(0);
246
            }
247
        }
248
        return init;
249
    }
250
 
251
    public void exportToPdf() {
252
 
253
        // Export vers PDF
25 ilm 254
        final String fileName = getFileName();
255
        final File fileOutOO = getDocumentFile();
256
        final File outputPDFDirectory = DocumentLocalStorageManager.getInstance().getPDFOutputDirectory(this.templateId);
257
        final File fileOutPDF = new File(outputPDFDirectory, fileName + ".pdf");
18 ilm 258
 
259
        if (!fileOutOO.exists()) {
260
            generate(false, false, "");
261
        }
262
 
263
        try {
264
 
265
            final OOConnexion ooConnexion = ComptaPropsConfiguration.getOOConnexion();
266
            if (ooConnexion == null) {
267
                return;
268
            }
269
            final Component doc = ooConnexion.loadDocument(fileOutOO, true);
270
            doc.saveToPDF(fileOutPDF, "writer_pdf_Export");
271
            doc.close();
272
 
273
        } catch (Exception e) {
274
            e.printStackTrace();
275
            ExceptionHandler.handle("Impossible de charger le document OpenOffice", e);
276
        }
277
        // Ouverture
278
        int result = JOptionPane.showOptionDialog(null, "Ouvrir le pdf ?", "Ouverture du PDF", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null);
279
 
280
        if (result == JOptionPane.YES_OPTION) {
25 ilm 281
            Gestion.openPDF(fileOutPDF);
18 ilm 282
        } else {
283
            try {
284
                FileUtils.openFile(fileOutPDF.getParentFile());
285
            } catch (IOException e) {
286
                // TODO Auto-generated catch block
287
                e.printStackTrace();
288
                JOptionPane.showMessageDialog(null, "Impossible d'ouvrir le dossier : " + fileOutPDF.getParentFile() + ".");
289
            }
290
        }
291
 
292
    }
293
 
294
    protected static String getVille(final String name) {
295
 
296
        Ville ville = Ville.getVilleFromVilleEtCode(name);
297
        if (ville == null) {
298
            SwingUtilities.invokeLater(new Runnable() {
299
                public void run() {
300
                    JOptionPane.showMessageDialog(null, "La ville " + "\"" + name + "\"" + " est introuvable! Veuillez corriger l'erreur!");
301
                }
302
            });
303
            return null;
304
        }
305
        return ville.getName();
306
    }
307
 
308
    protected static String getVilleCP(String name) {
309
        Ville ville = Ville.getVilleFromVilleEtCode(name);
310
        if (ville == null) {
311
 
312
            return null;
313
        }
314
        return ville.getCodepostal();
315
    }
316
}