OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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