18 |
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 |
|
132 |
ilm |
16 |
import org.openconcerto.erp.core.common.element.StyleSQLElement;
|
|
|
17 |
import org.openconcerto.erp.core.finance.tax.model.TaxeCache;
|
|
|
18 |
import org.openconcerto.sql.Configuration;
|
|
|
19 |
import org.openconcerto.sql.model.DBRoot;
|
18 |
ilm |
20 |
import org.openconcerto.sql.model.SQLField;
|
|
|
21 |
import org.openconcerto.sql.model.SQLRow;
|
|
|
22 |
import org.openconcerto.sql.model.SQLRowAccessor;
|
|
|
23 |
import org.openconcerto.sql.model.SQLRowListRSH;
|
|
|
24 |
import org.openconcerto.sql.model.SQLRowValues;
|
132 |
ilm |
25 |
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
|
18 |
ilm |
26 |
import org.openconcerto.sql.model.SQLSelect;
|
156 |
ilm |
27 |
import org.openconcerto.sql.model.SQLSelectJoin;
|
18 |
ilm |
28 |
import org.openconcerto.sql.model.SQLTable;
|
|
|
29 |
import org.openconcerto.sql.model.Where;
|
91 |
ilm |
30 |
import org.openconcerto.utils.CompareUtils;
|
132 |
ilm |
31 |
import org.openconcerto.utils.ListMap;
|
|
|
32 |
import org.openconcerto.utils.cc.ITransformer;
|
18 |
ilm |
33 |
|
132 |
ilm |
34 |
import java.math.BigDecimal;
|
18 |
ilm |
35 |
import java.util.ArrayList;
|
91 |
ilm |
36 |
import java.util.Collections;
|
|
|
37 |
import java.util.Comparator;
|
18 |
ilm |
38 |
import java.util.HashMap;
|
|
|
39 |
import java.util.List;
|
|
|
40 |
import java.util.Map;
|
|
|
41 |
|
|
|
42 |
public class OOXMLCache {
|
63 |
ilm |
43 |
|
144 |
ilm |
44 |
private ITransformer<List<SQLRowAccessor>, List<SQLRowAccessor>> postProcess = null;
|
63 |
ilm |
45 |
private Map<SQLRowAccessor, Map<SQLTable, List<SQLRowAccessor>>> cacheReferent = new HashMap<SQLRowAccessor, Map<SQLTable, List<SQLRowAccessor>>>();
|
|
|
46 |
private Map<String, Map<Integer, SQLRowAccessor>> cacheForeign = new HashMap<String, Map<Integer, SQLRowAccessor>>();
|
|
|
47 |
|
144 |
ilm |
48 |
public void setPostProcess(ITransformer<List<SQLRowAccessor>, List<SQLRowAccessor>> postProcess) {
|
|
|
49 |
this.postProcess = postProcess;
|
|
|
50 |
}
|
|
|
51 |
|
63 |
ilm |
52 |
protected SQLRowAccessor getForeignRow(SQLRowAccessor row, SQLField field) {
|
18 |
ilm |
53 |
Map<Integer, SQLRowAccessor> c = cacheForeign.get(field.getName());
|
|
|
54 |
|
28 |
ilm |
55 |
if (row.getObject(field.getName()) == null) {
|
|
|
56 |
return null;
|
|
|
57 |
}
|
|
|
58 |
|
132 |
ilm |
59 |
int i = row.getForeignID(field.getName());
|
18 |
ilm |
60 |
|
|
|
61 |
if (c != null && c.get(i) != null) {
|
|
|
62 |
return c.get(i);
|
|
|
63 |
} else {
|
|
|
64 |
|
|
|
65 |
SQLRowAccessor foreign = row.getForeign(field.getName());
|
|
|
66 |
|
|
|
67 |
if (c == null) {
|
|
|
68 |
Map<Integer, SQLRowAccessor> map = new HashMap<Integer, SQLRowAccessor>();
|
|
|
69 |
map.put(i, foreign);
|
|
|
70 |
cacheForeign.put(field.getName(), map);
|
|
|
71 |
} else {
|
|
|
72 |
c.put(i, foreign);
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
return foreign;
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
}
|
|
|
79 |
|
144 |
ilm |
80 |
public List<? extends SQLRowAccessor> getReferentRows(List<? extends SQLRowAccessor> row, SQLTable tableForeign) {
|
132 |
ilm |
81 |
return getReferentRows(row, tableForeign, null, null, false, null, false);
|
18 |
ilm |
82 |
}
|
|
|
83 |
|
132 |
ilm |
84 |
protected List<? extends SQLRowAccessor> getReferentRows(List<? extends SQLRowAccessor> row, final SQLTable tableForeign, String groupBy, final String orderBy, boolean expandNomenclature,
|
|
|
85 |
String foreignField, boolean excludeQteZero) {
|
41 |
ilm |
86 |
Map<SQLTable, List<SQLRowAccessor>> c = cacheReferent.get(row.get(0));
|
18 |
ilm |
87 |
|
|
|
88 |
if (c != null && c.get(tableForeign) != null) {
|
|
|
89 |
System.err.println("get referent rows From Cache ");
|
|
|
90 |
return c.get(tableForeign);
|
|
|
91 |
} else {
|
|
|
92 |
List<SQLRowAccessor> list;
|
41 |
ilm |
93 |
if (row.isEmpty() || (row.size() > 0 && row.get(0).isUndefined())) {
|
18 |
ilm |
94 |
list = new ArrayList<SQLRowAccessor>();
|
41 |
ilm |
95 |
} else if (row.size() > 0 && (groupBy == null || groupBy.trim().length() == 0)) {
|
93 |
ilm |
96 |
|
41 |
ilm |
97 |
list = new ArrayList<SQLRowAccessor>();
|
144 |
ilm |
98 |
// SQLSelect sel = new SQLSelect();
|
|
|
99 |
// sel.addSelectStar(tableForeign);
|
|
|
100 |
SQLRowValues rowValsToFetch = new SQLRowValues(tableForeign);
|
|
|
101 |
rowValsToFetch.putNulls(tableForeign.getFieldsName());
|
|
|
102 |
if (tableForeign.getFieldsName().contains("ID_ARTICLE")) {
|
|
|
103 |
SQLRowValues rowValsToFetchArt = new SQLRowValues(tableForeign.getForeignTable("ID_ARTICLE"));
|
|
|
104 |
rowValsToFetchArt.putNulls(rowValsToFetchArt.getTable().getFieldsName());
|
|
|
105 |
rowValsToFetch.put("ID_ARTICLE", rowValsToFetchArt);
|
|
|
106 |
}
|
93 |
ilm |
107 |
|
|
|
108 |
Where w = null;
|
132 |
ilm |
109 |
if (foreignField != null && foreignField.trim().length() > 0) {
|
|
|
110 |
for (SQLRowAccessor rowAccess : row) {
|
|
|
111 |
if (rowAccess != null && !rowAccess.isUndefined()) {
|
|
|
112 |
if (w == null) {
|
|
|
113 |
w = new Where((SQLField) tableForeign.getField(foreignField), "=", rowAccess.getID());
|
|
|
114 |
} else {
|
|
|
115 |
w = w.or(new Where((SQLField) tableForeign.getField(foreignField), "=", rowAccess.getID()));
|
|
|
116 |
}
|
93 |
ilm |
117 |
}
|
41 |
ilm |
118 |
}
|
132 |
ilm |
119 |
} else {
|
|
|
120 |
for (SQLRowAccessor rowAccess : row) {
|
|
|
121 |
if (rowAccess != null && !rowAccess.isUndefined()) {
|
|
|
122 |
if (w == null) {
|
|
|
123 |
w = new Where((SQLField) tableForeign.getForeignKeys(rowAccess.getTable()).toArray()[0], "=", rowAccess.getID());
|
|
|
124 |
} else {
|
|
|
125 |
w = w.or(new Where((SQLField) tableForeign.getForeignKeys(rowAccess.getTable()).toArray()[0], "=", rowAccess.getID()));
|
|
|
126 |
}
|
|
|
127 |
}
|
|
|
128 |
}
|
41 |
ilm |
129 |
}
|
132 |
ilm |
130 |
|
|
|
131 |
if (excludeQteZero) {
|
|
|
132 |
w = w.and(new Where(tableForeign.getField("QTE"), "!=", 0));
|
|
|
133 |
}
|
144 |
ilm |
134 |
final Where wFinal = w;
|
|
|
135 |
SQLRowValuesListFetcher fetcher = SQLRowValuesListFetcher.create(rowValsToFetch);
|
|
|
136 |
fetcher.setSelTransf(new ITransformer<SQLSelect, SQLSelect>() {
|
|
|
137 |
@Override
|
|
|
138 |
public SQLSelect transformChecked(SQLSelect sel) {
|
|
|
139 |
sel.setWhere(wFinal);
|
|
|
140 |
addSelectOrder(tableForeign, orderBy, sel);
|
|
|
141 |
System.err.println(sel.asString());
|
|
|
142 |
return sel;
|
|
|
143 |
}
|
|
|
144 |
});
|
93 |
ilm |
145 |
|
144 |
ilm |
146 |
list.addAll(fetcher.fetch());
|
|
|
147 |
|
18 |
ilm |
148 |
} else {
|
|
|
149 |
|
|
|
150 |
final List<String> params = SQLRow.toList(groupBy);
|
93 |
ilm |
151 |
SQLSelect sel = new SQLSelect();
|
18 |
ilm |
152 |
sel.addSelect(tableForeign.getKey());
|
|
|
153 |
for (int i = 0; i < params.size(); i++) {
|
|
|
154 |
sel.addSelect(tableForeign.getField(params.get(i)));
|
|
|
155 |
}
|
|
|
156 |
|
41 |
ilm |
157 |
Where w = null;
|
|
|
158 |
for (SQLRowAccessor rowAccess : row) {
|
|
|
159 |
if (w == null) {
|
|
|
160 |
w = new Where((SQLField) tableForeign.getForeignKeys(rowAccess.getTable()).toArray()[0], "=", rowAccess.getID());
|
|
|
161 |
} else {
|
|
|
162 |
w = w.or(new Where((SQLField) tableForeign.getForeignKeys(rowAccess.getTable()).toArray()[0], "=", rowAccess.getID()));
|
|
|
163 |
}
|
|
|
164 |
}
|
|
|
165 |
sel.setWhere(w);
|
93 |
ilm |
166 |
addSelectOrder(tableForeign, orderBy, sel);
|
41 |
ilm |
167 |
System.err.println(sel.asString());
|
93 |
ilm |
168 |
List<SQLRow> result = SQLRowListRSH.execute(sel);
|
18 |
ilm |
169 |
|
|
|
170 |
list = new ArrayList<SQLRowAccessor>();
|
|
|
171 |
Map<Object, SQLRowValues> m = new HashMap<Object, SQLRowValues>();
|
|
|
172 |
for (SQLRow sqlRow : result) {
|
|
|
173 |
SQLRowValues rowVals;
|
|
|
174 |
final Integer object = sqlRow.getInt(params.get(0));
|
|
|
175 |
if (m.get(object) == null || object == 1) {
|
|
|
176 |
rowVals = sqlRow.asRowValues();
|
|
|
177 |
// if (object != 1) {
|
|
|
178 |
// rowVals.put("ID_STYLE", 3);
|
|
|
179 |
// }
|
|
|
180 |
m.put(object, rowVals);
|
|
|
181 |
list.add(rowVals);
|
|
|
182 |
} else {
|
|
|
183 |
rowVals = m.get(object);
|
|
|
184 |
cumulRows(params, sqlRow, rowVals);
|
|
|
185 |
}
|
|
|
186 |
}
|
|
|
187 |
}
|
|
|
188 |
|
132 |
ilm |
189 |
if (expandNomenclature) {
|
|
|
190 |
list = expandNomenclature(list);
|
|
|
191 |
}
|
144 |
ilm |
192 |
if (this.postProcess != null) {
|
|
|
193 |
list = this.postProcess.transformChecked(list);
|
|
|
194 |
}
|
132 |
ilm |
195 |
|
18 |
ilm |
196 |
if (c == null) {
|
|
|
197 |
Map<SQLTable, List<SQLRowAccessor>> map = new HashMap<SQLTable, List<SQLRowAccessor>>();
|
|
|
198 |
map.put(tableForeign, list);
|
41 |
ilm |
199 |
cacheReferent.put(row.get(0), map);
|
18 |
ilm |
200 |
} else {
|
|
|
201 |
c.put(tableForeign, list);
|
|
|
202 |
}
|
|
|
203 |
|
93 |
ilm |
204 |
if (orderBy != null && orderBy.trim().length() > 0 && !orderBy.contains(".")) {
|
91 |
ilm |
205 |
Collections.sort(list, new Comparator<SQLRowAccessor>() {
|
|
|
206 |
@Override
|
|
|
207 |
public int compare(SQLRowAccessor o1, SQLRowAccessor o2) {
|
|
|
208 |
|
|
|
209 |
return CompareUtils.compare(o1.getObject(orderBy), o2.getObject(orderBy));
|
|
|
210 |
}
|
|
|
211 |
});
|
|
|
212 |
}
|
|
|
213 |
|
18 |
ilm |
214 |
return list;
|
|
|
215 |
}
|
|
|
216 |
// return row.getReferentRows(tableForeign);
|
|
|
217 |
}
|
|
|
218 |
|
132 |
ilm |
219 |
private List<SQLRowAccessor> expandNomenclature(List<SQLRowAccessor> list) {
|
|
|
220 |
final List<Integer> idsArt = new ArrayList<Integer>(list.size());
|
|
|
221 |
DBRoot root = null;
|
|
|
222 |
SQLTable table = null;
|
|
|
223 |
for (SQLRowAccessor r : list) {
|
|
|
224 |
root = r.getTable().getDBRoot();
|
|
|
225 |
table = r.getTable();
|
|
|
226 |
if (!r.isForeignEmpty("ID_ARTICLE")) {
|
|
|
227 |
idsArt.add(r.getForeignID("ID_ARTICLE"));
|
|
|
228 |
}
|
|
|
229 |
}
|
|
|
230 |
List<SQLRowAccessor> result = new ArrayList<SQLRowAccessor>();
|
|
|
231 |
if (root != null) {
|
|
|
232 |
|
|
|
233 |
Map<String, Integer> style = Configuration.getInstance().getDirectory().getElement(StyleSQLElement.class).getAllStyleByName();
|
|
|
234 |
|
|
|
235 |
final SQLTable tableArtElt = root.getTable("ARTICLE_ELEMENT");
|
|
|
236 |
SQLRowValues rowVals = new SQLRowValues(tableArtElt);
|
|
|
237 |
rowVals.putNulls("QTE", "QTE_UNITAIRE", "ID_UNITE_VENTE", "ID_ARTICLE_PARENT");
|
142 |
ilm |
238 |
rowVals.putRowValues("ID_ARTICLE").setAllToNull();
|
132 |
ilm |
239 |
SQLRowValuesListFetcher fetch = SQLRowValuesListFetcher.create(rowVals);
|
|
|
240 |
fetch.setSelTransf(new ITransformer<SQLSelect, SQLSelect>() {
|
|
|
241 |
|
|
|
242 |
@Override
|
|
|
243 |
public SQLSelect transformChecked(SQLSelect input) {
|
|
|
244 |
input.setWhere(new Where(input.getAlias(tableArtElt.getField("ID_ARTICLE_PARENT")), idsArt));
|
|
|
245 |
return input;
|
|
|
246 |
}
|
|
|
247 |
});
|
|
|
248 |
List<SQLRowValues> l = fetch.fetch();
|
|
|
249 |
ListMap<Integer, SQLRowValues> map = new ListMap<Integer, SQLRowValues>();
|
|
|
250 |
for (SQLRowValues sqlRowValues : l) {
|
|
|
251 |
SQLRowValues rowInj = new SQLRowValues(table);
|
|
|
252 |
final SQLRowAccessor foreignArt = sqlRowValues.getForeign("ID_ARTICLE");
|
|
|
253 |
rowInj.put("ID_ARTICLE", foreignArt.asRowValues());
|
|
|
254 |
rowInj.put("QTE", sqlRowValues.getInt("QTE"));
|
|
|
255 |
rowInj.put("NOM", foreignArt.getObject("NOM"));
|
|
|
256 |
rowInj.put("CODE", foreignArt.getObject("CODE"));
|
|
|
257 |
rowInj.put("QTE_UNITAIRE", sqlRowValues.getObject("QTE_UNITAIRE"));
|
|
|
258 |
rowInj.put("ID_UNITE_VENTE", sqlRowValues.getObject("ID_UNITE_VENTE"));
|
|
|
259 |
rowInj.put("PV_HT", BigDecimal.ZERO);
|
|
|
260 |
rowInj.put("T_PV_HT", BigDecimal.ZERO);
|
|
|
261 |
|
|
|
262 |
rowInj.put("ID_TAXE", TaxeCache.getCache().getFirstTaxe().getID());
|
|
|
263 |
rowInj.put("NIVEAU", 1);
|
|
|
264 |
rowInj.put("ID_STYLE", style.get("Composant"));
|
|
|
265 |
map.add(sqlRowValues.getForeignID("ID_ARTICLE_PARENT"), rowInj);
|
|
|
266 |
}
|
|
|
267 |
|
|
|
268 |
int size = list.size();
|
|
|
269 |
|
|
|
270 |
for (int i = 0; i < size; i++) {
|
|
|
271 |
|
|
|
272 |
SQLRowAccessor sqlRowAccessor = list.get(i);
|
|
|
273 |
result.add(sqlRowAccessor);
|
|
|
274 |
|
|
|
275 |
if (!sqlRowAccessor.isForeignEmpty("ID_ARTICLE") && sqlRowAccessor.getInt("NIVEAU") == 1) {
|
|
|
276 |
final List<SQLRowValues> c = map.get(sqlRowAccessor.getForeignID("ID_ARTICLE"));
|
|
|
277 |
if (c != null) {
|
|
|
278 |
if (i + 1 < size) {
|
|
|
279 |
SQLRowAccessor rowAccessorNext = list.get(i + 1);
|
|
|
280 |
if (rowAccessorNext.getInt("NIVEAU") == 1) {
|
|
|
281 |
for (SQLRowValues sqlRowValues : c) {
|
|
|
282 |
sqlRowValues.put("QTE", sqlRowValues.getInt("QTE") * sqlRowAccessor.getInt("QTE"));
|
|
|
283 |
result.add(sqlRowValues);
|
|
|
284 |
}
|
|
|
285 |
}
|
|
|
286 |
} else {
|
|
|
287 |
for (SQLRowValues sqlRowValues : c) {
|
|
|
288 |
sqlRowValues.put("QTE", sqlRowValues.getInt("QTE") * sqlRowAccessor.getInt("QTE"));
|
|
|
289 |
result.add(sqlRowValues);
|
|
|
290 |
}
|
|
|
291 |
}
|
|
|
292 |
}
|
|
|
293 |
}
|
|
|
294 |
|
|
|
295 |
}
|
|
|
296 |
|
|
|
297 |
}
|
|
|
298 |
return result;
|
|
|
299 |
}
|
|
|
300 |
|
93 |
ilm |
301 |
private void addSelectOrder(final SQLTable tableForeign, final String orderBy, SQLSelect sel) {
|
|
|
302 |
if (orderBy != null && orderBy.contains(".")) {
|
|
|
303 |
String fieldRefTable = orderBy.substring(0, orderBy.indexOf('.'));
|
|
|
304 |
String field = orderBy.substring(orderBy.indexOf('.') + 1, orderBy.length());
|
156 |
ilm |
305 |
if (sel.getJoin(tableForeign.getField(fieldRefTable)) == null) {
|
|
|
306 |
sel.addJoin("LEFT", sel.getAlias(tableForeign).getField(fieldRefTable));
|
|
|
307 |
}
|
|
|
308 |
SQLSelectJoin join = sel.getJoin(tableForeign.getField(fieldRefTable));
|
|
|
309 |
sel.addFieldOrder(join.getJoinedTable().getField(field));
|
93 |
ilm |
310 |
} else {
|
|
|
311 |
sel.addFieldOrder(tableForeign.getOrderField());
|
|
|
312 |
}
|
|
|
313 |
}
|
|
|
314 |
|
63 |
ilm |
315 |
private void cumulRows(final List<String> params, SQLRow sqlRow, SQLRowValues rowVals) {
|
18 |
ilm |
316 |
|
|
|
317 |
for (int i = 1; i < params.size(); i++) {
|
|
|
318 |
|
|
|
319 |
if (rowVals.getTable().getField(params.get(i)).getType().getJavaType() == String.class) {
|
|
|
320 |
String string = sqlRow.getString(params.get(i));
|
|
|
321 |
if (params.get(i).equalsIgnoreCase("NOM")) {
|
|
|
322 |
string = sqlRow.getInt("QTE") + " x " + string;
|
|
|
323 |
}
|
|
|
324 |
rowVals.put(params.get(i), rowVals.getString(params.get(i)) + ", " + string);
|
|
|
325 |
} else if (!rowVals.getTable().getField(params.get(i)).isKey()) {
|
|
|
326 |
Long n = rowVals.getLong(params.get(i));
|
|
|
327 |
rowVals.put(params.get(i), n + sqlRow.getLong(params.get(i)));
|
|
|
328 |
}
|
|
|
329 |
|
|
|
330 |
}
|
|
|
331 |
}
|
|
|
332 |
|
63 |
ilm |
333 |
public Map<SQLRowAccessor, Map<SQLTable, List<SQLRowAccessor>>> getCacheReferent() {
|
18 |
ilm |
334 |
return cacheReferent;
|
|
|
335 |
}
|
|
|
336 |
|
63 |
ilm |
337 |
public void clearCache() {
|
18 |
ilm |
338 |
cacheReferent.clear();
|
|
|
339 |
cacheForeign.clear();
|
|
|
340 |
}
|
|
|
341 |
}
|