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