OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 156 | Go to most recent revision | 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
 *
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;
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()) {
137
                            if (w == null) {
138
                                w = new Where((SQLField) tableForeign.getForeignKeys(rowAccess.getTable()).toArray()[0], "=", rowAccess.getID());
139
                            } else {
140
                                w = w.or(new Where((SQLField) tableForeign.getForeignKeys(rowAccess.getTable()).toArray()[0], "=", rowAccess.getID()));
141
                            }
142
                        }
143
                    }
41 ilm 144
                }
132 ilm 145
 
146
                if (excludeQteZero) {
147
                    w = w.and(new Where(tableForeign.getField("QTE"), "!=", 0));
148
                }
144 ilm 149
                final Where wFinal = w;
150
                SQLRowValuesListFetcher fetcher = SQLRowValuesListFetcher.create(rowValsToFetch);
151
                fetcher.setSelTransf(new ITransformer<SQLSelect, SQLSelect>() {
152
                    @Override
153
                    public SQLSelect transformChecked(SQLSelect sel) {
154
                        sel.setWhere(wFinal);
155
                        addSelectOrder(tableForeign, orderBy, sel);
156
                        System.err.println(sel.asString());
157
                        return sel;
158
                    }
159
                });
93 ilm 160
 
144 ilm 161
                list.addAll(fetcher.fetch());
162
 
18 ilm 163
            } else {
164
 
165
                final List<String> params = SQLRow.toList(groupBy);
93 ilm 166
                SQLSelect sel = new SQLSelect();
18 ilm 167
                sel.addSelect(tableForeign.getKey());
168
                for (int i = 0; i < params.size(); i++) {
169
                    sel.addSelect(tableForeign.getField(params.get(i)));
170
                }
171
 
41 ilm 172
                Where w = null;
173
                for (SQLRowAccessor rowAccess : row) {
174
                    if (w == null) {
175
                        w = new Where((SQLField) tableForeign.getForeignKeys(rowAccess.getTable()).toArray()[0], "=", rowAccess.getID());
176
                    } else {
177
                        w = w.or(new Where((SQLField) tableForeign.getForeignKeys(rowAccess.getTable()).toArray()[0], "=", rowAccess.getID()));
178
                    }
179
                }
180
                sel.setWhere(w);
93 ilm 181
                addSelectOrder(tableForeign, orderBy, sel);
41 ilm 182
                System.err.println(sel.asString());
93 ilm 183
                List<SQLRow> result = SQLRowListRSH.execute(sel);
18 ilm 184
 
185
                list = new ArrayList<SQLRowAccessor>();
186
                Map<Object, SQLRowValues> m = new HashMap<Object, SQLRowValues>();
187
                for (SQLRow sqlRow : result) {
188
                    SQLRowValues rowVals;
189
                    final Integer object = sqlRow.getInt(params.get(0));
190
                    if (m.get(object) == null || object == 1) {
191
                        rowVals = sqlRow.asRowValues();
192
                        // if (object != 1) {
193
                        // rowVals.put("ID_STYLE", 3);
194
                        // }
195
                        m.put(object, rowVals);
196
                        list.add(rowVals);
197
                    } else {
198
                        rowVals = m.get(object);
199
                        cumulRows(params, sqlRow, rowVals);
200
                    }
201
                }
202
            }
203
 
132 ilm 204
            if (expandNomenclature) {
205
                list = expandNomenclature(list);
206
            }
144 ilm 207
            if (this.postProcess != null) {
208
                list = this.postProcess.transformChecked(list);
209
            }
132 ilm 210
 
18 ilm 211
            if (c == null) {
212
                Map<SQLTable, List<SQLRowAccessor>> map = new HashMap<SQLTable, List<SQLRowAccessor>>();
213
                map.put(tableForeign, list);
41 ilm 214
                cacheReferent.put(row.get(0), map);
18 ilm 215
            } else {
216
                c.put(tableForeign, list);
217
            }
218
 
93 ilm 219
            if (orderBy != null && orderBy.trim().length() > 0 && !orderBy.contains(".")) {
91 ilm 220
                Collections.sort(list, new Comparator<SQLRowAccessor>() {
221
                    @Override
222
                    public int compare(SQLRowAccessor o1, SQLRowAccessor o2) {
223
 
224
                        return CompareUtils.compare(o1.getObject(orderBy), o2.getObject(orderBy));
225
                    }
226
                });
227
            }
228
 
18 ilm 229
            return list;
230
        }
231
        // return row.getReferentRows(tableForeign);
232
    }
233
 
