OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 151 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 151 Rev 174
Line 26... Line 26...
26
    private boolean enableReloadIfTableModified = true;
26
    private boolean enableReloadIfTableModified = true;
27
 
27
 
28
    public SQLBackgroundTableCacheItem(final SQLTable t, final int second) {
28
    public SQLBackgroundTableCacheItem(final SQLTable t, final int second) {
29
        this.table = t;
29
        this.table = t;
30
        this.timeout = second;
30
        this.timeout = second;
-
 
31
 
-
 
32
        if (second == -1) {
-
 
33
            reloadFromDB();
-
 
34
        }
-
 
35
 
31
        this.table.addTableModifiedListener(new SQLTableModifiedListener() {
36
        this.table.addTableModifiedListener(new SQLTableModifiedListener() {
32
 
37
 
33
            @Override
38
            @Override
34
            public void tableModified(SQLTableEvent evt) {
39
            public void tableModified(SQLTableEvent evt) {
35
                if (enableReloadIfTableModified) {
40
                if (enableReloadIfTableModified) {
Line 42... Line 47...
42
 
47
 
43
    public void setEnableReloadIfTableModified(boolean enableReloadIfTableModified) {
48
    public void setEnableReloadIfTableModified(boolean enableReloadIfTableModified) {
44
        this.enableReloadIfTableModified = enableReloadIfTableModified;
49
        this.enableReloadIfTableModified = enableReloadIfTableModified;
45
    }
50
    }
46
 
51
 
47
    @SuppressWarnings("unchecked")
-
 
48
    public synchronized void reloadFromDbIfNeeded() {
52
    public synchronized void reloadFromDbIfNeeded() {
49
        final long delta = System.currentTimeMillis() - this.lastReload;
53
        final long delta = System.currentTimeMillis() - this.lastReload;
50
        if (delta / 1000 > this.timeout) {
54
        if (this.timeout >= 0 && delta / 1000 > this.timeout) {
-
 
55
            reloadFromDB();
-
 
56
        }
-
 
57
    }
-
 
58
 
-
 
59
    @SuppressWarnings("unchecked")
-
 
60
    private void reloadFromDB() {
51
            final SQLSelect sel = new SQLSelect();
61
        final SQLSelect sel = new SQLSelect();
52
            sel.addSelectStar(this.table);
62
        sel.addSelectStar(this.table);
53
            this.rows = Collections.unmodifiableList((List<SQLRow>) this.table.getBase().getDataSource().execute(sel.asString(), SQLRowListRSH.createFromSelect(sel, this.table)));
63
        this.rows = Collections.unmodifiableList((List<SQLRow>) this.table.getBase().getDataSource().execute(sel.asString(), SQLRowListRSH.createFromSelect(sel, this.table)));
54
            this.lastReload = System.currentTimeMillis();
64
        this.lastReload = System.currentTimeMillis();
55
        }
65
    }
56
    }
-
 
57
 
66
 
58
    public synchronized SQLRow getFirstRowContains(final int value, final SQLField field) {
67
    public synchronized SQLRow getFirstRowContains(final int value, final SQLField field) {
59
        for (SQLRow r : this.rows) {
68
        for (SQLRow r : this.rows) {
60
            if (r.getInt(field.getName()) == value && !r.isArchived()) {
69
            if (r.getInt(field.getName()) == value && !r.isArchived()) {
61
                return r;
70
                return r;