OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev 67 Rev 83
Line 27... Line 27...
27
import java.util.Arrays;
27
import java.util.Arrays;
28
import java.util.HashMap;
28
import java.util.HashMap;
29
import java.util.List;
29
import java.util.List;
30
import java.util.Map;
30
import java.util.Map;
31
 
31
 
-
 
32
import net.jcip.annotations.GuardedBy;
-
 
33
import net.jcip.annotations.ThreadSafe;
-
 
34
 
32
/**
35
/**
33
 * Gère la représentation des clefs externes. TODO Pour ne pas avoir la désignation du site :
36
 * Gère la représentation des clefs externes. TODO Pour ne pas avoir la désignation du site :
34
 * pouvoir spécifier ELEMENT_TABLEAU.ID_TABLEAU_ELECTRIQUE => DESIGNATION,
37
 * pouvoir spécifier ELEMENT_TABLEAU.ID_TABLEAU_ELECTRIQUE => DESIGNATION,
35
 * ID_LOCAL.ID_BATIMENT.DESIGNATION, ID_LOCAL.DESIGNATION
38
 * ID_LOCAL.ID_BATIMENT.DESIGNATION, ID_LOCAL.DESIGNATION
36
 * 
39
 * 
37
 * @author Sylvain CUAZ
40
 * @author Sylvain CUAZ
38
 */
41
 */
-
 
42
@ThreadSafe
39
public class ShowAs extends FieldExpander {
43
public class ShowAs extends FieldExpander {
40
 
44
 
-
 
45
    @GuardedBy("this")
41
    private DBRoot root;
46
    private DBRoot root;
42
    // eg /OBSERVATION/ -> [ID_ARTICLE_1, DESIGNATION]
47
    // eg /OBSERVATION/ -> [ID_ARTICLE_1, DESIGNATION]
-
 
48
    @GuardedBy("this")
43
    private final Map<SQLTable, List<SQLField>> byTables;
49
    private final Map<SQLTable, List<SQLField>> byTables;
44
    // eg |TABLEAU.ID_OBSERVATION| -> [DESIGNATION]
50
    // eg |TABLEAU.ID_OBSERVATION| -> [DESIGNATION]
-
 
51
    @GuardedBy("this")
45
    private final Map<SQLField, List<SQLField>> byFields;
52
    private final Map<SQLField, List<SQLField>> byFields;
46
 
53
 
47
    public ShowAs(DBRoot root) {
54
    public ShowAs(DBRoot root) {
48
        super();
55
        super();
49
        this.byTables = new HashMap<SQLTable, List<SQLField>>();
56
        this.byTables = new HashMap<SQLTable, List<SQLField>>();
50
        this.byFields = new HashMap<SQLField, List<SQLField>>();
57
        this.byFields = new HashMap<SQLField, List<SQLField>>();
51
 
58
 
52
        this.setRoot(root);
59
        this.setRoot(root);
53
    }
60
    }
54
 
61
 
55
    public final void putAll(ShowAs s) {
62
    public synchronized final void putAll(ShowAs s) {
56
        CollectionUtils.addIfNotPresent(this.byFields, s.byFields);
63
        CollectionUtils.addIfNotPresent(this.byFields, s.byFields);
57
        CollectionUtils.addIfNotPresent(this.byTables, s.byTables);
64
        CollectionUtils.addIfNotPresent(this.byTables, s.byTables);
58
        // s might have replaced some of our entries
65
        // s might have replaced some of our entries
59
        this.clearCache();
66
        this.clearCache();
60
    }
67
    }
61
 
68
 
62
    public List<SQLField> getFieldExpand(SQLTable table) {
69
    public synchronized List<SQLField> getFieldExpand(SQLTable table) {
63
        return this.byTables.get(table);
70
        return this.byTables.get(table);
64
    }
71
    }
65
 
72
 
66
    /**
73
    /**
67
     * Set the base which is used when passing String in lieu of SQLField or SQLTable.
74
     * Set the base which is used when passing String in lieu of SQLField or SQLTable.
68
     * 
75
     * 
69
     * @param root the base to use.
76
     * @param root the base to use.
70
     */
77
     */
71
    public final void setRoot(DBRoot root) {
78
    public synchronized final void setRoot(DBRoot root) {
72
        this.root = root;
79
        this.root = root;
73
    }
80
    }
74
 
81
 
75
    private SQLField getField(String fieldName) {
82
    private synchronized SQLField getField(String fieldName) {
76
        return this.root.getDesc(SQLName.parse(fieldName), SQLField.class);
83
        return this.root.getDesc(SQLName.parse(fieldName), SQLField.class);
77
    }
84
    }
78
 
85
 
79
    private SQLTable getTable(String tableName) {
86
    private synchronized SQLTable getTable(String tableName) {
80
        try {
87
        try {
81
            return this.root.getDesc(SQLName.parse(tableName), SQLTable.class);
88
            return this.root.getDesc(SQLName.parse(tableName), SQLTable.class);
82
        } catch (DBStructureItemNotFound e) {
89
        } catch (DBStructureItemNotFound e) {
83
            return null;
90
            return null;
84
        }
91
        }
Line 91... Line 98...
91
        }
98
        }
92
        return res;
99
        return res;
93
    }
100
    }
94
 
101
 
95
    // TODO a listener to remove tables and fields as they are dropped
102
    // TODO a listener to remove tables and fields as they are dropped
96
    public final void removeTable(SQLTable t) {
103
    public synchronized final void removeTable(SQLTable t) {
97
        this.byTables.remove(t);
104
        this.byTables.remove(t);
98
        for (final SQLField f : t.getFields())
105
        for (final SQLField f : t.getFields())
99
            this.byFields.remove(f);
106
            this.byFields.remove(f);
100
        this.clearCache();
107
        this.clearCache();
101
    }
108
    }
