OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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