OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 83 | Rev 132 | 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
 
94 ilm 19
import java.io.File;
20
import java.io.IOException;
21
import java.io.StringReader;
22
import java.util.List;
23
import java.util.concurrent.Executor;
24
import java.util.concurrent.ExecutorService;
25
import java.util.concurrent.Executors;
26
 
27
import javax.xml.parsers.DocumentBuilder;
28
import javax.xml.parsers.DocumentBuilderFactory;
29
import javax.xml.parsers.ParserConfigurationException;
30
 
31
import org.w3c.dom.Document;
32
import org.xml.sax.InputSource;
33
import org.xml.sax.SAXException;
34
 
35
import org.openconcerto.sql.element.SQLElement;
17 ilm 36
import org.openconcerto.sql.element.SQLElementDirectory;
37
import org.openconcerto.sql.model.DBFileCache;
38
import org.openconcerto.sql.model.DBItemFileCache;
39
import org.openconcerto.sql.model.DBRoot;
40
import org.openconcerto.sql.model.DBStructureItem;
41
import org.openconcerto.sql.model.DBSystemRoot;
80 ilm 42
import org.openconcerto.sql.model.FieldMapper;
17 ilm 43
import org.openconcerto.sql.model.SQLBase;
44
import org.openconcerto.sql.model.SQLFilter;
94 ilm 45
import org.openconcerto.sql.model.SQLRow;
46
import org.openconcerto.sql.model.SQLRowListRSH;
47
import org.openconcerto.sql.model.SQLSelect;
17 ilm 48
import org.openconcerto.sql.model.SQLTable;
94 ilm 49
import org.openconcerto.sql.model.Where;
17 ilm 50
import org.openconcerto.sql.request.SQLFieldTranslator;
51
import org.openconcerto.utils.FileUtils;
83 ilm 52
import net.jcip.annotations.GuardedBy;
53
 
17 ilm 54
/**
55
 * Regroupe les objets nécessaires au framework.
56
 *
57
 * @author Sylvain CUAZ
58
 */
