OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 81 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 81 Rev 83
Line 26... Line 26...
26
import java.util.HashMap;
26
import java.util.HashMap;
27
import java.util.List;
27
import java.util.List;
28
import java.util.Map;
28
import java.util.Map;
29
import java.util.Set;
29
import java.util.Set;
30
 
30
 
-
 
31
import net.jcip.annotations.GuardedBy;
-
 
32
import net.jcip.annotations.ThreadSafe;
-
 
33
 
-
 
34
@ThreadSafe
31
public abstract class FieldExpander {
35
public abstract class FieldExpander {
32
 
36
 
33
    static private final FieldExpander EMPTY = new FieldExpander() {
37
    static private final FieldExpander EMPTY = new FieldExpander() {
34
        @Override
38
        @Override
35
        protected List<SQLField> expandOnce(SQLField field) {
39
        protected List<SQLField> expandOnce(SQLField field) {
Line 40... Line 44...
40
    public static FieldExpander getEmpty() {
44
    public static FieldExpander getEmpty() {
41
        return EMPTY;
45
        return EMPTY;
42
    }
46
    }
43
 
47
 
44
    // eg |TABLEAU.ID_OBSERVATION| -> [[DESIGNATION], []]
48
    // eg |TABLEAU.ID_OBSERVATION| -> [[DESIGNATION], []]
-
 
49
    @GuardedBy("this")
45
    private final Map<IFieldPath, List<FieldPath>> cache;
50
    private final Map<IFieldPath, List<FieldPath>> cache;
-
 
51
    @GuardedBy("this")
46
    private final Map<List<? extends IFieldPath>, List<Tuple2<Path, List<FieldPath>>>> cacheGroupBy;
52
    private final Map<List<? extends IFieldPath>, List<Tuple2<Path, List<FieldPath>>>> cacheGroupBy;
47
 
53
 
48
    public FieldExpander() {
54
    public FieldExpander() {
49
        super();
55
        super();
50
        this.cache = new HashMap<IFieldPath, List<FieldPath>>();
56
        this.cache = new HashMap<IFieldPath, List<FieldPath>>();
Line 53... Line 59...
53
 
59
 
54
    /**
60
    /**
55
     * If for the same input expandOnce() will now return a different output, you have to call this
61
     * If for the same input expandOnce() will now return a different output, you have to call this
56
     * method.
62
     * method.
57
     */
63
     */
58
    protected void clearCache() {
64
    protected synchronized void clearCache() {
59
        this.cache.clear();
65
        this.cache.clear();
60
        this.cacheGroupBy.clear();
66
        this.cacheGroupBy.clear();
61
    }
67
    }
62
 
68
 
63
    // *** expand
69
    // *** expand
64
 
70
 
65
    protected abstract List<SQLField> expandOnce(SQLField field);
71
    protected abstract List<SQLField> expandOnce(SQLField field);
66
 
72
 
67
    protected final List<FieldPath> expandOnce(IFieldPath field) {
73
    protected synchronized final List<FieldPath> expandOnce(IFieldPath field) {
68
        final List<SQLField> e = this.expandOnce(field.getField());
74
        final List<SQLField> e = this.expandOnce(field.getField());
69
        if (e == null)
75
        if (e == null)
70
            return null;
76
            return null;
71
        if (e.size() == 0)
77
        if (e.size() == 0)
72
            return Collections.emptyList();
78
            return Collections.emptyList();
Line 96... Line 102...
96
     * Expand le champ.
102
     * Expand le champ.
97
     * 
103
     * 
98
     * @param field le nom du champ à expandre, eg "SITE.ID_ETABLISSEMENT".
104
     * @param field le nom du champ à expandre, eg "SITE.ID_ETABLISSEMENT".
99
     * @return les champs.
105
     * @return les champs.
100
     */
106
     */
101
    public final List<FieldPath> expand(IFieldPath field) {
107
    public synchronized final List<FieldPath> expand(IFieldPath field) {
102
        // eg field == BATIMENT.ID_SITE
108
        // eg field == BATIMENT.ID_SITE
103
        if (this.cache.containsKey(field)) {
109
        if (this.cache.containsKey(field)) {
104
            return this.cache.get(field);
110
            return this.cache.get(field);
105
        }
111
        }
106
 
112
 
Line 133... Line 139...
133
     * Expand the passed SQLRowValues by replacing each foreign key with a SQLRowValues containing
139
     * Expand the passed SQLRowValues by replacing each foreign key with a SQLRowValues containing
134
     * the expanded fields.
140
     * the expanded fields.
135
     * 
141
     * 
136
     * @param vals the SQLRowValues to expand.
142
     * @param vals the SQLRowValues to expand.
137
     */
143
     */
138
    public final void expand(SQLRowValues vals) {
144
    public synchronized final void expand(SQLRowValues vals) {
139
        final Set<SQLField> fks = vals.getTable().getForeignKeys();
145
        final Set<SQLField> fks = vals.getTable().getForeignKeys();
140
        for (final String fName : vals.getFields()) {
146
        for (final String fName : vals.getFields()) {
141
            final SQLField ffield = vals.getTable().getField(fName);
147
            final SQLField ffield = vals.getTable().getField(fName);
142
            if (fks.contains(ffield)) {
148
            if (fks.contains(ffield)) {
143
                final List<SQLField> expandedFields = this.expandOnce(ffield);
149
                final List<SQLField> expandedFields = this.expandOnce(ffield);
Line 161... Line 167...
161
     * 
167
     * 
162
     * @param fieldsOrig fields of a table, eg [ DESIGNATION, ID_BATIMENT].
168
     * @param fieldsOrig fields of a table, eg [ DESIGNATION, ID_BATIMENT].
163
     * @return the complete expansion, eg [ [LOCAL.DESIGNATION], [BAT.DES], [SITE.DES, ADRESSE.CP],
169
     * @return the complete expansion, eg [ [LOCAL.DESIGNATION], [BAT.DES], [SITE.DES, ADRESSE.CP],
164
     *         [ETABLISSEMENT.DES] ].
170
     *         [ETABLISSEMENT.DES] ].
165
     */
171
     */
166
    public final List<Tuple2<Path, List<FieldPath>>> expandGroupBy(List<? extends IFieldPath> fieldsOrig) {
172
    public synchronized final List<Tuple2<Path, List<FieldPath>>> expandGroupBy(List<? extends IFieldPath> fieldsOrig) {
167
        if (this.cacheGroupBy.containsKey(fieldsOrig))
173
        if (this.cacheGroupBy.containsKey(fieldsOrig))
168
            return this.cacheGroupBy.get(fieldsOrig);
174
            return this.cacheGroupBy.get(fieldsOrig);
169
 
175
 
170
        if (fieldsOrig.size() == 0)
176
        if (fieldsOrig.size() == 0)
171
            return Collections.emptyList();
177
            return Collections.emptyList();