OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 17 | Rev 81 | 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
 /*
15
 * Créé le 21 mai 2005
16
 */
17
package org.openconcerto.sql.navigator;
18
 
19
import org.openconcerto.sql.element.SQLElement;
25 ilm 20
import org.openconcerto.sql.model.IResultSetHandler;
17 ilm 21
import org.openconcerto.sql.model.SQLDataSource;
22
import org.openconcerto.sql.model.SQLRow;
23
import org.openconcerto.sql.model.SQLRowListRSH;
24
import org.openconcerto.sql.model.SQLSelect;
25 ilm 25
import org.openconcerto.sql.model.SQLTableEvent;
26
import org.openconcerto.sql.model.SQLTableModifiedListener;
17 ilm 27
import org.openconcerto.sql.model.Where;
28
 
29
import java.util.List;
30
import java.util.Set;
31
 
32
import org.apache.commons.dbutils.ResultSetHandler;
33
 
25 ilm 34
public class RowsSQLListModel extends SQLListModel<SQLRow> implements SQLTableModifiedListener {
17 ilm 35
 
36
    private final SQLElement element;
37
    private final ResultSetHandler handler;
38
 
39
    public RowsSQLListModel(SQLElement element) {
40
        super();
41
        this.element = element;
42
        this.handler = new SQLRowListRSH(this.getElement().getTable(), true);
25 ilm 43
        this.getElement().getTable().addTableModifiedListener(this);
17 ilm 44
    }
45
 
25 ilm 46
    @Override
47
    protected void reload(final boolean noCache) {
17 ilm 48
        final Set<Number> ids = getIds();
49
        final String key = this.getElement().getParentForeignField();
50
 
51
        final SQLSelect sel = new SQLSelect(this.getElement().getTable().getBase());
52
        sel.addSelectStar(this.getElement().getTable());
53
        sel.addOrderSilent(this.getElement().getTable().getName());
54
 
55
        // si null pas de where, on montre tout
56
        if (ids != null && key != null) {
57
            sel.setWhere(new Where(this.getElement().getTable().getField(key), ids));
58
        }
59
        final SQLDataSource source = this.getElement().getTable().getBase().getDataSource();
60
 
61
        // cannot just use a SwingWorker, cause some methods (like SQLBrowser#selectPath())
62
        // expect reload() to by synchronous.
25 ilm 63
        @SuppressWarnings("unchecked")
64
        final List<SQLRow> rows = (List<SQLRow>) source.execute(sel.asString(), new IResultSetHandler(this.handler) {
65
            @Override
66
            public boolean readCache() {
67
                return !noCache;
68
            }
69
 
70
            @Override
71
            public boolean writeCache() {
72
                return true;
73
            }
74
        });
75
        this.setAll(rows);
17 ilm 76
    }
77
 
78
    /**
79
     * Search the row with the passed <code>id</code> and return its index.
80
     *
81
     * @param id an ID of a SQLRow.
82
     * @return the index of the SQLRow or -1 if none is found.
83
     */
84
    public int indexFromID(int id) {
85
        for (int i = 0; i < this.getDisplayedItems().size(); i++) {
86
            final SQLRow row = this.getDisplayedItems().get(i);
87
            if (row != this.getALLValue() && row.getID() == id)
88
                return i;
89
        }
90
        return -1;
91
    }
92
 
93
    protected String toString(SQLRow item) {
94
        return this.getElement().getDescription(item);
95
    }
96
 
97
    public final SQLElement getElement() {
98
        return this.element;
99
    }
100
 
25 ilm 101
    @Override
102
    public void tableModified(SQLTableEvent evt) {
17 ilm 103
        // TODO test if that concern us
104
        this.reload();
105
    }
106
 
107
    public String toString() {
108
        return this.getClass().getName() + " on " + this.getElement();
109
    }
110
 
111
    protected void die() {
25 ilm 112
        this.getElement().getTable().removeTableModifiedListener(this);
17 ilm 113
    }
114
 
115
    @Override
116
    protected void idsChanged() {
117
        this.reload();
118
    }
119
 
120
    @Override
121
    protected boolean hasALLValue() {
122
        return true;
123
    }
124
}