OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 25 | Rev 65 | 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
 
79
    public String getAppName() {
80
        return "unnamed application";
81
    }
82
 
83
    public final String getAppID() {
84
        return this.getAppName() + getAppIDSuffix();
85
    }
86
 
87
    protected String getAppIDSuffix() {
88
        return "";
89
    }
90
 
91
    public File getConfDir() {
92
        return new File(getDefaultConfDir(), this.getAppID());
93
    }
94
 
95
    /**
96
     * A directory to store data depending on this {@link #getRoot() root}.
97
     *
98
     * @return a directory for this root.
99
     */
100
    public final File getConfDirForRoot() {
101
        return getConfDir(getRoot());
102
    }
103
 
104
    /**
105
     * Move {@link #getConfDir()}/<code>name</code> to {@link #getConfDirForRoot()}/
106
     * <code>name</code> if necessary.
107
     *
108
     * @param name the name of the file or directory to move.
109
     * @return the new file in <code>getConfDirForRoot()</code>.
110
     */
111
    public final File migrateToConfDirForRoot(final String name) {
112
        final File oldFile = new File(this.getConfDir(), name);
113
        final File newFile = new File(this.getConfDirForRoot(), name);
114
        if (oldFile.exists() && !newFile.exists()) {
115
            try {
116
                FileUtils.mkdir_p(newFile.getParentFile());
117
                oldFile.renameTo(newFile);
118
            } catch (IOException e) {
119
                e.printStackTrace();
120
                FileUtils.rmR(oldFile);
121
            }
122
        }
123
        return newFile;
124
    }
125
 
25 ilm 126
    public final File getConfDir(DBStructureItem<?> db) {
17 ilm 127
        return DBItemFileCache.getDescendant(new File(getConfDir(), "dataDepedent"), DBFileCache.getJDBCAncestorNames(db, true));
128
    }
129
 
130
    /**
131
     * Add the showAs, translator and directory of <code>o</code> to this.
132
     *
133
     * @param o the configuration to add.
134
     * @return this.
135
     * @see ShowAs#putAll(ShowAs)
136
     * @see SQLFieldTranslator#putAll(SQLFieldTranslator)
137
     * @see SQLElementDirectory#putAll(SQLElementDirectory)
138
     */
139
    public Configuration add(Configuration o) {
140
        this.getShowAs().putAll(o.getShowAs());
141
        this.getTranslator().putAll(o.getTranslator());
142
        this.getDirectory().putAll(o.getDirectory());
143
        return this;
144
    }
145
 
146
    /**
147
     * Signal that this conf will not be used anymore.
148
     */
149
    public abstract void destroy();
63 ilm 150
 
151
    public Executor getNonInteractiveSQLExecutor() {
152
        if (nonInteractiveSQLExecutor == null) {
153
            nonInteractiveSQLExecutor = createNonInteractiveSQLExecutor();
154
        }
155
        return nonInteractiveSQLExecutor;
156
    }
157
 
158
    protected Executor createNonInteractiveSQLExecutor() {
159
        return Executors.newFixedThreadPool(2);
160
    }
17 ilm 161
}