OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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