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 org.openconcerto.erp.config.ComptaPropsConfiguration;
|
|
|
17 |
import org.openconcerto.erp.config.Gestion;
|
|
|
18 |
import org.openconcerto.erp.preferences.PrinterNXProps;
|
|
|
19 |
import org.openconcerto.erp.preferences.TemplateNXProps;
|
|
|
20 |
import org.openconcerto.odtemplate.Template;
|
|
|
21 |
import org.openconcerto.odtemplate.engine.OGNLDataModel;
|
73 |
ilm |
22 |
import org.openconcerto.openoffice.ODSingleXMLDocument;
|
18 |
ilm |
23 |
import org.jopendocument.link.Component;
|
|
|
24 |
import org.jopendocument.link.OOConnexion;
|
57 |
ilm |
25 |
import org.openconcerto.sql.Configuration;
|
18 |
ilm |
26 |
import org.openconcerto.sql.model.SQLRow;
|
|
|
27 |
import org.openconcerto.utils.ExceptionHandler;
|
|
|
28 |
import org.openconcerto.utils.FileUtils;
|
|
|
29 |
|
174 |
ilm |
30 |
import java.awt.print.PrinterJob;
|
18 |
ilm |
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 |
|
83 |
ilm |
41 |
import javax.swing.JFrame;
|
18 |
ilm |
42 |
import javax.swing.JOptionPane;
|
|
|
43 |
|
|
|
44 |
public abstract class AbstractJOOReportsSheet {
|
|
|
45 |
private static final String defaultLocationTemplate = SpreadSheetGenerator.defaultLocationTemplate;
|
93 |
ilm |
46 |
protected final DateFormat dateFormat = new SimpleDateFormat("dd MMMM yyyy");
|
|
|
47 |
protected final DateFormat dateFormat2 = new SimpleDateFormat("dd/MM/yy");
|
|
|
48 |
protected final DateFormat yearFormat = new SimpleDateFormat("yyyy");
|
18 |
ilm |
49 |
private String year;
|
|
|
50 |
protected String locationTemplate = TemplateNXProps.getInstance().getStringProperty("LocationTemplate");
|
25 |
ilm |
51 |
protected String templateId;
|
18 |
ilm |
52 |
private String printer;
|
|
|
53 |
protected boolean askOverwriting = false;
|
|
|
54 |
|
|
|
55 |
/**
|
|
|
56 |
* @return une Map contenant les valeurs à remplacer dans la template
|
|
|
57 |
*/
|
|
|
58 |
abstract protected Map createMap();
|
|
|
59 |
|
65 |
ilm |
60 |
abstract protected String getName();
|
18 |
ilm |
61 |
|
65 |
ilm |
62 |
public String getFileName() {
|
|
|
63 |
return getValidFileName(getName());
|
|
|
64 |
}
|
|
|
65 |
|
73 |
ilm |
66 |
MetaDataSheet meta;
|
|
|
67 |
|
65 |
ilm |
68 |
/**
|
73 |
ilm |
69 |
* MetaData à inclure lors de la génération
|
|
|
70 |
*
|
|
|
71 |
* @param meta
|
|
|
72 |
*/
|
|
|
73 |
public void setMetaGeneration(MetaDataSheet meta) {
|
|
|
74 |
this.meta = meta;
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
/**
|
|
|
78 |
* MetaData à inclure lors de la génération
|
|
|
79 |
*
|
|
|
80 |
* @param meta
|
|
|
81 |
*/
|
|
|
82 |
|
|
|
83 |
public MetaDataSheet getMetaGeneration() {
|
|
|
84 |
return this.meta;
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
/**
|
65 |
ilm |
88 |
* Remplace tous les caracteres non alphanumeriques (seul le _ est autorisé) par un -. Cela
|
|
|
89 |
* permet d'avoir toujours un nom de fichier valide.
|
|
|
90 |
*
|
|
|
91 |
* @param fileName nom du fichier à créer ex:FACTURE_2007/03/001
|
|
|
92 |
* @return un nom fichier valide ex:FACTURE_2007-03-001
|
|
|
93 |
*/
|
|
|
94 |
static String getValidFileName(String fileName) {
|
|
|
95 |
final StringBuffer result = new StringBuffer(fileName.length());
|
|
|
96 |
for (int i = 0; i < fileName.length(); i++) {
|
|
|
97 |
char ch = fileName.charAt(i);
|
|
|
98 |
|
|
|
99 |
// Si c'est un caractere alphanumerique
|
|
|
100 |
if (Character.isLetterOrDigit(ch) || (ch == '_') || (ch == ' ')) {
|
|
|
101 |
result.append(ch);
|
|
|
102 |
} else {
|
|
|
103 |
result.append('-');
|
|
|
104 |
}
|
|
|
105 |
}
|
|
|
106 |
return result.toString();
|
|
|
107 |
}
|
|
|
108 |
|
67 |
ilm |
109 |
public abstract String getDefaultTemplateID();
|
|
|
110 |
|
|
|
111 |
public abstract String getDefaultLocationProperty();
|
|
|
112 |
|
25 |
ilm |
113 |
protected void init(String year, String templateId, String attributePrinter) {
|
18 |
ilm |
114 |
this.year = year;
|
25 |
ilm |
115 |
this.templateId = templateId;
|
18 |
ilm |
116 |
this.printer = PrinterNXProps.getInstance().getStringProperty(attributePrinter);
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
public final void generate(boolean print, boolean show, String printer) {
|
|
|
120 |
generate(print, show, printer, false);
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
/**
|
|
|
124 |
* Genere le document OO, le pdf, et ouvre le document OO
|
|
|
125 |
*
|
|
|
126 |
* @param print
|
|
|
127 |
* @param show
|
|
|
128 |
* @param printer
|
|
|
129 |
*/
|
|
|
130 |
public void generate(boolean print, boolean show, String printer, boolean overwrite) {
|
|
|
131 |
|
|
|
132 |
if (this.locationTemplate.trim().length() == 0) {
|
|
|
133 |
this.locationTemplate = defaultLocationTemplate;
|
|
|
134 |
}
|
|
|
135 |
try {
|
|
|
136 |
|
|
|
137 |
String fileName = getFileName();
|
25 |
ilm |
138 |
final InputStream fileTemplate = TemplateManager.getInstance().getTemplate(this.templateId);
|
67 |
ilm |
139 |
File outputDir = DocumentLocalStorageManager.getInstance().getDocumentOutputDirectory(getDefaultTemplateID());
|
25 |
ilm |
140 |
File fileOutOO = getDocumentFile();
|
18 |
ilm |
141 |
if (fileOutOO.exists() && overwrite) {
|
|
|
142 |
if (this.askOverwriting) {
|
|
|
143 |
int answer = JOptionPane.showConfirmDialog(null, "Voulez vous écraser le document ?", "Remplacement d'un document", JOptionPane.YES_NO_OPTION);
|
|
|
144 |
if (answer == JOptionPane.YES_OPTION) {
|
57 |
ilm |
145 |
SheetUtils.convertToOldFile(((ComptaPropsConfiguration) Configuration.getInstance()).getRootSociete(), fileName, outputDir, fileOutOO, ".odt");
|
18 |
ilm |
146 |
}
|
|
|
147 |
} else {
|
57 |
ilm |
148 |
SheetUtils.convertToOldFile(((ComptaPropsConfiguration) Configuration.getInstance()).getRootSociete(), fileName, outputDir, fileOutOO, ".odt");
|
18 |
ilm |
149 |
}
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
if (!fileOutOO.exists()) {
|
|
|
153 |
fileOutOO.getParentFile().mkdirs();
|
25 |
ilm |
154 |
Template template = new Template(new BufferedInputStream(fileTemplate));
|
18 |
ilm |
155 |
|
|
|
156 |
// creation du document
|
|
|
157 |
final Map createMap = createMap();
|
|
|
158 |
OGNLDataModel model = new OGNLDataModel(createMap);
|
|
|
159 |
|
|
|
160 |
model.putAll(createMap);
|
25 |
ilm |
161 |
|
73 |
ilm |
162 |
final ODSingleXMLDocument createDocument = template.createDocument(model);
|
|
|
163 |
if (this.meta != null) {
|
|
|
164 |
this.meta.applyTo(createDocument.getPackage().getMeta(true));
|
|
|
165 |
}
|
|
|
166 |
createDocument.saveToPackageAs(fileOutOO);
|
|
|
167 |
|
18 |
ilm |
168 |
}
|
|
|
169 |
|
|
|
170 |
// ouverture de OO
|
|
|
171 |
if (show || print) {
|
|
|
172 |
|
|
|
173 |
try {
|
|
|
174 |
final OOConnexion ooConnexion = ComptaPropsConfiguration.getOOConnexion();
|
|
|
175 |
if (ooConnexion == null) {
|
|
|
176 |
return;
|
|
|
177 |
}
|
144 |
ilm |
178 |
final Component doc = ooConnexion.loadDocument(fileOutOO, true);
|
18 |
ilm |
179 |
|
25 |
ilm |
180 |
if (this.savePDF()) {
|
67 |
ilm |
181 |
File pdfOutputDir = DocumentLocalStorageManager.getInstance().getPDFOutputDirectory(getDefaultTemplateID());
|
25 |
ilm |
182 |
doc.saveToPDF(new File(pdfOutputDir, fileName + ".pdf"), "writer_pdf_Export");
|
|
|
183 |
}
|
18 |
ilm |
184 |
if (print) {
|
|
|
185 |
Map<String, Object> map = new HashMap<String, Object>();
|
|
|
186 |
map.put("Name", printer);
|
|
|
187 |
doc.printDocument(map);
|
|
|
188 |
}
|
73 |
ilm |
189 |
if (!show) {
|
|
|
190 |
doc.close();
|
|
|
191 |
}
|
18 |
ilm |
192 |
|
|
|
193 |
} catch (Exception e) {
|
|
|
194 |
e.printStackTrace();
|
144 |
ilm |
195 |
ExceptionHandler.handle("Impossible de créer le document PDF", e);
|
18 |
ilm |
196 |
}
|
144 |
ilm |
197 |
if (show) {
|
|
|
198 |
FileUtils.openFile(fileOutOO);
|
|
|
199 |
}
|
18 |
ilm |
200 |
}
|
|
|
201 |
} catch (FileNotFoundException e) {
|
|
|
202 |
e.printStackTrace();
|
|
|
203 |
ExceptionHandler.handle("Impossible de trouver le modéle.", e);
|
|
|
204 |
} catch (IOException e) {
|
|
|
205 |
e.printStackTrace();
|
|
|
206 |
} catch (org.openconcerto.odtemplate.TemplateException e) {
|
|
|
207 |
e.printStackTrace();
|
|
|
208 |
}
|
|
|
209 |
}
|
|
|
210 |
|
|
|
211 |
protected boolean savePDF() {
|
|
|
212 |
return false;
|
|
|
213 |
}
|
|
|
214 |
|
|
|
215 |
public void showDocument() {
|
25 |
ilm |
216 |
File fileOutOO = getDocumentFile();
|
18 |
ilm |
217 |
if (fileOutOO.exists()) {
|
|
|
218 |
try {
|
|
|
219 |
final OOConnexion ooConnexion = ComptaPropsConfiguration.getOOConnexion();
|
|
|
220 |
if (ooConnexion == null) {
|
|
|
221 |
return;
|
|
|
222 |
}
|
|
|
223 |
ooConnexion.loadDocument(fileOutOO, false);
|
83 |
ilm |
224 |
} catch (LinkageError e) {
|
|
|
225 |
JOptionPane.showMessageDialog(new JFrame(), "Merci d'installer OpenOffice ou LibreOffice");
|
18 |
ilm |
226 |
} catch (Exception e) {
|
|
|
227 |
e.printStackTrace();
|
|
|
228 |
ExceptionHandler.handle("Impossible de charger le document OpenOffice", e);
|
|
|
229 |
}
|
|
|
230 |
} else {
|
|
|
231 |
generate(false, true, "");
|
|
|
232 |
}
|
|
|
233 |
}
|
|
|
234 |
|
156 |
ilm |
235 |
public File getDocumentFile() {
|
67 |
ilm |
236 |
File outputDir = DocumentLocalStorageManager.getInstance().getDocumentOutputDirectory(getDefaultTemplateID());
|
25 |
ilm |
237 |
return new File(outputDir, getFileName() + ".odt");
|
|
|
238 |
}
|
|
|
239 |
|
18 |
ilm |
240 |
public void printDocument() {
|
25 |
ilm |
241 |
File fileOutOO = getDocumentFile();
|
18 |
ilm |
242 |
if (fileOutOO.exists()) {
|
|
|
243 |
try {
|
|
|
244 |
final OOConnexion ooConnexion = ComptaPropsConfiguration.getOOConnexion();
|
|
|
245 |
if (ooConnexion == null) {
|
|
|
246 |
return;
|
|
|
247 |
}
|
|
|
248 |
final Component doc = ooConnexion.loadDocument(fileOutOO, true);
|
174 |
ilm |
249 |
if (this.printer != null && this.printer.trim().length() > 0) {
|
|
|
250 |
Map<String, Object> map = new HashMap<String, Object>();
|
|
|
251 |
map.put("Name", this.printer);
|
|
|
252 |
doc.printDocument(map);
|
|
|
253 |
} else {
|
|
|
254 |
doc.printDocument(PrinterJob.getPrinterJob());
|
|
|
255 |
}
|
18 |
ilm |
256 |
doc.close();
|
83 |
ilm |
257 |
} catch (LinkageError e) {
|
|
|
258 |
JOptionPane.showMessageDialog(new JFrame(), "Merci d'installer OpenOffice ou LibreOffice");
|
18 |
ilm |
259 |
} catch (Exception e) {
|
|
|
260 |
e.printStackTrace();
|
|
|
261 |
ExceptionHandler.handle("Impossible de charger le document OpenOffice", e);
|
|
|
262 |
}
|
|
|
263 |
} else {
|
|
|
264 |
generate(true, false, this.printer);
|
|
|
265 |
}
|
|
|
266 |
}
|
|
|
267 |
|
|
|
268 |
public void fastPrintDocument() {
|
25 |
ilm |
269 |
final File f = getDocumentFile();
|
18 |
ilm |
270 |
if (!f.exists()) {
|
|
|
271 |
generate(true, false, this.printer);
|
|
|
272 |
} else {
|
|
|
273 |
try {
|
|
|
274 |
final OOConnexion ooConnexion = ComptaPropsConfiguration.getOOConnexion();
|
|
|
275 |
if (ooConnexion == null) {
|
|
|
276 |
return;
|
|
|
277 |
}
|
|
|
278 |
final Component doc = ooConnexion.loadDocument(f, true);
|
|
|
279 |
Map<String, Object> map = new HashMap<String, Object>();
|
|
|
280 |
map.put("Name", this.printer);
|
|
|
281 |
Map<String, Object> map2 = new HashMap<String, Object>();
|
|
|
282 |
map2.put("CopyCount", 1);
|
|
|
283 |
doc.printDocument(map, map2);
|
|
|
284 |
doc.close();
|
83 |
ilm |
285 |
} catch (LinkageError e) {
|
|
|
286 |
JOptionPane.showMessageDialog(new JFrame(), "Merci d'installer OpenOffice ou LibreOffice");
|
18 |
ilm |
287 |
} catch (Exception e) {
|
|
|
288 |
|
|
|
289 |
ExceptionHandler.handle("Impossible de charger le document OpentOffice", e);
|
|
|
290 |
e.printStackTrace();
|
|
|
291 |
}
|
|
|
292 |
}
|
|
|
293 |
}
|
|
|
294 |
|
|
|
295 |
protected String getInitiales(SQLRow row) {
|
|
|
296 |
String init = "";
|
|
|
297 |
if (row != null) {
|
|
|
298 |
final String stringPrenom = row.getString("PRENOM");
|
|
|
299 |
if (stringPrenom != null && stringPrenom.trim().length() != 0) {
|
|
|
300 |
init += stringPrenom.trim().charAt(0);
|
|
|
301 |
}
|
|
|
302 |
final String stringNom = row.getString("NOM");
|
|
|
303 |
if (stringNom != null && stringNom.trim().length() != 0) {
|
|
|
304 |
init += stringNom.trim().charAt(0);
|
|
|
305 |
}
|
|
|
306 |
}
|
|
|
307 |
return init;
|
|
|
308 |
}
|
|
|
309 |
|
156 |
ilm |
310 |
public File getPDFDocumentFile() {
|
|
|
311 |
final File outputPDFDirectory = DocumentLocalStorageManager.getInstance().getPDFOutputDirectory(this.templateId);
|
|
|
312 |
return new File(outputPDFDirectory, getFileName() + ".pdf");
|
|
|
313 |
}
|
|
|
314 |
|
18 |
ilm |
315 |
public void exportToPdf() {
|
|
|
316 |
// Export vers PDF
|
25 |
ilm |
317 |
final File fileOutOO = getDocumentFile();
|
156 |
ilm |
318 |
final File fileOutPDF = getPDFDocumentFile();
|
18 |
ilm |
319 |
|
|
|
320 |
if (!fileOutOO.exists()) {
|
|
|
321 |
generate(false, false, "");
|
|
|
322 |
}
|
|
|
323 |
try {
|
|
|
324 |
final OOConnexion ooConnexion = ComptaPropsConfiguration.getOOConnexion();
|
|
|
325 |
if (ooConnexion == null) {
|
|
|
326 |
return;
|
|
|
327 |
}
|
|
|
328 |
final Component doc = ooConnexion.loadDocument(fileOutOO, true);
|
|
|
329 |
doc.saveToPDF(fileOutPDF, "writer_pdf_Export");
|
|
|
330 |
doc.close();
|
83 |
ilm |
331 |
} catch (LinkageError e) {
|
|
|
332 |
JOptionPane.showMessageDialog(new JFrame(), "Merci d'installer OpenOffice ou LibreOffice");
|
18 |
ilm |
333 |
} catch (Exception e) {
|
|
|
334 |
e.printStackTrace();
|
|
|
335 |
ExceptionHandler.handle("Impossible de charger le document OpenOffice", e);
|
|
|
336 |
}
|
|
|
337 |
// Ouverture
|
|
|
338 |
int result = JOptionPane.showOptionDialog(null, "Ouvrir le pdf ?", "Ouverture du PDF", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null);
|
|
|
339 |
|
|
|
340 |
if (result == JOptionPane.YES_OPTION) {
|
25 |
ilm |
341 |
Gestion.openPDF(fileOutPDF);
|
18 |
ilm |
342 |
} else {
|
|
|
343 |
try {
|
|
|
344 |
FileUtils.openFile(fileOutPDF.getParentFile());
|
|
|
345 |
} catch (IOException e) {
|
|
|
346 |
// TODO Auto-generated catch block
|
|
|
347 |
e.printStackTrace();
|
|
|
348 |
JOptionPane.showMessageDialog(null, "Impossible d'ouvrir le dossier : " + fileOutPDF.getParentFile() + ".");
|
|
|
349 |
}
|
|
|
350 |
}
|
|
|
351 |
|
|
|
352 |
}
|
|
|
353 |
|
93 |
ilm |
354 |
public String getYear() {
|
|
|
355 |
return year;
|
|
|
356 |
}
|
18 |
ilm |
357 |
}
|