132 ilm 234
    private List<SQLRowAccessor> expandNomenclature(List<SQLRowAccessor> list) {
235
        final List<Integer> idsArt = new ArrayList<Integer>(list.size());
236
        DBRoot root = null;
237
        SQLTable table = null;
238
        for (SQLRowAccessor r : list) {
239
            root = r.getTable().getDBRoot();
240
            table = r.getTable();
241
            if (!r.isForeignEmpty("ID_ARTICLE")) {
242
                idsArt.add(r.getForeignID("ID_ARTICLE"));
243
            }
244
        }
245
        List<SQLRowAccessor> result = new ArrayList<SQLRowAccessor>();
246
        if (root != null) {
247
 
248
            Map<String, Integer> style = Configuration.getInstance().getDirectory().getElement(StyleSQLElement.class).getAllStyleByName();
249
 
250
            final SQLTable tableArtElt = root.getTable("ARTICLE_ELEMENT");
251
            SQLRowValues rowVals = new SQLRowValues(tableArtElt);
252
            rowVals.putNulls("QTE", "QTE_UNITAIRE", "ID_UNITE_VENTE", "ID_ARTICLE_PARENT");
142 ilm 253
            rowVals.putRowValues("ID_ARTICLE").setAllToNull();
132 ilm 254
            SQLRowValuesListFetcher fetch = SQLRowValuesListFetcher.create(rowVals);
255
            fetch.setSelTransf(new ITransformer<SQLSelect, SQLSelect>() {
256
 
257
                @Override
258
                public SQLSelect transformChecked(SQLSelect input) {
259
                    input.setWhere(new Where(input.getAlias(tableArtElt.getField("ID_ARTICLE_PARENT")), idsArt));
260
                    return input;
261
                }
262
            });
263
            List<SQLRowValues> l = fetch.fetch();
264
            ListMap<Integer, SQLRowValues> map = new ListMap<Integer, SQLRowValues>();
265
            for (SQLRowValues sqlRowValues : l) {
266
                SQLRowValues rowInj = new SQLRowValues(table);
267
                final SQLRowAccessor foreignArt = sqlRowValues.getForeign("ID_ARTICLE");
268
                rowInj.put("ID_ARTICLE", foreignArt.asRowValues());
269
                rowInj.put("QTE", sqlRowValues.getInt("QTE"));
270
                rowInj.put("NOM", foreignArt.getObject("NOM"));
271
                rowInj.put("CODE", foreignArt.getObject("CODE"));
272
                rowInj.put("QTE_UNITAIRE", sqlRowValues.getObject("QTE_UNITAIRE"));
273
                rowInj.put("ID_UNITE_VENTE", sqlRowValues.getObject("ID_UNITE_VENTE"));
274
                rowInj.put("PV_HT", BigDecimal.ZERO);
275
                rowInj.put("T_PV_HT", BigDecimal.ZERO);
276
 
277
                rowInj.put("ID_TAXE", TaxeCache.getCache().getFirstTaxe().getID());
278
                rowInj.put("NIVEAU", 1);
279
                rowInj.put("ID_STYLE", style.get("Composant"));
280
                map.add(sqlRowValues.getForeignID("ID_ARTICLE_PARENT"), rowInj);
281
            }
282
 
283
            int size = list.size();
284
 
285
            for (int i = 0; i < size; i++) {
286
 
287
                SQLRowAccessor sqlRowAccessor = list.get(i);
288
                result.add(sqlRowAccessor);
289
 
290
                if (!sqlRowAccessor.isForeignEmpty("ID_ARTICLE") && sqlRowAccessor.getInt("NIVEAU") == 1) {
291
                    final List<SQLRowValues> c = map.get(sqlRowAccessor.getForeignID("ID_ARTICLE"));
292
                    if (c != null) {
293
                        if (i + 1 < size) {
294
                            SQLRowAccessor rowAccessorNext = list.get(i + 1);
295
                            if (rowAccessorNext.getInt("NIVEAU") == 1) {
296
                                for (SQLRowValues sqlRowValues : c) {
297
                                    sqlRowValues.put("QTE", sqlRowValues.getInt("QTE") * sqlRowAccessor.getInt("QTE"));
298
                                    result.add(sqlRowValues);
299
                                }
300
                            }
301
                        } else {
302
                            for (SQLRowValues sqlRowValues : c) {
303
                                sqlRowValues.put("QTE", sqlRowValues.getInt("QTE") * sqlRowAccessor.getInt("QTE"));
304
                                result.add(sqlRowValues);
305
                            }
306
                        }
307
                    }
308
                }
309
 
310
            }
311
 
312
        }
313
        return result;
314
    }
315
 
93 ilm 316
    private void addSelectOrder(final SQLTable tableForeign, final String orderBy, SQLSelect sel) {
317
        if (orderBy != null && orderBy.contains(".")) {
318
            String fieldRefTable = orderBy.substring(0, orderBy.indexOf('.'));
319
            String field = orderBy.substring(orderBy.indexOf('.') + 1, orderBy.length());
156 ilm 320
            if (sel.getJoin(tableForeign.getField(fieldRefTable)) == null) {
321
                sel.addJoin("LEFT", sel.getAlias(tableForeign).getField(fieldRefTable));
322
            }
323
            SQLSelectJoin join = sel.getJoin(tableForeign.getField(fieldRefTable));
324
            sel.addFieldOrder(join.getJoinedTable().getField(field));
93 ilm 325
        } else {
326
            sel.addFieldOrder(tableForeign.getOrderField());
327
        }
328
    }
329
 
63 ilm 330
    private void cumulRows(final List<String> params, SQLRow sqlRow, SQLRowValues rowVals) {
18 ilm 331
 
332
        for (int i = 1; i < params.size(); i++) {
333
 
334
            if (rowVals.getTable().getField(params.get(i)).getType().getJavaType() == String.class) {
335
                String string = sqlRow.getString(params.get(i));
336
                if (params.get(i).equalsIgnoreCase("NOM")) {
337
                    string = sqlRow.getInt("QTE") + " x " + string;
338
                }
339
                rowVals.put(params.get(i), rowVals.getString(params.get(i)) + ", " + string);
340
            } else if (!rowVals.getTable().getField(params.get(i)).isKey()) {
341
                Long n = rowVals.getLong(params.get(i));
342
                rowVals.put(params.get(i), n + sqlRow.getLong(params.get(i)));
343
            }
344
 
345
        }
346
    }
347
 
63 ilm 348
    public Map<SQLRowAccessor, Map<SQLTable, List<SQLRowAccessor>>> getCacheReferent() {
18 ilm 349
        return cacheReferent;
350
    }
351
 
63 ilm 352
    public void clearCache() {
18 ilm 353
        cacheReferent.clear();
354
        cacheForeign.clear();
355
    }
356
}