OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev 180 Rev 182
Line 1... Line 1...
1
/*
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 * 
3
 * 
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
5
 * 
5
 * 
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
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
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
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.
9
 * language governing permissions and limitations under the License.
Line 15... Line 15...
15
 
15
 
16
import org.openconcerto.sql.Log;
16
import org.openconcerto.sql.Log;
17
import org.openconcerto.sql.ShowAs;
17
import org.openconcerto.sql.ShowAs;
18
import org.openconcerto.sql.model.DBRoot;
18
import org.openconcerto.sql.model.DBRoot;
19
import org.openconcerto.sql.model.DBStructureItemNotFound;
19
import org.openconcerto.sql.model.DBStructureItemNotFound;
-
 
20
import org.openconcerto.sql.model.DBSystemRoot;
20
import org.openconcerto.sql.model.SQLName;
21
import org.openconcerto.sql.model.SQLName;
21
import org.openconcerto.sql.model.SQLTable;
22
import org.openconcerto.sql.model.SQLTable;
22
import org.openconcerto.sql.request.SQLFieldTranslator;
23
import org.openconcerto.sql.request.SQLFieldTranslator;
23
import org.openconcerto.utils.CollectionMap2;
24
import org.openconcerto.utils.CollectionMap2;
24
import org.openconcerto.utils.CollectionUtils;
25
import org.openconcerto.utils.CollectionUtils;
25
import org.openconcerto.utils.SetMap;
26
import org.openconcerto.utils.SetMap;
-
 
27
import org.openconcerto.utils.cc.Cookies;
26
import org.openconcerto.utils.cc.ITransformer;
28
import org.openconcerto.utils.cc.ITransformer;
27
 
29
 
28
import java.lang.reflect.InvocationTargetException;
30
import java.lang.reflect.InvocationTargetException;
29
import java.util.ArrayList;
31
import java.util.ArrayList;
30
import java.util.Collection;
32
import java.util.Collection;
Line 36... Line 38...
36
import java.util.Set;
38
import java.util.Set;
37
 
39
 
38
import net.jcip.annotations.GuardedBy;
40
import net.jcip.annotations.GuardedBy;
39
 
41
 
40
/**
42
/**
41
 * Directory of SQLElement by table.
43
 * Directory of SQLElement by table. <code>final</code> so that {@link SQLElement} can't depend on a
-
 
44
 * subclass and can be shared between applications (e.g. using
-
 
45
 * {@link #putAll(SQLElementDirectory)}). If some elements needs shared state, use
-
 
46
 * {@link #getCookies()}.
42
 * 
47
 * 
43
 * @author Sylvain CUAZ
48
 * @author Sylvain CUAZ
44
 */
49
 */
