OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 177 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
17 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
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
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.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.task.config;
15
 
16
import org.openconcerto.sql.Configuration;
17
import org.openconcerto.sql.PropsConfiguration;
18
import org.openconcerto.sql.element.ConfSQLElement;
19
import org.openconcerto.sql.element.SQLElementDirectory;
20
import org.openconcerto.sql.model.DBRoot;
21
import org.openconcerto.sql.model.DBSystemRoot;
22
import org.openconcerto.sql.model.SQLBase;
177 ilm 23
import org.openconcerto.sql.model.SQLDataSource;
17 ilm 24
import org.openconcerto.sql.model.SQLField;
25
import org.openconcerto.sql.model.SQLFilter;
26
import org.openconcerto.sql.model.SQLRow;
156 ilm 27
import org.openconcerto.sql.model.SQLTable;
177 ilm 28
import org.openconcerto.sql.model.SQLTableEvent;
29
import org.openconcerto.sql.model.SQLTableModifiedListener;
17 ilm 30
import org.openconcerto.sql.users.UserCommonSQLElement;
31
import org.openconcerto.sql.users.rights.RightSQLElement;
32
import org.openconcerto.sql.users.rights.UserRightSQLElement;
156 ilm 33
import org.openconcerto.task.TM;
34
import org.openconcerto.task.element.CompanyAccessSQLElement;
17 ilm 35
import org.openconcerto.task.element.TaskRightSQLElement;
36
import org.openconcerto.task.element.TaskSQLElement;
142 ilm 37
import org.openconcerto.utils.BaseDirs;
17 ilm 38
import org.openconcerto.utils.DesktopEnvironment;
39
import org.openconcerto.utils.LogUtils;
41 ilm 40
import org.openconcerto.utils.ProductInfo;
156 ilm 41
import org.openconcerto.utils.i18n.Grammar_fr;
42
import org.openconcerto.utils.i18n.NounClass;
17 ilm 43
 
142 ilm 44
import java.io.File;
45
import java.io.FileNotFoundException;
46
import java.io.InputStream;
47
import java.sql.SQLException;
156 ilm 48
import java.util.ArrayList;
142 ilm 49
import java.util.Arrays;
50
import java.util.Collections;
156 ilm 51
import java.util.List;
142 ilm 52
import java.util.Properties;
53
 
177 ilm 54
import net.jcip.annotations.GuardedBy;
55
 
