OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 20 | Go to most recent revision | Details | 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;
26
import org.openconcerto.sql.users.UserCommonSQLElement;
27
import org.openconcerto.sql.users.rights.RightSQLElement;
28
import org.openconcerto.sql.users.rights.UserRightSQLElement;
29
import org.openconcerto.task.element.TaskRightSQLElement;
30
import org.openconcerto.task.element.TaskSQLElement;
31
import org.openconcerto.utils.DesktopEnvironment;
32
import org.openconcerto.utils.LogUtils;
33
 
34
import java.io.File;
35
import java.io.FileInputStream;
36
import java.io.FileNotFoundException;
37
import java.io.IOException;
38
import java.io.InputStream;
39
import java.sql.SQLException;
40
import java.util.Arrays;
41
import java.util.Collections;
42
import java.util.Properties;
43
 
44
public abstract class ComptaBasePropsConfiguration extends PropsConfiguration {
45
 
46
    public static File getConfFile(final String appName) {
47
        return getConfFile(appName, new File("."));
48
    }
49
 
50
    public static File getConfFile(final String appName, final File wd) {
51
        final String confFilePath = System.getProperty("gestion.confFile");
52
        final File wdFile = new File(wd + "/Configuration", "main.properties");
53
        final File confFile;
54
        if (confFilePath != null) {
55
            confFile = new File(confFilePath);
56
        } else if (wdFile.isFile()) {
57
            confFile = wdFile;
58
        } else {
59
            confFile = new File(DesktopEnvironment.getDE().getPreferencesFolder(appName), "main.properties");
60
        }
61
        return confFile;
62
    }
63
 
64
    public static InputStream getStreamStatic(final String name) {
65
        InputStream stream = ((PropsConfiguration) Configuration.getInstance()).getStream(name);
66
 
67
        // FIXME Checker ailleurs ou throws filenotfoundexception
68
 
69
        // if (stream == null) {
70
        //
71
        // JOptionPane.showMessageDialog(null, "Impossible de trouver le fichier " + name);
72
        // }
73
        return stream;
74
    }
75
 
76
    public static InputStream getStream(final String name, final String... dirs) throws FileNotFoundException {
77
        InputStream res = null;
78
        for (final String dir : dirs) {
79
            // getResourceAsStream() doesn't handle dir//file
80
            res = getStreamStatic(dir + (dir.endsWith("/") ? "" : "/") + name);
81
            if (res != null)
82
                return res;
83
        }
84
        throw new FileNotFoundException(name + " not found in " + Arrays.asList(dirs));
85
    }
86
 
87
    private int idSociete = SQLRow.NONEXISTANT_ID;
88
    private SQLRow rowSociete = null;
89
    private DBRoot baseSociete;
90
 
91
    {
92
        // * logs
93
        LogUtils.rmRootHandlers();
94
        LogUtils.setUpConsoleHandler();
95
        this.setLoggersLevel();
96
    }
97
 
98
    public ComptaBasePropsConfiguration(File f, Properties defaults, final String appName) throws IOException {
99
        this(create(new FileInputStream(f), defaults), appName);
100
    }
101
 
102
    public ComptaBasePropsConfiguration(Properties props, final String appName) {
103
        super(props);
104
 
105
 
106
        this.setProperty("app.name", appName);
107
        String name = "ilm";
108
        this.setProperty("systemRoot.rootsToMap", name + "_Common");
109
        this.setProperty("systemRoot.rootPath", name + "_Common");
110
    }
111
 
112
 
113
    // use Configuration directory if it exists
114
    @Override
115
    protected FileMode getFileMode() {
116
        return FileMode.NORMAL_FILE;
117
    }
118
 
119
    @Override
120
    protected SQLElementDirectory createDirectory() {
121
        final SQLElementDirectory dir = super.createDirectory();
122
 
123
        // TACHE_COMMON points to SOCIETE but we never display it we don't need the full element
124
        dir.addSQLElement(new ConfSQLElement("SOCIETE_COMMON", "une société", "sociétés"));
125
        dir.addSQLElement(new ConfSQLElement("EXERCICE_COMMON", "un exercice", "exercices"));
126
        dir.addSQLElement(new ConfSQLElement("ADRESSE_COMMON", "une adresse", "adresses"));
127
 
128
        dir.addSQLElement(new TaskRightSQLElement());
129
        dir.addSQLElement(new TaskSQLElement());
130
 
131
        dir.addSQLElement(new UserCommonSQLElement());
132
        dir.addSQLElement(UserRightSQLElement.class);
133
        dir.addSQLElement(RightSQLElement.class);
134
 
135
        return dir;
136
    }
137
 
138
    @Override
139
    protected SQLFilter createFilter() {
140
        // we don't use the filter so remove everything
141
        return new SQLFilter(getDirectory(), getSystemRoot().getGraph().cloneForFilterKeep(Collections.<SQLField> emptySet()));
142
    }
143
 
144
    public final String getSocieteBaseName() {
145
        return getRowSociete().getString("DATABASE_NAME");
146
    }
147
 
148
    public final SQLRow getRowSociete() {
149
        return this.rowSociete;
150
    }
151
 
152
    public final int getSocieteID() {
153
        return this.idSociete;
154
    }
155
 
156
    protected final void setRowSociete(int id) {
157
        this.idSociete = id;
158
        this.rowSociete = getSystemRoot().findTable("SOCIETE_COMMON").getValidRow(this.getSocieteID());
159
    }
160
 
161
    public final SQLBase getSQLBaseSociete() {
162
        return this.getRootSociete().getBase();
163
    }
164
 
165
    public final DBRoot getRootSociete() {
166
        if (this.baseSociete == null)
167
            this.baseSociete = this.createSQLBaseSociete();
168
        return this.baseSociete;
169
    }
170
 
171
    private DBRoot createSQLBaseSociete() {
172
        final DBSystemRoot b = this.getSystemRoot();
173
        // now map the societe
174
        final String societeBaseName = this.getSocieteBaseName();
175
        b.getRootsToMap().add(societeBaseName);
176
        try {
177
            b.reload(Collections.singleton(societeBaseName));
178
        } catch (SQLException e) {
179
            throw new IllegalStateException("could not access societe base", e);
180
        }
181
        b.prependToRootPath(societeBaseName);
182
        return b.getRoot(societeBaseName);
183
    }
184
 
185
}