OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 144 | Rev 177 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 144 Rev 156
Line 16... Line 16...
16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
17
import org.openconcerto.sql.Configuration;
17
import org.openconcerto.sql.Configuration;
18
import org.openconcerto.sql.model.DBRoot;
18
import org.openconcerto.sql.model.DBRoot;
19
import org.openconcerto.sql.model.SQLRow;
19
import org.openconcerto.sql.model.SQLRow;
20
import org.openconcerto.sql.model.SQLRowAccessor;
20
import org.openconcerto.sql.model.SQLRowAccessor;
21
import org.openconcerto.sql.model.SQLRowListRSH;
21
import org.openconcerto.sql.model.SQLRowValues;
22
import org.openconcerto.sql.model.SQLSelect;
22
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
23
import org.openconcerto.sql.model.SQLTable;
23
import org.openconcerto.sql.model.SQLTable;
24
import org.openconcerto.sql.model.Where;
24
import org.openconcerto.sql.model.Where;
25
import org.openconcerto.utils.ExceptionHandler;
25
import org.openconcerto.utils.ExceptionHandler;
26
 
26
 
-
 
27
import java.util.Arrays;
27
import java.util.Collections;
28
import java.util.Collections;
28
import java.util.HashMap;
29
import java.util.HashMap;
29
import java.util.LinkedHashMap;
30
import java.util.LinkedHashMap;
30
import java.util.List;
31
import java.util.List;
31
import java.util.Map;
32
import java.util.Map;
32
import java.util.Set;
33
import java.util.Set;
33
 
34
 
