OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 156 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 156 Rev 174
Line 12... Line 12...
12
 */
12
 */
13
 
13
 
14
 package org.openconcerto.erp.model;
14
 package org.openconcerto.erp.model;
15
 
15
 
16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
-
 
17
import org.openconcerto.erp.core.customerrelationship.mail.EmailTemplate;
-
 
18
import org.openconcerto.erp.core.customerrelationship.mail.ValueListener;
17
import org.openconcerto.erp.generationDoc.AbstractSheetXml;
19
import org.openconcerto.erp.generationDoc.AbstractSheetXml;
18
import org.openconcerto.sql.Configuration;
20
import org.openconcerto.sql.Configuration;
19
import org.openconcerto.sql.model.SQLBase;
21
import org.openconcerto.sql.model.SQLBase;
20
import org.openconcerto.sql.model.SQLField;
22
import org.openconcerto.sql.model.SQLField;
21
import org.openconcerto.sql.model.SQLRow;
23
import org.openconcerto.sql.model.SQLRow;
Line 25... Line 27...
25
import org.openconcerto.sql.view.list.IListe;
27
import org.openconcerto.sql.view.list.IListe;
26
import org.openconcerto.sql.view.list.RowAction;
28
import org.openconcerto.sql.view.list.RowAction;
27
import org.openconcerto.ui.EmailComposer;
29
import org.openconcerto.ui.EmailComposer;
28
import org.openconcerto.utils.ExceptionHandler;
30
import org.openconcerto.utils.ExceptionHandler;
29
import org.openconcerto.utils.ListMap;
31
import org.openconcerto.utils.ListMap;
-
 
32
import org.openconcerto.utils.i18n.TranslationManager;
30
 
33
 
31
import java.awt.event.ActionEvent;
34
import java.awt.event.ActionEvent;
32
import java.io.File;
35
import java.io.File;
33
import java.lang.reflect.Constructor;
36
import java.lang.reflect.Constructor;
34
import java.util.ArrayList;
37
import java.util.ArrayList;
Line 110... Line 113...
110
 
113
 
111
    public void setShowHeader(boolean showHeader) {
114
    public void setShowHeader(boolean showHeader) {
112
        this.showHeader = showHeader;
115
        this.showHeader = showHeader;
113
    }
116
    }
114
 
117
 
115
    protected void sendMail(final AbstractSheetXml sheet, final boolean readOnly) {
118
    protected void sendMail(EmailTemplate template, final AbstractSheetXml sheet, final boolean readOnly) {
116
        List<AbstractSheetXml> l = new ArrayList<>(1);
119
        List<AbstractSheetXml> l = new ArrayList<>(1);
117
        l.add(sheet);
120
        l.add(sheet);
118
        sendMail(l, readOnly);
121
        sendMail(template, l, readOnly);
119
    }
122
    }
120
 
123
 
