OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 25 | Rev 65 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
25 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 java.io.File;
17
import java.io.FileInputStream;
18
import java.io.FileNotFoundException;
19
import java.io.InputStream;
20
 
61 ilm 21
import javax.swing.JFrame;
22
import javax.swing.JOptionPane;
23
 
25 ilm 24
public abstract class AbstractLocalTemplateProvider implements TemplateProvider {
25
 
26
    @Override
61 ilm 27
    public InputStream getTemplate(String templateId, String language, String type) {
28
        final File file = getFileTemplate(templateId, language, type);
29
        return (file == null ? null : getInputStream(file));
30
    }
31
 
32
    private FileInputStream getInputStream(File f) {
25 ilm 33
        try {
61 ilm 34
            return new FileInputStream(f);
25 ilm 35
        } catch (FileNotFoundException e) {
61 ilm 36
            System.err.println("Error: no file:" + f.getAbsolutePath());
37
            e.printStackTrace();
25 ilm 38
        }
61 ilm 39
        return null;
25 ilm 40
    }
41
 
61 ilm 42
    public File getFileTemplate(String templateId, String language, String type) {
43
        File templateFile = getTemplateFromLocalFile(templateId, language, type);
44
        if (templateFile != null && templateFile.exists()) {
45
            return templateFile;
25 ilm 46
        }
61 ilm 47
        templateFile = getTemplateFromLocalFile(templateId + ".ods", language, type);
48
        if (templateFile != null && templateFile.exists()) {
49
            return templateFile;
50
        }
51
        templateFile = getTemplateFromLocalFile(templateId + ".odt", language, type);
52
        if (templateFile != null && templateFile.exists()) {
53
            return templateFile;
54
        }
25 ilm 55
        return null;
56
    }
57
 
58
    @Override
61 ilm 59
    public InputStream getTemplatePrintConfiguration(String templateId, String language, String type) {
60
        final File file = getFileTemplatePrintConfiguration(templateId, language, type);
61
        return getInputStream(file);
25 ilm 62
    }
63
 
61 ilm 64
    public File getFileTemplatePrintConfiguration(String templateId, String language, String type) {
65
        final File file = getTemplateFromLocalFile(templateId + ".odsp", language, type);
66
        return file;
67
    }
25 ilm 68
 
69
    @Override
61 ilm 70
    public InputStream getTemplateConfiguration(String templateId, String language, String type) {
71
        final File file = getFileTemplateConfiguration(templateId, language, type);
72
        return getInputStream(file);
73
    }
25 ilm 74
 
61 ilm 75
    public File getFileTemplateConfiguration(String templateId, String language, String type) {
76
        final File file = getTemplateFromLocalFile(templateId + ".xml", language, type);
77
        return file;
78
    }
79
 
80
    protected abstract File getTemplateFromLocalFile(String templateIdWithExtension, String language, String type);
81
 
82
    @Override
83
    public abstract String getTemplatePath(String templateId, String language, String type);
84
 
85
    public static String insertBeforeExtenstion(String fileName, String text) {
86
        final int index = fileName.lastIndexOf('.');
87
        if (index < 0) {
88
            throw new IllegalArgumentException("No extension found in fileName '" + fileName + "'");
89
        }
90
        if (index == 0) {
91
            return fileName + text;
92
        }
93
        final String name = fileName.substring(0, index);
94
        final String ext = fileName.substring(index);
95
        return name + text + ext;
96
    }
97
 
98
    void ensureDelete(File f) {
99
        for (int i = 0; i < 2; i++) {
100
            if (f.delete()) {
101
                return;
102
            }
103
            JOptionPane.showMessageDialog(new JFrame(), "Fichier " + f.getAbsolutePath() + " vérrouillé.\nMerci de fermer tout programme pouvant y accéder (OpenOffice).");
104
        }
105
        f.deleteOnExit();
106
 
107
    }
25 ilm 108
}