OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 17 | Rev 73 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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