OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 41 | Rev 73 | 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
 
16
import org.openconcerto.sql.model.SQLField;
17
import org.openconcerto.sql.model.SQLRow;
18
import org.openconcerto.sql.model.SQLRowAccessor;
19
import org.openconcerto.sql.model.SQLRowListRSH;
20
import org.openconcerto.sql.model.SQLRowValues;
21
import org.openconcerto.sql.model.SQLSelect;
22
import org.openconcerto.sql.model.SQLTable;
23
import org.openconcerto.sql.model.Where;
24
 
25
import java.util.ArrayList;
26
import java.util.HashMap;
27
import java.util.List;
28
import java.util.Map;
29
 
30
public class OOXMLCache {
63 ilm 31
 
32
    private Map<SQLRowAccessor, Map<SQLTable, List<SQLRowAccessor>>> cacheReferent = new HashMap<SQLRowAccessor, Map<SQLTable, List<SQLRowAccessor>>>();
33
    private Map<String, Map<Integer, SQLRowAccessor>> cacheForeign = new HashMap<String, Map<Integer, SQLRowAccessor>>();
34
 
35
    protected SQLRowAccessor getForeignRow(SQLRowAccessor row, SQLField field) {
18 ilm 36
        Map<Integer, SQLRowAccessor> c = cacheForeign.get(field.getName());
37
 
28 ilm 38
        if (row.getObject(field.getName()) == null) {
39
            return null;
40
        }
41
 
18 ilm 42
        int i = row.getInt(field.getName());
43
 
44
        if (c != null && c.get(i) != null) {
45
            System.err.println("get foreign row From Cache ");
46
            return c.get(i);
47
        } else {
48
 
49
            SQLRowAccessor foreign = row.getForeign(field.getName());
50
 
51
            if (c == null) {
52
                Map<Integer, SQLRowAccessor> map = new HashMap<Integer, SQLRowAccessor>();
53
                map.put(i, foreign);
54
                cacheForeign.put(field.getName(), map);
55
            } else {
56
                c.put(i, foreign);
57
            }
58
 
59
            return foreign;
60
        }
61
 
62
    }
63
 
63 ilm 64
    protected List<? extends SQLRowAccessor> getReferentRows(List<? extends SQLRowAccessor> row, SQLTable tableForeign) {
18 ilm 65
        return getReferentRows(row, tableForeign, null);
66
    }
67
 
63 ilm 68
    protected List<? extends SQLRowAccessor> getReferentRows(List<? extends SQLRowAccessor> row, final SQLTable tableForeign, String groupBy) {
41 ilm 69
        Map<SQLTable, List<SQLRowAccessor>> c = cacheReferent.get(row.get(0));
18 ilm 70
 
71
        if (c != null && c.get(tableForeign) != null) {
72
            System.err.println("get referent rows From Cache ");
73
            return c.get(tableForeign);
74
        } else {
75
            List<SQLRowAccessor> list;
41 ilm 76
            if (row.isEmpty() || (row.size() > 0 && row.get(0).isUndefined())) {
18 ilm 77
                list = new ArrayList<SQLRowAccessor>();
41 ilm 78
            } else if (row.size() > 0 && (groupBy == null || groupBy.trim().length() == 0)) {
79
                list = new ArrayList<SQLRowAccessor>();
80
                for (SQLRowAccessor sqlRowAccessor : row) {
81
                    if (sqlRowAccessor != null && !sqlRowAccessor.isUndefined()) {
82
                        list.addAll(sqlRowAccessor.getReferentRows(tableForeign));
83
                    }
84
                }
18 ilm 85
            } else {
86
 
87
                final List<String> params = SQLRow.toList(groupBy);
41 ilm 88
                SQLSelect sel = new SQLSelect(row.get(0).getTable().getBase());
18 ilm 89
                sel.addSelect(tableForeign.getKey());
90
                for (int i = 0; i < params.size(); i++) {
91
                    sel.addSelect(tableForeign.getField(params.get(i)));
92
                }
93
 
41 ilm 94
                Where w = null;
95
                for (SQLRowAccessor rowAccess : row) {
96
                    if (w == null) {
97
                        w = new Where((SQLField) tableForeign.getForeignKeys(rowAccess.getTable()).toArray()[0], "=", rowAccess.getID());
98
                    } else {
99
                        w = w.or(new Where((SQLField) tableForeign.getForeignKeys(rowAccess.getTable()).toArray()[0], "=", rowAccess.getID()));
100
                    }
101
                }
102
                sel.setWhere(w);
103
                System.err.println(sel.asString());
104
                List<SQLRow> result = (List<SQLRow>) row.get(0).getTable().getBase().getDataSource().execute(sel.asString(), new SQLRowListRSH(tableForeign));
18 ilm 105
 
106
                list = new ArrayList<SQLRowAccessor>();
107
                Map<Object, SQLRowValues> m = new HashMap<Object, SQLRowValues>();
108
                for (SQLRow sqlRow : result) {
109
                    SQLRowValues rowVals;
110
                    final Integer object = sqlRow.getInt(params.get(0));
111
                    if (m.get(object) == null || object == 1) {
112
                        rowVals = sqlRow.asRowValues();
113
                        // if (object != 1) {
114
                        // rowVals.put("ID_STYLE", 3);
115
                        // }
116
                        m.put(object, rowVals);
117
                        list.add(rowVals);
118
                    } else {
119
                        rowVals = m.get(object);
120
                        cumulRows(params, sqlRow, rowVals);
121
                    }
122
                }
123
            }
124
 
125
            if (c == null) {
126
                Map<SQLTable, List<SQLRowAccessor>> map = new HashMap<SQLTable, List<SQLRowAccessor>>();
127
                map.put(tableForeign, list);
41 ilm 128
                cacheReferent.put(row.get(0), map);
18 ilm 129
            } else {
130
                c.put(tableForeign, list);
131
            }
132
 
133
            return list;
134
        }
135
        // return row.getReferentRows(tableForeign);
136
    }
137
 
63 ilm 138
    private void cumulRows(final List<String> params, SQLRow sqlRow, SQLRowValues rowVals) {
18 ilm 139
 
140
        for (int i = 1; i < params.size(); i++) {
141
 
142
            if (rowVals.getTable().getField(params.get(i)).getType().getJavaType() == String.class) {
143
                String string = sqlRow.getString(params.get(i));
144
                if (params.get(i).equalsIgnoreCase("NOM")) {
145
                    string = sqlRow.getInt("QTE") + " x " + string;
146
                }
147
                rowVals.put(params.get(i), rowVals.getString(params.get(i)) + ", " + string);
148
            } else if (!rowVals.getTable().getField(params.get(i)).isKey()) {
149
                Long n = rowVals.getLong(params.get(i));
150
                rowVals.put(params.get(i), n + sqlRow.getLong(params.get(i)));
151
            }
152
 
153
        }
154
    }
155
 
63 ilm 156
    public Map<SQLRowAccessor, Map<SQLTable, List<SQLRowAccessor>>> getCacheReferent() {
18 ilm 157
        return cacheReferent;
158
    }
159
 
63 ilm 160
    public void clearCache() {
18 ilm 161
        cacheReferent.clear();
162
        cacheForeign.clear();
163
    }
164
}