OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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