OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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