45
public final class SQLElementDirectory {
50
public final class SQLElementDirectory {
46
 
51
 
-
 
52
    private final DBSystemRoot systemRoot;
47
    private final Map<SQLTable, SQLElement> elements;
53
    private final Map<SQLTable, SQLElement> elements;
48
    private final SetMap<String, SQLTable> tableNames;
54
    private final SetMap<String, SQLTable> tableNames;
49
    private final SetMap<String, SQLTable> byCode;
55
    private final SetMap<String, SQLTable> byCode;
50
    private final SetMap<Class<? extends SQLElement>, SQLTable> byClass;
56
    private final SetMap<Class<? extends SQLElement>, SQLTable> byClass;
51
    private final List<DirectoryListener> listeners;
57
    private final List<DirectoryListener> listeners;
Line 54... Line 60...
54
 
60
 
55
    @GuardedBy("this")
61
    @GuardedBy("this")
56
    private SQLFieldTranslator translator;
62
    private SQLFieldTranslator translator;
57
    private final ShowAs showAs;
63
    private final ShowAs showAs;
58
 
64
 
-
 
65
    private final Cookies cookies;
-
 
66
 
59
    public SQLElementDirectory() {
67
    public SQLElementDirectory(DBSystemRoot systemRoot) {
-
 
68
        this.systemRoot = systemRoot;
60
        this.elements = new HashMap<SQLTable, SQLElement>();
69
        this.elements = new HashMap<SQLTable, SQLElement>();
61
        // to mimic elements behaviour, if we add twice the same table
70
        // to mimic elements behaviour, if we add twice the same table
62
        // the second one should replace the first one
71
        // the second one should replace the first one
63
        this.tableNames = new SetMap<String, SQLTable>();
72
        this.tableNames = new SetMap<String, SQLTable>();
64
        this.byCode = new SetMap<String, SQLTable>();
73
        this.byCode = new SetMap<String, SQLTable>();
Line 67... Line 76...
67
        this.listeners = new ArrayList<DirectoryListener>();
76
        this.listeners = new ArrayList<DirectoryListener>();
68
 
77
 
69
        this.phrasesPkgName = null;
78
        this.phrasesPkgName = null;
70
 
79
 
71
        this.showAs = new ShowAs((DBRoot) null);
80
        this.showAs = new ShowAs((DBRoot) null);
-
 
81
        this.cookies = new Cookies();
72
    }
82
    }
73
 
83
 
74
    public synchronized final void destroy() {
84
    public synchronized final void destroy() {
75
        for (final SQLElement elem : this.elements.values()) {
85
        for (final SQLElement elem : this.elements.values()) {
76
            elem.destroy();
86
            elem.destroy();
Line 89... Line 99...
89
 
99
 
90
    public synchronized final SQLFieldTranslator getTranslator() {
100
    public synchronized final SQLFieldTranslator getTranslator() {
91
        return this.translator;
101
        return this.translator;
92
    }
102
    }
93
 
103
 
-
 
104
    public final Cookies getCookies() {
-
 
105
        return this.cookies;
-
 
106
    }
-
 
107
 
94
    private static <K> SQLTable getSoleTable(SetMap<K, SQLTable> m, K key) throws IllegalArgumentException {
108
    private static <K> SQLTable getSoleTable(SetMap<K, SQLTable> m, K key) throws IllegalArgumentException {
95
        final Collection<SQLTable> res = m.getNonNull(key);
109
        final Collection<SQLTable> res = m.getNonNull(key);
96
        if (res.size() > 1)
110
        if (res.size() > 1)
97
            throw new IllegalArgumentException(key + " is not unique: " + CollectionUtils.join(res, ",", new ITransformer<SQLTable, SQLName>() {
111
            throw new IllegalArgumentException(key + " is not unique: " + CollectionUtils.join(res, ",", new ITransformer<SQLTable, SQLName>() {
98
                @Override
112
                @Override
Line 102... Line 116...
102
            }));
116
            }));
103
        return CollectionUtils.getSole(res);
117
        return CollectionUtils.getSole(res);
104
    }
118
    }
105
 
119
 
106
    public synchronized final void putAll(SQLElementDirectory o) {
120
    public synchronized final void putAll(SQLElementDirectory o) {
-
 
121
        // Cookies are needed in addSQLElement() (e.g. by directoryChanged() and getShowAs())
-
 
122
        this.cookies.putAll(o.getCookies());
-
 
123
        // copy since addSQLElement() removes elements from their previous directory
107
        for (final SQLElement elem : o.getElements()) {
124
        for (final SQLElement elem : new ArrayList<>(o.getElements())) {
108
            if (!this.contains(elem.getTable()))
125
            if (!this.contains(elem.getTable()))
109
                this.addSQLElement(elem);
126
                this.addSQLElement(elem);
110
        }
127
        }
111
        this.translator.putAll(o.getTranslator());
128
        this.translator.putAll(o.getTranslator());
112
    }
129
    }
Line 145... Line 162...
145
     * 
162
     * 
146
     * @param elem the SQLElement to add.
163
     * @param elem the SQLElement to add.
147
     * @return the previously added element.
164
     * @return the previously added element.
148
     */
165
     */
149
    public synchronized final SQLElement addSQLElement(SQLElement elem) {
166
    public synchronized final SQLElement addSQLElement(SQLElement elem) {
-
 
167
        if (elem.getTable().getDBSystemRoot() != this.systemRoot) {
-
 
168
            System.err.println("SQLElementDirectory.addSQLElement() warning : Not in this system root : " + elem + " " + elem.getTable().getDBSystemRoot() + "  != " + this.systemRoot);
-
 
169
        }
150
        final SQLElement res = this.removeSQLElement(elem.getTable());
170
        final SQLElement res = this.removeSQLElement(elem.getTable());
151
        this.elements.put(elem.getTable(), elem);
171
        this.elements.put(elem.getTable(), elem);
152
        this.tableNames.add(elem.getTable().getName(), elem.getTable());
172
        this.tableNames.add(elem.getTable().getName(), elem.getTable());
153
        this.byCode.add(elem.getCode(), elem.getTable());
173
        this.byCode.add(elem.getCode(), elem.getTable());
154
        this.byClass.add(elem.getClass(), elem.getTable());
174
        this.byClass.add(elem.getClass(), elem.getTable());