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