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 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 149 Rev 156
Line 24... Line 24...
24
import org.openconcerto.sql.model.SQLTable;
24
import org.openconcerto.sql.model.SQLTable;
25
import org.openconcerto.sql.view.list.IListe;
25
import org.openconcerto.sql.view.list.IListe;
26
import org.openconcerto.sql.view.list.RowAction;
26
import org.openconcerto.sql.view.list.RowAction;
27
import org.openconcerto.ui.EmailComposer;
27
import org.openconcerto.ui.EmailComposer;
28
import org.openconcerto.utils.ExceptionHandler;
28
import org.openconcerto.utils.ExceptionHandler;
-
 
29
import org.openconcerto.utils.ListMap;
29
 
30
 
30
import java.awt.event.ActionEvent;
31
import java.awt.event.ActionEvent;
31
import java.io.File;
32
import java.io.File;
32
import java.lang.reflect.Constructor;
33
import java.lang.reflect.Constructor;
33
import java.util.ArrayList;
34
import java.util.ArrayList;
34
import java.util.List;
35
import java.util.List;
35
import java.util.Set;
36
import java.util.Set;
-
 
37
import java.util.StringJoiner;
36
 
38
 
37
import javax.swing.AbstractAction;
39
import javax.swing.AbstractAction;
38
import javax.swing.Action;
40
import javax.swing.Action;
39
import javax.swing.JOptionPane;
41
import javax.swing.JOptionPane;
40
import javax.swing.SwingUtilities;
42
import javax.swing.SwingUtilities;
Line 109... Line 111...
109
    public void setShowHeader(boolean showHeader) {
111
    public void setShowHeader(boolean showHeader) {
110
        this.showHeader = showHeader;
112
        this.showHeader = showHeader;
111
    }
113
    }
112
 
114
 
113
    protected void sendMail(final AbstractSheetXml sheet, final boolean readOnly) {
115
    protected void sendMail(final AbstractSheetXml sheet, final boolean readOnly) {
114
        List<AbstractSheetXml> l = new ArrayList<AbstractSheetXml>(1);
116
        List<AbstractSheetXml> l = new ArrayList<>(1);
115
        l.add(sheet);
117
        l.add(sheet);
116
        sendMail(l, readOnly);
118
        sendMail(l, readOnly);
117
    }
119
    }
118
 
120
 
119
    protected void sendMail(final List<AbstractSheetXml> sheets, final boolean readOnly) {
121
    protected void sendMail(final List<AbstractSheetXml> sheets, final boolean readOnly) {
-
 
122
        final Thread t = new Thread() {
120
        String mail = "";
123
            @Override
121
 
-
 
-
 
124
            public void run() {
-
 
125
                ListMap<String, File> mailFilesMap = new ListMap<>();
122
        for (AbstractSheetXml sheet : sheets) {
126
                for (AbstractSheetXml sheet : sheets) {
-
 
127
                    String mail = "";
123
            final SQLRow row = sheet.getSQLRow();
128
                    final SQLRow row = sheet.getSQLRow();
124
            Set<SQLField> setContact = null;
129
                    Set<SQLField> setContact = null;
125
            SQLTable tableContact = Configuration.getInstance().getRoot().findTable("CONTACT");
130
                    SQLTable tableContact = Configuration.getInstance().getRoot().findTable("CONTACT");
126
            setContact = row.getTable().getForeignKeys(tableContact);
131
                    setContact = row.getTable().getForeignKeys(tableContact);
127
 
132
 
Line 132... Line 137...
132
            for (SQLField field : setContact) {
137
                    for (SQLField field : setContact) {
133
                if (mail == null || mail.trim().length() == 0) {
138
                        if (mail == null || mail.trim().length() == 0) {
134
                    mail = row.getForeignRow(field.getName()).getString("EMAIL");
139
                            mail = row.getForeignRow(field.getName()).getString("EMAIL");
135
                }
140
                        }
136
            }
141
                    }
137
 
-
 
-
 
142
                    String nomClient = "";
138
            if (setClient != null && (mail == null || mail.trim().length() == 0)) {
143
                    if (setClient != null && (mail == null || mail.trim().length() == 0)) {
139
                    for (SQLField field : setClient) {
144
                            for (SQLField field : setClient) {
140
                        SQLRow rowCli = row.getForeignRow(field.getName());
145
                                SQLRow rowCli = row.getForeignRow(field.getName());
141
                        if (mail == null || mail.trim().length() == 0) {
146
                                if (mail == null || mail.trim().length() == 0) {
142
                            mail = rowCli.getString("MAIL");
147
                                    mail = rowCli.getString("MAIL");
143
                        }
148
                                }
-
 
149
                                nomClient = rowCli.getString("NOM");
144
                    }
150
                            }
145
            }
151
                    }
146
 
152
 
-
 
153
                    if (mail.trim().length() == 0) {
-
 
154
                        mail = nomClient;
-
 
155
                    }
-
 
156
 
-
 
157
                    String table = row.getTable().getName();
-
 
158
                    if (table.equalsIgnoreCase("COMMANDE") || table.equalsIgnoreCase("DEMANDE_PRIX") || table.equalsIgnoreCase("FACTURE_FOURNISSEUR")) {
-
 
159
                        mail = "";
-
 
160
                    }
147
            if (mail == null || mail.trim().length() == 0) {
161
                    if (mail == null || mail.trim().length() == 0) {
148
                SQLTable tableF = ((ComptaPropsConfiguration) Configuration.getInstance()).getSQLBaseSociete().getTable("FOURNISSEUR");
162
                        SQLTable tableF = ((ComptaPropsConfiguration) Configuration.getInstance()).getSQLBaseSociete().getTable("FOURNISSEUR");
149
                Set<SQLField> setF = null;
163
                        Set<SQLField> setF = null;
150
                setF = row.getTable().getForeignKeys(tableF);
164
                        setF = row.getTable().getForeignKeys(tableF);
151
 
165
 
Line 193... Line 207...
193
                            }
207
                                    }
194
                        }
208
                                }
195
                    }
209
                            }
196
                }
210
                        }
