OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 65 | Rev 80 | 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
 /*
15
 * Créé le 18 avr. 2005
16
 */
17
package org.openconcerto.sql;
18
 
19
import org.openconcerto.sql.element.SQLElementDirectory;
20
import org.openconcerto.sql.model.DBFileCache;
21
import org.openconcerto.sql.model.DBItemFileCache;
22
import org.openconcerto.sql.model.DBRoot;
23
import org.openconcerto.sql.model.DBStructureItem;
24
import org.openconcerto.sql.model.DBSystemRoot;
25
import org.openconcerto.sql.model.SQLBase;
26
import org.openconcerto.sql.model.SQLFilter;
27
import org.openconcerto.sql.model.SQLTable;
28
import org.openconcerto.sql.request.SQLFieldTranslator;
29
import org.openconcerto.utils.FileUtils;
30
 
31
import java.io.File;
32
import java.io.IOException;
63 ilm 33
import java.util.concurrent.Executor;
34
import java.util.concurrent.Executors;
17 ilm 35
 
36
/**
37
 * Regroupe les objets nécessaires au framework.
38
 *
39
 * @author Sylvain CUAZ
40
 */
41
public abstract class Configuration {
42
 
43
    public static File getDefaultConfDir() {
44
        return new File(System.getProperty("user.home"), ".java/ilm/sql-config/");
45
    }
46
 
47
    private static Configuration instance;
48
 
49
    public static SQLFieldTranslator getTranslator(SQLTable t) {
50
        return getInstance().getTranslator();
51
    }
52
 
53
    public static Configuration getInstance() {
54
        return instance;
55
    }
56
 
57
    public static final void setInstance(Configuration instance) {
58
        Configuration.instance = instance;
59
    }
60
 
63 ilm 61
    private Executor nonInteractiveSQLExecutor;
62
 
17 ilm 63
    public abstract ShowAs getShowAs();
64
 
65
    public abstract SQLBase getBase();
66
 
67
    public abstract DBRoot getRoot();
68
 
69
    public abstract DBSystemRoot getSystemRoot();
70
 
71
    public abstract SQLFilter getFilter();
72
 
73
    public abstract SQLFieldTranslator getTranslator();
74
 
75
    public abstract SQLElementDirectory getDirectory();
76
 
77
    public abstract File getWD();
78
 
73 ilm 79
    // abstract :
80
    // - we can't return a default name as we don't know how to localize it
81
    // - avoid that 2 different application share the same name (and perhaps configuration)
82
    public abstract String getAppName();
17 ilm 83
 
73 ilm 84
    /**
85
     * A string that should be unique to an application and this configuration. E.g. allow to store
86
     * different settings for different uses of a same application.
87
     *
88
     * @return a string beginning with {@link #getAppName()}, <code>null</code> if appName is
89
     *         <code>null</code> or empty.
90
     */
17 ilm 91
    public final String getAppID() {
73 ilm 92
        final String appName = this.getAppName();
93
        if (appName == null || appName.length() == 0)
94
            return null;
95
        return appName + getAppIDSuffix();
17 ilm 96
    }
97
 
98
    protected String getAppIDSuffix() {
99
        return "";
100
    }
101
 
102
    public File getConfDir() {
103
        return new File(getDefaultConfDir(), this.getAppID());
104
    }
105
 
106
    /**
107
     * A directory to store data depending on this {@link #getRoot() root}.
108
     *
109
     * @return a directory for this root.
110
     */
111
    public final File getConfDirForRoot() {
112
        return getConfDir(getRoot());
113
    }
114
 
115
    /**
116
     * Move {@link #getConfDir()}/<code>name</code> to {@link #getConfDirForRoot()}/
117
     * <code>name</code> if necessary.
118
     *
119
     * @param name the name of the file or directory to move.
120
     * @return the new file in <code>getConfDirForRoot()</code>.
121
     */
122
    public final File migrateToConfDirForRoot(final String name) {
123
        final File oldFile = new File(this.getConfDir(), name);
124
        final File newFile = new File(this.getConfDirForRoot(), name);
125
        if (oldFile.exists() && !newFile.exists()) {
126
            try {
127
                FileUtils.mkdir_p(newFile.getParentFile());
128
                oldFile.renameTo(newFile);
129
            } catch (IOException e) {
130
                e.printStackTrace();
131
                FileUtils.rmR(oldFile);
132
            }
133
        }
134
        return newFile;
135
    }
136
 
25 ilm 137
    public final File getConfDir(DBStructureItem<?> db) {
17 ilm 138
        return DBItemFileCache.getDescendant(new File(getConfDir(), "dataDepedent"), DBFileCache.getJDBCAncestorNames(db, true));
139
    }
140
 
141
    /**
142
     * Add the showAs, translator and directory of <code>o</code> to this.
143
     *
144
     * @param o the configuration to add.
145
     * @return this.
146
     * @see ShowAs#putAll(ShowAs)
147
     * @see SQLFieldTranslator#putAll(SQLFieldTranslator)
148
     * @see SQLElementDirectory#putAll(SQLElementDirectory)
149
     */
150
    public Configuration add(Configuration o) {
151
        this.getShowAs().putAll(o.getShowAs());
152
        this.getTranslator().putAll(o.getTranslator());
153
        this.getDirectory().putAll(o.getDirectory());
154
        return this;
155
    }
156
 
157
    /**
158
     * Signal that this conf will not be used anymore.
159
     */
160
    public abstract void destroy();
63 ilm 161
 
65 ilm 162
    /**
163
     * An executor that should be used for background SQL requests. It can be used to limit the
164
     * concurrent number of database connections (as establishing a connection is expensive and the
165
     * server might have restrictions).
166
     *
167
     * @return a SQL executor.
168
     */
63 ilm 169
    public Executor getNonInteractiveSQLExecutor() {
65 ilm 170
        if (this.nonInteractiveSQLExecutor == null) {
171
            this.nonInteractiveSQLExecutor = createNonInteractiveSQLExecutor();
63 ilm 172
        }
65 ilm 173
        return this.nonInteractiveSQLExecutor;
63 ilm 174
    }
175
 
176
    protected Executor createNonInteractiveSQLExecutor() {
177
        return Executors.newFixedThreadPool(2);
178
    }
17 ilm 179
}