OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 180 | Details | Compare with Previous | Last modification | View Log | RSS feed

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