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