OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 19 | Rev 80 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
18 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.erp.core.finance.tax.model;
15
 
16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
17
import org.openconcerto.sql.Configuration;
18
import org.openconcerto.sql.model.DBRoot;
19
import org.openconcerto.sql.model.SQLBase;
20
import org.openconcerto.sql.model.SQLSelect;
21
import org.openconcerto.sql.model.SQLTable;
22
 
23
import java.sql.ResultSet;
24
import java.sql.SQLException;
25
import java.util.HashMap;
26
import java.util.List;
27
import java.util.Map;
19 ilm 28
import java.util.Set;
18 ilm 29
 
30
import org.apache.commons.dbutils.ResultSetHandler;
31
import org.apache.commons.dbutils.handlers.ArrayListHandler;
32
 
33
public final class TaxeCache {
34
 
65 ilm 35
    static private final SQLSelect getSel() {
18 ilm 36
        final DBRoot root = ((ComptaPropsConfiguration) Configuration.getInstance()).getRootSociete();
37
        final SQLTable table = root.getTable("TAXE");
38
        final SQLSelect sel = new SQLSelect(table.getBase());
39
        sel.addSelect(table.getField("ID_TAXE"));
40
        sel.addSelect(table.getField("TAUX"));
65 ilm 41
        return sel;
42
    }
43
 
44
    private transient final Map<Integer, Float> mapTaux = new HashMap<Integer, Float>();
45
    private static TaxeCache instance;
46
    private transient Integer firstIdTaxe = null;
47
 
48
    private TaxeCache() {
49
        final SQLSelect sel = getSel();
18 ilm 50
        final String req = sel.asString();
65 ilm 51
        sel.getSelectFields().get(0).getDBSystemRoot().getDataSource().execute(req, new ResultSetHandler() {
18 ilm 52
 
53
            public Object handle(final ResultSet resultSet) throws SQLException {
54
                while (resultSet.next()) {
55
                    final int idTaxe = resultSet.getInt(1);
56
                    final Float resultTaux = Float.valueOf(resultSet.getFloat(2));
57
                    TaxeCache.this.mapTaux.put(Integer.valueOf(idTaxe), resultTaux);
58
                }
59
                return null;
60
            }
61
        });
62
 
63
    }
64
 
65
    synchronized public static TaxeCache getCache() {
66
        if (instance == null) {
67
            instance = new TaxeCache();
68
        }
69
        return instance;
70
    }
71
 
72
    public Float getTauxFromId(final int idTaux) {
73
        return this.mapTaux.get(Integer.valueOf(idTaux));
74
    }
75
 
76
    public Integer getFirstTaxe() {
77
        if (this.firstIdTaxe == null) {
78
            final SQLBase table = ((ComptaPropsConfiguration) Configuration.getInstance()).getSQLBaseSociete();
65 ilm 79
            final SQLSelect sel = getSel();
18 ilm 80
            final String req = sel.asString();
81
            final List<Object[]> list = (List<Object[]>) table.getDataSource().execute(req, new ArrayListHandler());
82
            if (list != null && !list.isEmpty()) {
83
                final Object[] tmp = list.get(0);
84
                this.firstIdTaxe = Integer.parseInt(tmp[0].toString());
85
            }
86
        }
87
        return this.firstIdTaxe;
88
 
89
    }
19 ilm 90
 
91
    public Integer getIdFromTaux(Float tax) {
92
        Set<Integer> s = (Set<Integer>) mapTaux.keySet();
93
        for (Integer integer : s) {
94
            if (this.mapTaux.get(integer).equals(tax)) {
95
                return integer;
96
            }
97
        }
98
        return null;
99
    }
18 ilm 100
}