OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 142 | Rev 149 | Go to most recent revision | 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;
23
import org.openconcerto.sql.model.SQLField;
24
import org.openconcerto.sql.model.SQLFilter;
25
import org.openconcerto.sql.model.SQLRow;
80 ilm 26
import org.openconcerto.sql.users.CompanyAccessSQLElement;
17 ilm 27
import org.openconcerto.sql.users.UserCommonSQLElement;
28
import org.openconcerto.sql.users.rights.RightSQLElement;
29
import org.openconcerto.sql.users.rights.UserRightSQLElement;
30
import org.openconcerto.task.element.TaskRightSQLElement;
31
import org.openconcerto.task.element.TaskSQLElement;
142 ilm 32
import org.openconcerto.utils.BaseDirs;
17 ilm 33
import org.openconcerto.utils.DesktopEnvironment;
73 ilm 34
import org.openconcerto.utils.ExceptionHandler;
17 ilm 35
import org.openconcerto.utils.LogUtils;
41 ilm 36
import org.openconcerto.utils.ProductInfo;
17 ilm 37
 
142 ilm 38
import java.awt.GraphicsEnvironment;
39
import java.io.File;
40
import java.io.FileNotFoundException;
41
import java.io.IOException;
42
import java.io.InputStream;
43
import java.sql.SQLException;
44
import java.util.Arrays;
45
import java.util.Collections;
46
import java.util.Properties;
47
 
48
import javax.swing.JFrame;
49
import javax.swing.JOptionPane;
50
 
51
import com.jcraft.jsch.Session;
52
 
