1 |
/*
|
1 |
/*
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
3 |
*
|
3 |
*
|
4 |
* Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
|
4 |
* Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
|
5 |
*
|
5 |
*
|
6 |
* The contents of this file are subject to the terms of the GNU General Public License Version 3
|
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
|
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
|
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.
|
9 |
* language governing permissions and limitations under the License.
|
10 |
*
|
10 |
*
|
11 |
* When distributing the software, include this License Header Notice in each file.
|
11 |
* When distributing the software, include this License Header Notice in each file.
|
12 |
*/
|
12 |
*/
|
13 |
|
13 |
|
14 |
package org.openconcerto.erp.generationDoc;
|
14 |
package org.openconcerto.erp.generationDoc;
|
15 |
|
15 |
|
16 |
import org.openconcerto.erp.config.ComptaPropsConfiguration;
|
16 |
import org.openconcerto.erp.config.ComptaPropsConfiguration;
|
17 |
import org.openconcerto.erp.core.common.element.StyleSQLElement;
|
17 |
import org.openconcerto.erp.core.common.element.StyleSQLElement;
|
18 |
import org.openconcerto.openoffice.ODPackage;
|
18 |
import org.openconcerto.openoffice.ODPackage;
|
19 |
import org.openconcerto.openoffice.spreadsheet.MutableCell;
|
19 |
import org.openconcerto.openoffice.spreadsheet.MutableCell;
|
20 |
import org.openconcerto.openoffice.spreadsheet.Sheet;
|
20 |
import org.openconcerto.openoffice.spreadsheet.Sheet;
|
21 |
import org.openconcerto.openoffice.spreadsheet.SpreadSheet;
|
21 |
import org.openconcerto.openoffice.spreadsheet.SpreadSheet;
|
22 |
import org.openconcerto.sql.Configuration;
|
22 |
import org.openconcerto.sql.Configuration;
|
23 |
import org.openconcerto.sql.model.SQLRow;
|
23 |
import org.openconcerto.sql.model.SQLRow;
|
24 |
import org.openconcerto.utils.ExceptionHandler;
|
24 |
import org.openconcerto.utils.ExceptionHandler;
|
25 |
import org.openconcerto.utils.StreamUtils;
|
25 |
import org.openconcerto.utils.StreamUtils;
|
26 |
import org.openconcerto.utils.StringUtils;
|
26 |
import org.openconcerto.utils.StringUtils;
|
27 |
import org.openconcerto.utils.io.BOMSkipper;
|
27 |
import org.openconcerto.utils.io.BOMSkipper;
|
28 |
|
28 |
|
29 |
import java.awt.Point;
|
29 |
import java.awt.Point;
|
30 |
import java.io.BufferedReader;
|
30 |
import java.io.BufferedReader;
|
31 |
import java.io.File;
|
31 |
import java.io.File;
|
32 |
import java.io.FileNotFoundException;
|
32 |
import java.io.FileNotFoundException;
|
33 |
import java.io.IOException;
|
33 |
import java.io.IOException;
|
34 |
import java.io.InputStream;
|
34 |
import java.io.InputStream;
|
35 |
import java.io.InputStreamReader;
|
35 |
import java.io.InputStreamReader;
|
36 |
import java.nio.charset.Charset;
|
36 |
import java.nio.charset.Charset;
|
37 |
import java.util.HashMap;
|
37 |
import java.util.HashMap;
|
38 |
import java.util.Iterator;
|
38 |
import java.util.Iterator;
|
39 |
import java.util.List;
|
39 |
import java.util.List;
|
40 |
import java.util.Map;
|
40 |
import java.util.Map;
|
41 |
|
41 |
|
42 |
import javax.swing.JOptionPane;
|
42 |
import javax.swing.JOptionPane;
|
43 |
import javax.swing.SwingUtilities;
|
43 |
import javax.swing.SwingUtilities;
|
44 |
|
44 |
|
45 |
import org.jdom2.Document;
|
45 |
import org.jdom2.Document;
|
46 |
import org.jdom2.Element;
|
46 |
import org.jdom2.Element;
|
47 |
import org.jdom2.JDOMException;
|
47 |
import org.jdom2.JDOMException;
|
48 |
import org.jdom2.input.SAXBuilder;
|
48 |
import org.jdom2.input.SAXBuilder;
|
49 |
|
49 |
|
50 |
public class OOgenerationListeXML {
|
50 |
public class OOgenerationListeXML {
|
51 |
|
51 |
|
52 |
// Cache pour la recherche des styles
|
52 |
// Cache pour la recherche des styles
|
53 |
private static Map<Sheet, Map<String, Map<Integer, String>>> cacheStyle = new HashMap<Sheet, Map<String, Map<Integer, String>>>();
|
53 |
private static Map<Sheet, Map<String, Map<Integer, String>>> cacheStyle = new HashMap<Sheet, Map<String, Map<Integer, String>>>();
|
54 |
private static Point testPoint = new Point(0, 0);
|
54 |
private static Point testPoint = new Point(0, 0);
|
55 |
|
55 |
|
56 |
public static File genere(String modele, File pathDest, String fileDest, Map<Integer, List<Map<String, Object>>> liste, Map<Integer, Map<String, Object>> values) {
|
56 |
public static File genere(String modele, File pathDest, String fileDest, Map<Integer, List<Map<String, Object>>> liste, Map<Integer, Map<String, Object>> values) {
|
57 |
return genere(modele, pathDest, fileDest, liste, values, new HashMap<Integer, Map<Integer, String>>(), null, null);
|
57 |
return genere(modele, pathDest, fileDest, liste, values, new HashMap<Integer, Map<Integer, String>>(), null, null);
|
58 |
}
|
58 |
}
|
59 |
|
59 |
|
60 |
public static File genere(String templateId, File pathDest, String fileDest, Map<Integer, List<Map<String, Object>>> liste, Map<Integer, Map<String, Object>> values,
|
60 |
public static File genere(String templateId, File pathDest, String fileDest, Map<Integer, List<Map<String, Object>>> liste, Map<Integer, Map<String, Object>> values,
|
61 |
Map<Integer, Map<Integer, String>> mapStyle, List<String> sheetName, SQLRow rowLanguage) {
|
61 |
Map<Integer, Map<Integer, String>> mapStyle, List<String> sheetName, SQLRow rowLanguage) {
|
62 |
cacheStyle.clear();
|
62 |
cacheStyle.clear();
|
63 |
final SAXBuilder builder = new SAXBuilder();
|
63 |
final SAXBuilder builder = new SAXBuilder();
|
64 |
try {
|
64 |
try {
|
65 |
InputStream xmlConfiguration = TemplateManager.getInstance().getTemplateConfiguration(templateId, rowLanguage != null ? rowLanguage.getString("CHEMIN") : null, null);
|
65 |
InputStream xmlConfiguration = TemplateManager.getInstance().getTemplateConfiguration(templateId, rowLanguage != null ? rowLanguage.getString("CHEMIN") : null, null);
|
66 |
if (xmlConfiguration == null) {
|
66 |
if (xmlConfiguration == null) {
|
67 |
throw new IllegalStateException("Template configuration " + templateId + " not found (" + TemplateManager.getInstance().getClass().getName() + ")");
|
67 |
throw new IllegalStateException("Template configuration " + templateId + " not found (" + TemplateManager.getInstance().getClass().getName() + ")");
|
68 |
}
|
68 |
}
|
69 |
final BufferedReader xmlConfigurationReader = new BufferedReader(new InputStreamReader(xmlConfiguration, Charset.forName("UTF8")));
|
69 |
final BufferedReader xmlConfigurationReader = new BufferedReader(new InputStreamReader(xmlConfiguration, Charset.forName("UTF8")));
|
70 |
BOMSkipper.skip(xmlConfigurationReader);
|
70 |
BOMSkipper.skip(xmlConfigurationReader);
|
71 |
final Document doc = builder.build(xmlConfigurationReader);
|
71 |
final Document doc = builder.build(xmlConfigurationReader);
|
72 |
xmlConfigurationReader.close();
|
72 |
xmlConfigurationReader.close();
|
73 |
xmlConfiguration.close();
|
73 |
xmlConfiguration.close();
|
74 |
|
74 |
|
75 |
// On initialise un nouvel élément racine avec l'élément racine du
|
75 |
// On initialise un nouvel élément racine avec l'élément racine du
|
76 |
// document.
|
76 |
// document.
|
77 |
final Element racine = doc.getRootElement();
|
77 |
final Element racine = doc.getRootElement();
|
78 |
|
78 |
|
79 |
// Création et génération du fichier OO
|
79 |
// Création et génération du fichier OO
|
80 |
final InputStream template = TemplateManager.getInstance().getTemplate(templateId, rowLanguage != null ? rowLanguage.getString("CHEMIN") : null, null);
|
80 |
final InputStream template = TemplateManager.getInstance().getTemplate(templateId, rowLanguage != null ? rowLanguage.getString("CHEMIN") : null, null);
|
81 |
if (template == null) {
|
81 |
if (template == null) {
|
82 |
throw new IllegalStateException("Template " + templateId + " not found (" + TemplateManager.getInstance().getClass().getName() + ")");
|
82 |
throw new IllegalStateException("Template " + templateId + " not found (" + TemplateManager.getInstance().getClass().getName() + ")");
|
83 |
}
|
83 |
}
|
84 |
final SpreadSheet spreadSheet = new ODPackage(template).getSpreadSheet();
|
84 |
final SpreadSheet spreadSheet = new ODPackage(template).getSpreadSheet();
|
85 |
|
85 |
|
86 |
Sheet sheet0 = spreadSheet.getSheet(0);
|
86 |
Sheet sheet0 = spreadSheet.getSheet(0);
|
87 |
if (sheetName != null && sheetName.size() > 0) {
|
87 |
if (sheetName != null && sheetName.size() > 0) {
|
88 |
for (int i = 1; i < sheetName.size(); i++) {
|
88 |
for (int i = 1; i < sheetName.size(); i++) {
|
89 |
sheet0.copy(i, (sheetName != null) ? sheetName.get(i) : "Feuille " + i);
|
89 |
sheet0.copy(i, (sheetName != null) ? sheetName.get(i) : "Feuille " + i);
|
90 |
}
|
90 |
}
|
91 |
spreadSheet.getSheet(0).setName(sheetName.get(0));
|
91 |
spreadSheet.getSheet(0).setName(sheetName.get(0));
|
92 |
}
|
92 |
}
|
93 |
|
93 |
|
94 |
for (Integer i : liste.keySet()) {
|
94 |
for (Integer i : liste.keySet()) {
|
95 |
final Sheet sheet;
|
95 |
final Sheet sheet;
|
96 |
try {
|
96 |
try {
|
97 |
sheet = spreadSheet.getSheet(i);
|
97 |
sheet = spreadSheet.getSheet(i);
|
98 |
} catch (Exception e) {
|
98 |
} catch (Exception e) {
|
99 |
throw new InvalidTemplateException("La feuille numéro " + i + " n'est pas dans le modèle", e);
|
99 |
throw new InvalidTemplateException("La feuille numéro " + i + " n'est pas dans le modèle", e);
|
100 |
}
|
100 |
}
|
101 |
List children = racine.getChildren("element" + i);
|
101 |
List children = racine.getChildren("element" + i);
|
102 |
if (children.size() == 0) {
|
102 |
if (children.size() == 0) {
|
103 |
children = racine.getChildren("element");
|
103 |
children = racine.getChildren("element");
|
104 |
}
|
104 |
}
|
105 |
parseElementsXML(children, sheet, values.get(i));
|
105 |
parseElementsXML(children, sheet, values.get(i));
|
106 |
Element child = racine.getChild("table" + i);
|
106 |
Element child = racine.getChild("table" + i);
|
107 |
if (child == null) {
|
107 |
if (child == null) {
|
108 |
child = racine.getChild("table");
|
108 |
child = racine.getChild("table");
|
109 |
}
|
109 |
}
|
110 |
parseListeXML(child, liste.get(i), sheet, mapStyle.get(i));
|
110 |
parseListeXML(child, liste.get(i), sheet, mapStyle.get(i));
|
111 |
}
|
111 |
}
|
112 |
cacheStyle.clear();
|
112 |
cacheStyle.clear();
|
113 |
|
113 |
|
114 |
// Sauvegarde du fichier
|
114 |
// Sauvegarde du fichier
|
115 |
return saveSpreadSheet(spreadSheet, pathDest, fileDest, templateId, rowLanguage);
|
115 |
return saveSpreadSheet(spreadSheet, pathDest, fileDest, templateId, rowLanguage);
|
116 |
|
116 |
|
117 |
} catch (JDOMException e) {
|
117 |
} catch (JDOMException e) {
|
118 |
ExceptionHandler.handle("Erreur lors de la génération du fichier " + fileDest, e);
|
118 |
ExceptionHandler.handle("Erreur lors de la génération du fichier " + fileDest, e);
|
119 |
} catch (IOException e) {
|
119 |
} catch (IOException e) {
|
120 |
ExceptionHandler.handle("Erreur lors de la création du fichier " + fileDest, e);
|
120 |
ExceptionHandler.handle("Erreur lors de la création du fichier " + fileDest, e);
|
121 |
}
|
121 |
}
|
122 |
cacheStyle.clear();
|
122 |
cacheStyle.clear();
|
123 |
return null;
|
123 |
return null;
|
124 |
}
|
124 |
}
|
125 |
|
125 |
|
126 |
private static void parseElementsXML(List<Element> elts, Sheet sheet, Map<String, Object> values) {
|
126 |
private static void parseElementsXML(List<Element> elts, Sheet sheet, Map<String, Object> values) {
|
127 |
if (values == null) {
|
127 |
if (values == null) {
|
128 |
return;
|
128 |
return;
|
129 |
}
|
129 |
}
|
130 |
for (Element elt : elts) {
|
130 |
for (Element elt : elts) {
|
131 |
|
131 |
|
132 |
String name = elt.getAttributeValue("ValueName");
|
132 |
String name = elt.getAttributeValue("ValueName");
|
133 |
Object result = values.get(name);
|
133 |
Object result = values.get(name);
|
134 |
|
134 |
|
135 |
if (result != null) {
|
135 |
if (result != null) {
|
136 |
result = resizeValue(elt, result);
|
136 |
result = resizeValue(elt, result);
|
137 |
boolean controlLine = elt.getAttributeValue("controleMultiline") == null ? true : !elt.getAttributeValue("controleMultiline").equalsIgnoreCase("false");
|
137 |
boolean controlLine = elt.getAttributeValue("controleMultiline") == null ? true : !elt.getAttributeValue("controleMultiline").equalsIgnoreCase("false");
|
138 |
boolean replace = elt.getAttributeValue("type").equalsIgnoreCase("Replace");
|
138 |
boolean replace = elt.getAttributeValue("type").equalsIgnoreCase("Replace");
|
139 |
String replacePattern = elt.getAttributeValue("replacePattern");
|
139 |
String replacePattern = elt.getAttributeValue("replacePattern");
|
140 |
fill(sheet.resolveHint(elt.getAttributeValue("location")), result, sheet, replace, replacePattern, null, false, controlLine);
|
140 |
fill(sheet.resolveHint(elt.getAttributeValue("location")), result, sheet, replace, replacePattern, null, false, controlLine);
|
141 |
}
|
141 |
}
|
142 |
}
|
142 |
}
|
143 |
}
|
143 |
}
|
144 |
|
144 |
|
145 |
/**
|
145 |
/**
|
146 |
* Remplit le tableau
|
146 |
* Remplit le tableau
|
147 |
*
|
147 |
*
|
148 |
* @param tableau
|
148 |
* @param tableau
|
149 |
* @param elt
|
149 |
* @param elt
|
150 |
* @param id
|
150 |
* @param id
|
151 |
* @param sheet
|
151 |
* @param sheet
|
152 |
*/
|
152 |
*/
|
153 |
private static void parseListeXML(Element tableau, List<Map<String, Object>> liste, Sheet sheet, Map<Integer, String> style) {
|
153 |
private static void parseListeXML(Element tableau, List<Map<String, Object>> liste, Sheet sheet, Map<Integer, String> style) {
|
154 |
|
154 |
|
155 |
if (liste == null || tableau == null) {
|
155 |
if (liste == null || tableau == null) {
|
156 |
return;
|
156 |
return;
|
157 |
}
|
157 |
}
|
158 |
Object oLastColTmp = tableau.getAttributeValue("lastColumn");
|
158 |
Object oLastColTmp = tableau.getAttributeValue("lastColumn");
|
159 |
int lastColumn = -1;
|
159 |
int lastColumn = -1;
|
160 |
int endPageLine = Integer.valueOf(tableau.getAttributeValue("endPageLine"));
|
160 |
int endPageLine = Integer.valueOf(tableau.getAttributeValue("endPageLine"));
|
161 |
if (oLastColTmp != null) {
|
161 |
if (oLastColTmp != null) {
|
162 |
lastColumn = sheet.resolveHint(oLastColTmp.toString() + 1).x + 1;
|
162 |
lastColumn = sheet.resolveHint(oLastColTmp.toString() + 1).x + 1;
|
163 |
}
|
163 |
}
|
164 |
Map<String, Map<Integer, String>> mapStyle = searchStyle(sheet, lastColumn, endPageLine);
|
164 |
Map<String, Map<Integer, String>> mapStyle = searchStyle(sheet, lastColumn, endPageLine);
|
165 |
|
165 |
|
166 |
int nbPage = fillTable(tableau, liste, sheet, mapStyle, true, style);
|
166 |
int nbPage = fillTable(tableau, liste, sheet, mapStyle, true, style);
|
167 |
int firstLine = Integer.valueOf(tableau.getAttributeValue("firstLine"));
|
167 |
int firstLine = Integer.valueOf(tableau.getAttributeValue("firstLine"));
|
168 |
int endLine = Integer.valueOf(tableau.getAttributeValue("endLine"));
|
168 |
int endLine = Integer.valueOf(tableau.getAttributeValue("endLine"));
|
169 |
Object printRangeObj = sheet.getPrintRanges();
|
169 |
Object printRangeObj = sheet.getPrintRanges();
|
170 |
|
170 |
|
171 |
System.err.println("Nombre de page == " + nbPage);
|
171 |
System.err.println("Nombre de page == " + nbPage);
|
172 |
if (nbPage == 1) {
|
172 |
if (nbPage == 1) {
|
173 |
fillTable(tableau, liste, sheet, mapStyle, false, style);
|
173 |
fillTable(tableau, liste, sheet, mapStyle, false, style);
|
174 |
} else {
|
174 |
} else {
|
175 |
|
175 |
|
176 |
if (printRangeObj != null) {
|
176 |
if (printRangeObj != null) {
|
177 |
String s = printRangeObj.toString();
|
177 |
String s = printRangeObj.toString();
|
178 |
String[] range = s.split(":");
|
178 |
String[] range = s.split(":");
|
179 |
|
179 |
|
180 |
for (int i = 0; i < range.length; i++) {
|
180 |
for (int i = 0; i < range.length; i++) {
|
181 |
String string = range[i];
|
181 |
String string = range[i];
|
182 |
range[i] = string.subSequence(string.indexOf('.') + 1, string.length()).toString();
|
182 |
range[i] = string.subSequence(string.indexOf('.') + 1, string.length()).toString();
|
183 |
}
|
183 |
}
|
184 |
|
184 |
|
185 |
int rowEnd = -1;
|
- |
|
186 |
if (range.length > 1) {
|
185 |
if (range.length > 1) {
|
187 |
rowEnd = sheet.resolveHint(range[1]).y + 1;
|
186 |
int rowEnd = sheet.resolveHint(range[1]).y + 1;
|
188 |
int rowEndNew = rowEnd * (nbPage + 1);
|
187 |
int rowEndNew = rowEnd * nbPage;
|
189 |
String sNew = s.replaceAll(String.valueOf(rowEnd), String.valueOf(rowEndNew));
|
188 |
String sNew = s.replaceAll(String.valueOf(rowEnd), String.valueOf(rowEndNew));
|
190 |
sheet.setPrintRanges(sNew);
|
189 |
sheet.setPrintRanges(sNew);
|
191 |
System.err.println(" ****** Replace print ranges; Old:" + rowEnd + "--" + s + " New:" + rowEndNew + "--" + sNew);
|
190 |
System.err.println("OOgenerationListXML ****** Replace print ranges; Old:" + rowEnd + "--" + s + " New:" + rowEndNew + "--" + sNew);
|
192 |
}
|
191 |
}
|
193 |
}
|
192 |
}
|
194 |
|
193 |
|
195 |
// le nombre d'éléments ne tient pas dans le tableau du modéle
|
194 |
// le nombre d'éléments ne tient pas dans le tableau du modéle
|
196 |
sheet.duplicateFirstRows(endLine, 1);
|
195 |
sheet.duplicateFirstRows(endLine, 1);
|
197 |
|
196 |
|
198 |
int lineToAdd = endPageLine - endLine;
|
197 |
int lineToAdd = endPageLine - endLine;
|
199 |
sheet.insertDuplicatedRows(firstLine, lineToAdd);
|
198 |
sheet.insertDuplicatedRows(firstLine, lineToAdd);
|
200 |
|
199 |
|
201 |
// On duplique la premiere page si on a besoin de plus de deux pages
|
200 |
// On duplique la premiere page si on a besoin de plus de deux pages
|
202 |
System.err.println("nbPage == " + nbPage);
|
201 |
System.err.println("nbPage == " + nbPage);
|
203 |
if (nbPage > 2) {
|
202 |
if (nbPage > 2) {
|
204 |
sheet.duplicateFirstRows(endPageLine, nbPage - 2);
|
203 |
sheet.duplicateFirstRows(endPageLine, nbPage - 2);
|
205 |
}
|
204 |
}
|
206 |
String pageRef = tableau.getAttributeValue("pageRef");
|
205 |
String pageRef = tableau.getAttributeValue("pageRef");
|
207 |
if (pageRef != null && pageRef.trim().length() > 0) {
|
206 |
if (pageRef != null && pageRef.trim().length() > 0) {
|
208 |
MutableCell<SpreadSheet> cell = sheet.getCellAt(pageRef);
|
207 |
MutableCell<SpreadSheet> cell = sheet.getCellAt(pageRef);
|
209 |
cell.setValue("Page 1/" + nbPage);
|
208 |
cell.setValue("Page 1/" + nbPage);
|
210 |
for (int i = 1; i < nbPage; i++) {
|
209 |
for (int i = 1; i < nbPage; i++) {
|
211 |
MutableCell<SpreadSheet> cell2 = sheet.getCellAt(cell.getX(), cell.getY() + (endPageLine * i));
|
210 |
MutableCell<SpreadSheet> cell2 = sheet.getCellAt(cell.getX(), cell.getY() + (endPageLine * i));
|
212 |
cell2.setValue("Page " + (i + 1) + "/" + nbPage);
|
211 |
cell2.setValue("Page " + (i + 1) + "/" + nbPage);
|
213 |
}
|
212 |
}
|
214 |
}
|
213 |
}
|
215 |
fillTable(tableau, liste, sheet, mapStyle, false, style);
|
214 |
fillTable(tableau, liste, sheet, mapStyle, false, style);
|
216 |
}
|
215 |
}
|
217 |
}
|
216 |
}
|
218 |
|
217 |
|
219 |
/**
|
218 |
/**
|
220 |
* Remplit le tableau d'éléments avec les données
|
219 |
* Remplit le tableau d'éléments avec les données
|
221 |
*
|
220 |
*
|
222 |
* @param tableau Element Xml contenant les informations sur le tableau
|
221 |
* @param tableau Element Xml contenant les informations sur le tableau
|
223 |
* @param elt SQLElement (ex : Bon de livraison)
|
222 |
* @param elt SQLElement (ex : Bon de livraison)
|
224 |
* @param id id de l'élément de la table
|
223 |
* @param id id de l'élément de la table
|
225 |
* @param sheet feuille calc à remplir
|
224 |
* @param sheet feuille calc à remplir
|
226 |
* @param mapStyle styles trouvés dans la page
|
225 |
* @param mapStyle styles trouvés dans la page
|
227 |
* @param test remplir ou non avec les valeurs
|
226 |
* @param test remplir ou non avec les valeurs
|
228 |
* @return le nombre de page
|
227 |
* @return le nombre de page
|
229 |
*/
|
228 |
*/
|
230 |
private static int fillTable(Element tableau, List<Map<String, Object>> liste, Sheet sheet, Map<String, Map<Integer, String>> mapStyle, boolean test, Map<Integer, String> style) {
|
229 |
private static int fillTable(Element tableau, List<Map<String, Object>> liste, Sheet sheet, Map<String, Map<Integer, String>> mapStyle, boolean test, Map<Integer, String> style) {
|
231 |
|
230 |
|
232 |
int nbPage = 1;
|
231 |
int nbPage = 1;
|
233 |
int currentLineTmp = Integer.valueOf(tableau.getAttributeValue("firstLine"));
|
232 |
int currentLineTmp = Integer.valueOf(tableau.getAttributeValue("firstLine"));
|
234 |
int currentLine = Integer.valueOf(tableau.getAttributeValue("firstLine"));
|
233 |
int currentLine = Integer.valueOf(tableau.getAttributeValue("firstLine"));
|
235 |
int endPageLine = Integer.valueOf(tableau.getAttributeValue("endPageLine"));
|
234 |
int endPageLine = Integer.valueOf(tableau.getAttributeValue("endPageLine"));
|
236 |
|
235 |
|
237 |
List listElts = tableau.getChildren("element");
|
236 |
List listElts = tableau.getChildren("element");
|
238 |
|
237 |
|
239 |
Object o = null;
|
238 |
Object o = null;
|
240 |
String columnSousTotal = tableau.getAttributeValue("groupSousTotalColumn");
|
239 |
String columnSousTotal = tableau.getAttributeValue("groupSousTotalColumn");
|
241 |
|
240 |
|
242 |
Map<String, Double> mapSousTotal = new HashMap<String, Double>();
|
241 |
Map<String, Double> mapSousTotal = new HashMap<String, Double>();
|
243 |
Map<String, Double> mapTotal = new HashMap<String, Double>();
|
242 |
Map<String, Double> mapTotal = new HashMap<String, Double>();
|
244 |
|
243 |
|
245 |
// on remplit chaque ligne à partir des rows recuperées
|
244 |
// on remplit chaque ligne à partir des rows recuperées
|
246 |
|
245 |
|
247 |
for (int i = 0; i < liste.size(); i++) {
|
246 |
for (int i = 0; i < liste.size(); i++) {
|
248 |
Map<String, Object> mValues = liste.get(i);
|
247 |
Map<String, Object> mValues = liste.get(i);
|
249 |
// System.err.println(mValues);
|
248 |
// System.err.println(mValues);
|
250 |
|
249 |
|
251 |
String styleName = null;
|
250 |
String styleName = null;
|
252 |
int nbCellule = 1;
|
251 |
int nbCellule = 1;
|
253 |
// on remplit chaque cellule de la ligne
|
252 |
// on remplit chaque cellule de la ligne
|
254 |
for (Iterator j = listElts.iterator(); j.hasNext();) {
|
253 |
for (Iterator j = listElts.iterator(); j.hasNext();) {
|
255 |
|
254 |
|
256 |
if ((currentLine - 1 + fill(testPoint, "test", sheet, false, null, null, true)) > (endPageLine * nbPage)) {
|
255 |
if ((currentLine - 1 + fill(testPoint, "test", sheet, false, null, null, true)) > (endPageLine * nbPage)) {
|
257 |
currentLine = currentLineTmp + endPageLine;
|
256 |
currentLine = currentLineTmp + endPageLine;
|
258 |
currentLineTmp = currentLine;
|
257 |
currentLineTmp = currentLine;
|
259 |
nbPage++;
|
258 |
nbPage++;
|
260 |
}
|
259 |
}
|
261 |
|
260 |
|
262 |
Element e = (Element) j.next();
|
261 |
Element e = (Element) j.next();
|
263 |
String loc = e.getAttributeValue("location").trim() + currentLine;
|
262 |
String loc = e.getAttributeValue("location").trim() + currentLine;
|
264 |
boolean controlLine = e.getAttributeValue("controleMultiline") == null ? true : !e.getAttributeValue("controleMultiline").equalsIgnoreCase("false");
|
263 |
boolean controlLine = e.getAttributeValue("controleMultiline") == null ? true : !e.getAttributeValue("controleMultiline").equalsIgnoreCase("false");
|
265 |
// Type normaux fill ou replace
|
264 |
// Type normaux fill ou replace
|
266 |
if (e.getAttributeValue("type").equalsIgnoreCase("fill") || e.getAttributeValue("type").equalsIgnoreCase("replace")) {
|
265 |
if (e.getAttributeValue("type").equalsIgnoreCase("fill") || e.getAttributeValue("type").equalsIgnoreCase("replace")) {
|
267 |
|
266 |
|
268 |
Object value = getElementValue(e, mValues);
|
267 |
Object value = getElementValue(e, mValues);
|
269 |
Point resolveHint = sheet.resolveHint(loc);
|
268 |
Point resolveHint = sheet.resolveHint(loc);
|
270 |
if (e.getAttributeValue("location").trim().equals(columnSousTotal)) {
|
269 |
if (e.getAttributeValue("location").trim().equals(columnSousTotal)) {
|
271 |
if (o != null) {
|
270 |
if (o != null) {
|
272 |
if (!o.equals(value)) {
|
271 |
if (!o.equals(value)) {
|
273 |
for (String object : mapSousTotal.keySet()) {
|
272 |
for (String object : mapSousTotal.keySet()) {
|
274 |
System.err.println(object + " = " + mapSousTotal.get(object));
|
273 |
System.err.println(object + " = " + mapSousTotal.get(object));
|
275 |
String styleSousTotalName = "Titre 1";
|
274 |
String styleSousTotalName = "Titre 1";
|
276 |
if (style != null && style.get(i) != null) {
|
275 |
if (style != null && style.get(i) != null) {
|
277 |
styleSousTotalName = style.get(i);
|
276 |
styleSousTotalName = style.get(i);
|
278 |
}
|
277 |
}
|
279 |
Map<Integer, String> mTmp = mapStyle.get(styleSousTotalName);
|
278 |
Map<Integer, String> mTmp = mapStyle.get(styleSousTotalName);
|
280 |
String styleOO = null;
|
279 |
String styleOO = null;
|
281 |
String styleOOA = null;
|
280 |
String styleOOA = null;
|
282 |
if (mTmp != null) {
|
281 |
if (mTmp != null) {
|
283 |
|
282 |
|
284 |
Object oTmp = mTmp.get(Integer.valueOf(resolveHint.x));
|
283 |
Object oTmp = mTmp.get(Integer.valueOf(resolveHint.x));
|
285 |
styleOO = oTmp == null ? null : oTmp.toString();
|
284 |
styleOO = oTmp == null ? null : oTmp.toString();
|
286 |
Object oTmpA = mTmp.get(Integer.valueOf(0));
|
285 |
Object oTmpA = mTmp.get(Integer.valueOf(0));
|
287 |
styleOOA = oTmpA == null ? null : oTmpA.toString();
|
286 |
styleOOA = oTmpA == null ? null : oTmpA.toString();
|
288 |
}
|
287 |
}
|
289 |
fill(test ? testPoint : sheet.resolveHint("A" + currentLine), "Sous total", sheet, false, null, styleOOA, test, controlLine);
|
288 |
fill(test ? testPoint : sheet.resolveHint("A" + currentLine), "Sous total", sheet, false, null, styleOOA, test, controlLine);
|
290 |
fill(test ? testPoint : sheet.resolveHint(object + "" + currentLine), mapSousTotal.get(object), sheet, false, null, styleOO, test, controlLine);
|
289 |
fill(test ? testPoint : sheet.resolveHint(object + "" + currentLine), mapSousTotal.get(object), sheet, false, null, styleOO, test, controlLine);
|
291 |
}
|
290 |
}
|
292 |
mapSousTotal.clear();
|
291 |
mapSousTotal.clear();
|
293 |
currentLine += 2;
|
292 |
currentLine += 2;
|
294 |
loc = e.getAttributeValue("location").trim() + currentLine;
|
293 |
loc = e.getAttributeValue("location").trim() + currentLine;
|
295 |
resolveHint = sheet.resolveHint(loc);
|
294 |
resolveHint = sheet.resolveHint(loc);
|
296 |
o = value;
|
295 |
o = value;
|
297 |
}
|
296 |
}
|
298 |
} else {
|
297 |
} else {
|
299 |
o = value;
|
298 |
o = value;
|
300 |
}
|
299 |
}
|
301 |
}
|
300 |
}
|
302 |
if (value instanceof Number) {
|
301 |
if (value instanceof Number) {
|
303 |
final String attributeValue = e.getAttributeValue("total");
|
302 |
final String attributeValue = e.getAttributeValue("total");
|
304 |
if (attributeValue != null && attributeValue.equalsIgnoreCase("true")) {
|
303 |
if (attributeValue != null && attributeValue.equalsIgnoreCase("true")) {
|
305 |
incrementTotal(e.getAttributeValue("location"), (Number) value, mapTotal);
|
304 |
incrementTotal(e.getAttributeValue("location"), (Number) value, mapTotal);
|
306 |
}
|
305 |
}
|
307 |
|
306 |
|
308 |
final String attributeValue2 = e.getAttributeValue("sousTotal");
|
307 |
final String attributeValue2 = e.getAttributeValue("sousTotal");
|
309 |
if (attributeValue2 != null && attributeValue2.equalsIgnoreCase("true")) {
|
308 |
if (attributeValue2 != null && attributeValue2.equalsIgnoreCase("true")) {
|
310 |
incrementTotal(e.getAttributeValue("location"), (Number) value, mapSousTotal);
|
309 |
incrementTotal(e.getAttributeValue("location"), (Number) value, mapSousTotal);
|
311 |
}
|
310 |
}
|
312 |
}
|
311 |
}
|
313 |
boolean replace = e.getAttributeValue("type").equalsIgnoreCase("replace");
|
312 |
boolean replace = e.getAttributeValue("type").equalsIgnoreCase("replace");
|
314 |
|
313 |
|
315 |
if (test || sheet.isCellValid(resolveHint.x, resolveHint.y)) {
|
314 |
if (test || sheet.isCellValid(resolveHint.x, resolveHint.y)) {
|
316 |
if (style != null) {
|
315 |
if (style != null) {
|
317 |
styleName = style.get(i);
|
316 |
styleName = style.get(i);
|
318 |
}
|
317 |
}
|
319 |
Map mTmp = styleName == null ? null : (Map) mapStyle.get(styleName);
|
318 |
Map mTmp = styleName == null ? null : (Map) mapStyle.get(styleName);
|
320 |
String styleOO = null;
|
319 |
String styleOO = null;
|
321 |
if (mTmp != null) {
|
320 |
if (mTmp != null) {
|
322 |
|
321 |
|
323 |
Object oTmp = mTmp.get(new Integer(resolveHint.x));
|
322 |
Object oTmp = mTmp.get(new Integer(resolveHint.x));
|
324 |
styleOO = oTmp == null ? null : oTmp.toString();
|
323 |
styleOO = oTmp == null ? null : oTmp.toString();
|
325 |
// System.err.println("Set style " + styleOO);
|
324 |
// System.err.println("Set style " + styleOO);
|
326 |
}
|
325 |
}
|
327 |
|
326 |
|
328 |
int tmpCelluleAffect = fill(test ? testPoint : resolveHint, value, sheet, replace, null, styleOO, test, controlLine);
|
327 |
int tmpCelluleAffect = fill(test ? testPoint : resolveHint, value, sheet, replace, null, styleOO, test, controlLine);
|
329 |
nbCellule = Math.max(nbCellule, tmpCelluleAffect);
|
328 |
nbCellule = Math.max(nbCellule, tmpCelluleAffect);
|
330 |
} else {
|
329 |
} else {
|
331 |
System.err.println("Cell not valid at " + loc);
|
330 |
System.err.println("Cell not valid at " + loc);
|
332 |
}
|
331 |
}
|
333 |
}
|
332 |
}
|
334 |
}
|
333 |
}
|
335 |
currentLine += nbCellule;
|
334 |
currentLine += nbCellule;
|
336 |
|
335 |
|
337 |
}
|
336 |
}
|
338 |
for (String object : mapSousTotal.keySet()) {
|
337 |
for (String object : mapSousTotal.keySet()) {
|
339 |
System.err.println(object + " = " + mapSousTotal.get(object));
|
338 |
System.err.println(object + " = " + mapSousTotal.get(object));
|
340 |
Map<Integer, String> mTmp = mapStyle.get("Titre 1");
|
339 |
Map<Integer, String> mTmp = mapStyle.get("Titre 1");
|
341 |
String styleOO = null;
|
340 |
String styleOO = null;
|
342 |
String styleOOA = null;
|
341 |
String styleOOA = null;
|
343 |
if (mTmp != null) {
|
342 |
if (mTmp != null) {
|
344 |
|
343 |
|
345 |
Object oTmp = mTmp.get(Integer.valueOf(sheet.resolveHint(object + "" + currentLine).x));
|
344 |
Object oTmp = mTmp.get(Integer.valueOf(sheet.resolveHint(object + "" + currentLine).x));
|
346 |
styleOO = oTmp == null ? null : oTmp.toString();
|
345 |
styleOO = oTmp == null ? null : oTmp.toString();
|
347 |
Object oTmpA = mTmp.get(Integer.valueOf(0));
|
346 |
Object oTmpA = mTmp.get(Integer.valueOf(0));
|
348 |
styleOOA = oTmpA == null ? null : oTmpA.toString();
|
347 |
styleOOA = oTmpA == null ? null : oTmpA.toString();
|
349 |
}
|
348 |
}
|
350 |
|
349 |
|
351 |
fill(test ? testPoint : sheet.resolveHint("A" + currentLine), "Sous total", sheet, false, null, styleOOA, test);
|
350 |
fill(test ? testPoint : sheet.resolveHint("A" + currentLine), "Sous total", sheet, false, null, styleOOA, test);
|
352 |
fill(test ? testPoint : sheet.resolveHint(object + "" + currentLine), mapSousTotal.get(object), sheet, false, null, styleOO, test);
|
351 |
fill(test ? testPoint : sheet.resolveHint(object + "" + currentLine), mapSousTotal.get(object), sheet, false, null, styleOO, test);
|
353 |
}
|
352 |
}
|
354 |
for (String object : mapTotal.keySet()) {
|
353 |
for (String object : mapTotal.keySet()) {
|
355 |
System.err.println(object + " = " + mapTotal.get(object));
|
354 |
System.err.println(object + " = " + mapTotal.get(object));
|
356 |
Map<Integer, String> mTmp = mapStyle.get("Titre 1");
|
355 |
Map<Integer, String> mTmp = mapStyle.get("Titre 1");
|
357 |
String styleOO = null;
|
356 |
String styleOO = null;
|
358 |
String styleOOA = null;
|
357 |
String styleOOA = null;
|
359 |
if (mTmp != null) {
|
358 |
if (mTmp != null) {
|
360 |
|
359 |
|
361 |
Object oTmp = mTmp.get(Integer.valueOf(sheet.resolveHint(object + "" + (currentLine + 1)).x));
|
360 |
Object oTmp = mTmp.get(Integer.valueOf(sheet.resolveHint(object + "" + (currentLine + 1)).x));
|
362 |
styleOO = oTmp == null ? null : oTmp.toString();
|
361 |
styleOO = oTmp == null ? null : oTmp.toString();
|
363 |
Object oTmpA = mTmp.get(Integer.valueOf(0));
|
362 |
Object oTmpA = mTmp.get(Integer.valueOf(0));
|
364 |
styleOOA = oTmpA == null ? null : oTmpA.toString();
|
363 |
styleOOA = oTmpA == null ? null : oTmpA.toString();
|
365 |
}
|
364 |
}
|
366 |
fill(test ? testPoint : sheet.resolveHint("A" + (currentLine + 1)), "Total", sheet, false, null, styleOOA, test);
|
365 |
fill(test ? testPoint : sheet.resolveHint("A" + (currentLine + 1)), "Total", sheet, false, null, styleOOA, test);
|
367 |
fill(test ? testPoint : sheet.resolveHint(object + "" + (currentLine + 1)), mapTotal.get(object), sheet, false, null, styleOO, test);
|
366 |
fill(test ? testPoint : sheet.resolveHint(object + "" + (currentLine + 1)), mapTotal.get(object), sheet, false, null, styleOO, test);
|
368 |
}
|
367 |
}
|
369 |
return nbPage;
|
368 |
return nbPage;
|
370 |
}
|
369 |
}
|
371 |
|
370 |
|
372 |
private static void incrementTotal(String field, Number value, Map<String, Double> map) {
|
371 |
private static void incrementTotal(String field, Number value, Map<String, Double> map) {
|
373 |
Double d = map.get(field);
|
372 |
Double d = map.get(field);
|
374 |
if (d == null) {
|
373 |
if (d == null) {
|
375 |
map.put(field, value.doubleValue());
|
374 |
map.put(field, value.doubleValue());
|
376 |
} else {
|
375 |
} else {
|
377 |
map.put(field, d + value.doubleValue());
|
376 |
map.put(field, d + value.doubleValue());
|
378 |
}
|
377 |
}
|
379 |
}
|
378 |
}
|
380 |
|
379 |
|
381 |
private static Object getElementValue(Element elt, Map<String, Object> mValues) {
|
380 |
private static Object getElementValue(Element elt, Map<String, Object> mValues) {
|
382 |
Object res = "";
|
381 |
Object res = "";
|
383 |
|
382 |
|
384 |
final List eltFields = elt.getChildren("field");
|
383 |
final List eltFields = elt.getChildren("field");
|
385 |
|
384 |
|
386 |
if (eltFields != null) {
|
385 |
if (eltFields != null) {
|
387 |
if (eltFields.size() > 1) {
|
386 |
if (eltFields.size() > 1) {
|
388 |
String result = "";
|
387 |
String result = "";
|
389 |
for (Iterator j = eltFields.iterator(); j.hasNext();) {
|
388 |
for (Iterator j = eltFields.iterator(); j.hasNext();) {
|
390 |
Object o = getValueOfComposant((Element) j.next(), mValues);
|
389 |
Object o = getValueOfComposant((Element) j.next(), mValues);
|
391 |
if (o != null) {
|
390 |
if (o != null) {
|
392 |
result += o.toString() + " ";
|
391 |
result += o.toString() + " ";
|
393 |
}
|
392 |
}
|
394 |
}
|
393 |
}
|
395 |
res = result;
|
394 |
res = result;
|
396 |
} else {
|
395 |
} else {
|
397 |
res = getValueOfComposant((Element) eltFields.get(0), mValues);
|
396 |
res = getValueOfComposant((Element) eltFields.get(0), mValues);
|
398 |
}
|
397 |
}
|
399 |
}
|
398 |
}
|
400 |
res = resizeValue(elt, res);
|
399 |
res = resizeValue(elt, res);
|
401 |
return res;
|
400 |
return res;
|
402 |
}
|
401 |
}
|
403 |
|
402 |
|
404 |
private static Object resizeValue(Element elt, Object res) {
|
403 |
private static Object resizeValue(Element elt, Object res) {
|
405 |
{
|
404 |
{
|
406 |
String attributeValueMaxChar = elt.getAttributeValue("maxChar");
|
405 |
String attributeValueMaxChar = elt.getAttributeValue("maxChar");
|
407 |
if (attributeValueMaxChar != null) {
|
406 |
if (attributeValueMaxChar != null) {
|
408 |
int maxChar = Integer.valueOf(attributeValueMaxChar);
|
407 |
int maxChar = Integer.valueOf(attributeValueMaxChar);
|
409 |
if (res != null && res.toString().length() > maxChar) {
|
408 |
if (res != null && res.toString().length() > maxChar) {
|
410 |
res = res.toString().substring(0, maxChar);
|
409 |
res = res.toString().substring(0, maxChar);
|
411 |
}
|
410 |
}
|
412 |
}
|
411 |
}
|
413 |
}
|
412 |
}
|
414 |
{
|
413 |
{
|
415 |
String attributeValueCellSize = elt.getAttributeValue("cellSize");
|
414 |
String attributeValueCellSize = elt.getAttributeValue("cellSize");
|
416 |
if (attributeValueCellSize != null) {
|
415 |
if (attributeValueCellSize != null) {
|
417 |
int size = Integer.valueOf(attributeValueCellSize);
|
416 |
int size = Integer.valueOf(attributeValueCellSize);
|
418 |
if (res != null && res.toString().length() > size) {
|
417 |
if (res != null && res.toString().length() > size) {
|
419 |
try {
|
418 |
try {
|
420 |
res = StringUtils.splitString(res.toString(), size);
|
419 |
res = StringUtils.splitString(res.toString(), size);
|
421 |
} catch (NumberFormatException e) {
|
420 |
} catch (NumberFormatException e) {
|
422 |
e.printStackTrace();
|
421 |
e.printStackTrace();
|
423 |
}
|
422 |
}
|
424 |
|
423 |
|
425 |
}
|
424 |
}
|
426 |
}
|
425 |
}
|
427 |
}
|
426 |
}
|
428 |
return res;
|
427 |
return res;
|
429 |
}
|
428 |
}
|
430 |
|
429 |
|
431 |
/**
|
430 |
/**
|
432 |
* permet d'obtenir la valeur d'un élément field
|
431 |
* permet d'obtenir la valeur d'un élément field
|
433 |
*
|
432 |
*
|
434 |
* @param eltField
|
433 |
* @param eltField
|
435 |
* @param row
|
434 |
* @param row
|
436 |
* @param elt
|
435 |
* @param elt
|
437 |
* @param id
|
436 |
* @param id
|
438 |
* @return value of composant
|
437 |
* @return value of composant
|
439 |
*/
|
438 |
*/
|
440 |
private static Object getValueOfComposant(Element eltField, Map<String, Object> mValues) {
|
439 |
private static Object getValueOfComposant(Element eltField, Map<String, Object> mValues) {
|
441 |
|
440 |
|
442 |
String field = eltField.getAttributeValue("name");
|
441 |
String field = eltField.getAttributeValue("name");
|
443 |
|
442 |
|
444 |
return mValues.get(field);
|
443 |
return mValues.get(field);
|
445 |
}
|
444 |
}
|
446 |
|
445 |
|
447 |
private static int fill(Point resolveHint, Object value, Sheet sheet, boolean replace, String replacePattern, String styleOO, boolean test) {
|
446 |
private static int fill(Point resolveHint, Object value, Sheet sheet, boolean replace, String replacePattern, String styleOO, boolean test) {
|
448 |
return fill(resolveHint, value, sheet, replace, replacePattern, styleOO, test, true);
|
447 |
return fill(resolveHint, value, sheet, replace, replacePattern, styleOO, test, true);
|
449 |
}
|
448 |
}
|
450 |
|
449 |
|
451 |
/**
|
450 |
/**
|
452 |
* Permet de remplir une cellule
|
451 |
* Permet de remplir une cellule
|
453 |
*
|
452 |
*
|
454 |
* @param location position de la cellule exemple : A3
|
453 |
* @param location position de la cellule exemple : A3
|
455 |
* @param value valeur à insérer dans la cellule
|
454 |
* @param value valeur à insérer dans la cellule
|
456 |
* @param sheet feuille sur laquelle on travaille
|
455 |
* @param sheet feuille sur laquelle on travaille
|
457 |
* @param replace efface ou non le contenu original de la cellule
|
456 |
* @param replace efface ou non le contenu original de la cellule
|
458 |
* @param styleOO style à appliquer
|
457 |
* @param styleOO style à appliquer
|
459 |
*/
|
458 |
*/
|
460 |
private static int fill(Point resolveHint, Object value, Sheet sheet, boolean replace, String replacePattern, String styleOO, boolean test, boolean controlLine) {
|
459 |
private static int fill(Point resolveHint, Object value, Sheet sheet, boolean replace, String replacePattern, String styleOO, boolean test, boolean controlLine) {
|
461 |
|
460 |
|
462 |
int nbCellule = 1;
|
461 |
int nbCellule = 1;
|
463 |
// est ce que la cellule est valide
|
462 |
// est ce que la cellule est valide
|
464 |
if (test || sheet.isCellValid(resolveHint.x, resolveHint.y)) {
|
463 |
if (test || sheet.isCellValid(resolveHint.x, resolveHint.y)) {
|
465 |
|
464 |
|
466 |
// on divise en 2 cellules si il y a des retours à la ligne
|
465 |
// on divise en 2 cellules si il y a des retours à la ligne
|
467 |
if (controlLine && (value != null && value.toString().indexOf('\n') >= 0)) {
|
466 |
if (controlLine && (value != null && value.toString().indexOf('\n') >= 0)) {
|
468 |
String[] values = value.toString().split("\n");
|
467 |
String[] values = value.toString().split("\n");
|
469 |
if (!test) {
|
468 |
if (!test) {
|
470 |
|
469 |
|
471 |
int y = 0;
|
470 |
int y = 0;
|
472 |
for (String string : values) {
|
471 |
for (String string : values) {
|
473 |
if (string != null && string.trim().length() != 0) {
|
472 |
if (string != null && string.trim().length() != 0) {
|
474 |
try {
|
473 |
try {
|
475 |
MutableCell c = sheet.getCellAt(resolveHint.x, resolveHint.y + y);
|
474 |
MutableCell c = sheet.getCellAt(resolveHint.x, resolveHint.y + y);
|
476 |
setCellValue(c, string, replace, replacePattern);
|
475 |
setCellValue(c, string, replace, replacePattern);
|
477 |
if (styleOO != null) {
|
476 |
if (styleOO != null) {
|
478 |
c.setStyleName(styleOO);
|
477 |
c.setStyleName(styleOO);
|
479 |
}
|
478 |
}
|
480 |
y++;
|
479 |
y++;
|
481 |
} catch (IllegalArgumentException e) {
|
480 |
} catch (IllegalArgumentException e) {
|
482 |
SwingUtilities.invokeLater(new Runnable() {
|
481 |
SwingUtilities.invokeLater(new Runnable() {
|
483 |
|
482 |
|
484 |
@Override
|
483 |
@Override
|
485 |
public void run() {
|
484 |
public void run() {
|
486 |
System.err.println(resolveHint + " : " + value);
|
485 |
System.err.println(resolveHint + " : " + value);
|
487 |
JOptionPane.showMessageDialog(null, "La cellule " + resolveHint + " n'existe pas ou est fusionnée.", "Erreur pendant la génération", JOptionPane.ERROR_MESSAGE);
|
486 |
JOptionPane.showMessageDialog(null, "La cellule " + resolveHint + " n'existe pas ou est fusionnée.", "Erreur pendant la génération", JOptionPane.ERROR_MESSAGE);
|
488 |
}
|
487 |
}
|
489 |
});
|
488 |
});
|
490 |
}
|
489 |
}
|
491 |
}
|
490 |
}
|
492 |
}
|
491 |
}
|
493 |
}
|
492 |
}
|
494 |
|
493 |
|
495 |
nbCellule = values.length;
|
494 |
nbCellule = values.length;
|
496 |
} else {
|
495 |
} else {
|
497 |
if (!test) {
|
496 |
if (!test) {
|
498 |
MutableCell cell = sheet.getCellAt(resolveHint.x, resolveHint.y);
|
497 |
MutableCell cell = sheet.getCellAt(resolveHint.x, resolveHint.y);
|
499 |
// application de la valeur
|
498 |
// application de la valeur
|
500 |
setCellValue(cell, value, replace, replacePattern);
|
499 |
setCellValue(cell, value, replace, replacePattern);
|
501 |
|
500 |
|
502 |
// Application du style
|
501 |
// Application du style
|
503 |
if (styleOO != null) {
|
502 |
if (styleOO != null) {
|
504 |
cell.setStyleName(styleOO);
|
503 |
cell.setStyleName(styleOO);
|
505 |
}
|
504 |
}
|
506 |
}
|
505 |
}
|
507 |
}
|
506 |
}
|
508 |
}
|
507 |
}
|
509 |
return nbCellule;
|
508 |
return nbCellule;
|
510 |
}
|
509 |
}
|
511 |
|
510 |
|
512 |
/**
|
511 |
/**
|
513 |
* remplit une cellule
|
512 |
* remplit une cellule
|
514 |
*
|
513 |
*
|
515 |
* @param cell
|
514 |
* @param cell
|
516 |
* @param value
|
515 |
* @param value
|
517 |
* @param replace
|
516 |
* @param replace
|
518 |
*/
|
517 |
*/
|
519 |
private static void setCellValue(MutableCell cell, Object value, boolean replace, String replacePattern) {
|
518 |
private static void setCellValue(MutableCell cell, Object value, boolean replace, String replacePattern) {
|
520 |
if (value == null) {
|
519 |
if (value == null) {
|
521 |
value = "";
|
520 |
value = "";
|
522 |
}
|
521 |
}
|
523 |
|
522 |
|
524 |
if (replace) {
|
523 |
if (replace) {
|
525 |
if (replacePattern != null) {
|
524 |
if (replacePattern != null) {
|
526 |
cell.replaceBy(replacePattern, value.toString());
|
525 |
cell.replaceBy(replacePattern, value.toString());
|
527 |
} else {
|
526 |
} else {
|
528 |
cell.replaceBy("_", value.toString());
|
527 |
cell.replaceBy("_", value.toString());
|
529 |
}
|
528 |
}
|
530 |
} else {
|
529 |
} else {
|
531 |
cell.setValue(value);
|
530 |
cell.setValue(value);
|
532 |
}
|
531 |
}
|
533 |
}
|
532 |
}
|
534 |
|
533 |
|
535 |
/**
|
534 |
/**
|
536 |
* Sauver le document au format OpenOffice. Si le fichier existe déjà, le fichier existant sera
|
535 |
* Sauver le document au format OpenOffice. Si le fichier existe déjà, le fichier existant sera
|
537 |
* renommé sous la forme nomFic_1.sxc.
|
536 |
* renommé sous la forme nomFic_1.sxc.
|
538 |
*
|
537 |
*
|
539 |
* @param ssheet SpreadSheet à sauvegarder
|
538 |
* @param ssheet SpreadSheet à sauvegarder
|
540 |
* @param pathDest répertoire de destination du fichier
|
539 |
* @param pathDest répertoire de destination du fichier
|
541 |
* @param fileName nom du fichier à créer
|
540 |
* @param fileName nom du fichier à créer
|
542 |
* @return un File pointant sur le fichier créé
|
541 |
* @return un File pointant sur le fichier créé
|
543 |
* @throws IOException
|
542 |
* @throws IOException
|
544 |
*/
|
543 |
*/
|
545 |
private static File saveSpreadSheet(SpreadSheet ssheet, File pathDest, String fileName, String templateId, SQLRow rowLanguage) throws IOException {
|
544 |
private static File saveSpreadSheet(SpreadSheet ssheet, File pathDest, String fileName, String templateId, SQLRow rowLanguage) throws IOException {
|
546 |
|
545 |
|
547 |
// Test des arguments
|
546 |
// Test des arguments
|
548 |
if (ssheet == null || pathDest == null || fileName.trim().length() == 0) {
|
547 |
if (ssheet == null || pathDest == null || fileName.trim().length() == 0) {
|
549 |
throw new IllegalArgumentException();
|
548 |
throw new IllegalArgumentException();
|
550 |
}
|
549 |
}
|
551 |
|
550 |
|
552 |
// Renommage du fichier si il existe déja
|
551 |
// Renommage du fichier si il existe déja
|
553 |
File fDest = new File(pathDest, fileName + ".ods");
|
552 |
File fDest = new File(pathDest, fileName + ".ods");
|
554 |
|
553 |
|
555 |
if (!pathDest.exists()) {
|
554 |
if (!pathDest.exists()) {
|
556 |
pathDest.mkdirs();
|
555 |
pathDest.mkdirs();
|
557 |
}
|
556 |
}
|
558 |
|
557 |
|
559 |
SheetUtils.convertToOldFile(((ComptaPropsConfiguration) Configuration.getInstance()).getRootSociete(), fileName, pathDest, fDest);
|
558 |
SheetUtils.convertToOldFile(((ComptaPropsConfiguration) Configuration.getInstance()).getRootSociete(), fileName, pathDest, fDest);
|
560 |
|
559 |
|
561 |
// Sauvegarde
|
560 |
// Sauvegarde
|
562 |
try {
|
561 |
try {
|
563 |
ssheet.saveAs(fDest);
|
562 |
ssheet.saveAs(fDest);
|
564 |
} catch (FileNotFoundException e) {
|
563 |
} catch (FileNotFoundException e) {
|
565 |
final File F = fDest;
|
564 |
final File F = fDest;
|
566 |
SwingUtilities.invokeLater(new Runnable() {
|
565 |
SwingUtilities.invokeLater(new Runnable() {
|
567 |
public void run() {
|
566 |
public void run() {
|
568 |
try {
|
567 |
try {
|
569 |
JOptionPane.showMessageDialog(null, "Le fichier " + F.getCanonicalPath() + " n'a pu être créé. \n Vérifiez qu'il n'est pas déjà ouvert.");
|
568 |
JOptionPane.showMessageDialog(null, "Le fichier " + F.getCanonicalPath() + " n'a pu être créé. \n Vérifiez qu'il n'est pas déjà ouvert.");
|
570 |
} catch (IOException e) {
|
569 |
} catch (IOException e) {
|
571 |
e.printStackTrace();
|
570 |
e.printStackTrace();
|
572 |
}
|
571 |
}
|
573 |
}
|
572 |
}
|
574 |
});
|
573 |
});
|
575 |
|
574 |
|
576 |
e.printStackTrace();
|
575 |
e.printStackTrace();
|
577 |
}
|
576 |
}
|
578 |
|
577 |
|
579 |
// Copie de l'odsp
|
578 |
// Copie de l'odsp
|
580 |
File odspOut = new File(pathDest, fileName + ".odsp");
|
579 |
File odspOut = new File(pathDest, fileName + ".odsp");
|
581 |
try (final InputStream odspIn = TemplateManager.getInstance().getTemplatePrintConfiguration(templateId, rowLanguage != null ? rowLanguage.getString("CHEMIN") : null, null);) {
|
580 |
try (final InputStream odspIn = TemplateManager.getInstance().getTemplatePrintConfiguration(templateId, rowLanguage != null ? rowLanguage.getString("CHEMIN") : null, null);) {
|
582 |
if (odspIn != null) {
|
581 |
if (odspIn != null) {
|
583 |
StreamUtils.copy(odspIn, odspOut);
|
582 |
StreamUtils.copy(odspIn, odspOut);
|
584 |
}
|
583 |
}
|
585 |
} catch (FileNotFoundException e) {
|
584 |
} catch (FileNotFoundException e) {
|
586 |
System.err.println("Le fichier odsp n'existe pas.");
|
585 |
System.err.println("Le fichier odsp n'existe pas.");
|
587 |
}
|
586 |
}
|
588 |
|
587 |
|
589 |
return fDest;
|
588 |
return fDest;
|
590 |
}
|
589 |
}
|
591 |
|
590 |
|
592 |
/**
|
591 |
/**
|
593 |
* parcourt l'ensemble de la feuille pour trouver les style définit
|
592 |
* parcourt l'ensemble de la feuille pour trouver les style définit
|
594 |
*/
|
593 |
*/
|
595 |
private static Map<String, Map<Integer, String>> searchStyle(Sheet sheet, int colEnd, int rowEnd) {
|
594 |
private static Map<String, Map<Integer, String>> searchStyle(Sheet sheet, int colEnd, int rowEnd) {
|
596 |
|
595 |
|
597 |
if (cacheStyle.get(sheet) != null) {
|
596 |
if (cacheStyle.get(sheet) != null) {
|
598 |
return cacheStyle.get(sheet);
|
597 |
return cacheStyle.get(sheet);
|
599 |
}
|
598 |
}
|
600 |
|
599 |
|
601 |
Map<String, Map<Integer, String>> mapStyleDef = StyleSQLElement.getMapAllStyle();
|
600 |
Map<String, Map<Integer, String>> mapStyleDef = StyleSQLElement.getMapAllStyle();
|
602 |
|
601 |
|
603 |
Map<String, Map<Integer, String>> mapStyleFounded = new HashMap<String, Map<Integer, String>>();
|
602 |
Map<String, Map<Integer, String>> mapStyleFounded = new HashMap<String, Map<Integer, String>>();
|
604 |
|
603 |
|
605 |
// on parcourt chaque ligne de la feuille pour recuperer les styles
|
604 |
// on parcourt chaque ligne de la feuille pour recuperer les styles
|
606 |
int columnCount = (colEnd == -1) ? sheet.getColumnCount() : (colEnd + 1);
|
605 |
int columnCount = (colEnd == -1) ? sheet.getColumnCount() : (colEnd + 1);
|
607 |
System.err.println("End column search : " + columnCount);
|
606 |
System.err.println("End column search : " + columnCount);
|
608 |
|
607 |
|
609 |
int rowCount = (rowEnd > 0) ? rowEnd : sheet.getRowCount();
|
608 |
int rowCount = (rowEnd > 0) ? rowEnd : sheet.getRowCount();
|
610 |
|
609 |
|
611 |
System.err.println("End row search : " + rowCount);
|
610 |
System.err.println("End row search : " + rowCount);
|
612 |
for (int i = 0; i < rowCount && (mapStyleDef.keySet().size() - 2) > mapStyleFounded.keySet().size(); i++) {
|
611 |
for (int i = 0; i < rowCount && (mapStyleDef.keySet().size() - 2) > mapStyleFounded.keySet().size(); i++) {
|
613 |
|
612 |
|
614 |
int x = 0;
|
613 |
int x = 0;
|
615 |
Map<Integer, String> mapCellStyle = new HashMap<Integer, String>();
|
614 |
Map<Integer, String> mapCellStyle = new HashMap<Integer, String>();
|
616 |
String style = "";
|
615 |
String style = "";
|
617 |
|
616 |
|
618 |
for (int j = 0; j < columnCount; j++) {
|
617 |
for (int j = 0; j < columnCount; j++) {
|
619 |
|
618 |
|
620 |
try {
|
619 |
try {
|
621 |
if (sheet.isCellValid(j, i)) {
|
620 |
if (sheet.isCellValid(j, i)) {
|
622 |
|
621 |
|
623 |
MutableCell c = sheet.getCellAt(j, i);
|
622 |
MutableCell c = sheet.getCellAt(j, i);
|
624 |
String cellStyle = c.getStyleName();
|
623 |
String cellStyle = c.getStyleName();
|
625 |
|
624 |
|
626 |
try {
|
625 |
try {
|
627 |
if (mapStyleDef.containsKey(c.getValue().toString())) {
|
626 |
if (mapStyleDef.containsKey(c.getValue().toString())) {
|
628 |
style = c.getValue().toString();
|
627 |
style = c.getValue().toString();
|
629 |
}
|
628 |
}
|
630 |
} catch (IllegalStateException e) {
|
629 |
} catch (IllegalStateException e) {
|
631 |
e.printStackTrace();
|
630 |
e.printStackTrace();
|
632 |
}
|
631 |
}
|
633 |
mapCellStyle.put(Integer.valueOf(x), cellStyle);
|
632 |
mapCellStyle.put(Integer.valueOf(x), cellStyle);
|
634 |
if (style.trim().length() > 0) {
|
633 |
if (style.trim().length() > 0) {
|
635 |
c.clearValue();
|
634 |
c.clearValue();
|
636 |
if (!style.trim().equalsIgnoreCase("Normal") && mapStyleDef.get("Normal") != null) {
|
635 |
if (!style.trim().equalsIgnoreCase("Normal") && mapStyleDef.get("Normal") != null) {
|
637 |
String styleCell = mapStyleDef.get("Normal").get(Integer.valueOf(x));
|
636 |
String styleCell = mapStyleDef.get("Normal").get(Integer.valueOf(x));
|
638 |
if (styleCell != null && styleCell.length() != 0) {
|
637 |
if (styleCell != null && styleCell.length() != 0) {
|
639 |
c.setStyleName(styleCell);
|
638 |
c.setStyleName(styleCell);
|
640 |
}
|
639 |
}
|
641 |
}
|
640 |
}
|
642 |
|
641 |
|
643 |
mapStyleFounded.put(style, mapCellStyle);
|
642 |
mapStyleFounded.put(style, mapCellStyle);
|
644 |
|
643 |
|
645 |
}
|
644 |
}
|
646 |
}
|
645 |
}
|
647 |
} catch (IndexOutOfBoundsException e) {
|
646 |
} catch (IndexOutOfBoundsException e) {
|
648 |
System.err.println("Index out of bounds Exception");
|
647 |
System.err.println("Index out of bounds Exception");
|
649 |
}
|
648 |
}
|
650 |
x++;
|
649 |
x++;
|
651 |
}
|
650 |
}
|
652 |
|
651 |
|
653 |
}
|
652 |
}
|
654 |
cacheStyle.put(sheet, mapStyleFounded);
|
653 |
cacheStyle.put(sheet, mapStyleFounded);
|
655 |
return mapStyleFounded;
|
654 |
return mapStyleFounded;
|
656 |
}
|
655 |
}
|
657 |
|
656 |
|
658 |
public static void main(String[] args) {
|
657 |
public static void main(String[] args) {
|
659 |
ComptaPropsConfiguration conf = ComptaPropsConfiguration.create();
|
658 |
ComptaPropsConfiguration conf = ComptaPropsConfiguration.create();
|
660 |
System.err.println("Conf created");
|
659 |
System.err.println("Conf created");
|
661 |
Configuration.setInstance(conf);
|
660 |
Configuration.setInstance(conf);
|
662 |
conf.setUpSocieteDataBaseConnexion(36);
|
661 |
conf.setUpSocieteDataBaseConnexion(36);
|
663 |
System.err.println("Connection Set up");
|
662 |
System.err.println("Connection Set up");
|
664 |
|
663 |
|
665 |
System.err.println("Start Genere");
|
664 |
System.err.println("Start Genere");
|
666 |
// genere("Devis", "C:\\", "Test", elt, 19);
|
665 |
// genere("Devis", "C:\\", "Test", elt, 19);
|
667 |
System.err.println("Stop genere");
|
666 |
System.err.println("Stop genere");
|
668 |
}
|
667 |
}
|
669 |
}
|
668 |
}
|