OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 149 | Rev 177 | 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;
156 ilm 26
import org.openconcerto.sql.model.SQLTable;
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;
156 ilm 30
import org.openconcerto.task.TM;
31
import org.openconcerto.task.element.CompanyAccessSQLElement;
17 ilm 32
import org.openconcerto.task.element.TaskRightSQLElement;
33
import org.openconcerto.task.element.TaskSQLElement;
142 ilm 34
import org.openconcerto.utils.BaseDirs;
17 ilm 35
import org.openconcerto.utils.DesktopEnvironment;
36
import org.openconcerto.utils.LogUtils;
41 ilm 37
import org.openconcerto.utils.ProductInfo;
156 ilm 38
import org.openconcerto.utils.i18n.Grammar_fr;
39
import org.openconcerto.utils.i18n.NounClass;
17 ilm 40
 
142 ilm 41
import java.io.File;
42
import java.io.FileNotFoundException;
43
import java.io.IOException;
44
import java.io.InputStream;
45
import java.sql.SQLException;
156 ilm 46
import java.util.ArrayList;
142 ilm 47
import java.util.Arrays;
48
import java.util.Collections;
156 ilm 49
import java.util.List;
142 ilm 50
import java.util.Properties;
51
 
17 ilm 52
public abstract class ComptaBasePropsConfiguration extends PropsConfiguration {
53
 
93 ilm 54
    public abstract void setUpSocieteDataBaseConnexion(int base);
55
 
142 ilm 56
    public static File getConfFile(final ProductInfo info) {
17 ilm 57
        final String confFilePath = System.getProperty("gestion.confFile");
142 ilm 58
        final File wdFile = new File("Configuration", "main.properties");
17 ilm 59
        final File confFile;
60
        if (confFilePath != null) {
61
            confFile = new File(confFilePath);
62
        } else if (wdFile.isFile()) {
63
            confFile = wdFile;
64
        } else {
142 ilm 65
            // we added organisation name, so migrate preferences
66
            final File prefsFolder = BaseDirs.create(info).getPreferencesFolder();
67
            if (!prefsFolder.exists()) {
68
                try {
69
                    final File oldDir = DesktopEnvironment.getDE().getPreferencesFolder(info.getName());
70
                    Configuration.migrateToNewDir(oldDir, prefsFolder);
71
                } catch (IOException ex) {
72
                    throw new IllegalStateException("Couldn't migrate preferences dir", ex);
73
                }
74
            }
75
            confFile = new File(prefsFolder, "main.properties");
17 ilm 76
        }
77
        return confFile;
78
    }
79
 
80
    public static InputStream getStreamStatic(final String name) {
81
        InputStream stream = ((PropsConfiguration) Configuration.getInstance()).getStream(name);
82
 
83
        // FIXME Checker ailleurs ou throws filenotfoundexception
84
 
85
        // if (stream == null) {
86
        //
87
        // JOptionPane.showMessageDialog(null, "Impossible de trouver le fichier " + name);
88
        // }
89
        return stream;
90
    }
91
 
92
    public static InputStream getStream(final String name, final String... dirs) throws FileNotFoundException {
93
        InputStream res = null;
94
        for (final String dir : dirs) {
95
            // getResourceAsStream() doesn't handle dir//file
96
            res = getStreamStatic(dir + (dir.endsWith("/") ? "" : "/") + name);
97
            if (res != null)
98
                return res;
99
        }
100
        throw new FileNotFoundException(name + " not found in " + Arrays.asList(dirs));
101
    }
102
 
103
    private int idSociete = SQLRow.NONEXISTANT_ID;
104
    private SQLRow rowSociete = null;
105
    private DBRoot baseSociete;
106
 
107
    {
108
        // * logs
109
        LogUtils.rmRootHandlers();
110
        LogUtils.setUpConsoleHandler();
111
        this.setLoggersLevel();
112
    }
113
 
41 ilm 114
    public ComptaBasePropsConfiguration(Properties props, final ProductInfo productInfo) {
17 ilm 115
        super(props);
116
 
41 ilm 117
        this.setProductInfo(productInfo);
17 ilm 118
        String name = "ilm";
80 ilm 119
        // don't overwrite (allow to map no roots, just to test connection)
120
        if (getProperty("systemRoot.rootsToMap") == null) {
121
            this.setProperty("systemRoot.rootsToMap", name + "_Common");
122
            this.setProperty("systemRoot.rootPath", name + "_Common");
123
        }
17 ilm 124
    }
125
 
126
 
127
    // use Configuration directory if it exists
128
    @Override
129
    protected FileMode getFileMode() {
130
        return FileMode.NORMAL_FILE;
131
    }
132
 
133
    @Override
156 ilm 134
    protected List<String> getMappings() {
135
        final List<String> res = new ArrayList<>(super.getMappings());
136
        final String pkg = "/" + TM.class.getPackage().getName().replace('.', '/');
137
        res.add(pkg + "/translation/SQLElementNames");
138
        return res;
139
    }
140
 
141
    @Override
17 ilm 142
    protected SQLElementDirectory createDirectory() {
143
        final SQLElementDirectory dir = super.createDirectory();
144
 
145
        // TACHE_COMMON points to SOCIETE but we never display it we don't need the full element
156 ilm 146
        dir.addSQLElement(new ConfSQLElement("SOCIETE_COMMON", Grammar_fr.getInstance().createPhrase(NounClass.FEMININE, "société")));
147
        dir.addSQLElement(new ConfSQLElement("EXERCICE_COMMON", Grammar_fr.getInstance().createPhrase(NounClass.MASCULINE, "exercice")));
148
        dir.addSQLElement(new ConfSQLElement("ADRESSE_COMMON", Grammar_fr.getInstance().createPhrase(NounClass.FEMININE, "adresse")));
17 ilm 149
 
150
        dir.addSQLElement(new TaskRightSQLElement());
151
        dir.addSQLElement(new TaskSQLElement());
152
 
144 ilm 153
        dir.addSQLElement(new UserCommonSQLElement(getRoot(), false));
80 ilm 154
        dir.addSQLElement(new CompanyAccessSQLElement());
156 ilm 155
        dir.addSQLElement(new UserRightSQLElement(getRoot()));
156
        dir.addSQLElement(new RightSQLElement(getRoot()));
17 ilm 157
 
158
        return dir;
159
    }
160
 
161
    @Override
162
    protected SQLFilter createFilter() {
163
        // we don't use the filter so remove everything
164
        return new SQLFilter(getDirectory(), getSystemRoot().getGraph().cloneForFilterKeep(Collections.<SQLField> emptySet()));
165
    }
166
 
167
    public final String getSocieteBaseName() {
168
        return getRowSociete().getString("DATABASE_NAME");
169
    }
170
 
171
    public final SQLRow getRowSociete() {
172
        return this.rowSociete;
173
    }
174
 
175
    public final int getSocieteID() {
176
        return this.idSociete;
177
    }
178
 
179
    protected final void setRowSociete(int id) {
180
        this.idSociete = id;
156 ilm 181
        final SQLTable tableSociete = getSystemRoot().findTable("SOCIETE_COMMON");
182
        final SQLRow row = tableSociete.getRow(id);
183
        if (row == null) {
184
            throw new IllegalArgumentException("no row for id " + id + " in " + tableSociete);
185
        } else if (!row.isValid()) {
186
            throw new IllegalArgumentException("invalid row : " + row);
187
        }
188
        this.rowSociete = row;
17 ilm 189
    }
190
 
191
    public final SQLBase getSQLBaseSociete() {
192
        return this.getRootSociete().getBase();
193
    }
194
 
195
    public final DBRoot getRootSociete() {
20 ilm 196
        if (this.baseSociete == null && this.rowSociete != null)
17 ilm 197
            this.baseSociete = this.createSQLBaseSociete();
198
        return this.baseSociete;
199
    }
200
 
201
    private DBRoot createSQLBaseSociete() {
202
        final DBSystemRoot b = this.getSystemRoot();
203
        // now map the societe
204
        final String societeBaseName = this.getSocieteBaseName();
65 ilm 205
        b.addRootToMap(societeBaseName);
17 ilm 206
        try {
207
            b.reload(Collections.singleton(societeBaseName));
208
        } catch (SQLException e) {
209
            throw new IllegalStateException("could not access societe base", e);
210
        }
211
        b.prependToRootPath(societeBaseName);
212
        return b.getRoot(societeBaseName);
213
    }
214
 
215
}