OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Details | 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.util.ArrayList;
18
import java.util.HashMap;
19
import java.util.List;
20
import java.util.Map;
21
 
22
public class DocumentLocalStorageManager {
23
    private static DocumentLocalStorageManager instance = new DocumentLocalStorageManager();
24
    private Map<String, File> dirs = new HashMap<String, File>();
25
    private Map<String, File> dirsPDF = new HashMap<String, File>();
26
    private File documentDefaultDirectory;
27
    private File PDFDefaultDirectory;
28
 
29
    public static DocumentLocalStorageManager getInstance() {
30
        return instance;
31
    }
32
 
33
    /**
34
     * Returns the directory to store the document created from a template
35
     *
36
     * @param templateId : the id of the template used to create the document
37
     * */
38
    public File getDocumentOutputDirectory(String templateId) {
39
        if (templateId == null) {
40
            throw new IllegalArgumentException("null template id");
41
        }
42
        final File f = dirs.get(templateId);
43
        if (f != null) {
44
            return f;
45
        }
46
        return documentDefaultDirectory;
47
 
48
    }
49
 
50
    /**
51
     * Returns the directory to store the PDF document created from a template
52
     *
53
     * @param templateId : the id of the template used to create the document
54
     * */
55
    public File getPDFOutputDirectory(String templateId) {
56
        if (templateId == null) {
57
            throw new IllegalArgumentException("null template id");
58
        }
59
        final File f = dirsPDF.get(templateId);
60
        if (f != null) {
61
            return f;
62
        }
63
        return PDFDefaultDirectory;
64
    }
65
 
66
    public void addDocumentDirectory(String templateId, File directory) {
67
        if (templateId == null) {
68
            throw new IllegalArgumentException("null template id");
69
        }
70
        this.dirs.put(templateId, directory);
71
        TemplateManager.getInstance().register(templateId);
72
    }
73
 
74
    public void removeDocumentDirectory(String templateId, File directory) {
75
        if (templateId == null) {
76
            throw new IllegalArgumentException("null template id");
77
        }
78
        this.dirs.remove(templateId);
79
    }
80
 
81
    public void addPDFDirectory(String templateId, File directory) {
82
        if (templateId == null) {
83
            throw new IllegalArgumentException("null template id");
84
        }
85
        this.dirsPDF.put(templateId, directory);
86
        TemplateManager.getInstance().register(templateId);
87
    }
88
 
89
    public void removePDFDirectory(String templateId, File directory) {
90
        if (templateId == null) {
91
            throw new IllegalArgumentException("null template id");
92
        }
93
        this.dirsPDF.remove(templateId);
94
    }
95
 
96
    public void setDocumentDefaultDirectory(File directory) {
97
        this.documentDefaultDirectory = directory;
98
    }
99
 
100
    public void setPDFDefaultDirectory(File directory) {
101
        this.PDFDefaultDirectory = directory;
102
    }
103
 
104
    public List<File> getAllPDFDirectories() {
105
        List<File> list = new ArrayList<File>();
106
        for (String id : this.dirsPDF.keySet()) {
107
            list.add(this.dirsPDF.get(id));
108
        }
109
        return list;
110
    }
111
 
112
    public List<File> getAllDocumentDirectories() {
113
        List<File> list = new ArrayList<File>();
114
        for (String id : this.dirs.keySet()) {
115
            list.add(this.dirs.get(id));
116
        }
117
        return list;
118
    }
119
 
120
    public void dump() {
121
        System.out.println(this.getClass().getCanonicalName());
122
        System.out.println("Default document directory: " + getAndTest(this.documentDefaultDirectory));
123
        System.out.println("Default PFD directory     : " + getAndTest(this.PDFDefaultDirectory));
124
        System.out.println("Document directories:");
125
        for (String key : this.dirs.keySet()) {
126
            System.out.println(rightAlign("'" + key + "'") + " : " + getAndTest(this.dirs.get(key)));
127
        }
128
        System.out.println("PDF directories:");
129
        for (String key : this.dirsPDF.keySet()) {
130
            System.out.println(rightAlign("'" + key + "'") + " : " + getAndTest(this.dirsPDF.get(key)));
131
        }
132
    }
133
 
134
    private String rightAlign(String s) {
135
        String r = s;
136
        int n = 20 - s.length();
137
        for (int i = 0; i < n; i++) {
138
            r = ' ' + r;
139
        }
140
        return r;
141
    }
142
 
143
    private String getAndTest(File dir) {
144
        if (dir == null) {
145
            return "null !!!!!!";
146
        }
147
        String r = "'" + dir.getAbsolutePath() + "'";
148
        if (dir.exists()) {
149
            if (!dir.isDirectory()) {
150
                r += " is not a directory!!";
151
            } else if (!dir.canWrite()) {
152
                r += " is write protected!!";
153
            }
154
        } else {
155
            r += " does not exist !!";
156
        }
157
        return r;
158
    }
159
 
160
}