59
public abstract class Configuration {
60
 
61
    public static File getDefaultConfDir() {
62
        return new File(System.getProperty("user.home"), ".java/ilm/sql-config/");
63
    }
64
 
65
    private static Configuration instance;
66
 
67
    public static SQLFieldTranslator getTranslator(SQLTable t) {
68
        return getInstance().getTranslator();
69
    }
70
 
71
    public static Configuration getInstance() {
72
        return instance;
73
    }
74
 
75
    public static final void setInstance(Configuration instance) {
76
        Configuration.instance = instance;
77
    }
78
 
83 ilm 79
    @GuardedBy("this")
80
    private ExecutorService nonInteractiveSQLExecutor;
63 ilm 81
 
17 ilm 82
    public abstract ShowAs getShowAs();
83
 
84
    public abstract SQLBase getBase();
85
 
86
    public abstract DBRoot getRoot();
87
 
88
    public abstract DBSystemRoot getSystemRoot();
89
 
90
    public abstract SQLFilter getFilter();
91
 
92
    public abstract SQLFieldTranslator getTranslator();
93
 
94
    public abstract SQLElementDirectory getDirectory();
95
 
80 ilm 96
    public abstract FieldMapper getFieldMapper();
97
 
17 ilm 98
    public abstract File getWD();
99
 
73 ilm 100
    // abstract :
101
    // - we can't return a default name as we don't know how to localize it
102
    // - avoid that 2 different application share the same name (and perhaps configuration)
103
    public abstract String getAppName();
17 ilm 104
 
73 ilm 105
    /**
106
     * A string that should be unique to an application and this configuration. E.g. allow to store
107
     * different settings for different uses of a same application.
108
     *
109
     * @return a string beginning with {@link #getAppName()}, <code>null</code> if appName is
110
     *         <code>null</code> or empty.
111
     */
17 ilm 112
    public final String getAppID() {
73 ilm 113
        final String appName = this.getAppName();
114
        if (appName == null || appName.length() == 0)
115
            return null;
116
        return appName + getAppIDSuffix();
17 ilm 117
    }
118
 
119
    protected String getAppIDSuffix() {
120
        return "";
121
    }
122
 
123
    public File getConfDir() {
124
        return new File(getDefaultConfDir(), this.getAppID());
125
    }
126
 
127
    /**
128
     * A directory to store data depending on this {@link #getRoot() root}.
129
     *
130
     * @return a directory for this root.
131
     */
132
    public final File getConfDirForRoot() {
133
        return getConfDir(getRoot());
134
    }
135
 
136
    /**
137
     * Move {@link #getConfDir()}/<code>name</code> to {@link #getConfDirForRoot()}/
138
     * <code>name</code> if necessary.
139
     *
140
     * @param name the name of the file or directory to move.
141
     * @return the new file in <code>getConfDirForRoot()</code>.
142
     */
143
    public final File migrateToConfDirForRoot(final String name) {
144
        final File oldFile = new File(this.getConfDir(), name);
145
        final File newFile = new File(this.getConfDirForRoot(), name);
146
        if (oldFile.exists() && !newFile.exists()) {
147
            try {
148
                FileUtils.mkdir_p(newFile.getParentFile());
149
                oldFile.renameTo(newFile);
150
            } catch (IOException e) {
151
                e.printStackTrace();
152
                FileUtils.rmR(oldFile);
153
            }
154
        }
155
        return newFile;
156
    }
157
 
25 ilm 158
    public final File getConfDir(DBStructureItem<?> db) {
17 ilm 159
        return DBItemFileCache.getDescendant(new File(getConfDir(), "dataDepedent"), DBFileCache.getJDBCAncestorNames(db, true));
160
    }
161
 
162
    /**
163
     * Add the showAs, translator and directory of <code>o</code> to this.
164
     *
165
     * @param o the configuration to add.
166
     * @return this.
167
     * @see ShowAs#putAll(ShowAs)
168
     * @see SQLFieldTranslator#putAll(SQLFieldTranslator)
169
     * @see SQLElementDirectory#putAll(SQLElementDirectory)
170
     */
171
    public Configuration add(Configuration o) {
172
        this.getShowAs().putAll(o.getShowAs());
173
        this.getTranslator().putAll(o.getTranslator());
174
        this.getDirectory().putAll(o.getDirectory());
175
        return this;
176
    }
177
 
178
    /**
179
     * Signal that this conf will not be used anymore.
180
     */
83 ilm 181
    public void destroy() {
182
        synchronized (this) {
183
            if (this.nonInteractiveSQLExecutor != null) {
184
                this.nonInteractiveSQLExecutor.shutdown();
185
            }
186
        }
187
    }
63 ilm 188
 
65 ilm 189
    /**
94 ilm 190
     * Get xml value from table FWK_LIST_PREFS for an user and a table.
191
     *
192
     * @throws ParserConfigurationException
193
     * @throws IOException
194
     * @throws SAXException
195
     */
196
    public Document getXMLConf(final long userId, final String idTable) throws ParserConfigurationException, SAXException, IOException {
197
        final SQLElement element = this.getDirectory().getElement("FWK_LIST_PREFS");
198
        final SQLTable columnPrefsTable = element.getTable();
199
        final SQLSelect select = new SQLSelect();
200
        select.addSelectStar(columnPrefsTable);
201
        select.setWhere((new Where(columnPrefsTable.getField("ID_USER"), "=", userId)).and(new Where(columnPrefsTable.getField("ID_TABLE"), "=", idTable)));
202
        final List<SQLRow> rqResult = SQLRowListRSH.execute(select);
203
        if (rqResult != null && !rqResult.isEmpty()) {
204
            final DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
205
            final DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
206
            return docBuilder.parse(new InputSource(new StringReader(rqResult.get(0).getString("VALUE"))));
207
        }
208
        return null;
209
    }
210
 
211
    /**
65 ilm 212
     * An executor that should be used for background SQL requests. It can be used to limit the
213
     * concurrent number of database connections (as establishing a connection is expensive and the
214
     * server might have restrictions).
215
     *
216
     * @return a SQL executor.
217
     */
83 ilm 218
    public synchronized final Executor getNonInteractiveSQLExecutor() {
65 ilm 219
        if (this.nonInteractiveSQLExecutor == null) {
220
            this.nonInteractiveSQLExecutor = createNonInteractiveSQLExecutor();
63 ilm 221
        }
65 ilm 222
        return this.nonInteractiveSQLExecutor;
63 ilm 223
    }
224
 
83 ilm 225
    protected ExecutorService createNonInteractiveSQLExecutor() {
63 ilm 226
        return Executors.newFixedThreadPool(2);
227
    }
17 ilm 228
}