OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 151 | 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
 
16
import java.util.ArrayList;
17
import java.util.Collections;
18
import java.util.List;
19
 
151 ilm 20
public class SQLBackgroundTableCacheItem {
17 ilm 21
 
22
    private SQLTable table;
23
    private int timeout;
24
    private List<SQLRow> rows = new ArrayList<SQLRow>();
25
    private long lastReload;// time in millis
151 ilm 26
    private boolean enableReloadIfTableModified = true;
17 ilm 27
 
28
    public SQLBackgroundTableCacheItem(final SQLTable t, final int second) {
29
        this.table = t;
30
        this.timeout = second;
174 ilm 31
 
32
        if (second == -1) {
33
            reloadFromDB();
34
        }
35
 
151 ilm 36
        this.table.addTableModifiedListener(new SQLTableModifiedListener() {
17 ilm 37
 
151 ilm 38
            @Override
39
            public void tableModified(SQLTableEvent evt) {
40
                if (enableReloadIfTableModified) {
41
                    lastReload = 0;
42
                }
43
                reloadFromDbIfNeeded();
44
            }
45
        });
17 ilm 46
    }
47
 
151 ilm 48
    public void setEnableReloadIfTableModified(boolean enableReloadIfTableModified) {
49
        this.enableReloadIfTableModified = enableReloadIfTableModified;
17 ilm 50
    }
51
 
52
    public synchronized void reloadFromDbIfNeeded() {
53
        final long delta = System.currentTimeMillis() - this.lastReload;
174 ilm 54
        if (this.timeout >= 0 && delta / 1000 > this.timeout) {
55
            reloadFromDB();
17 ilm 56
        }
57
    }
58
 
174 ilm 59
    @SuppressWarnings("unchecked")
60
    private void reloadFromDB() {
61
        final SQLSelect sel = new SQLSelect();
62
        sel.addSelectStar(this.table);
63
        this.rows = Collections.unmodifiableList((List<SQLRow>) this.table.getBase().getDataSource().execute(sel.asString(), SQLRowListRSH.createFromSelect(sel, this.table)));
64
        this.lastReload = System.currentTimeMillis();
65
    }
66
 
67 ilm 67
    public synchronized SQLRow getFirstRowContains(final int value, final SQLField field) {
17 ilm 68
        for (SQLRow r : this.rows) {
67 ilm 69
            if (r.getInt(field.getName()) == value && !r.isArchived()) {
17 ilm 70
                return r;
71
            }
72
        }
73
        return null;
74
    }
75
 
67 ilm 76
    public synchronized SQLRow getFirstRowContains(final String value, final SQLField field) {
77
        for (SQLRow r : this.rows) {
78
            if (r.getString(field.getName()).equals(value) && !r.isArchived()) {
79
                return r;
80
            }
81
        }
82
        return null;
83
    }
84
 
17 ilm 85
    public synchronized SQLRow getRowFromId(final int i) {
86
        return getFirstRowContains(i, this.table.getKey());
87
    }
88
 
89
    public synchronized List<SQLRow> getRows() {
90
        return this.rows;
91
    }
92
 
93
    public SQLTable getTable() {
94
        return this.table;
95
    }
96
}