OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 149 | 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.model;
15
 
16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
17
import org.openconcerto.erp.generationDoc.AbstractSheetXml;
18
import org.openconcerto.sql.Configuration;
41 ilm 19
import org.openconcerto.sql.model.SQLBase;
18 ilm 20
import org.openconcerto.sql.model.SQLField;
21
import org.openconcerto.sql.model.SQLRow;
19 ilm 22
import org.openconcerto.sql.model.SQLRowAccessor;
93 ilm 23
import org.openconcerto.sql.model.SQLRowValues;
18 ilm 24
import org.openconcerto.sql.model.SQLTable;
25
import org.openconcerto.sql.view.list.IListe;
19 ilm 26
import org.openconcerto.sql.view.list.RowAction;
18 ilm 27
import org.openconcerto.ui.EmailComposer;
28
import org.openconcerto.utils.ExceptionHandler;
156 ilm 29
import org.openconcerto.utils.ListMap;
18 ilm 30
 
31
import java.awt.event.ActionEvent;
32
import java.io.File;
33
import java.lang.reflect.Constructor;
34
import java.util.ArrayList;
35
import java.util.List;
36
import java.util.Set;
156 ilm 37
import java.util.StringJoiner;
18 ilm 38
 
39
import javax.swing.AbstractAction;
65 ilm 40
import javax.swing.Action;
73 ilm 41
import javax.swing.JOptionPane;
41 ilm 42
import javax.swing.SwingUtilities;
18 ilm 43
 