197
            }
211
                    }
-
 
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
                    }
198
        }
222
                }
199
        final String adresseMail = mail;
-
 
200
 
223
 
201
        final String subject = sheets.get(0).getReference();
224
                for (final String mailDest : mailFilesMap.keySet()) {
202
 
225
 
203
        if (readOnly) {
-
 
204
            final Thread t = new Thread() {
-
 
205
                @Override
-
 
206
                public void run() {
-
 
207
                    final List<File> files = new ArrayList<File>();
226
                    final List<File> files = mailFilesMap.get(mailDest);
208
                    try {
-
 
209
                        for (AbstractSheetXml sheet : sheets) {
-
 
210
                            files.add(sheet.getOrCreatePDFDocumentFile(true).getAbsoluteFile());
-
 
211
                        }
-
 
212
 
227
 
213
                        SwingUtilities.invokeLater(new Runnable() {
228
                    SwingUtilities.invokeLater(new Runnable() {
214
 
229
 
215
                            @Override
230
                        @Override
216
                            public void run() {
231
                        public void run() {
217
                                try {
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();
-
 
240
                                }
218
                                    EmailComposer.getInstance().compose(adresseMail, subject + (subject.trim().length() == 0 ? "" : ", ") + files.get(0).getName(),
241
                                final String message = getMailObject(sheets.get(0).getSQLRow());
219
                                            getMailObject(sheets.get(0).getSQLRow()), files.toArray(new File[files.size()]));
242
                                EmailComposer.getInstance().compose(mailDest, subject, message, files.toArray(new File[files.size()]));
220
                                } catch (Exception e) {
243
                            } catch (Exception e) {
221
                                    ExceptionHandler.handle("Impossible de charger le document PDF dans l'email!", e);
244
                                ExceptionHandler.handle("Impossible d'envoyer le courriel!", e);
222
                                }
245
                            }
223
                            }
246
                        }
224
                        });
247
                    });
225
                    } catch (Exception e) {
-
 
226
                        ExceptionHandler.handle("Impossible de charger le document PDF", e);
-
 
227
                    }
248
                }
-
 
249
 
228
                }
250
            }
-
 
251
 
229
            };
252
        };
230
            t.start();
253
        t.start();
231
        } else {
-
 
232
            try {
-
 
233
                final List<File> files = new ArrayList<File>();
-
 
234
                for (AbstractSheetXml sheet : sheets) {
-
 
235
                    files.add(sheet.getGeneratedFile().getAbsoluteFile());
-
 
236
                }
-
 
237
                EmailComposer.getInstance().compose(adresseMail, subject + (subject.trim().length() == 0 ? "" : ", ") + sheets.get(0).getGeneratedFile().getName(),
-
 
238
                        getMailObject(sheets.get(0).getSQLRow()), files.toArray(new File[files.size()]));
-
 
239
            } catch (Exception exn) {
-
 
240
                ExceptionHandler.handle(null, "Impossible de créer le courriel", exn);
-
 
241
            }
-
 
242
        }
-
 
243
 
254
 
244
    }
255
    }
245
 
256
 
246
    public List<RowAction> addToMenu() {
257
    public List<RowAction> addToMenu() {
247
        return null;
258
        return null;
Line 363... Line 374...
363
                }
374
                }
364
            }, this.generateHeader, "document.create") {
375
            }, this.generateHeader, "document.create") {
365
 
376
 
366
                @Override
377
                @Override
367
                public boolean enabledFor(List<SQLRowValues> selection) {
378
                public boolean enabledFor(List<SQLRowValues> selection) {
368
                    return selection != null && selection.size() > 0;
379
                    return selection != null && !selection.isEmpty();
369
                }
380
                }
370
 
381
 
371
            });
382
            });
372
        }
383
        }
373
 
384