121
    protected void sendMail(final List<AbstractSheetXml> sheets, final boolean readOnly) {
124
    protected void sendMail(EmailTemplate template, final List<AbstractSheetXml> sheets, final boolean readOnly) {
122
        final Thread t = new Thread() {
125
        final Thread t = new Thread() {
123
            @Override
126
            @Override
124
            public void run() {
127
            public void run() {
125
                ListMap<String, File> mailFilesMap = new ListMap<>();
128
                ListMap<String, File> mailFilesMap = new ListMap<>();
126
                for (AbstractSheetXml sheet : sheets) {
129
                for (AbstractSheetXml sheet : sheets) {
Line 229... Line 232...
229
 
232
 
230
                        @Override
233
                        @Override
231
                        public void run() {
234
                        public void run() {
232
                            try {
235
                            try {
233
                                String subject = sheets.get(0).getReference();
236
                                String subject = sheets.get(0).getReference();
-
 
237
                                if (template != null) {
-
 
238
                                    subject = template.getTitle() + " " + sheets.get(0).getReference().trim();
-
 
239
                                }
-
 
240
                                subject = subject.trim();
234
                                if (subject.isEmpty()) {
241
                                if (subject.isEmpty()) {
235
                                    final StringJoiner joiner = new StringJoiner(", ");
242
                                    final StringJoiner joiner = new StringJoiner(", ");
236
                                    for (File f : files) {
243
                                    for (File f : files) {
237
                                        joiner.add(f.getName());
244
                                        joiner.add(f.getName());
238
                                    }
245
                                    }
239
                                    subject = joiner.toString();
246
                                    subject = joiner.toString();
240
                                }
247
                                }
241
                                final String message = getMailObject(sheets.get(0).getSQLRow());
248
                                String message = getMailObject(sheets.get(0).getSQLRow());
-
 
249
                                if (template != null) {
-
 
250
                                    if (template.getText().contains("{message}")) {
-
 
251
                                        message = template.getText().replace("{message}", message);
-
 
252
                                    } else {
-
 
253
                                        message += template.getText();
-
 
254
                                    }
-
 
255
                                }
242
                                EmailComposer.getInstance().compose(mailDest, subject, message, files.toArray(new File[files.size()]));
256
                                EmailComposer.getInstance().compose(mailDest, subject, message, files.toArray(new File[files.size()]));
243
                            } catch (Exception e) {
257
                            } catch (Exception e) {
244
                                ExceptionHandler.handle("Impossible d'envoyer le courriel!", e);
258
                                ExceptionHandler.handle("Impossible d'envoyer le courriel!", e);
245
                            }
259
                            }
246
                        }
260
                        }
Line 284... Line 298...
284
                };
298
                };
285
                l.add(action);
299
                l.add(action);
286
 
300
 
287
            }
301
            }
288
        } else {
302
        } else {
-
 
303
            // ODS Viewer
289
            if (this.previewIsVisible) {
304
            if (this.previewIsVisible) {
290
                l.add(new RowAction(new AbstractAction() {
305
                l.add(new RowAction(new AbstractAction() {
291
                    public void actionPerformed(ActionEvent ev) {
306
                    public void actionPerformed(ActionEvent ev) {
292
                        try {
307
                        try {
-
 
308
                            final AbstractSheetXml sheet = createAbstractSheet(IListe.get(ev).fetchSelectedRow());
293
                            createAbstractSheet(IListe.get(ev).fetchSelectedRow()).showPreviewDocument();
309
                            final SQLTable table = IListe.get(ev).getSource().getPrimaryTable();
-
 
310
                            sheet.showPreviewDocument(TranslationManager.getInstance().getTranslationForAction("email"), new Runnable() {
-
 
311
 
-
 
312
                                @Override
-
 
313
                                public void run() {
-
 
314
                                    EmailTemplate.askTemplate(IListe.get(ev), table.getDBRoot(), new ValueListener() {
-
 
315
 
-
 
316
                                        @Override
-
 
317
                                        public void valueSelected(Object value) {
-
 
318
                                            sendMail((EmailTemplate) value, sheet, true);
-
 
319
                                        }
-
 
320
                                    });
-
 
321
 
-
 
322
                                }
-
 
323
                            });
294
                        } catch (Exception e) {
324
                        } catch (Exception e) {
295
                            ExceptionHandler.handle("Impossible d'ouvrir le fichier", e);
325
                            ExceptionHandler.handle("Impossible d'ouvrir le fichier", e);
296
                        }
326
                        }
297
                    }
327
                    }
298
 
328
 
Line 386... Line 416...
386
    }
416
    }
387
 
417
 
388
    public RowAction getSendMail() {
418
    public RowAction getSendMail() {
389
        return new RowAction(new AbstractAction() {
419
        return new RowAction(new AbstractAction() {
390
            public void actionPerformed(ActionEvent ev) {
420
            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);
421
                sendMail(ev, false);
398
            }
422
            }
399
        }, false, "document.send.email") {
423
        }, false, "document.send.email") {
400
 
424
 
401
            @Override
425
            @Override
402
            public boolean enabledFor(IListeEvent evt) {
426
            public boolean enabledFor(IListeEvent evt) {
Line 409... Line 433...
409
    }
433
    }
410
 
434
 
411
    public RowAction getSendMailPDF() {
435
    public RowAction getSendMailPDF() {
412
        return new RowAction(new AbstractAction() {
436
        return new RowAction(new AbstractAction() {
413
            public void actionPerformed(ActionEvent ev) {
437
            public void actionPerformed(ActionEvent ev) {
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) {
438
                sendMail(ev, true);
423
                    ExceptionHandler.handle("Impossible d'envoyer le(s) fichier(s)", e);
-
 
424
                }
-
 
425
            }
439
            }
426
        }, false, "document.pdf.send.email") {
440
        }, false, "document.pdf.send.email") {
427
 
441
 
428
            @Override
442
            @Override
429
            public boolean enabledFor(IListeEvent evt) {
443
            public boolean enabledFor(IListeEvent evt) {
Line 494... Line 508...
494
            public Action getDefaultAction(final IListeEvent evt) {
508
            public Action getDefaultAction(final IListeEvent evt) {
495
                return this.getAction();
509
                return this.getAction();
496
            }
510
            }
497
        };
511
        };
498
    }
512
    }
-
 
513
 
-
 
514
    private void sendMail(ActionEvent ev, boolean pdf) {
-
 
515
        final List<SQLRowValues> selectedRows = IListe.get(ev).getSelectedRows();
-
 
516
        final SQLTable table = IListe.get(ev).getSource().getPrimaryTable();
-
 
517
        final List<SQLRow> rows = new ArrayList<>();
-
 
518
        for (SQLRowValues r : selectedRows) {
-
 
519
            rows.add(table.getRow(r.getID()));
-
 
520
        }
-
 
521
 
-
 
522
        EmailTemplate.askTemplate(IListe.get(ev), table.getDBRoot(), new ValueListener() {
-
 
523
 
-
 
524
            @Override
-
 
525
            public void valueSelected(Object value) {
-
 
526
                sendMail((EmailTemplate) value, createAbstractSheets(rows), pdf);
-
 
527
            }
-
 
528
        });
-
 
529
    }
499
}
530
}