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) {
|
80 |
ilm |
28 |
if (templateId == null) {
|
|
|
29 |
throw new NullPointerException("null templateId");
|
|
|
30 |
}
|
61 |
ilm |
31 |
final File file = getFileTemplate(templateId, language, type);
|
|
|
32 |
return (file == null ? null : getInputStream(file));
|
|
|
33 |
}
|
|
|
34 |
|
|
|
35 |
private FileInputStream getInputStream(File f) {
|
25 |
ilm |
36 |
try {
|
61 |
ilm |
37 |
return new FileInputStream(f);
|
25 |
ilm |
38 |
} catch (FileNotFoundException e) {
|
61 |
ilm |
39 |
System.err.println("Error: no file:" + f.getAbsolutePath());
|
|
|
40 |
e.printStackTrace();
|
25 |
ilm |
41 |
}
|
61 |
ilm |
42 |
return null;
|
25 |
ilm |
43 |
}
|
|
|
44 |
|
61 |
ilm |
45 |
public File getFileTemplate(String templateId, String language, String type) {
|
80 |
ilm |
46 |
if (templateId == null) {
|
|
|
47 |
throw new NullPointerException("null templateId");
|
|
|
48 |
}
|
93 |
ilm |
49 |
File templateFile1 = getTemplateFromLocalFile(templateId, language, type);
|
156 |
ilm |
50 |
if (templateFile1 != null && templateFile1.exists() && !templateFile1.isDirectory()) {
|
93 |
ilm |
51 |
return templateFile1;
|
25 |
ilm |
52 |
}
|
93 |
ilm |
53 |
File templateFile2 = getTemplateFromLocalFile(templateId + ".ods", language, type);
|
|
|
54 |
if (templateFile2 != null && templateFile2.exists()) {
|
|
|
55 |
return templateFile2;
|
61 |
ilm |
56 |
}
|
93 |
ilm |
57 |
File templateFile3 = getTemplateFromLocalFile(templateId + ".odt", language, type);
|
|
|
58 |
if (templateFile3 != null && templateFile3.exists()) {
|
|
|
59 |
return templateFile3;
|
61 |
ilm |
60 |
}
|
93 |
ilm |
61 |
String files = "";
|
|
|
62 |
if (templateFile1 != null) {
|
|
|
63 |
files += "'" + templateFile1.getAbsolutePath() + "' ";
|
|
|
64 |
}
|
|
|
65 |
if (templateFile2 != null) {
|
|
|
66 |
files += "'" + templateFile2.getAbsolutePath() + "' ";
|
|
|
67 |
}
|
|
|
68 |
if (templateFile3 != null) {
|
|
|
69 |
files += "'" + templateFile3.getAbsolutePath() + "'";
|
|
|
70 |
}
|
|
|
71 |
System.err.println("AbstractLocalTemplateProvider.getFileTemplate() failed for id:" + templateId + " language:" + language + " type:" + type + " files:" + files);
|
25 |
ilm |
72 |
return null;
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
@Override
|
61 |
ilm |
76 |
public InputStream getTemplatePrintConfiguration(String templateId, String language, String type) {
|
80 |
ilm |
77 |
if (templateId == null) {
|
|
|
78 |
throw new NullPointerException("null templateId");
|
|
|
79 |
}
|
61 |
ilm |
80 |
final File file = getFileTemplatePrintConfiguration(templateId, language, type);
|
|
|
81 |
return getInputStream(file);
|
25 |
ilm |
82 |
}
|
|
|
83 |
|
61 |
ilm |
84 |
public File getFileTemplatePrintConfiguration(String templateId, String language, String type) {
|
80 |
ilm |
85 |
if (templateId == null) {
|
|
|
86 |
throw new NullPointerException("null templateId");
|
|
|
87 |
}
|
61 |
ilm |
88 |
final File file = getTemplateFromLocalFile(templateId + ".odsp", language, type);
|
|
|
89 |
return file;
|
|
|
90 |
}
|
25 |
ilm |
91 |
|
|
|
92 |
@Override
|
61 |
ilm |
93 |
public InputStream getTemplateConfiguration(String templateId, String language, String type) {
|
80 |
ilm |
94 |
if (templateId == null) {
|
|
|
95 |
throw new NullPointerException("null templateId");
|
|
|
96 |
}
|
61 |
ilm |
97 |
final File file = getFileTemplateConfiguration(templateId, language, type);
|
132 |
ilm |
98 |
if (!file.exists()) {
|
|
|
99 |
return null;
|
|
|
100 |
}
|
61 |
ilm |
101 |
return getInputStream(file);
|
|
|
102 |
}
|
25 |
ilm |
103 |
|
61 |
ilm |
104 |
public File getFileTemplateConfiguration(String templateId, String language, String type) {
|
80 |
ilm |
105 |
if (templateId == null) {
|
|
|
106 |
throw new NullPointerException("null templateId");
|
|
|
107 |
}
|
61 |
ilm |
108 |
final File file = getTemplateFromLocalFile(templateId + ".xml", language, type);
|
|
|
109 |
return file;
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
protected abstract File getTemplateFromLocalFile(String templateIdWithExtension, String language, String type);
|
|
|
113 |
|
|
|
114 |
@Override
|
|
|
115 |
public abstract String getTemplatePath(String templateId, String language, String type);
|
|
|
116 |
|
|
|
117 |
public static String insertBeforeExtenstion(String fileName, String text) {
|
80 |
ilm |
118 |
if (fileName == null) {
|
|
|
119 |
throw new NullPointerException("null fileName");
|
|
|
120 |
}
|
|
|
121 |
if (text == null) {
|
|
|
122 |
throw new NullPointerException("null text");
|
|
|
123 |
}
|
|
|
124 |
|
61 |
ilm |
125 |
final int index = fileName.lastIndexOf('.');
|
|
|
126 |
if (index < 0) {
|
65 |
ilm |
127 |
return null;
|
61 |
ilm |
128 |
}
|
|
|
129 |
if (index == 0) {
|
|
|
130 |
return fileName + text;
|
|
|
131 |
}
|
|
|
132 |
final String name = fileName.substring(0, index);
|
|
|
133 |
final String ext = fileName.substring(index);
|
|
|
134 |
return name + text + ext;
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
void ensureDelete(File f) {
|
|
|
138 |
for (int i = 0; i < 2; i++) {
|
|
|
139 |
if (f.delete()) {
|
|
|
140 |
return;
|
|
|
141 |
}
|
|
|
142 |
JOptionPane.showMessageDialog(new JFrame(), "Fichier " + f.getAbsolutePath() + " vérrouillé.\nMerci de fermer tout programme pouvant y accéder (OpenOffice).");
|
|
|
143 |
}
|
|
|
144 |
f.deleteOnExit();
|
|
|
145 |
|
|
|
146 |
}
|
25 |
ilm |
147 |
}
|