OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 18 | Rev 57 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 18 Rev 25
Line 39... Line 39...
39
import java.util.Map;
39
import java.util.Map;
40
 
40
 
41
import javax.swing.JOptionPane;
41
import javax.swing.JOptionPane;
42
import javax.swing.SwingUtilities;
42
import javax.swing.SwingUtilities;
43
 
43
 
44
 
-
 
45
public abstract class AbstractJOOReportsSheet {
44
public abstract class AbstractJOOReportsSheet {
46
    private static final String defaultLocationTemplate = SpreadSheetGenerator.defaultLocationTemplate;
45
    private static final String defaultLocationTemplate = SpreadSheetGenerator.defaultLocationTemplate;
47
    protected static final DateFormat dateFormat = new SimpleDateFormat("dd MMMM yyyy");
46
    protected static final DateFormat dateFormat = new SimpleDateFormat("dd MMMM yyyy");
48
    protected static final DateFormat dateFormat2 = new SimpleDateFormat("dd/MM/yy");
47
    protected static final DateFormat dateFormat2 = new SimpleDateFormat("dd/MM/yy");
49
    protected static final DateFormat yearFormat = new SimpleDateFormat("yyyy");
48
    protected static final DateFormat yearFormat = new SimpleDateFormat("yyyy");
50
    private String year;
49
    private String year;
51
    protected String locationTemplate = TemplateNXProps.getInstance().getStringProperty("LocationTemplate");
50
    protected String locationTemplate = TemplateNXProps.getInstance().getStringProperty("LocationTemplate");
52
    private String locationOO, locationPDF;
-
 
53
    protected String templateFileName;
51
    protected String templateId;
54
    private String printer;
52
    private String printer;
55
    protected boolean askOverwriting = false;
53
    protected boolean askOverwriting = false;
56
 
54
 
57
    /**
55
    /**
58
     * @return une Map contenant les valeurs à remplacer dans la template
56
     * @return une Map contenant les valeurs à remplacer dans la template
59
     */
57
     */
60
    abstract protected Map createMap();
58
    abstract protected Map createMap();
61
 
59
 
62
    abstract public String getFileName();
60
    abstract public String getFileName();
63
 
61
 
64
    protected void init(String year, String templateFileName, String attributePrinter, Tuple2<String, String> t) {
62
    protected void init(String year, String templateId, String attributePrinter) {
65
        this.year = year;
63
        this.year = year;
66
        this.templateFileName = templateFileName;
64
        this.templateId = templateId;
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);
65
        this.printer = PrinterNXProps.getInstance().getStringProperty(attributePrinter);
70
    }
66
    }
71
 
67
 
72
    public final void generate(boolean print, boolean show, String printer) {
68
    public final void generate(boolean print, boolean show, String printer) {
73
        generate(print, show, printer, false);
69
        generate(print, show, printer, false);
Line 86... Line 82...
86
            this.locationTemplate = defaultLocationTemplate;
82
            this.locationTemplate = defaultLocationTemplate;
87
        }
83
        }
88
        try {
84
        try {
89
 
85
 
90
            String fileName = getFileName();
86
            String fileName = getFileName();
91
            final InputStream fileTemplate = getStream(this.templateFileName, this.locationTemplate, defaultLocationTemplate);
87
            final InputStream fileTemplate = TemplateManager.getInstance().getTemplate(this.templateId);
92
 
-
 
93
            File fileOutOO = new File(this.locationOO, fileName + ".odt");
88
            File outputDir = DocumentLocalStorageManager.getInstance().getDocumentOutputDirectory(this.templateId);
94
            // File fileOutPDF = new File(locationPropositionPDF, fileName);
89
            File fileOutOO = getDocumentFile();
95
 
-
 
96
            if (fileOutOO.exists() && overwrite) {
90
            if (fileOutOO.exists() && overwrite) {
97
                if (this.askOverwriting) {
91
                if (this.askOverwriting) {
98
                    int answer = JOptionPane.showConfirmDialog(null, "Voulez vous écraser le document ?", "Remplacement d'un document", JOptionPane.YES_NO_OPTION);
92
                    int answer = JOptionPane.showConfirmDialog(null, "Voulez vous écraser le document ?", "Remplacement d'un document", JOptionPane.YES_NO_OPTION);
99
                    if (answer == JOptionPane.YES_OPTION) {
93
                    if (answer == JOptionPane.YES_OPTION) {
100
                        SheetUtils.getInstance().convertToOldFile(fileName, new File(this.locationOO), fileOutOO, ".odt");
94
                        SheetUtils.convertToOldFile(fileName, outputDir, fileOutOO, ".odt");
101
                    }
95
                    }
102
                } else {
96
                } else {
103
                    SheetUtils.getInstance().convertToOldFile(fileName, new File(this.locationOO), fileOutOO, ".odt");
97
                    SheetUtils.convertToOldFile(fileName, outputDir, fileOutOO, ".odt");
104
                }
98
                }
105
            }
99
            }
106
 
100
 
107
            if (!fileOutOO.exists()) {
101
            if (!fileOutOO.exists()) {
108
                fileOutOO.getParentFile().mkdirs();
102
                fileOutOO.getParentFile().mkdirs();
109
                Template template;
-
 
110
                // try {
-
 
111
                template = new Template(new BufferedInputStream(fileTemplate));
103
                Template template = new Template(new BufferedInputStream(fileTemplate));
112
 
104
 
113
                // creation du document
105
                // creation du document
114
                final Map createMap = createMap();
106
                final Map createMap = createMap();
115
                OGNLDataModel model = new OGNLDataModel(createMap);
107
                OGNLDataModel model = new OGNLDataModel(createMap);
116
 
108
 
117
                model.putAll(createMap);
109
                model.putAll(createMap);
118
                template.createDocument(model).saveAs(fileOutOO);
110
                template.createDocument(model).saveAs(fileOutOO);
119
                // template.createDocument(model, new BufferedOutputStream(new
-
 
120
                // FileOutputStream(fileOutOO)));
-
 
121
                // } catch (JDOMException e) {
-
 
122
                // e.printStackTrace();
-
 
123
                // }
-
 
-
 
111
 
124
            }
112
            }
125
 
113
 
126
            // ouverture de OO
114
            // ouverture de OO
127
            if (show || print) {
115
            if (show || print) {
128
 
116
 
Line 131... Line 119...
131
                    if (ooConnexion == null) {
119
                    if (ooConnexion == null) {
132
                        return;
120
                        return;
133
                    }
121
                    }
134
                    final Component doc = ooConnexion.loadDocument(fileOutOO, !show);
122
                    final Component doc = ooConnexion.loadDocument(fileOutOO, !show);
135
 
123
 
136
                    if (this.savePDF())
124
                    if (this.savePDF()) {
-
 
125
                        File pdfOutputDir = DocumentLocalStorageManager.getInstance().getPDFOutputDirectory(templateId);
137
                        doc.saveToPDF(new File(this.locationPDF, fileName + ".pdf"), "writer_pdf_Export");
126
                        doc.saveToPDF(new File(pdfOutputDir, fileName + ".pdf"), "writer_pdf_Export");
138
 
-
 
-
 
127
                    }
139
                    if (print) {
128
                    if (print) {
140
                        Map<String, Object> map = new HashMap<String, Object>();
129
                        Map<String, Object> map = new HashMap<String, Object>();
141
                        map.put("Name", printer);
130
                        map.put("Name", printer);
142
                        doc.printDocument(map);
131
                        doc.printDocument(map);
143
                    }
132
                    }
Line 162... Line 151...
162
    protected boolean savePDF() {
151
    protected boolean savePDF() {
163
        return false;
152
        return false;
164
    }
153
    }
165
 
154
 
166
    public void showDocument() {
155
    public void showDocument() {
167
        File fileOutOO = new File(this.locationOO, getFileName() + ".odt");
156
        File fileOutOO = getDocumentFile();
168
        if (fileOutOO.exists()) {
157
        if (fileOutOO.exists()) {
169
            try {
158
            try {
170
                final OOConnexion ooConnexion = ComptaPropsConfiguration.getOOConnexion();
159
                final OOConnexion ooConnexion = ComptaPropsConfiguration.getOOConnexion();
171
                if (ooConnexion == null) {
160
                if (ooConnexion == null) {
172
                    return;
161
                    return;
Line 181... Line 170...
181
        } else {
170
        } else {
182
            generate(false, true, "");
171
            generate(false, true, "");
183
        }
172
        }
184
    }
173
    }
185
 
174
 
-
 
175
    private File getDocumentFile() {
-
 
176
        File outputDir = DocumentLocalStorageManager.getInstance().getDocumentOutputDirectory(templateId);
-
 
177
        return new File(outputDir, getFileName() + ".odt");
-
 
178
    }
-
 
179
 
186
    public void printDocument() {
180
    public void printDocument() {
187
        File fileOutOO = new File(this.locationOO, getFileName() + ".odt");
181
        File fileOutOO = getDocumentFile();
188
        if (fileOutOO.exists()) {
182
        if (fileOutOO.exists()) {
189
 
183
 
190
            try {
184
            try {
191
                final OOConnexion ooConnexion = ComptaPropsConfiguration.getOOConnexion();
185
                final OOConnexion ooConnexion = ComptaPropsConfiguration.getOOConnexion();
192
                if (ooConnexion == null) {
186
                if (ooConnexion == null) {
Line 209... Line 203...
209
        }
203
        }
210
    }
204
    }
211
 
205
 
212
    public void fastPrintDocument() {
206
    public void fastPrintDocument() {
213
 
207
 
214
        final File f = new File(this.locationOO, getFileName() + ".odt");
208
        final File f = getDocumentFile();
215
 
209
 
216
        if (!f.exists()) {
210
        if (!f.exists()) {
217
            generate(true, false, this.printer);
211
            generate(true, false, this.printer);
218
        } else {
212
        } else {
219
 
213
 
Line 237... Line 231...
237
                e.printStackTrace();
231
                e.printStackTrace();
238
            }
232
            }
239
        }
233
        }
240
    }
234
    }
241
 
235
 
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) {
236
    protected String getInitiales(SQLRow row) {
250
        String init = "";
237
        String init = "";
251
        if (row != null) {
238
        if (row != null) {
252
            final String stringPrenom = row.getString("PRENOM");
239
            final String stringPrenom = row.getString("PRENOM");
253
            if (stringPrenom != null && stringPrenom.trim().length() != 0) {
240
            if (stringPrenom != null && stringPrenom.trim().length() != 0) {
Line 262... Line 249...
262
    }
249
    }
263
 
250
 
264
    public void exportToPdf() {
251
    public void exportToPdf() {
265
 
252
 
266
        // Export vers PDF
253
        // Export vers PDF
267
        String fileName = getFileName();
254
        final String fileName = getFileName();
268
        File fileOutOO = new File(this.locationOO, fileName + ".odt");
255
        final File fileOutOO = getDocumentFile();
-
 
256
        final File outputPDFDirectory = DocumentLocalStorageManager.getInstance().getPDFOutputDirectory(this.templateId);
269
        File fileOutPDF = new File(this.locationPDF, fileName);
257
        final File fileOutPDF = new File(outputPDFDirectory, fileName + ".pdf");
270
 
258
 
271
        if (!fileOutOO.exists()) {
259
        if (!fileOutOO.exists()) {
272
            generate(false, false, "");
260
            generate(false, false, "");
273
        }
261
        }
274
 
262
 
Line 288... Line 276...
288
        }
276
        }
289
        // Ouverture
277
        // Ouverture
290
        int result = JOptionPane.showOptionDialog(null, "Ouvrir le pdf ?", "Ouverture du PDF", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null);
278
        int result = JOptionPane.showOptionDialog(null, "Ouvrir le pdf ?", "Ouverture du PDF", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null);
291
 
279
 
292
        if (result == JOptionPane.YES_OPTION) {
280
        if (result == JOptionPane.YES_OPTION) {
293
            Gestion.openPDF(new File(fileOutPDF.getAbsolutePath() + ".pdf"));
281
            Gestion.openPDF(fileOutPDF);
294
        } else {
282
        } else {
295
            try {
283
            try {
296
                FileUtils.openFile(fileOutPDF.getParentFile());
284
                FileUtils.openFile(fileOutPDF.getParentFile());
297
            } catch (IOException e) {
285
            } catch (IOException e) {
298
                // TODO Auto-generated catch block
286
                // TODO Auto-generated catch block
Line 315... Line 303...
315
            return null;
303
            return null;
316
        }
304
        }
317
        return ville.getName();
305
        return ville.getName();
318
    }
306
    }
319
 
307
 
320
    public String getLocationOO() {
-
 
321
        return locationOO;
-
 
322
    }
-
 
323
 
-
 
324
    protected static String getVilleCP(String name) {
308
    protected static String getVilleCP(String name) {
325
        Ville ville = Ville.getVilleFromVilleEtCode(name);
309
        Ville ville = Ville.getVilleFromVilleEtCode(name);
326
        if (ville == null) {
310
        if (ville == null) {
327
 
311
 
328
            return null;
312
            return null;