OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 19 | Go to most recent revision | Details | 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;
28
 
29
import org.apache.commons.dbutils.ResultSetHandler;
30
import org.apache.commons.dbutils.handlers.ArrayListHandler;
31
 
32
public final class TaxeCache {
33
    private transient final Map<Integer, Float> mapTaux = new HashMap<Integer, Float>();
34
    private static TaxeCache instance;
35
    private transient Integer firstIdTaxe = null;
36
 
37
    private TaxeCache() {
38
        final DBRoot root = ((ComptaPropsConfiguration) Configuration.getInstance()).getRootSociete();
39
        final SQLTable table = root.getTable("TAXE");
40
        final SQLSelect sel = new SQLSelect(table.getBase());
41
        sel.addSelect(table.getField("ID_TAXE"));
42
        sel.addSelect(table.getField("TAUX"));
43
        final String req = sel.asString();
44
        root.getDBSystemRoot().getDataSource().execute(req, new ResultSetHandler() {
45
 
46
            public Object handle(final ResultSet resultSet) throws SQLException {
47
                while (resultSet.next()) {
48
                    final int idTaxe = resultSet.getInt(1);
49
                    final Float resultTaux = Float.valueOf(resultSet.getFloat(2));
50
                    TaxeCache.this.mapTaux.put(Integer.valueOf(idTaxe), resultTaux);
51
                }
52
                return null;
53
            }
54
        });
55
 
56
    }
57
 
58
    synchronized public static TaxeCache getCache() {
59
        if (instance == null) {
60
            instance = new TaxeCache();
61
        }
62
        return instance;
63
    }
64
 
65
    public Float getTauxFromId(final int idTaux) {
66
        return this.mapTaux.get(Integer.valueOf(idTaux));
67
    }
68
 
69
    public Integer getFirstTaxe() {
70
        if (this.firstIdTaxe == null) {
71
            final SQLBase table = ((ComptaPropsConfiguration) Configuration.getInstance()).getSQLBaseSociete();
72
            final SQLSelect sel = new SQLSelect(table);
73
            sel.addSelect("TAXE.ID_TAXE");
74
            sel.addSelect("TAXE.TAUX");
75
            final String req = sel.asString();
76
            final List<Object[]> list = (List<Object[]>) table.getDataSource().execute(req, new ArrayListHandler());
77
            if (list != null && !list.isEmpty()) {
78
                final Object[] tmp = list.get(0);
79
                this.firstIdTaxe = Integer.parseInt(tmp[0].toString());
80
            }
81
        }
82
        return this.firstIdTaxe;
83
 
84
    }
85
}