OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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