OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev 80 Rev 83
Line 16... Line 16...
16
import org.openconcerto.sql.Log;
16
import org.openconcerto.sql.Log;
17
import org.openconcerto.sql.TM;
17
import org.openconcerto.sql.TM;
18
import org.openconcerto.sql.model.DBStructureItemNotFound;
18
import org.openconcerto.sql.model.DBStructureItemNotFound;
19
import org.openconcerto.sql.model.SQLName;
19
import org.openconcerto.sql.model.SQLName;
20
import org.openconcerto.sql.model.SQLTable;
20
import org.openconcerto.sql.model.SQLTable;
21
import org.openconcerto.utils.CollectionMap;
-
 
22
import org.openconcerto.utils.CollectionUtils;
21
import org.openconcerto.utils.CollectionUtils;
-
 
22
import org.openconcerto.utils.SetMap;
23
import org.openconcerto.utils.cc.ITransformer;
23
import org.openconcerto.utils.cc.ITransformer;
24
import org.openconcerto.utils.i18n.LocalizedInstances;
24
import org.openconcerto.utils.i18n.LocalizedInstances;
25
import org.openconcerto.utils.i18n.Phrase;
25
import org.openconcerto.utils.i18n.Phrase;
26
import org.openconcerto.utils.i18n.TranslationManager;
26
import org.openconcerto.utils.i18n.TranslationManager;
27
 
27
 
Line 30... Line 30...
30
import java.lang.reflect.InvocationTargetException;
30
import java.lang.reflect.InvocationTargetException;
31
import java.util.ArrayList;
31
import java.util.ArrayList;
32
import java.util.Collection;
32
import java.util.Collection;
33
import java.util.Collections;
33
import java.util.Collections;
34
import java.util.HashMap;
34
import java.util.HashMap;
35
import java.util.HashSet;
-
 
36
import java.util.List;
35
import java.util.List;
37
import java.util.Locale;
36
import java.util.Locale;
38
import java.util.Map;
37
import java.util.Map;
39
import java.util.Set;
38
import java.util.Set;
40
 
39
 
Line 69... Line 68...
69
            return res;
68
            return res;
70
        }
69
        }
71
    };
70
    };
72
 
71
 
73
    private final Map<SQLTable, SQLElement> elements;
72
    private final Map<SQLTable, SQLElement> elements;
74
    private final CollectionMap<String, SQLTable> tableNames;
73
    private final SetMap<String, SQLTable> tableNames;
75
    private final CollectionMap<String, SQLTable> byCode;
74
    private final SetMap<String, SQLTable> byCode;
76
    private final CollectionMap<Class<? extends SQLElement>, SQLTable> byClass;
75
    private final SetMap<Class<? extends SQLElement>, SQLTable> byClass;
77
    private final List<DirectoryListener> listeners;
76
    private final List<DirectoryListener> listeners;
78
 
77
 
79
    private String phrasesPkgName;
78
    private String phrasesPkgName;
80
    private final Map<String, SQLElementNames> elementNames;
79
    private final Map<String, SQLElementNames> elementNames;
81
 
80
 
82
    public SQLElementDirectory() {
81
    public SQLElementDirectory() {
83
        this.elements = new HashMap<SQLTable, SQLElement>();
82
        this.elements = new HashMap<SQLTable, SQLElement>();
84
        // to mimic elements behaviour, if we add twice the same table
83
        // to mimic elements behaviour, if we add twice the same table
85
        // the second one should replace the first one
84
        // the second one should replace the first one
86
        this.tableNames = new CollectionMap<String, SQLTable>(HashSet.class);
85
        this.tableNames = new SetMap<String, SQLTable>();
87
        this.byCode = new CollectionMap<String, SQLTable>(HashSet.class);
86
        this.byCode = new SetMap<String, SQLTable>();
88
        this.byClass = new CollectionMap<Class<? extends SQLElement>, SQLTable>(HashSet.class);
87
        this.byClass = new SetMap<Class<? extends SQLElement>, SQLTable>();
89
 
88
 
90
        this.listeners = new ArrayList<DirectoryListener>();
89
        this.listeners = new ArrayList<DirectoryListener>();
91
 
90
 
92
        this.phrasesPkgName = null;
91
        this.phrasesPkgName = null;
93
        this.elementNames = new HashMap<String, SQLElementNames>();
92
        this.elementNames = new HashMap<String, SQLElementNames>();
94
    }
93
    }
95
 
94
 
96
    private static <K> SQLTable getSoleTable(CollectionMap<K, SQLTable> m, K key) throws IllegalArgumentException {
95
    private static <K> SQLTable getSoleTable(SetMap<K, SQLTable> m, K key) throws IllegalArgumentException {
97
        final Collection<SQLTable> res = m.getNonNull(key);
96
        final Collection<SQLTable> res = m.getNonNull(key);
98
        if (res.size() > 1)
97
        if (res.size() > 1)
99
            throw new IllegalArgumentException(key + " is not unique: " + CollectionUtils.join(res, ",", new ITransformer<SQLTable, SQLName>() {
98
            throw new IllegalArgumentException(key + " is not unique: " + CollectionUtils.join(res, ",", new ITransformer<SQLTable, SQLName>() {
100
                @Override
99
                @Override
101
                public SQLName transformChecked(SQLTable input) {
100
                public SQLName transformChecked(SQLTable input) {
Line 139... Line 138...
139
     * @return the previously added element.
138
     * @return the previously added element.
140
     */
139
     */
141
    public synchronized final SQLElement addSQLElement(SQLElement elem) {
140
    public synchronized final SQLElement addSQLElement(SQLElement elem) {
142
        final SQLElement res = this.removeSQLElement(elem.getTable());
141
        final SQLElement res = this.removeSQLElement(elem.getTable());
143
        this.elements.put(elem.getTable(), elem);
142
        this.elements.put(elem.getTable(), elem);
144
        this.tableNames.put(elem.getTable().getName(), elem.getTable());
143
        this.tableNames.add(elem.getTable().getName(), elem.getTable());
145
        this.byCode.put(elem.getCode(), elem.getTable());
144
        this.byCode.add(elem.getCode(), elem.getTable());
146
        this.byClass.put(elem.getClass(), elem.getTable());
145
        this.byClass.add(elem.getClass(), elem.getTable());
147
        for (final DirectoryListener dl : this.listeners) {
146
        for (final DirectoryListener dl : this.listeners) {
148
            dl.elementAdded(elem);
147
            dl.elementAdded(elem);
149
        }
148
        }
150
        elem.setDirectory(this);
149
        elem.setDirectory(this);
151
        return res;
150
        return res;