34
public final class TaxeCache {
35
public final class TaxeCache {
35
 
36
 
36
    static private final SQLSelect getSel() {
37
    private static final SQLRowValuesListFetcher getSel() {
37
        // FIXME récupérer les comptes de TVA pour eviter les fetchs
-
 
38
        final DBRoot root = ((ComptaPropsConfiguration) Configuration.getInstance()).getRootSociete();
38
        final DBRoot root = ((ComptaPropsConfiguration) Configuration.getInstance()).getRootSociete();
39
        final SQLTable table = root.getTable("TAXE");
39
        final SQLTable table = root.getTable("TAXE");
40
        final SQLSelect sel = new SQLSelect();
40
        SQLRowValues rowVals = new SQLRowValues(table);
-
 
41
        rowVals.putNulls("TAUX", "CODE", "NOM", "ID_TAXE", "DEFAULT");
-
 
42
        List<String> compteFields = Arrays.asList("ID_COMPTE_PCE_COLLECTE", "ID_COMPTE_PCE_DED", "ID_COMPTE_PCE", "ID_COMPTE_PCE_VENTE", "ID_COMPTE_PCE_VENTE_SERVICE", "ID_COMPTE_PCE_COLLECTE_INTRA",
41
        sel.addSelectStar(table);
43
                "ID_COMPTE_PCE_DED_INTRA");
-
 
44
        for (String foreignFieldName : compteFields) {
-
 
45
            rowVals.putRowValues(foreignFieldName).putNulls("NUMERO", "NOM");
42
        return sel;
46
        }
-
 
47
        return SQLRowValuesListFetcher.create(rowVals);
43
    }
48
    }
44
 
49
 
45
    private transient final Map<Integer, Float> mapTaux = new HashMap<Integer, Float>();
50
    private final Map<Integer, Float> mapTaux = new HashMap<>();
46
    private transient final Map<SQLRowAccessor, Float> mapRowTaux = new LinkedHashMap<SQLRowAccessor, Float>();
51
    private final Map<SQLRowAccessor, Float> mapRowTaux = new LinkedHashMap<>();
47
    private static TaxeCache instance;
52
    private static TaxeCache instance;
48
    private transient SQLRow firstIdTaxe = null;
53
    private SQLRow firstIdTaxe = null;
49
 
54
 
50
    private TaxeCache() {
55
    private TaxeCache() {
-
 
56
        loadCache();
-
 
57
    }
-
 
58
 
-
 
59
    private void loadCache() {
-
 
60
 
-
 
61
        this.mapRowTaux.clear();
-
 
62
        this.mapTaux.clear();
51
        final SQLSelect sel = getSel();
63
        this.firstIdTaxe = null;
52
 
64
 
-
 
65
        final SQLRowValuesListFetcher sel = getSel();
-
 
66
 
53
        final List<SQLRow> l = SQLRowListRSH.execute(sel);
67
        final List<SQLRowValues> l = sel.fetch();
54
        for (SQLRow sqlRow : l) {
68
        for (SQLRowValues sqlRow : l) {
55
            this.mapRowTaux.put(sqlRow, sqlRow.getFloat("TAUX"));
69
            this.mapRowTaux.put(sqlRow, sqlRow.getFloat("TAUX"));
56
            this.mapTaux.put(sqlRow.getID(), sqlRow.getFloat("TAUX"));
70
            this.mapTaux.put(sqlRow.getID(), sqlRow.getFloat("TAUX"));
57
            if (sqlRow.getBoolean("DEFAULT")) {
71
            if (sqlRow.getBoolean("DEFAULT")) {
58
                this.firstIdTaxe = sqlRow;
72
                this.firstIdTaxe = sqlRow.asRow();
59
            }
73
            }
60
        }
74
        }
61
    }
75
    }
62
 
76
 
63
    synchronized public static TaxeCache getCache() {
77
    public static synchronized TaxeCache getCache() {
64
        if (instance == null) {
78
        if (instance == null) {
65
            instance = new TaxeCache();
79
            instance = new TaxeCache();
66
        }
80
        }
67
        return instance;
81
        return instance;
68
    }
82
    }
69
 
83
 
70
    public Float getTauxFromId(final int idTaux) {
84
    public synchronized Float getTauxFromId(final int idTaux) {
-
 
85
        Float f = this.mapTaux.get(Integer.valueOf(idTaux));
-
 
86
        if (f == null) {
-
 
87
            loadCache();
71
        return this.mapTaux.get(Integer.valueOf(idTaux));
88
            f = this.mapTaux.get(Integer.valueOf(idTaux));
-
 
89
        }
-
 
90
        return f;
72
    }
91
    }
73
 
92
 
74
    public SQLRowAccessor getRowFromId(final int idTaux) {
93
    public synchronized SQLRowAccessor getRowFromId(final int idTaux) {
75
        Set<SQLRowAccessor> s = mapRowTaux.keySet();
94
        Set<SQLRowAccessor> s = mapRowTaux.keySet();
76
        for (SQLRowAccessor r : s) {
95
        for (SQLRowAccessor r : s) {
77
            if (r.getID() == idTaux) {
96
            if (r.getID() == idTaux) {
78
                return r;
97
                return r;
79
            }
98
            }
80
        }
99
        }
-
 
100
        loadCache();
-
 
101
        for (SQLRowAccessor r : s) {
-
 
102
            if (r.getID() == idTaux) {
-
 
103
                return r;
-
 
104
            }
-
 
105
        }
81
        return null;
106
        return null;
82
 
107
 
83
    }
108
    }
84
 
109
 
85
    public SQLRow getFirstTaxe() {
110
    public synchronized SQLRow getFirstTaxe() {
86
        if (this.firstIdTaxe == null) {
111
        if (this.firstIdTaxe == null) {
87
            final SQLSelect sel = getSel();
112
            final SQLRowValuesListFetcher sel = getSel();
88
            sel.setWhere(new Where(sel.getTable("TAXE").getField("DEFAULT"), "=", Boolean.TRUE));
113
            final List<SQLRowValues> rows = sel.fetch(new Where(sel.getReq().getTable("TAXE").getField("DEFAULT"), "=", Boolean.TRUE));
89
            final List<SQLRow> rows = SQLRowListRSH.execute(sel);
-
 
90
            if (rows != null && !rows.isEmpty()) {
114
            if (rows != null && !rows.isEmpty()) {
91
 
115
 
92
                this.firstIdTaxe = rows.get(0);
116
                this.firstIdTaxe = rows.get(0).asRow();
93
            } else {
117
            } else {
94
                ExceptionHandler.handle("Aucune TVA par défaut définie!", new IllegalArgumentException("Aucune TVA par défaut définie!"));
118
                ExceptionHandler.handle("Aucune TVA par défaut définie!", new IllegalArgumentException("Aucune TVA par défaut définie!"));
95
                return mapRowTaux.keySet().iterator().next().asRow();
119
                return mapRowTaux.keySet().iterator().next().asRow();
96
            }
120
            }
97
 
121
 
98
        }
122
        }
99
        return this.firstIdTaxe;
123
        return this.firstIdTaxe;
100
    }
124
    }
101
 
125
 
102
    public Integer getIdFromTaux(Float tax) {
126
    public synchronized Integer getIdFromTaux(Float tax) {
103
        Set<Integer> s = (Set<Integer>) mapTaux.keySet();
127
        Set<Integer> s = mapTaux.keySet();
-
 
128
        for (Integer integer : s) {
-
 
129
            if (this.mapTaux.get(integer).equals(tax)) {
-
 
130
                return integer;
-
 
131
            }
-
 
132
        }
-
 
133
        loadCache();
104
        for (Integer integer : s) {
134
        for (Integer integer : s) {
105
            if (this.mapTaux.get(integer).equals(tax)) {
135
            if (this.mapTaux.get(integer).equals(tax)) {
106
                return integer;
136
                return integer;
107
            }
137
            }
108
        }
138
        }
109
        return null;
139
        return null;
110
    }
140
    }
111
 
141
 
112
    public Set<SQLRowAccessor> getAllTaxe() {
142
    public synchronized Set<SQLRowAccessor> getAllTaxe() {
113
        return Collections.unmodifiableSet(this.mapRowTaux.keySet());
143
        return Collections.unmodifiableSet(this.mapRowTaux.keySet());
114
    }
144
    }
115
 
145
 
116
}
146
}