17 ilm 56
public abstract class ComptaBasePropsConfiguration extends PropsConfiguration {
57
 
93 ilm 58
    public abstract void setUpSocieteDataBaseConnexion(int base);
59
 
142 ilm 60
    public static File getConfFile(final ProductInfo info) {
17 ilm 61
        final String confFilePath = System.getProperty("gestion.confFile");
142 ilm 62
        final File wdFile = new File("Configuration", "main.properties");
17 ilm 63
        final File confFile;
64
        if (confFilePath != null) {
65
            confFile = new File(confFilePath);
66
        } else if (wdFile.isFile()) {
67
            confFile = wdFile;
68
        } else {
142 ilm 69
            final File prefsFolder = BaseDirs.create(info).getPreferencesFolder();
70
            confFile = new File(prefsFolder, "main.properties");
17 ilm 71
        }
72
        return confFile;
73
    }
74
 
75
    public static InputStream getStreamStatic(final String name) {
76
        InputStream stream = ((PropsConfiguration) Configuration.getInstance()).getStream(name);
77
 
78
        // FIXME Checker ailleurs ou throws filenotfoundexception
79
 
80
        // if (stream == null) {
81
        //
82
        // JOptionPane.showMessageDialog(null, "Impossible de trouver le fichier " + name);
83
        // }
84
        return stream;
85
    }
86
 
87
    public static InputStream getStream(final String name, final String... dirs) throws FileNotFoundException {
88
        InputStream res = null;
89
        for (final String dir : dirs) {
90
            // getResourceAsStream() doesn't handle dir//file
91
            res = getStreamStatic(dir + (dir.endsWith("/") ? "" : "/") + name);
92
            if (res != null)
93
                return res;
94
        }
95
        throw new FileNotFoundException(name + " not found in " + Arrays.asList(dirs));
96
    }
97
 
177 ilm 98
    @GuardedBy("this")
17 ilm 99
    private SQLRow rowSociete = null;
177 ilm 100
    private final SQLTableModifiedListener rowSocieteListener = (evt) -> updateRowSociete(evt);
101
    @GuardedBy("this")
17 ilm 102
    private DBRoot baseSociete;
103
 
104
    {
105
        // * logs
106
        LogUtils.rmRootHandlers();
107
        LogUtils.setUpConsoleHandler();
108
        this.setLoggersLevel();
109
    }
110
 
41 ilm 111
    public ComptaBasePropsConfiguration(Properties props, final ProductInfo productInfo) {
17 ilm 112
        super(props);
113
 
41 ilm 114
        this.setProductInfo(productInfo);
80 ilm 115
        // don't overwrite (allow to map no roots, just to test connection)
116
        if (getProperty("systemRoot.rootsToMap") == null) {
177 ilm 117
            final String interAppRootName = getInterAppRootName();
118
            this.setProperty("systemRoot.rootsToMap", interAppRootName);
119
            this.setProperty("systemRoot.rootPath", interAppRootName);
80 ilm 120
        }
17 ilm 121
    }
122
 
177 ilm 123
    @Override
124
    public void destroy() {
125
        this.setRowSociete(null);
126
        super.destroy();
127
    }
17 ilm 128
 
177 ilm 129
    public final String getInterAppRootName() {
130
        String name = "ilm";
131
        return name + "_Common";
132
    }
133
 
134
 
17 ilm 135
    // use Configuration directory if it exists
136
    @Override
137
    protected FileMode getFileMode() {
138
        return FileMode.NORMAL_FILE;
139
    }
140
 
141
    @Override
177 ilm 142
    protected void initDS(SQLDataSource ds) {
143
        super.initDS(ds);
144
        // Old H2 versions need this to safely open page store files
145
        // (https://github.com/h2database/h2database/issues/1023), and new version ignore it
146
        // (https://github.com/h2database/h2database/pull/1208).
147
        ds.addConnectionProperty("MVCC", "false");
148
        // Don't begin transition from page store just yet.
149
        ds.addConnectionProperty("MV_STORE", "false");
150
    }
151
 
152
    @Override
156 ilm 153
    protected List<String> getMappings() {
154
        final List<String> res = new ArrayList<>(super.getMappings());
155
        final String pkg = "/" + TM.class.getPackage().getName().replace('.', '/');
156
        res.add(pkg + "/translation/SQLElementNames");
157
        return res;
158
    }
159
 
160
    @Override
17 ilm 161
    protected SQLElementDirectory createDirectory() {
162
        final SQLElementDirectory dir = super.createDirectory();
163
 
164
        // TACHE_COMMON points to SOCIETE but we never display it we don't need the full element
156 ilm 165
        dir.addSQLElement(new ConfSQLElement("SOCIETE_COMMON", Grammar_fr.getInstance().createPhrase(NounClass.FEMININE, "société")));
166
        dir.addSQLElement(new ConfSQLElement("EXERCICE_COMMON", Grammar_fr.getInstance().createPhrase(NounClass.MASCULINE, "exercice")));
167
        dir.addSQLElement(new ConfSQLElement("ADRESSE_COMMON", Grammar_fr.getInstance().createPhrase(NounClass.FEMININE, "adresse")));
17 ilm 168
 
169
        dir.addSQLElement(new TaskRightSQLElement());
170
        dir.addSQLElement(new TaskSQLElement());
171
 
144 ilm 172
        dir.addSQLElement(new UserCommonSQLElement(getRoot(), false));
80 ilm 173
        dir.addSQLElement(new CompanyAccessSQLElement());
156 ilm 174
        dir.addSQLElement(new UserRightSQLElement(getRoot()));
175
        dir.addSQLElement(new RightSQLElement(getRoot()));
17 ilm 176
 
177
        return dir;
178
    }
179
 
180
    @Override
181
    protected SQLFilter createFilter() {
182
        // we don't use the filter so remove everything
183
        return new SQLFilter(getDirectory(), getSystemRoot().getGraph().cloneForFilterKeep(Collections.<SQLField> emptySet()));
184
    }
185
 
186
    public final String getSocieteBaseName() {
187
        return getRowSociete().getString("DATABASE_NAME");
188
    }
189
 
177 ilm 190
    public synchronized final SQLRow getRowSociete() {
17 ilm 191
        return this.rowSociete;
192
    }
193
 
194
    public final int getSocieteID() {
177 ilm 195
        final SQLRow row = this.getRowSociete();
196
        return row == null ? SQLRow.NONEXISTANT_ID : row.getID();
17 ilm 197
    }
198
 
177 ilm 199
    private synchronized void updateRowSociete(SQLTableEvent evt) {
200
        if (evt.getRow().equals(this.rowSociete)) {
201
            this.rowSociete.fetchValues();
156 ilm 202
        }
17 ilm 203
    }
204
 
177 ilm 205
    public final SQLTable getSocieteTable() {
206
        return getSocieteTable(true);
207
    }
208
 
209
    protected final SQLTable getSocieteTable(final boolean mustExist) {
210
        return getSystemRoot().findTable("SOCIETE_COMMON", mustExist);
211
    }
212
 
213
    // undefined ID is allowed (used for the template root)
214
    protected final void setRowSociete(Number id) {
215
        final boolean clear = id == null;
216
        final SQLRow row;
217
        final SQLTable tableSociete = getSocieteTable(!clear);
218
        if (clear) {
219
            row = null;
220
            // might be null if mapsToRoot is empty (e.g. tests)
221
            if (tableSociete != null)
222
                tableSociete.removeTableModifiedListener(this.rowSocieteListener);
223
        } else {
224
            row = tableSociete.getRow(id.intValue());
225
            if (row == null) {
226
                throw new IllegalArgumentException("no row for id " + id + " in " + tableSociete);
227
            } else if (!row.isValid()) {
228
                throw new IllegalArgumentException("invalid row : " + row);
229
            }
230
            tableSociete.addTableModifiedListener(this.rowSocieteListener);
231
        }
232
        synchronized (this) {
233
            this.rowSociete = row;
234
        }
235
    }
236
 
237
    @Deprecated
17 ilm 238
    public final SQLBase getSQLBaseSociete() {
239
        return this.getRootSociete().getBase();
240
    }
241
 
242
    public final DBRoot getRootSociete() {
177 ilm 243
        return this.getRootSociete(true);
244
    }
245
 
246
    protected synchronized final DBRoot getRootSociete(final boolean create) {
247
        if (create && this.baseSociete == null && this.getRowSociete() != null)
17 ilm 248
            this.baseSociete = this.createSQLBaseSociete();
249
        return this.baseSociete;
250
    }
251
 
252
    private DBRoot createSQLBaseSociete() {
253
        final DBSystemRoot b = this.getSystemRoot();
254
        // now map the societe
255
        final String societeBaseName = this.getSocieteBaseName();
65 ilm 256
        b.addRootToMap(societeBaseName);
17 ilm 257
        try {
258
            b.reload(Collections.singleton(societeBaseName));
259
        } catch (SQLException e) {
260
            throw new IllegalStateException("could not access societe base", e);
261
        }
262
        b.prependToRootPath(societeBaseName);
263
        return b.getRoot(societeBaseName);
264
    }
265
 
266
}