17 |
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.sql.model;
|
|
|
15 |
|
73 |
ilm |
16 |
import org.openconcerto.sql.Configuration;
|
80 |
ilm |
17 |
import org.openconcerto.sql.Log;
|
73 |
ilm |
18 |
import org.openconcerto.sql.element.SQLElementDirectory;
|
67 |
ilm |
19 |
import org.openconcerto.sql.request.MultipleSQLSelectExecutor;
|
|
|
20 |
import org.openconcerto.utils.ExceptionHandler;
|
|
|
21 |
|
|
|
22 |
import java.sql.SQLException;
|
|
|
23 |
import java.util.ArrayList;
|
17 |
ilm |
24 |
import java.util.HashMap;
|
67 |
ilm |
25 |
import java.util.List;
|
17 |
ilm |
26 |
import java.util.Map;
|
|
|
27 |
|
|
|
28 |
// do not use fields DEFAULT since they're not always constants, plus for some systems we need a
|
|
|
29 |
// trip to the db anyway to interpret them and they're not easily updateable.
|
|
|
30 |
// MAYBE don't piggyback on undefinedID, rather create a defaultValuesID (eg add column in
|
|
|
31 |
// FWK_UNDEF_ID) that would be archived so as to not appear in results
|
|
|
32 |
public class UndefinedRowValuesCache {
|
|
|
33 |
private static final UndefinedRowValuesCache instance = new UndefinedRowValuesCache();
|
|
|
34 |
|
|
|
35 |
public synchronized static UndefinedRowValuesCache getInstance() {
|
|
|
36 |
return instance;
|
|
|
37 |
}
|
|
|
38 |
|
|
|
39 |
private final Map<SQLTable, SQLRowValues> map = new HashMap<SQLTable, SQLRowValues>();
|
|
|
40 |
|
73 |
ilm |
41 |
private final SQLElementDirectory getDirectory() {
|
|
|
42 |
return Configuration.getInstance().getDirectory();
|
|
|
43 |
}
|
|
|
44 |
|
17 |
ilm |
45 |
public SQLRowValues getDefaultRowValues(final SQLTable t) {
|
|
|
46 |
SQLRowValues rv = this.map.get(t);
|
|
|
47 |
if (rv == null) {
|
|
|
48 |
rv = new SQLRowValues(t);
|
|
|
49 |
final SQLRow undefRow = t.getRow(t.getUndefinedID());
|
80 |
ilm |
50 |
if (undefRow == null) {
|
|
|
51 |
Log.get().warning(t.getSQLName() + " doesn't contain undef ID " + t.getUndefinedID());
|
|
|
52 |
} else {
|
|
|
53 |
getDirectory().getElement(t).loadAllSafe(rv, undefRow);
|
|
|
54 |
}
|
17 |
ilm |
55 |
this.map.put(t, rv);
|
|
|
56 |
}
|
|
|
57 |
return rv;
|
|
|
58 |
}
|
67 |
ilm |
59 |
|
|
|
60 |
public void preload(List<SQLTable> tablesToCache) {
|
|
|
61 |
if (tablesToCache.size() <= 0) {
|
|
|
62 |
throw new IllegalArgumentException("Empty list");
|
|
|
63 |
}
|
|
|
64 |
final List<SQLSelect> queries = new ArrayList<SQLSelect>(tablesToCache.size());
|
|
|
65 |
final int size = tablesToCache.size();
|
|
|
66 |
for (int i = 0; i < size; i++) {
|
|
|
67 |
final SQLTable sqlTable = tablesToCache.get(i);
|
|
|
68 |
final SQLSelect select = new SQLSelect(true);
|
|
|
69 |
select.addSelectStar(sqlTable);
|
|
|
70 |
select.setWhere(sqlTable.getKey(), "=", sqlTable.getUndefinedID());
|
|
|
71 |
queries.add(select);
|
|
|
72 |
}
|
|
|
73 |
final MultipleSQLSelectExecutor executor = new MultipleSQLSelectExecutor(tablesToCache.get(0).getDBSystemRoot(), queries);
|
|
|
74 |
try {
|
|
|
75 |
final List<List<SQLRow>> l = executor.execute();
|
|
|
76 |
if (l.size() != tablesToCache.size()) {
|
|
|
77 |
throw new IllegalStateException("Internal SQL error while preloading");
|
|
|
78 |
}
|
|
|
79 |
for (int i = 0; i < size; i++) {
|
|
|
80 |
final SQLTable sqlTable = tablesToCache.get(i);
|
|
|
81 |
final List<SQLRow> rows = l.get(i);
|
|
|
82 |
if (rows.size() > 0) {
|
|
|
83 |
final SQLRowValues rv = new SQLRowValues(sqlTable);
|
73 |
ilm |
84 |
getDirectory().getElement(sqlTable).loadAllSafe(rv, rows.get(0));
|
67 |
ilm |
85 |
this.map.put(sqlTable, rv);
|
|
|
86 |
} else {
|
|
|
87 |
System.err.println("Warning: no undefined row in table: " + sqlTable.getName() + " id: " + sqlTable.getUndefinedID());
|
|
|
88 |
}
|
|
|
89 |
}
|
|
|
90 |
} catch (SQLException e) {
|
|
|
91 |
ExceptionHandler.handle("Unable to preload tables", e);
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
}
|
17 |
ilm |
95 |
}
|