25 ilm 44
public class MouseSheetXmlListeListener {
18 ilm 45
 
46
    private Class<? extends AbstractSheetXml> clazz;
47
    protected IListe liste;
48
 
49
    private boolean previewIsVisible = true;
50
    private boolean showIsVisible = true;
51
    private boolean printIsVisible = true;
52
    private boolean generateIsVisible = true;
28 ilm 53
    private boolean previewHeader = false;
54
    private boolean showHeader = false;
55
    private boolean generateHeader = false;
18 ilm 56
 
25 ilm 57
    public MouseSheetXmlListeListener(Class<? extends AbstractSheetXml> clazz) {
58
        this(clazz, true, true, true, true);
59
 
18 ilm 60
    }
61
 
25 ilm 62
    public MouseSheetXmlListeListener(Class<? extends AbstractSheetXml> clazz, boolean show, boolean preview, boolean print, boolean generate) {
18 ilm 63
        this.clazz = clazz;
64
        this.printIsVisible = print;
65
        this.previewIsVisible = preview;
66
        this.showIsVisible = show;
67
        this.generateIsVisible = generate;
68
    }
69
 
70
    protected Class<? extends AbstractSheetXml> getSheetClass() {
71
        return this.clazz;
72
    }
73
 
74
    protected AbstractSheetXml createAbstractSheet(SQLRow row) {
41 ilm 75
        try {
76
            Constructor<? extends AbstractSheetXml> ctor = getSheetClass().getConstructor(SQLRow.class);
77
            AbstractSheetXml sheet = ctor.newInstance(row);
78
            return sheet;
79
        } catch (Exception e) {
73 ilm 80
            ExceptionHandler.handle("sheet creation error", e);
18 ilm 81
        }
41 ilm 82
        return null;
18 ilm 83
    }
84
 
142 ilm 85
    public List<AbstractSheetXml> createAbstractSheets(List<SQLRow> rows) {
149 ilm 86
        final List<AbstractSheetXml> sheets = new ArrayList<>(rows.size());
142 ilm 87
        try {
88
            final Constructor<? extends AbstractSheetXml> ctor = getSheetClass().getConstructor(SQLRow.class);
89
            for (SQLRow row : rows) {
90
                AbstractSheetXml sheet = ctor.newInstance(row);
91
                sheets.add(sheet);
92
            }
93
        } catch (Exception e) {
94
            ExceptionHandler.handle("sheet creation error", e);
95
        }
96
        return sheets;
97
    }
98
 
41 ilm 99
    protected String getMailObject(SQLRow row) {
100
        return "";
101
    }
102
 
28 ilm 103
    public void setPreviewHeader(boolean previewHeader) {
104
        this.previewHeader = previewHeader;
105
    }
18 ilm 106
 
28 ilm 107
    public void setGenerateHeader(boolean generateHeader) {
108
        this.generateHeader = generateHeader;
109
    }
18 ilm 110
 
28 ilm 111
    public void setShowHeader(boolean showHeader) {
112
        this.showHeader = showHeader;
113
    }
114
 
115
    protected void sendMail(final AbstractSheetXml sheet, final boolean readOnly) {
156 ilm 116
        List<AbstractSheetXml> l = new ArrayList<>(1);
142 ilm 117
        l.add(sheet);
118
        sendMail(l, readOnly);
119
    }
28 ilm 120
 
142 ilm 121
    protected void sendMail(final List<AbstractSheetXml> sheets, final boolean readOnly) {
156 ilm 122
        final Thread t = new Thread() {
123
            @Override
124
            public void run() {
125
                ListMap<String, File> mailFilesMap = new ListMap<>();
126
                for (AbstractSheetXml sheet : sheets) {
127
                    String mail = "";
128
                    final SQLRow row = sheet.getSQLRow();
129
                    Set<SQLField> setContact = null;
130
                    SQLTable tableContact = Configuration.getInstance().getRoot().findTable("CONTACT");
131
                    setContact = row.getTable().getForeignKeys(tableContact);
18 ilm 132
 
156 ilm 133
                    Set<SQLField> setClient = null;
134
                    SQLTable tableClient = ((ComptaPropsConfiguration) Configuration.getInstance()).getSQLBaseSociete().getTable("CLIENT");
135
                    setClient = row.getTable().getForeignKeys(tableClient);
18 ilm 136
 
156 ilm 137
                    for (SQLField field : setContact) {
142 ilm 138
                        if (mail == null || mail.trim().length() == 0) {
156 ilm 139
                            mail = row.getForeignRow(field.getName()).getString("EMAIL");
142 ilm 140
                        }
18 ilm 141
                    }
156 ilm 142
                    String nomClient = "";
143
                    if (setClient != null && (mail == null || mail.trim().length() == 0)) {
144
                            for (SQLField field : setClient) {
145
                                SQLRow rowCli = row.getForeignRow(field.getName());
146
                                if (mail == null || mail.trim().length() == 0) {
147
                                    mail = rowCli.getString("MAIL");
148
                                }
149
                                nomClient = rowCli.getString("NOM");
150
                            }
151
                    }
18 ilm 152
 
156 ilm 153
                    if (mail.trim().length() == 0) {
154
                        mail = nomClient;
155
                    }
41 ilm 156
 
156 ilm 157
                    String table = row.getTable().getName();
158
                    if (table.equalsIgnoreCase("COMMANDE") || table.equalsIgnoreCase("DEMANDE_PRIX") || table.equalsIgnoreCase("FACTURE_FOURNISSEUR")) {
159
                        mail = "";
160
                    }
161
                    if (mail == null || mail.trim().length() == 0) {
162
                        SQLTable tableF = ((ComptaPropsConfiguration) Configuration.getInstance()).getSQLBaseSociete().getTable("FOURNISSEUR");
163
                        Set<SQLField> setF = null;
164
                        setF = row.getTable().getForeignKeys(tableF);
41 ilm 165
 
156 ilm 166
                        if (setF != null) {
167
 
168
                            for (SQLField field : setF) {
169
                                SQLRow rowF = row.getForeignRow(field.getName());
170
                                if (mail == null || mail.trim().length() == 0) {
171
                                    mail = rowF.getString("MAIL");
172
                                }
173
                            }
142 ilm 174
                        }
41 ilm 175
 
156 ilm 176
                        if (mail == null || mail.trim().length() == 0) {
177
                            SQLBase base = ((ComptaPropsConfiguration) Configuration.getInstance()).getSQLBaseSociete();
178
                            if (base.containsTable("MONTEUR")) {
179
                                SQLTable tableM = ((ComptaPropsConfiguration) Configuration.getInstance()).getSQLBaseSociete().getTable("MONTEUR");
180
                                Set<SQLField> setM = null;
181
                                setM = row.getTable().getForeignKeys(tableM);
182
                                if (setM != null) {
183
                                    for (SQLField field : setM) {
184
                                        SQLRow rowM = row.getForeignRow(field.getName());
185
                                        if (rowM.getForeignRow("ID_CONTACT_FOURNISSEUR") != null && !rowM.getForeignRow("ID_CONTACT_FOURNISSEUR").isUndefined()) {
186
                                            mail = rowM.getForeignRow("ID_CONTACT_FOURNISSEUR").getString("EMAIL");
187
                                        }
188
                                    }
142 ilm 189
                                }
41 ilm 190
                            }
191
                        }
156 ilm 192
                        if (mail == null || mail.trim().length() == 0) {
193
                            SQLBase base = ((ComptaPropsConfiguration) Configuration.getInstance()).getSQLBaseSociete();
194
                            if (base.containsTable("TRANSPORTEUR")) {
41 ilm 195
 
156 ilm 196
                                SQLTable tableM = ((ComptaPropsConfiguration) Configuration.getInstance()).getSQLBaseSociete().getTable("TRANSPORTEUR");
197
                                Set<SQLField> setM = null;
198
                                setM = row.getTable().getForeignKeys(tableM);
41 ilm 199
 
156 ilm 200
                                if (setM != null) {
41 ilm 201
 
156 ilm 202
                                    for (SQLField field : setM) {
203
                                        SQLRow rowM = row.getForeignRow(field.getName());
204
                                        if (rowM.getForeignRow("ID_CONTACT_FOURNISSEUR") != null && !rowM.getForeignRow("ID_CONTACT_FOURNISSEUR").isUndefined()) {
205
                                            mail = rowM.getForeignRow("ID_CONTACT_FOURNISSEUR").getString("EMAIL");
206
                                        }
207
                                    }
142 ilm 208
                                }
41 ilm 209
                            }
210
                        }
211
                    }
156 ilm 212
                    try {
213
                        if (readOnly) {
214
                            mailFilesMap.add(mail, sheet.getOrCreatePDFDocumentFile(true).getAbsoluteFile());
215
                        } else {
216
                            mailFilesMap.add(mail, sheet.getOrCreateDocumentFile().getAbsoluteFile());
217
 
218
                        }
219
                    } catch (Exception e) {
220
                        ExceptionHandler.handle("Impossible de charger le document PDF", e);
221
                    }
41 ilm 222
                }
18 ilm 223
 
156 ilm 224
                for (final String mailDest : mailFilesMap.keySet()) {
19 ilm 225
 
156 ilm 226
                    final List<File> files = mailFilesMap.get(mailDest);
142 ilm 227
 
156 ilm 228
                    SwingUtilities.invokeLater(new Runnable() {
41 ilm 229
 
156 ilm 230
                        @Override
231
                        public void run() {
232
                            try {
233
                                String subject = sheets.get(0).getReference();
234
                                if (subject.isEmpty()) {
235
                                    final StringJoiner joiner = new StringJoiner(", ");
236
                                    for (File f : files) {
237
                                        joiner.add(f.getName());
238
                                    }
239
                                    subject = joiner.toString();
41 ilm 240
                                }
156 ilm 241
                                final String message = getMailObject(sheets.get(0).getSQLRow());
242
                                EmailComposer.getInstance().compose(mailDest, subject, message, files.toArray(new File[files.size()]));
243
                            } catch (Exception e) {
244
                                ExceptionHandler.handle("Impossible d'envoyer le courriel!", e);
41 ilm 245
                            }
156 ilm 246
                        }
247
                    });
18 ilm 248
                }
156 ilm 249
 
18 ilm 250
            }
251
 
156 ilm 252
        };
253
        t.start();
254
 
18 ilm 255
    }
256
 
25 ilm 257
    public List<RowAction> addToMenu() {
18 ilm 258
        return null;
259
    }
260
 
19 ilm 261
    public List<RowAction> getRowActions() {
149 ilm 262
        List<RowAction> l = new ArrayList<>();
19 ilm 263
 
264
        if (!Boolean.getBoolean("org.openconcerto.oo.useODSViewer")) {
265
            if (this.showIsVisible) {
67 ilm 266
                RowAction action = new RowAction(new AbstractAction() {
19 ilm 267
                    public void actionPerformed(ActionEvent ev) {
149 ilm 268
                        try {
269
                            createAbstractSheet(IListe.get(ev).fetchSelectedRow()).openDocument(false);
270
                        } catch (Exception e) {
271
                            ExceptionHandler.handle("Impossible d'ouvrir le fichier", e);
272
                        }
19 ilm 273
                    }
274
 
67 ilm 275
                }, this.previewHeader, "document.modify") {
142 ilm 276
 
19 ilm 277
                    @Override
21 ilm 278
                    public boolean enabledFor(IListeEvent evt) {
149 ilm 279
                        // On ne teste pas l'existence du fichier génété car les IOs peuvent prendre
280
                        // du temps
281
                        return evt.getSelectedRow() != null && (evt.getTotalRowCount() >= 1);
19 ilm 282
                    }
65 ilm 283
 
28 ilm 284
                };
285
                l.add(action);
19 ilm 286
 
287
            }
288
        } else {
289
            if (this.previewIsVisible) {
67 ilm 290
                l.add(new RowAction(new AbstractAction() {
19 ilm 291
                    public void actionPerformed(ActionEvent ev) {
25 ilm 292
                        try {
80 ilm 293
                            createAbstractSheet(IListe.get(ev).fetchSelectedRow()).showPreviewDocument();
25 ilm 294
                        } catch (Exception e) {
149 ilm 295
                            ExceptionHandler.handle("Impossible d'ouvrir le fichier", e);
25 ilm 296
                        }
19 ilm 297
                    }
298
 
67 ilm 299
                }, this.previewHeader, "document.preview") {
25 ilm 300
 
19 ilm 301
                    @Override
25 ilm 302
                    public boolean enabledFor(IListeEvent evt) {
149 ilm 303
                        // On ne teste pas l'existence du fichier génété car les IOs peuvent prendre
304
                        // du temps
305
                        return evt.getSelectedRow() != null && evt.getTotalRowCount() >= 1;
19 ilm 306
                    }
307
                });
25 ilm 308
 
19 ilm 309
            }
310
        }
311
 
312
        // action supplémentaire
25 ilm 313
        List<RowAction> list = addToMenu();
19 ilm 314
        if (list != null) {
25 ilm 315
            for (RowAction rowAction : list) {
316
                l.add(rowAction);
19 ilm 317
            }
318
        }
319
 
149 ilm 320
        if (Boolean.getBoolean("org.openconcerto.oo.useODSViewer") && this.showIsVisible) {
321
            l.add(new RowAction(new AbstractAction() {
322
                public void actionPerformed(ActionEvent ev) {
323
                    try {
80 ilm 324
                        createAbstractSheet(IListe.get(ev).fetchSelectedRow()).openDocument(false);
149 ilm 325
                    } catch (Exception e) {
326
                        ExceptionHandler.handle("Impossible d'ouvrir le fichier", e);
19 ilm 327
                    }
149 ilm 328
                }
329
            }, this.showHeader, "document.modify") {
142 ilm 330
 
149 ilm 331
                @Override
332
                public boolean enabledFor(IListeEvent evt) {
333
                    // On ne teste pas l'existence du fichier génété car les IOs peuvent prendre
334
                    // du temps
335
                    return evt.getSelectedRow() != null && evt.getTotalRowCount() >= 1;
336
                }
337
            });
338
 
19 ilm 339
        }
340
 
341
        if (this.printIsVisible) {
342
 
132 ilm 343
            // Impression rapide : imprime le ou les documents sélectionnés (les génère si besoin)
344
            // en proposant l'interface Java d'impression
149 ilm 345
            l.add(new RowAction(new PrintDocumentAction(this), false, "document.print") {
65 ilm 346
 
19 ilm 347
                @Override
25 ilm 348
                public boolean enabledFor(IListeEvent evt) {
132 ilm 349
                    return evt.getSelectedRow() != null && evt.getSelectedRows().size() > 0;
19 ilm 350
                }
351
            });
352
 
353
        }
354
 
149 ilm 355
        if (this.showIsVisible)
19 ilm 356
 
149 ilm 357
        {
358
 
144 ilm 359
            l.add(getSendMailPDF());
142 ilm 360
 
144 ilm 361
            l.add(getSendMail());
142 ilm 362
 
19 ilm 363
        }
364
        if (this.generateIsVisible) {
67 ilm 365
            l.add(new RowAction(new AbstractAction() {
19 ilm 366
                public void actionPerformed(ActionEvent ev) {
93 ilm 367
                    List<SQLRowValues> l = IListe.get(ev).getSelectedRows();
90 ilm 368
 
369
                    if (l.size() == 1) {
370
                        createDocument(ev);
371
                    } else {
372
                        createDocuments(l);
373
                    }
19 ilm 374
                }
67 ilm 375
            }, this.generateHeader, "document.create") {
73 ilm 376
 
19 ilm 377
                @Override
93 ilm 378
                public boolean enabledFor(List<SQLRowValues> selection) {
156 ilm 379
                    return selection != null && !selection.isEmpty();
19 ilm 380
                }
65 ilm 381
 
19 ilm 382
            });
383
        }
384
 
385
        return l;
386
    }
65 ilm 387
 
144 ilm 388
    public RowAction getSendMail() {
389
        return new RowAction(new AbstractAction() {
390
            public void actionPerformed(ActionEvent ev) {
391
                final List<SQLRowValues> selectedRows = IListe.get(ev).getSelectedRows();
392
                final SQLTable table = IListe.get(ev).getSource().getPrimaryTable();
393
                final List<SQLRow> rows = new ArrayList<SQLRow>();
394
                for (SQLRowValues r : selectedRows) {
395
                    rows.add(table.getRow(r.getID()));
396
                }
397
                sendMail(createAbstractSheets(rows), false);
398
            }
399
        }, false, "document.send.email") {
400
 
401
            @Override
402
            public boolean enabledFor(IListeEvent evt) {
149 ilm 403
                // On ne teste pas l'existence du fichier génété car les IOs peuvent prendre du
404
                // temps
405
                return evt.getSelectedRow() != null && evt.getTotalRowCount() >= 1;
144 ilm 406
            }
149 ilm 407
 
144 ilm 408
        };
409
    }
410
 
411
    public RowAction getSendMailPDF() {
412
        return new RowAction(new AbstractAction() {
413
            public void actionPerformed(ActionEvent ev) {
149 ilm 414
                try {
415
                    final List<SQLRowValues> selectedRows = IListe.get(ev).getSelectedRows();
416
                    final SQLTable table = IListe.get(ev).getSource().getPrimaryTable();
417
                    final List<SQLRow> rows = new ArrayList<SQLRow>();
418
                    for (SQLRowValues r : selectedRows) {
419
                        rows.add(table.getRow(r.getID()));
420
                    }
421
                    sendMail(createAbstractSheets(rows), true);
422
                } catch (Exception e) {
423
                    ExceptionHandler.handle("Impossible d'envoyer le(s) fichier(s)", e);
144 ilm 424
                }
425
            }
426
        }, false, "document.pdf.send.email") {
427
 
428
            @Override
429
            public boolean enabledFor(IListeEvent evt) {
149 ilm 430
                // On ne teste pas l'existence du fichier génété car les IOs peuvent prendre du
431
                // temps
432
                return evt.getSelectedRow() != null && evt.getTotalRowCount() >= 1;
144 ilm 433
            }
149 ilm 434
 
144 ilm 435
        };
436
 
437
    }
438
 
93 ilm 439
    private void createDocuments(List<? extends SQLRowAccessor> selection) {
90 ilm 440
        int a = JOptionPane.showConfirmDialog(null, "Voulez vous recréer l'ensemble des documents sélectionnés?", "Génération de documents", JOptionPane.YES_NO_OPTION);
441
        if (a == JOptionPane.YES_OPTION) {
442
            for (SQLRowAccessor sqlRowAccessor : selection) {
443
                final AbstractSheetXml sheet = createAbstractSheet(sqlRowAccessor.getTable().getRow(sqlRowAccessor.getID()));
444
                sheet.createDocumentAsynchronous();
445
                sheet.showPrintAndExportAsynchronous(false, false, true);
446
            }
447
        }
448
    }
449
 
73 ilm 450
    private void createDocument(ActionEvent ev) {
80 ilm 451
        final AbstractSheetXml sheet = createAbstractSheet(IListe.get(ev).fetchSelectedRow());
73 ilm 452
        if (sheet.getGeneratedFile().exists()) {
453
            int a = JOptionPane.showConfirmDialog(null, "Voulez vous remplacer le document existant?", "Génération de documents", JOptionPane.YES_NO_OPTION);
454
            if (a == JOptionPane.YES_OPTION) {
455
                sheet.createDocumentAsynchronous();
456
                sheet.showPrintAndExportAsynchronous(true, false, true);
457
                return;
458
            }
459
        }
460
 
461
        try {
462
            sheet.getOrCreateDocumentFile();
463
            sheet.showPrintAndExportAsynchronous(true, false, false);
464
        } catch (Exception exn) {
465
            exn.printStackTrace();
466
        }
467
 
468
    }
469
 
65 ilm 470
    /**
471
     * Action sur le double clic
472
     *
473
     * @return
474
     */
475
    public RowAction getDefaultRowAction() {
67 ilm 476
        return new RowAction(new AbstractAction() {
65 ilm 477
            public void actionPerformed(ActionEvent ev) {
80 ilm 478
                final AbstractSheetXml sheet = createAbstractSheet(IListe.get(ev).fetchSelectedRow().asRow());
65 ilm 479
                try {
480
                    sheet.getOrCreateDocumentFile();
481
                    sheet.showPrintAndExportAsynchronous(true, false, true);
482
                } catch (Exception exn) {
483
                    ExceptionHandler.handle("Une erreur est survenue lors de la création du document.", exn);
484
                }
485
            }
67 ilm 486
        }, false, false, "document.create") {
142 ilm 487
 
65 ilm 488
            @Override
93 ilm 489
            public boolean enabledFor(List<SQLRowValues> selection) {
65 ilm 490
                return selection != null && selection.size() == 1;
491
            }
492
 
493
            @Override
494
            public Action getDefaultAction(final IListeEvent evt) {
495
                return this.getAction();
496
            }
497
        };
498
    }
18 ilm 499
}