OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 25 | Go to most recent revision | Details | 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
 
47
    protected void reload() {
48
        this.counts.clear();
49
        final List<SQLElement> res = new ArrayList<SQLElement>(this.elements.size());
50
        for (final SQLElement elem : this.elements) {
51
            // if (this.getCount(elem) > 0)
52
            res.add(elem);
53
        }
54
        this.setAll(res);
55
    }
56
 
57
    private final int getCount(SQLElement elem) {
58
        if (!this.counts.containsKey(elem)) {
59
            final SQLSelect sel = new SQLSelect(elem.getTable().getBase());
60
            sel.addSelectFunctionStar("count");
61
            sel.setWhere(new Where(elem.getTable().getField(elem.getParentForeignField()), this.getIds()));
62
            final Number count = (Number) elem.getTable().getBase().getDataSource().executeScalar(sel.asString());
63
            this.counts.put(elem, count);
64
        }
65
        return this.counts.get(elem).intValue();
66
    }
67
 
68
    protected String toString(SQLElement e) {
69
        return e.getPluralName() + " (" + this.getCount(e) + ")";
70
    }
71
 
72
    @Override
73
    protected void idsChanged() {
74
        this.reload();
75
    }
76
 
77
}