18 |
ilm |
1 |
/*
|
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
|
|
3 |
*
|
182 |
ilm |
4 |
* Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
|
18 |
ilm |
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;
|
182 |
ilm |
17 |
import org.openconcerto.erp.core.common.ui.PreviewFrame;
|
174 |
ilm |
18 |
import org.openconcerto.erp.core.customerrelationship.mail.EmailTemplate;
|
|
|
19 |
import org.openconcerto.erp.core.customerrelationship.mail.ValueListener;
|
18 |
ilm |
20 |
import org.openconcerto.erp.generationDoc.AbstractSheetXml;
|
|
|
21 |
import org.openconcerto.sql.Configuration;
|
182 |
ilm |
22 |
import org.openconcerto.sql.element.SQLComponent;
|
|
|
23 |
import org.openconcerto.sql.element.SQLElement;
|
41 |
ilm |
24 |
import org.openconcerto.sql.model.SQLBase;
|
18 |
ilm |
25 |
import org.openconcerto.sql.model.SQLField;
|
|
|
26 |
import org.openconcerto.sql.model.SQLRow;
|
19 |
ilm |
27 |
import org.openconcerto.sql.model.SQLRowAccessor;
|
18 |
ilm |
28 |
import org.openconcerto.sql.model.SQLTable;
|
182 |
ilm |
29 |
import org.openconcerto.sql.view.EditFrame;
|
|
|
30 |
import org.openconcerto.sql.view.EditPanel.EditMode;
|
18 |
ilm |
31 |
import org.openconcerto.sql.view.list.IListe;
|
19 |
ilm |
32 |
import org.openconcerto.sql.view.list.RowAction;
|
182 |
ilm |
33 |
import org.openconcerto.sql.view.list.action.ListEvent;
|
18 |
ilm |
34 |
import org.openconcerto.ui.EmailComposer;
|
182 |
ilm |
35 |
import org.openconcerto.ui.FrameUtil;
|
|
|
36 |
import org.openconcerto.utils.Action;
|
18 |
ilm |
37 |
import org.openconcerto.utils.ExceptionHandler;
|
156 |
ilm |
38 |
import org.openconcerto.utils.ListMap;
|
182 |
ilm |
39 |
import org.openconcerto.utils.i18n.Grammar;
|
174 |
ilm |
40 |
import org.openconcerto.utils.i18n.TranslationManager;
|
18 |
ilm |
41 |
|
|
|
42 |
import java.awt.event.ActionEvent;
|
|
|
43 |
import java.io.File;
|
182 |
ilm |
44 |
import java.io.FileInputStream;
|
|
|
45 |
import java.io.FileOutputStream;
|
|
|
46 |
import java.io.IOException;
|
|
|
47 |
import java.io.InputStream;
|
|
|
48 |
import java.io.OutputStream;
|
18 |
ilm |
49 |
import java.lang.reflect.Constructor;
|
|
|
50 |
import java.util.ArrayList;
|
182 |
ilm |
51 |
import java.util.Arrays;
|
|
|
52 |
import java.util.Collections;
|
18 |
ilm |
53 |
import java.util.List;
|
|
|
54 |
import java.util.Set;
|
156 |
ilm |
55 |
import java.util.StringJoiner;
|
18 |
ilm |
56 |
|
|
|
57 |
import javax.swing.AbstractAction;
|
182 |
ilm |
58 |
import javax.swing.JFileChooser;
|
73 |
ilm |
59 |
import javax.swing.JOptionPane;
|
41 |
ilm |
60 |
import javax.swing.SwingUtilities;
|
18 |
ilm |
61 |
|
25 |
ilm |
62 |
public class MouseSheetXmlListeListener {
|
18 |
ilm |
63 |
|
|
|
64 |
private Class<? extends AbstractSheetXml> clazz;
|
|
|
65 |
protected IListe liste;
|
|
|
66 |
|
|
|
67 |
private boolean previewIsVisible = true;
|
|
|
68 |
private boolean showIsVisible = true;
|
|
|
69 |
private boolean printIsVisible = true;
|
|
|
70 |
private boolean generateIsVisible = true;
|
28 |
ilm |
71 |
private boolean previewHeader = false;
|
|
|
72 |
private boolean showHeader = false;
|
|
|
73 |
private boolean generateHeader = false;
|
182 |
ilm |
74 |
private SQLElement element;
|
18 |
ilm |
75 |
|
182 |
ilm |
76 |
public MouseSheetXmlListeListener(SQLElement element, Class<? extends AbstractSheetXml> clazz) {
|
|
|
77 |
this(element, clazz, true, true, true, true);
|
25 |
ilm |
78 |
|
18 |
ilm |
79 |
}
|
|
|
80 |
|
182 |
ilm |
81 |
public MouseSheetXmlListeListener(SQLElement element, Class<? extends AbstractSheetXml> clazz, boolean show, boolean preview, boolean print, boolean generate) {
|
|
|
82 |
this.element = element;
|
18 |
ilm |
83 |
this.clazz = clazz;
|
|
|
84 |
this.printIsVisible = print;
|
|
|
85 |
this.previewIsVisible = preview;
|
|
|
86 |
this.showIsVisible = show;
|
|
|
87 |
this.generateIsVisible = generate;
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
protected Class<? extends AbstractSheetXml> getSheetClass() {
|
|
|
91 |
return this.clazz;
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
protected AbstractSheetXml createAbstractSheet(SQLRow row) {
|
41 |
ilm |
95 |
try {
|
|
|
96 |
Constructor<? extends AbstractSheetXml> ctor = getSheetClass().getConstructor(SQLRow.class);
|
|
|
97 |
AbstractSheetXml sheet = ctor.newInstance(row);
|
|
|
98 |
return sheet;
|
|
|
99 |
} catch (Exception e) {
|
73 |
ilm |
100 |
ExceptionHandler.handle("sheet creation error", e);
|
18 |
ilm |
101 |
}
|
41 |
ilm |
102 |
return null;
|
18 |
ilm |
103 |
}
|
|
|
104 |
|
142 |
ilm |
105 |
public List<AbstractSheetXml> createAbstractSheets(List<SQLRow> rows) {
|
149 |
ilm |
106 |
final List<AbstractSheetXml> sheets = new ArrayList<>(rows.size());
|
142 |
ilm |
107 |
try {
|
|
|
108 |
final Constructor<? extends AbstractSheetXml> ctor = getSheetClass().getConstructor(SQLRow.class);
|
|
|
109 |
for (SQLRow row : rows) {
|
|
|
110 |
AbstractSheetXml sheet = ctor.newInstance(row);
|
|
|
111 |
sheets.add(sheet);
|
|
|
112 |
}
|
|
|
113 |
} catch (Exception e) {
|
|
|
114 |
ExceptionHandler.handle("sheet creation error", e);
|
|
|
115 |
}
|
|
|
116 |
return sheets;
|
|
|
117 |
}
|
|
|
118 |
|
41 |
ilm |
119 |
protected String getMailObject(SQLRow row) {
|
|
|
120 |
return "";
|
|
|
121 |
}
|
|
|
122 |
|
28 |
ilm |
123 |
public void setPreviewHeader(boolean previewHeader) {
|
|
|
124 |
this.previewHeader = previewHeader;
|
|
|
125 |
}
|
18 |
ilm |
126 |
|
28 |
ilm |
127 |
public void setGenerateHeader(boolean generateHeader) {
|
|
|
128 |
this.generateHeader = generateHeader;
|
|
|
129 |
}
|
18 |
ilm |
130 |
|
28 |
ilm |
131 |
public void setShowHeader(boolean showHeader) {
|
|
|
132 |
this.showHeader = showHeader;
|
|
|
133 |
}
|
|
|
134 |
|
182 |
ilm |
135 |
public void sendMail(EmailTemplate template, final AbstractSheetXml sheet, final boolean readOnly) {
|
156 |
ilm |
136 |
List<AbstractSheetXml> l = new ArrayList<>(1);
|
142 |
ilm |
137 |
l.add(sheet);
|
174 |
ilm |
138 |
sendMail(template, l, readOnly);
|
142 |
ilm |
139 |
}
|
28 |
ilm |
140 |
|
174 |
ilm |
141 |
protected void sendMail(EmailTemplate template, final List<AbstractSheetXml> sheets, final boolean readOnly) {
|
156 |
ilm |
142 |
final Thread t = new Thread() {
|
|
|
143 |
@Override
|
|
|
144 |
public void run() {
|
|
|
145 |
ListMap<String, File> mailFilesMap = new ListMap<>();
|
|
|
146 |
for (AbstractSheetXml sheet : sheets) {
|
|
|
147 |
String mail = "";
|
|
|
148 |
final SQLRow row = sheet.getSQLRow();
|
|
|
149 |
Set<SQLField> setContact = null;
|
|
|
150 |
SQLTable tableContact = Configuration.getInstance().getRoot().findTable("CONTACT");
|
|
|
151 |
setContact = row.getTable().getForeignKeys(tableContact);
|
18 |
ilm |
152 |
|
156 |
ilm |
153 |
Set<SQLField> setClient = null;
|
|
|
154 |
SQLTable tableClient = ((ComptaPropsConfiguration) Configuration.getInstance()).getSQLBaseSociete().getTable("CLIENT");
|
|
|
155 |
setClient = row.getTable().getForeignKeys(tableClient);
|
18 |
ilm |
156 |
|
156 |
ilm |
157 |
for (SQLField field : setContact) {
|
142 |
ilm |
158 |
if (mail == null || mail.trim().length() == 0) {
|
156 |
ilm |
159 |
mail = row.getForeignRow(field.getName()).getString("EMAIL");
|
142 |
ilm |
160 |
}
|
18 |
ilm |
161 |
}
|
156 |
ilm |
162 |
String nomClient = "";
|
|
|
163 |
if (setClient != null && (mail == null || mail.trim().length() == 0)) {
|
|
|
164 |
for (SQLField field : setClient) {
|
|
|
165 |
SQLRow rowCli = row.getForeignRow(field.getName());
|
|
|
166 |
if (mail == null || mail.trim().length() == 0) {
|
|
|
167 |
mail = rowCli.getString("MAIL");
|
|
|
168 |
}
|
|
|
169 |
nomClient = rowCli.getString("NOM");
|
|
|
170 |
}
|
|
|
171 |
}
|
18 |
ilm |
172 |
|
156 |
ilm |
173 |
if (mail.trim().length() == 0) {
|
|
|
174 |
mail = nomClient;
|
|
|
175 |
}
|
41 |
ilm |
176 |
|
156 |
ilm |
177 |
String table = row.getTable().getName();
|
|
|
178 |
if (table.equalsIgnoreCase("COMMANDE") || table.equalsIgnoreCase("DEMANDE_PRIX") || table.equalsIgnoreCase("FACTURE_FOURNISSEUR")) {
|
|
|
179 |
mail = "";
|
|
|
180 |
}
|
|
|
181 |
if (mail == null || mail.trim().length() == 0) {
|
|
|
182 |
SQLTable tableF = ((ComptaPropsConfiguration) Configuration.getInstance()).getSQLBaseSociete().getTable("FOURNISSEUR");
|
|
|
183 |
Set<SQLField> setF = null;
|
|
|
184 |
setF = row.getTable().getForeignKeys(tableF);
|
41 |
ilm |
185 |
|
156 |
ilm |
186 |
if (setF != null) {
|
|
|
187 |
|
|
|
188 |
for (SQLField field : setF) {
|
|
|
189 |
SQLRow rowF = row.getForeignRow(field.getName());
|
|
|
190 |
if (mail == null || mail.trim().length() == 0) {
|
|
|
191 |
mail = rowF.getString("MAIL");
|
|
|
192 |
}
|
|
|
193 |
}
|
142 |
ilm |
194 |
}
|
41 |
ilm |
195 |
|
156 |
ilm |
196 |
if (mail == null || mail.trim().length() == 0) {
|
|
|
197 |
SQLBase base = ((ComptaPropsConfiguration) Configuration.getInstance()).getSQLBaseSociete();
|
|
|
198 |
if (base.containsTable("MONTEUR")) {
|
|
|
199 |
SQLTable tableM = ((ComptaPropsConfiguration) Configuration.getInstance()).getSQLBaseSociete().getTable("MONTEUR");
|
|
|
200 |
Set<SQLField> setM = null;
|
|
|
201 |
setM = row.getTable().getForeignKeys(tableM);
|
|
|
202 |
if (setM != null) {
|
|
|
203 |
for (SQLField field : setM) {
|
|
|
204 |
SQLRow rowM = row.getForeignRow(field.getName());
|
|
|
205 |
if (rowM.getForeignRow("ID_CONTACT_FOURNISSEUR") != null && !rowM.getForeignRow("ID_CONTACT_FOURNISSEUR").isUndefined()) {
|
|
|
206 |
mail = rowM.getForeignRow("ID_CONTACT_FOURNISSEUR").getString("EMAIL");
|
|
|
207 |
}
|
|
|
208 |
}
|
142 |
ilm |
209 |
}
|
41 |
ilm |
210 |
}
|
|
|
211 |
}
|
156 |
ilm |
212 |
if (mail == null || mail.trim().length() == 0) {
|
|
|
213 |
SQLBase base = ((ComptaPropsConfiguration) Configuration.getInstance()).getSQLBaseSociete();
|
|
|
214 |
if (base.containsTable("TRANSPORTEUR")) {
|
41 |
ilm |
215 |
|
156 |
ilm |
216 |
SQLTable tableM = ((ComptaPropsConfiguration) Configuration.getInstance()).getSQLBaseSociete().getTable("TRANSPORTEUR");
|
|
|
217 |
Set<SQLField> setM = null;
|
|
|
218 |
setM = row.getTable().getForeignKeys(tableM);
|
41 |
ilm |
219 |
|
156 |
ilm |
220 |
if (setM != null) {
|
41 |
ilm |
221 |
|
156 |
ilm |
222 |
for (SQLField field : setM) {
|
|
|
223 |
SQLRow rowM = row.getForeignRow(field.getName());
|
|
|
224 |
if (rowM.getForeignRow("ID_CONTACT_FOURNISSEUR") != null && !rowM.getForeignRow("ID_CONTACT_FOURNISSEUR").isUndefined()) {
|
|
|
225 |
mail = rowM.getForeignRow("ID_CONTACT_FOURNISSEUR").getString("EMAIL");
|
|
|
226 |
}
|
|
|
227 |
}
|
142 |
ilm |
228 |
}
|
41 |
ilm |
229 |
}
|
|
|
230 |
}
|
|
|
231 |
}
|
156 |
ilm |
232 |
try {
|
|
|
233 |
if (readOnly) {
|
|
|
234 |
mailFilesMap.add(mail, sheet.getOrCreatePDFDocumentFile(true).getAbsoluteFile());
|
|
|
235 |
} else {
|
|
|
236 |
mailFilesMap.add(mail, sheet.getOrCreateDocumentFile().getAbsoluteFile());
|
|
|
237 |
|
|
|
238 |
}
|
|
|
239 |
} catch (Exception e) {
|
|
|
240 |
ExceptionHandler.handle("Impossible de charger le document PDF", e);
|
|
|
241 |
}
|
41 |
ilm |
242 |
}
|
18 |
ilm |
243 |
|
156 |
ilm |
244 |
for (final String mailDest : mailFilesMap.keySet()) {
|
19 |
ilm |
245 |
|
156 |
ilm |
246 |
final List<File> files = mailFilesMap.get(mailDest);
|
142 |
ilm |
247 |
|
156 |
ilm |
248 |
SwingUtilities.invokeLater(new Runnable() {
|
41 |
ilm |
249 |
|
156 |
ilm |
250 |
@Override
|
|
|
251 |
public void run() {
|
|
|
252 |
try {
|
|
|
253 |
String subject = sheets.get(0).getReference();
|
174 |
ilm |
254 |
if (template != null) {
|
|
|
255 |
subject = template.getTitle() + " " + sheets.get(0).getReference().trim();
|
|
|
256 |
}
|
|
|
257 |
subject = subject.trim();
|
156 |
ilm |
258 |
if (subject.isEmpty()) {
|
|
|
259 |
final StringJoiner joiner = new StringJoiner(", ");
|
|
|
260 |
for (File f : files) {
|
|
|
261 |
joiner.add(f.getName());
|
|
|
262 |
}
|
|
|
263 |
subject = joiner.toString();
|
41 |
ilm |
264 |
}
|
174 |
ilm |
265 |
String message = getMailObject(sheets.get(0).getSQLRow());
|
|
|
266 |
if (template != null) {
|
|
|
267 |
if (template.getText().contains("{message}")) {
|
|
|
268 |
message = template.getText().replace("{message}", message);
|
|
|
269 |
} else {
|
|
|
270 |
message += template.getText();
|
|
|
271 |
}
|
|
|
272 |
}
|
156 |
ilm |
273 |
EmailComposer.getInstance().compose(mailDest, subject, message, files.toArray(new File[files.size()]));
|
|
|
274 |
} catch (Exception e) {
|
|
|
275 |
ExceptionHandler.handle("Impossible d'envoyer le courriel!", e);
|
41 |
ilm |
276 |
}
|
156 |
ilm |
277 |
}
|
|
|
278 |
});
|
18 |
ilm |
279 |
}
|
156 |
ilm |
280 |
|
18 |
ilm |
281 |
}
|
|
|
282 |
|
156 |
ilm |
283 |
};
|
|
|
284 |
t.start();
|
|
|
285 |
|
18 |
ilm |
286 |
}
|
|
|
287 |
|
25 |
ilm |
288 |
public List<RowAction> addToMenu() {
|
18 |
ilm |
289 |
return null;
|
|
|
290 |
}
|
|
|
291 |
|
19 |
ilm |
292 |
public List<RowAction> getRowActions() {
|
149 |
ilm |
293 |
List<RowAction> l = new ArrayList<>();
|
19 |
ilm |
294 |
|
|
|
295 |
if (!Boolean.getBoolean("org.openconcerto.oo.useODSViewer")) {
|
|
|
296 |
if (this.showIsVisible) {
|
67 |
ilm |
297 |
RowAction action = new RowAction(new AbstractAction() {
|
19 |
ilm |
298 |
public void actionPerformed(ActionEvent ev) {
|
149 |
ilm |
299 |
try {
|
|
|
300 |
createAbstractSheet(IListe.get(ev).fetchSelectedRow()).openDocument(false);
|
|
|
301 |
} catch (Exception e) {
|
|
|
302 |
ExceptionHandler.handle("Impossible d'ouvrir le fichier", e);
|
|
|
303 |
}
|
19 |
ilm |
304 |
}
|
|
|
305 |
|
67 |
ilm |
306 |
}, this.previewHeader, "document.modify") {
|
142 |
ilm |
307 |
|
19 |
ilm |
308 |
@Override
|
182 |
ilm |
309 |
public boolean enabledFor(ListEvent evt) {
|
149 |
ilm |
310 |
// On ne teste pas l'existence du fichier génété car les IOs peuvent prendre
|
|
|
311 |
// du temps
|
|
|
312 |
return evt.getSelectedRow() != null && (evt.getTotalRowCount() >= 1);
|
19 |
ilm |
313 |
}
|
65 |
ilm |
314 |
|
28 |
ilm |
315 |
};
|
|
|
316 |
l.add(action);
|
19 |
ilm |
317 |
|
|
|
318 |
}
|
|
|
319 |
} else {
|
174 |
ilm |
320 |
// ODS Viewer
|
19 |
ilm |
321 |
if (this.previewIsVisible) {
|
67 |
ilm |
322 |
l.add(new RowAction(new AbstractAction() {
|
19 |
ilm |
323 |
public void actionPerformed(ActionEvent ev) {
|
25 |
ilm |
324 |
try {
|
174 |
ilm |
325 |
final AbstractSheetXml sheet = createAbstractSheet(IListe.get(ev).fetchSelectedRow());
|
|
|
326 |
final SQLTable table = IListe.get(ev).getSource().getPrimaryTable();
|
182 |
ilm |
327 |
final Action emailAction = new Action(TranslationManager.getInstance().getTranslationForAction("document.pdf.send.email")) {
|
174 |
ilm |
328 |
|
|
|
329 |
@Override
|
182 |
ilm |
330 |
public void run(Object source) {
|
174 |
ilm |
331 |
EmailTemplate.askTemplate(IListe.get(ev), table.getDBRoot(), new ValueListener() {
|
|
|
332 |
|
|
|
333 |
@Override
|
|
|
334 |
public void valueSelected(Object value) {
|
|
|
335 |
sendMail((EmailTemplate) value, sheet, true);
|
|
|
336 |
}
|
|
|
337 |
});
|
|
|
338 |
|
|
|
339 |
}
|
182 |
ilm |
340 |
};
|
|
|
341 |
final Action modifyAction = new Action(
|
|
|
342 |
TranslationManager.getInstance().getTranslationForAction("modify") + " " + element.getName().getVariant(Grammar.DEFINITE_ARTICLE_SINGULAR)) {
|
|
|
343 |
|
|
|
344 |
@Override
|
|
|
345 |
public void run(Object source) {
|
|
|
346 |
if (source instanceof PreviewFrame) {
|
|
|
347 |
((PreviewFrame) source).dispose();
|
|
|
348 |
}
|
|
|
349 |
final SQLComponent component = element.createDefaultComponent();
|
|
|
350 |
final EditFrame f = new EditFrame(component, EditMode.MODIFICATION);
|
|
|
351 |
f.selectionId(IListe.get(ev).getSelectedId());
|
|
|
352 |
FrameUtil.show(f);
|
|
|
353 |
|
|
|
354 |
}
|
|
|
355 |
};
|
|
|
356 |
sheet.showPreviewDocument(Arrays.asList(emailAction, modifyAction));
|
25 |
ilm |
357 |
} catch (Exception e) {
|
149 |
ilm |
358 |
ExceptionHandler.handle("Impossible d'ouvrir le fichier", e);
|
25 |
ilm |
359 |
}
|
19 |
ilm |
360 |
}
|
|
|
361 |
|
67 |
ilm |
362 |
}, this.previewHeader, "document.preview") {
|
25 |
ilm |
363 |
|
19 |
ilm |
364 |
@Override
|
182 |
ilm |
365 |
public boolean enabledFor(ListEvent evt) {
|
149 |
ilm |
366 |
// On ne teste pas l'existence du fichier génété car les IOs peuvent prendre
|
|
|
367 |
// du temps
|
|
|
368 |
return evt.getSelectedRow() != null && evt.getTotalRowCount() >= 1;
|
19 |
ilm |
369 |
}
|
|
|
370 |
});
|
25 |
ilm |
371 |
|
19 |
ilm |
372 |
}
|
|
|
373 |
}
|
|
|
374 |
|
|
|
375 |
// action supplémentaire
|
25 |
ilm |
376 |
List<RowAction> list = addToMenu();
|
19 |
ilm |
377 |
if (list != null) {
|
25 |
ilm |
378 |
for (RowAction rowAction : list) {
|
|
|
379 |
l.add(rowAction);
|
19 |
ilm |
380 |
}
|
|
|
381 |
}
|
|
|
382 |
|
149 |
ilm |
383 |
if (Boolean.getBoolean("org.openconcerto.oo.useODSViewer") && this.showIsVisible) {
|
|
|
384 |
l.add(new RowAction(new AbstractAction() {
|
|
|
385 |
public void actionPerformed(ActionEvent ev) {
|
|
|
386 |
try {
|
80 |
ilm |
387 |
createAbstractSheet(IListe.get(ev).fetchSelectedRow()).openDocument(false);
|
149 |
ilm |
388 |
} catch (Exception e) {
|
|
|
389 |
ExceptionHandler.handle("Impossible d'ouvrir le fichier", e);
|
19 |
ilm |
390 |
}
|
149 |
ilm |
391 |
}
|
|
|
392 |
}, this.showHeader, "document.modify") {
|
142 |
ilm |
393 |
|
149 |
ilm |
394 |
@Override
|
182 |
ilm |
395 |
public boolean enabledFor(ListEvent evt) {
|
149 |
ilm |
396 |
// On ne teste pas l'existence du fichier génété car les IOs peuvent prendre
|
|
|
397 |
// du temps
|
|
|
398 |
return evt.getSelectedRow() != null && evt.getTotalRowCount() >= 1;
|
|
|
399 |
}
|
|
|
400 |
});
|
|
|
401 |
|
19 |
ilm |
402 |
}
|
|
|
403 |
|
182 |
ilm |
404 |
l.add(new RowAction(new AbstractAction() {
|
|
|
405 |
public void actionPerformed(ActionEvent ev) {
|
|
|
406 |
try {
|
|
|
407 |
File file = createAbstractSheet(IListe.get(ev).fetchSelectedRow()).getOrCreatePDFDocumentFile(true);
|
|
|
408 |
|
|
|
409 |
JFileChooser fileChooser = new JFileChooser();
|
|
|
410 |
fileChooser.setSelectedFile(file);
|
|
|
411 |
fileChooser.setDialogTitle(TranslationManager.getInstance().getTranslationForAction("document.saveToPDF"));
|
|
|
412 |
|
|
|
413 |
int userSelection = fileChooser.showSaveDialog(IListe.get(ev));
|
|
|
414 |
|
|
|
415 |
if (userSelection == JFileChooser.APPROVE_OPTION) {
|
|
|
416 |
File fileToSave = fileChooser.getSelectedFile();
|
|
|
417 |
InputStream is = null;
|
|
|
418 |
OutputStream os = null;
|
|
|
419 |
try {
|
|
|
420 |
is = new FileInputStream(file);
|
|
|
421 |
os = new FileOutputStream(fileToSave);
|
|
|
422 |
byte[] buffer = new byte[4096];
|
|
|
423 |
int length;
|
|
|
424 |
while ((length = is.read(buffer)) > 0) {
|
|
|
425 |
os.write(buffer, 0, length);
|
|
|
426 |
}
|
|
|
427 |
} catch (Exception ex) {
|
|
|
428 |
ex.printStackTrace();
|
|
|
429 |
} finally {
|
|
|
430 |
|
|
|
431 |
try {
|
|
|
432 |
if (is != null)
|
|
|
433 |
is.close();
|
|
|
434 |
} catch (IOException e1) {
|
|
|
435 |
e1.printStackTrace();
|
|
|
436 |
}
|
|
|
437 |
try {
|
|
|
438 |
if (os != null)
|
|
|
439 |
os.close();
|
|
|
440 |
} catch (IOException e1) {
|
|
|
441 |
e1.printStackTrace();
|
|
|
442 |
}
|
|
|
443 |
}
|
|
|
444 |
}
|
|
|
445 |
|
|
|
446 |
} catch (Exception e) {
|
|
|
447 |
ExceptionHandler.handle("Impossible d'ouvrir le fichier", e);
|
|
|
448 |
}
|
|
|
449 |
}
|
|
|
450 |
}, false, "document.saveToPDF") {
|
|
|
451 |
|
|
|
452 |
@Override
|
|
|
453 |
public boolean enabledFor(ListEvent evt) {
|
|
|
454 |
// On ne teste pas l'existence du fichier génété car les IOs peuvent prendre
|
|
|
455 |
// du temps
|
|
|
456 |
return evt.getSelectedRow() != null && evt.getSelectedRowAccessors().size() == 1;
|
|
|
457 |
}
|
|
|
458 |
});
|
|
|
459 |
|
19 |
ilm |
460 |
if (this.printIsVisible) {
|
|
|
461 |
|
132 |
ilm |
462 |
// Impression rapide : imprime le ou les documents sélectionnés (les génère si besoin)
|
|
|
463 |
// en proposant l'interface Java d'impression
|
149 |
ilm |
464 |
l.add(new RowAction(new PrintDocumentAction(this), false, "document.print") {
|
65 |
ilm |
465 |
|
19 |
ilm |
466 |
@Override
|
182 |
ilm |
467 |
public boolean enabledFor(ListEvent evt) {
|
|
|
468 |
return evt.getSelectedRow() != null && evt.getTotalRowCount() > 0;
|
19 |
ilm |
469 |
}
|
|
|
470 |
});
|
|
|
471 |
|
|
|
472 |
}
|
|
|
473 |
|
149 |
ilm |
474 |
if (this.showIsVisible)
|
19 |
ilm |
475 |
|
149 |
ilm |
476 |
{
|
|
|
477 |
|
144 |
ilm |
478 |
l.add(getSendMailPDF());
|
142 |
ilm |
479 |
|
144 |
ilm |
480 |
l.add(getSendMail());
|
142 |
ilm |
481 |
|
19 |
ilm |
482 |
}
|
|
|
483 |
if (this.generateIsVisible) {
|
67 |
ilm |
484 |
l.add(new RowAction(new AbstractAction() {
|
19 |
ilm |
485 |
public void actionPerformed(ActionEvent ev) {
|
90 |
ilm |
486 |
|
182 |
ilm |
487 |
List<SQLRowAccessor> l = IListe.get(ev).getSelectedRowAccessors();
|
|
|
488 |
|
90 |
ilm |
489 |
if (l.size() == 1) {
|
|
|
490 |
createDocument(ev);
|
|
|
491 |
} else {
|
|
|
492 |
createDocuments(l);
|
|
|
493 |
}
|
19 |
ilm |
494 |
}
|
67 |
ilm |
495 |
}, this.generateHeader, "document.create") {
|
73 |
ilm |
496 |
|
19 |
ilm |
497 |
@Override
|
182 |
ilm |
498 |
public boolean enabledFor(ListEvent selection) {
|
|
|
499 |
return selection != null && selection.getTotalRowCount() > 0;
|
19 |
ilm |
500 |
}
|
65 |
ilm |
501 |
|
19 |
ilm |
502 |
});
|
|
|
503 |
}
|
|
|
504 |
|
|
|
505 |
return l;
|
|
|
506 |
}
|
65 |
ilm |
507 |
|
144 |
ilm |
508 |
public RowAction getSendMail() {
|
|
|
509 |
return new RowAction(new AbstractAction() {
|
|
|
510 |
public void actionPerformed(ActionEvent ev) {
|
174 |
ilm |
511 |
sendMail(ev, false);
|
144 |
ilm |
512 |
}
|
|
|
513 |
}, false, "document.send.email") {
|
|
|
514 |
|
|
|
515 |
@Override
|
182 |
ilm |
516 |
public boolean enabledFor(ListEvent evt) {
|
149 |
ilm |
517 |
// On ne teste pas l'existence du fichier génété car les IOs peuvent prendre du
|
|
|
518 |
// temps
|
|
|
519 |
return evt.getSelectedRow() != null && evt.getTotalRowCount() >= 1;
|
144 |
ilm |
520 |
}
|
149 |
ilm |
521 |
|
144 |
ilm |
522 |
};
|
|
|
523 |
}
|
|
|
524 |
|
|
|
525 |
public RowAction getSendMailPDF() {
|
|
|
526 |
return new RowAction(new AbstractAction() {
|
|
|
527 |
public void actionPerformed(ActionEvent ev) {
|
174 |
ilm |
528 |
sendMail(ev, true);
|
144 |
ilm |
529 |
}
|
|
|
530 |
}, false, "document.pdf.send.email") {
|
|
|
531 |
|
|
|
532 |
@Override
|
182 |
ilm |
533 |
public boolean enabledFor(ListEvent evt) {
|
149 |
ilm |
534 |
// On ne teste pas l'existence du fichier génété car les IOs peuvent prendre du
|
|
|
535 |
// temps
|
|
|
536 |
return evt.getSelectedRow() != null && evt.getTotalRowCount() >= 1;
|
144 |
ilm |
537 |
}
|
149 |
ilm |
538 |
|
144 |
ilm |
539 |
};
|
|
|
540 |
|
|
|
541 |
}
|
|
|
542 |
|
93 |
ilm |
543 |
private void createDocuments(List<? extends SQLRowAccessor> selection) {
|
90 |
ilm |
544 |
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);
|
|
|
545 |
if (a == JOptionPane.YES_OPTION) {
|
|
|
546 |
for (SQLRowAccessor sqlRowAccessor : selection) {
|
|
|
547 |
final AbstractSheetXml sheet = createAbstractSheet(sqlRowAccessor.getTable().getRow(sqlRowAccessor.getID()));
|
|
|
548 |
sheet.createDocumentAsynchronous();
|
182 |
ilm |
549 |
sheet.showPrintAndExportAsynchronous(false, false, true, Collections.emptyList());
|
90 |
ilm |
550 |
}
|
|
|
551 |
}
|
|
|
552 |
}
|
|
|
553 |
|
73 |
ilm |
554 |
private void createDocument(ActionEvent ev) {
|
80 |
ilm |
555 |
final AbstractSheetXml sheet = createAbstractSheet(IListe.get(ev).fetchSelectedRow());
|
73 |
ilm |
556 |
if (sheet.getGeneratedFile().exists()) {
|
|
|
557 |
int a = JOptionPane.showConfirmDialog(null, "Voulez vous remplacer le document existant?", "Génération de documents", JOptionPane.YES_NO_OPTION);
|
|
|
558 |
if (a == JOptionPane.YES_OPTION) {
|
|
|
559 |
sheet.createDocumentAsynchronous();
|
182 |
ilm |
560 |
sheet.showPrintAndExportAsynchronous(true, false, true, Collections.emptyList());
|
73 |
ilm |
561 |
return;
|
|
|
562 |
}
|
|
|
563 |
}
|
|
|
564 |
|
|
|
565 |
try {
|
|
|
566 |
sheet.getOrCreateDocumentFile();
|
182 |
ilm |
567 |
sheet.showPrintAndExportAsynchronous(true, false, false, Collections.emptyList());
|
73 |
ilm |
568 |
} catch (Exception exn) {
|
|
|
569 |
exn.printStackTrace();
|
|
|
570 |
}
|
|
|
571 |
|
|
|
572 |
}
|
|
|
573 |
|
65 |
ilm |
574 |
/**
|
|
|
575 |
* Action sur le double clic
|
|
|
576 |
*
|
|
|
577 |
* @return
|
|
|
578 |
*/
|
|
|
579 |
public RowAction getDefaultRowAction() {
|
67 |
ilm |
580 |
return new RowAction(new AbstractAction() {
|
65 |
ilm |
581 |
public void actionPerformed(ActionEvent ev) {
|
80 |
ilm |
582 |
final AbstractSheetXml sheet = createAbstractSheet(IListe.get(ev).fetchSelectedRow().asRow());
|
65 |
ilm |
583 |
try {
|
|
|
584 |
sheet.getOrCreateDocumentFile();
|
182 |
ilm |
585 |
sheet.showPrintAndExportAsynchronous(true, false, true, Collections.emptyList());
|
65 |
ilm |
586 |
} catch (Exception exn) {
|
|
|
587 |
ExceptionHandler.handle("Une erreur est survenue lors de la création du document.", exn);
|
|
|
588 |
}
|
|
|
589 |
}
|
67 |
ilm |
590 |
}, false, false, "document.create") {
|
142 |
ilm |
591 |
|
65 |
ilm |
592 |
@Override
|
182 |
ilm |
593 |
public boolean enabledFor(ListEvent selection) {
|
|
|
594 |
return selection != null && selection.getTotalRowCount() == 1;
|
65 |
ilm |
595 |
}
|
|
|
596 |
|
|
|
597 |
@Override
|
182 |
ilm |
598 |
public javax.swing.Action getDefaultAction(final ListEvent evt) {
|
65 |
ilm |
599 |
return this.getAction();
|
|
|
600 |
}
|
|
|
601 |
};
|
|
|
602 |
}
|
174 |
ilm |
603 |
|
182 |
ilm |
604 |
protected void sendMail(ActionEvent ev, boolean pdf) {
|
|
|
605 |
final List<SQLRowAccessor> selectedRows = IListe.get(ev).getSelectedRowAccessors();
|
|
|
606 |
|
174 |
ilm |
607 |
final SQLTable table = IListe.get(ev).getSource().getPrimaryTable();
|
|
|
608 |
final List<SQLRow> rows = new ArrayList<>();
|
182 |
ilm |
609 |
for (SQLRowAccessor r : selectedRows) {
|
174 |
ilm |
610 |
rows.add(table.getRow(r.getID()));
|
|
|
611 |
}
|
|
|
612 |
|
|
|
613 |
EmailTemplate.askTemplate(IListe.get(ev), table.getDBRoot(), new ValueListener() {
|
|
|
614 |
|
|
|
615 |
@Override
|
|
|
616 |
public void valueSelected(Object value) {
|
|
|
617 |
sendMail((EmailTemplate) value, createAbstractSheets(rows), pdf);
|
|
|
618 |
}
|
|
|
619 |
});
|
|
|
620 |
}
|
18 |
ilm |
621 |
}
|