17 ilm 53
public abstract class ComptaBasePropsConfiguration extends PropsConfiguration {
54
 
93 ilm 55
    public abstract void setUpSocieteDataBaseConnexion(int base);
56
 
142 ilm 57
    public static File getConfFile(final ProductInfo info) {
17 ilm 58
        final String confFilePath = System.getProperty("gestion.confFile");
142 ilm 59
        final File wdFile = new File("Configuration", "main.properties");
17 ilm 60
        final File confFile;
61
        if (confFilePath != null) {
62
            confFile = new File(confFilePath);
63
        } else if (wdFile.isFile()) {
64
            confFile = wdFile;
65
        } else {
142 ilm 66
            // we added organisation name, so migrate preferences
67
            final File prefsFolder = BaseDirs.create(info).getPreferencesFolder();
68
            if (!prefsFolder.exists()) {
69
                try {
70
                    final File oldDir = DesktopEnvironment.getDE().getPreferencesFolder(info.getName());
71
                    Configuration.migrateToNewDir(oldDir, prefsFolder);
72
                } catch (IOException ex) {
73
                    throw new IllegalStateException("Couldn't migrate preferences dir", ex);
74
                }
75
            }
76
            confFile = new File(prefsFolder, "main.properties");
17 ilm 77
        }
78
        return confFile;
79
    }
80
 
81
    public static InputStream getStreamStatic(final String name) {
82
        InputStream stream = ((PropsConfiguration) Configuration.getInstance()).getStream(name);
83
 
84
        // FIXME Checker ailleurs ou throws filenotfoundexception
85
 
86
        // if (stream == null) {
87
        //
88
        // JOptionPane.showMessageDialog(null, "Impossible de trouver le fichier " + name);
89
        // }
90
        return stream;
91
    }
92
 
93
    public static InputStream getStream(final String name, final String... dirs) throws FileNotFoundException {
94
        InputStream res = null;
95
        for (final String dir : dirs) {
96
            // getResourceAsStream() doesn't handle dir//file
97
            res = getStreamStatic(dir + (dir.endsWith("/") ? "" : "/") + name);
98
            if (res != null)
99
                return res;
100
        }
101
        throw new FileNotFoundException(name + " not found in " + Arrays.asList(dirs));
102
    }
103
 
104
    private int idSociete = SQLRow.NONEXISTANT_ID;
105
    private SQLRow rowSociete = null;
106
    private DBRoot baseSociete;
73 ilm 107
    private Thread sslThread = null;
17 ilm 108
 
109
    {
110
        // * logs
111
        LogUtils.rmRootHandlers();
112
        LogUtils.setUpConsoleHandler();
113
        this.setLoggersLevel();
114
    }
115
 
41 ilm 116
    public ComptaBasePropsConfiguration(Properties props, final ProductInfo productInfo) {
17 ilm 117
        super(props);
118
 
41 ilm 119
        this.setProductInfo(productInfo);
17 ilm 120
        String name = "ilm";
80 ilm 121
        // don't overwrite (allow to map no roots, just to test connection)
122
        if (getProperty("systemRoot.rootsToMap") == null) {
123
            this.setProperty("systemRoot.rootsToMap", name + "_Common");
124
            this.setProperty("systemRoot.rootPath", name + "_Common");
125
        }
17 ilm 126
    }
127
 
128
 
73 ilm 129
    @Override
130
    protected void afterSSLConnect(final Session conn) {
131
        if (conn.isConnected()) {
132
            final Thread t = new Thread(new Runnable() {
133
                @Override
134
                public void run() {
135
                    while (true) {
136
                        try {
137
                            Thread.sleep(1000);
138
                            if (!conn.isConnected()) {
142 ilm 139
                                if (!GraphicsEnvironment.isHeadless()) {
140
                                    JOptionPane.showMessageDialog(null, "Liaison sécurisée déconnectée!\nVérifiez votre connexion internet et relancez le logiciel.");
141
                                } else {
142
                                    ExceptionHandler.die("Liaison sécurisée déconnectée!\nVérifiez votre connexion internet et relancez le logiciel.");
143
                                }
73 ilm 144
                                break;
145
                            }
146
                        } catch (InterruptedException e) {
147
                            // used by destroy()
148
                            break;
149
                        }
150
                    }
151
                }
152
            });
153
            t.setDaemon(true);
154
            t.setName("SSL connection watcher");
155
            t.start();
156
            assert this.sslThread == null;
157
            this.sslThread = t;
158
        } else {
159
            ExceptionHandler.die("Impossible d'établir la liaison sécurisée!\nVérifiez votre connexion internet et relancez le logiciel.");
160
        }
161
    }
162
 
163
    @Override
164
    public void destroy() {
165
        if (this.sslThread != null) {
166
            this.sslThread.interrupt();
167
            this.sslThread = null;
168
        }
169
        super.destroy();
170
    }
171
 
17 ilm 172
    // use Configuration directory if it exists
173
    @Override
174
    protected FileMode getFileMode() {
175
        return FileMode.NORMAL_FILE;
176
    }
177
 
178
    @Override
179
    protected SQLElementDirectory createDirectory() {
180
        final SQLElementDirectory dir = super.createDirectory();
181
 
182
        // TACHE_COMMON points to SOCIETE but we never display it we don't need the full element
183
        dir.addSQLElement(new ConfSQLElement("SOCIETE_COMMON", "une société", "sociétés"));
184
        dir.addSQLElement(new ConfSQLElement("EXERCICE_COMMON", "un exercice", "exercices"));
185
        dir.addSQLElement(new ConfSQLElement("ADRESSE_COMMON", "une adresse", "adresses"));
186
 
187
        dir.addSQLElement(new TaskRightSQLElement());
188
        dir.addSQLElement(new TaskSQLElement());
189
 
144 ilm 190
        dir.addSQLElement(new UserCommonSQLElement(getRoot(), false));
80 ilm 191
        dir.addSQLElement(new CompanyAccessSQLElement());
17 ilm 192
        dir.addSQLElement(UserRightSQLElement.class);
193
        dir.addSQLElement(RightSQLElement.class);
194
 
195
        return dir;
196
    }
197
 
198
    @Override
199
    protected SQLFilter createFilter() {
200
        // we don't use the filter so remove everything
201
        return new SQLFilter(getDirectory(), getSystemRoot().getGraph().cloneForFilterKeep(Collections.<SQLField> emptySet()));
202
    }
203
 
204
    public final String getSocieteBaseName() {
205
        return getRowSociete().getString("DATABASE_NAME");
206
    }
207
 
208
    public final SQLRow getRowSociete() {
209
        return this.rowSociete;
210
    }
211
 
212
    public final int getSocieteID() {
213
        return this.idSociete;
214
    }
215
 
216
    protected final void setRowSociete(int id) {
217
        this.idSociete = id;
218
        this.rowSociete = getSystemRoot().findTable("SOCIETE_COMMON").getValidRow(this.getSocieteID());
219
    }
220
 
221
    public final SQLBase getSQLBaseSociete() {
222
        return this.getRootSociete().getBase();
223
    }
224
 
225
    public final DBRoot getRootSociete() {
20 ilm 226
        if (this.baseSociete == null && this.rowSociete != null)
17 ilm 227
            this.baseSociete = this.createSQLBaseSociete();
228
        return this.baseSociete;
229
    }
230
 
231
    private DBRoot createSQLBaseSociete() {
232
        final DBSystemRoot b = this.getSystemRoot();
233
        // now map the societe
234
        final String societeBaseName = this.getSocieteBaseName();
65 ilm 235
        b.addRootToMap(societeBaseName);
17 ilm 236
        try {
237
            b.reload(Collections.singleton(societeBaseName));
238
        } catch (SQLException e) {
239
            throw new IllegalStateException("could not access societe base", e);
240
        }
241
        b.prependToRootPath(societeBaseName);
242
        return b.getRoot(societeBaseName);
243
    }
244
 
245
}