OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 25 | 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 28 mai 2005
16
 *
17
 */
18
package org.openconcerto.sql.navigator;
19
 
20
import org.openconcerto.sql.element.SQLElement;
21
import org.openconcerto.sql.model.SQLSelect;
22
import org.openconcerto.sql.model.Where;
23
 
24
import java.util.ArrayList;
25
import java.util.HashMap;
26
import java.util.List;
27
import java.util.Map;
28
 
29
/**
30
 * TODO hide empty item
31
 *
32
 * @author Sylvain CUAZ
33
 */
34
public class ElementsSQLListModel extends SQLListModel<SQLElement> {
35
 
36
    private final List<SQLElement> elements;
37
    private final Map<SQLElement, Number> counts;
38
 
39
    public ElementsSQLListModel(List<SQLElement> elements) {
40
        super();
41
        this.counts = new HashMap<SQLElement, Number>();
42
        if (elements.size() == 0)
43
            throw new IllegalArgumentException("elements empty");
44
        this.elements = new ArrayList<SQLElement>(elements);
45
    }
46
 
25 ilm 47
    @Override
48
    protected void reload(boolean noCache) {
17 ilm 49
        this.counts.clear();
50
        final List<SQLElement> res = new ArrayList<SQLElement>(this.elements.size());
51
        for (final SQLElement elem : this.elements) {
52
            // if (this.getCount(elem) > 0)
53
            res.add(elem);
54
        }
55
        this.setAll(res);
56
    }
57
 
58
    private final int getCount(SQLElement elem) {
59
        if (!this.counts.containsKey(elem)) {
81 ilm 60
            final SQLSelect sel = new SQLSelect();
17 ilm 61
            sel.addSelectFunctionStar("count");
81 ilm 62
            sel.setWhere(new Where(elem.getParentForeignField(), this.getIds()));
17 ilm 63
            final Number count = (Number) elem.getTable().getBase().getDataSource().executeScalar(sel.asString());
64
            this.counts.put(elem, count);
65
        }
66
        return this.counts.get(elem).intValue();
67
    }
68
 
69
    protected String toString(SQLElement e) {
70
        return e.getPluralName() + " (" + this.getCount(e) + ")";
71
    }
72
 
73
    @Override
74
    protected void idsChanged() {
75
        this.reload();
76
    }
77
 
78
}