102
 
109
 
103
    public final void clear() {
110
    public synchronized final void clear() {
104
        this.setRoot(null);
111
        this.setRoot(null);
105
        this.byTables.clear();
112
        this.byTables.clear();
106
        this.byFields.clear();
113
        this.byFields.clear();
107
        this.clearCache();
114
        this.clearCache();
108
    }
115
    }
Line 118... Line 125...
118
     * n'existe pas, cette méthode n'a pas d'effet.
125
     * n'existe pas, cette méthode n'a pas d'effet.
119
     * 
126
     * 
120
     * @param tableName le nom de la table, eg "ETABLISSEMENT".
127
     * @param tableName le nom de la table, eg "ETABLISSEMENT".
121
     * @param fields les noms des champs, eg ["DESCRIPTION", "NUMERO"].
128
     * @param fields les noms des champs, eg ["DESCRIPTION", "NUMERO"].
122
     */
129
     */
123
    public void show(String tableName, List<String> fields) {
130
    public synchronized void show(String tableName, List<String> fields) {
124
        final SQLTable table = this.getTable(tableName);
131
        final SQLTable table = this.getTable(tableName);
125
        if (table != null) {
132
        if (table != null) {
126
            this.show(table, fields);
133
            this.show(table, fields);
127
        } else {
134
        } else {
128
            Log.get().warning(this.root + " does not contain the table:" + tableName);
135
            Log.get().warning(this.root + " does not contain the table:" + tableName);
Line 131... Line 138...
131
 
138
 
132
    public void show(SQLTable table, String... fields) {
139
    public void show(SQLTable table, String... fields) {
133
        this.show(table, Arrays.asList(fields));
140
        this.show(table, Arrays.asList(fields));
134
    }
141
    }
135
 
142
 
136
    public void show(SQLTable table, List<String> fields) {
143
    public synchronized void show(SQLTable table, List<String> fields) {
137
        this.byTables.put(table, namesToFields(fields, table));
144
        this.byTables.put(table, namesToFields(fields, table));
138
        this.clearCache();
145
        this.clearCache();
139
    }
146
    }
140
 
147
 
141
    // *** byFields
148
    // *** byFields
Line 149... Line 156...
149
     * contrôler plus en finesse.
156
     * contrôler plus en finesse.
150
     * 
157
     * 
151
     * @param fieldName le nom du champ, eg "CONTACT.ID_ETABLISSEMENT".
158
     * @param fieldName le nom du champ, eg "CONTACT.ID_ETABLISSEMENT".
152
     * @param fields les noms des champs, eg ["DESCRIPTION"].
159
     * @param fields les noms des champs, eg ["DESCRIPTION"].
153
     */
160
     */
154
    public void showField(String fieldName, List<String> fields) {
161
    public synchronized void showField(String fieldName, List<String> fields) {
155
        this.show(getField(fieldName), fields);
162
        this.show(getField(fieldName), fields);
156
    }
163
    }
157
 
164
 
158
    public final void show(SQLField field, List<String> fields) {
165
    public synchronized final void show(SQLField field, List<String> fields) {
159
        this.byFields.put(field, namesToFields(fields, field.getTable().getBase().getGraph().getForeignTable(field)));
166
        this.byFields.put(field, namesToFields(fields, field.getTable().getBase().getGraph().getForeignTable(field)));
160
        this.clearCache();
167
        this.clearCache();
161
    }
168
    }
162
 
169
 
163
    // *** expand
170
    // *** expand
164
 
171
 
165
    @Override
172
    @Override
166
    protected List<SQLField> expandOnce(SQLField field) {
173
    protected synchronized List<SQLField> expandOnce(SQLField field) {
167
        // c'est une clef externe, donc elle pointe sur une table
174
        // c'est une clef externe, donc elle pointe sur une table
168
        final SQLTable foreignTable = field.getTable().getBase().getGraph().getForeignTable(field);
175
        final SQLTable foreignTable = field.getTable().getBase().getGraph().getForeignTable(field);
169
        final List<SQLField> res;
176
        final List<SQLField> res;
170
        if (this.byFields.containsKey(field)) {
177
        if (this.byFields.containsKey(field)) {
171
            res = this.byFields.get(field);
178
            res = this.byFields.get(field);
Line 182... Line 189...
182
     * Expand le champ.
189
     * Expand le champ.
183
     * 
190
     * 
184
     * @param fieldName le nom du champ à expandre, eg "SITE.ID_ETABLISSEMENT".
191
     * @param fieldName le nom du champ à expandre, eg "SITE.ID_ETABLISSEMENT".
185
     * @return la liste des champs, eg [|ETABLISSEMENT.DESCRIPTION|, |ETABLISSEMENT.NUMERO|].
192
     * @return la liste des champs, eg [|ETABLISSEMENT.DESCRIPTION|, |ETABLISSEMENT.NUMERO|].
186
     */
193
     */
187
    public List<SQLField> simpleExpand(String fieldName) {
194
    public synchronized List<SQLField> simpleExpand(String fieldName) {
188
        return this.simpleExpand(getField(fieldName));
195
        return this.simpleExpand(getField(fieldName));
189
    }
196
    }
190
 
197
 
191
    @Override
198
    @Override
192
    public String toString() {
199
    public synchronized String toString() {
193
        return super.toString() + " byTables: " + this.byTables + " byFields: " + this.byFields;
200
        return super.toString() + " byTables: " + this.byTables + " byFields: " + this.byFields;
194
    }
201
    }
195
 
202
 
196
}
203
}