17 |
ilm |
1 |
/*
|
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
|
|
3 |
*
|
182 |
ilm |
4 |
* Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
|
17 |
ilm |
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 org.openconcerto.sql.element.SQLElement;
|
17 |
ilm |
20 |
import org.openconcerto.sql.element.SQLElementDirectory;
|
|
|
21 |
import org.openconcerto.sql.model.DBFileCache;
|
|
|
22 |
import org.openconcerto.sql.model.DBItemFileCache;
|
|
|
23 |
import org.openconcerto.sql.model.DBRoot;
|
|
|
24 |
import org.openconcerto.sql.model.DBStructureItem;
|
|
|
25 |
import org.openconcerto.sql.model.DBSystemRoot;
|
80 |
ilm |
26 |
import org.openconcerto.sql.model.FieldMapper;
|
17 |
ilm |
27 |
import org.openconcerto.sql.model.SQLBase;
|
|
|
28 |
import org.openconcerto.sql.model.SQLFilter;
|
94 |
ilm |
29 |
import org.openconcerto.sql.model.SQLRow;
|
|
|
30 |
import org.openconcerto.sql.model.SQLRowListRSH;
|
|
|
31 |
import org.openconcerto.sql.model.SQLSelect;
|
17 |
ilm |
32 |
import org.openconcerto.sql.model.SQLTable;
|
94 |
ilm |
33 |
import org.openconcerto.sql.model.Where;
|
17 |
ilm |
34 |
import org.openconcerto.sql.request.SQLFieldTranslator;
|
182 |
ilm |
35 |
import org.openconcerto.sql.users.User;
|
|
|
36 |
import org.openconcerto.sql.users.UserManager;
|
|
|
37 |
import org.openconcerto.sql.users.rights.UserRights;
|
|
|
38 |
import org.openconcerto.sql.users.rights.UserRightsManager;
|
142 |
ilm |
39 |
import org.openconcerto.utils.BaseDirs;
|
17 |
ilm |
40 |
import org.openconcerto.utils.FileUtils;
|
142 |
ilm |
41 |
import org.openconcerto.utils.StringUtils;
|
|
|
42 |
|
|
|
43 |
import java.io.File;
|
|
|
44 |
import java.io.IOException;
|
|
|
45 |
import java.io.StringReader;
|
|
|
46 |
import java.util.List;
|
177 |
ilm |
47 |
import java.util.Locale;
|
142 |
ilm |
48 |
import java.util.concurrent.Executor;
|
|
|
49 |
import java.util.concurrent.ExecutorService;
|
|
|
50 |
import java.util.concurrent.Executors;
|
|
|
51 |
|
|
|
52 |
import javax.xml.parsers.DocumentBuilder;
|
|
|
53 |
import javax.xml.parsers.DocumentBuilderFactory;
|
|
|
54 |
import javax.xml.parsers.ParserConfigurationException;
|
|
|
55 |
|
|
|
56 |
import org.w3c.dom.Document;
|
|
|
57 |
import org.xml.sax.InputSource;
|
|
|
58 |
import org.xml.sax.SAXException;
|
|
|
59 |
|
83 |
ilm |
60 |
import net.jcip.annotations.GuardedBy;
|
|
|
61 |
|
17 |
ilm |
62 |
/**
|
|
|
63 |
* Regroupe les objets nécessaires au framework.
|
|
|
64 |
*
|
|
|
65 |
* @author Sylvain CUAZ
|
|
|
66 |
*/
|
|
|
67 |
public abstract class Configuration {
|
|
|
68 |
|
|
|
69 |
public static File getDefaultConfDir() {
|
|
|
70 |
return new File(System.getProperty("user.home"), ".java/ilm/sql-config/");
|
|
|
71 |
}
|
|
|
72 |
|
144 |
ilm |
73 |
@GuardedBy("this")
|
17 |
ilm |
74 |
private static Configuration instance;
|
|
|
75 |
|
|
|
76 |
public static SQLFieldTranslator getTranslator(SQLTable t) {
|
132 |
ilm |
77 |
// FIXME : Parametre inutile...
|
17 |
ilm |
78 |
return getInstance().getTranslator();
|
|
|
79 |
}
|
|
|
80 |
|
144 |
ilm |
81 |
public synchronized static Configuration getInstance() {
|
17 |
ilm |
82 |
return instance;
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
public static final void setInstance(Configuration instance) {
|
144 |
ilm |
86 |
setInstance(instance, false);
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
public static final Configuration setInstance(final Configuration instance, final boolean destroyPrevious) {
|
|
|
90 |
final Configuration prev;
|
|
|
91 |
synchronized (Configuration.class) {
|
|
|
92 |
prev = Configuration.instance;
|
|
|
93 |
// don't destroy new instance
|
|
|
94 |
if (prev == instance)
|
|
|
95 |
return prev;
|
|
|
96 |
Configuration.instance = instance;
|
|
|
97 |
if (instance != null) {
|
|
|
98 |
try {
|
|
|
99 |
instance.migrateToNewDBConfDir();
|
|
|
100 |
} catch (IOException e) {
|
|
|
101 |
throw new IllegalStateException("Couldn't migrate");
|
|
|
102 |
}
|
|
|
103 |
}
|
142 |
ilm |
104 |
}
|
144 |
ilm |
105 |
if (prev != null && destroyPrevious)
|
|
|
106 |
prev.destroy();
|
|
|
107 |
return prev;
|
17 |
ilm |
108 |
}
|
|
|
109 |
|
142 |
ilm |
110 |
static public final void migrateToNewDir(final File oldDir, final File newDir) throws IOException {
|
|
|
111 |
if (oldDir.exists() && !newDir.exists()) {
|
|
|
112 |
if (!oldDir.isDirectory())
|
|
|
113 |
throw new IOException("Old file isn't a directory : " + oldDir);
|
|
|
114 |
FileUtils.mkdir_p(newDir.getParentFile());
|
|
|
115 |
final String err = FileUtils.mv(oldDir, newDir);
|
|
|
116 |
if (err != null)
|
|
|
117 |
throw new IOException("Couldn't migrate from " + oldDir + " : " + err);
|
|
|
118 |
}
|
|
|
119 |
}
|
|
|
120 |
|
83 |
ilm |
121 |
@GuardedBy("this")
|
|
|
122 |
private ExecutorService nonInteractiveSQLExecutor;
|
63 |
ilm |
123 |
|
17 |
ilm |
124 |
public abstract ShowAs getShowAs();
|
|
|
125 |
|
|
|
126 |
public abstract SQLBase getBase();
|
|
|
127 |
|
|
|
128 |
public abstract DBRoot getRoot();
|
|
|
129 |
|
182 |
ilm |
130 |
public abstract UserManager getUserManager();
|
|
|
131 |
|
|
|
132 |
public final User getCurrentUser() {
|
|
|
133 |
return UserManager.getCurrentUser(getUserManager());
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
public abstract UserRightsManager getUserRightsManager();
|
|
|
137 |
|
|
|
138 |
public final UserRights getCurrentUserRights() {
|
|
|
139 |
return UserRightsManager.getCurrentUserRights(getUserRightsManager(), getUserManager());
|
|
|
140 |
}
|
|
|
141 |
|
17 |
ilm |
142 |
public abstract DBSystemRoot getSystemRoot();
|
|
|
143 |
|
|
|
144 |
public abstract SQLFilter getFilter();
|
|
|
145 |
|
156 |
ilm |
146 |
public final SQLFieldTranslator getTranslator() {
|
|
|
147 |
return this.getDirectory().getTranslator();
|
|
|
148 |
}
|
17 |
ilm |
149 |
|
|
|
150 |
public abstract SQLElementDirectory getDirectory();
|
|
|
151 |
|
80 |
ilm |
152 |
public abstract FieldMapper getFieldMapper();
|
|
|
153 |
|
17 |
ilm |
154 |
public abstract File getWD();
|
|
|
155 |
|
73 |
ilm |
156 |
// abstract :
|
|
|
157 |
// - we can't return a default name as we don't know how to localize it
|
|
|
158 |
// - avoid that 2 different application share the same name (and perhaps configuration)
|
|
|
159 |
public abstract String getAppName();
|
17 |
ilm |
160 |
|
73 |
ilm |
161 |
/**
|
|
|
162 |
* A string that should be unique to an application and this configuration. E.g. allow to store
|
|
|
163 |
* different settings for different uses of a same application.
|
|
|
164 |
*
|
|
|
165 |
* @return a string beginning with {@link #getAppName()}, <code>null</code> if appName is
|
|
|
166 |
* <code>null</code> or empty.
|
|
|
167 |
*/
|
17 |
ilm |
168 |
public final String getAppID() {
|
73 |
ilm |
169 |
final String appName = this.getAppName();
|
142 |
ilm |
170 |
if (StringUtils.isEmpty(appName))
|
73 |
ilm |
171 |
return null;
|
142 |
ilm |
172 |
final String variant = this.getAppVariant();
|
|
|
173 |
if (StringUtils.isEmpty(variant, true))
|
|
|
174 |
return appName;
|
|
|
175 |
return appName + '-' + variant;
|
17 |
ilm |
176 |
}
|
|
|
177 |
|
142 |
ilm |
178 |
public String getAppVariant() {
|
|
|
179 |
return null;
|
17 |
ilm |
180 |
}
|
|
|
181 |
|
142 |
ilm |
182 |
public abstract BaseDirs getBaseDirs();
|
|
|
183 |
|
|
|
184 |
public final File getConfDir() {
|
|
|
185 |
return getBaseDirs().getPreferencesFolder();
|
|
|
186 |
}
|
|
|
187 |
|
|
|
188 |
// for migration use
|
|
|
189 |
@Deprecated
|
|
|
190 |
protected File getOldConfDir() {
|
17 |
ilm |
191 |
return new File(getDefaultConfDir(), this.getAppID());
|
|
|
192 |
}
|
|
|
193 |
|
|
|
194 |
/**
|
|
|
195 |
* A directory to store data depending on this {@link #getRoot() root}.
|
|
|
196 |
*
|
|
|
197 |
* @return a directory for this root.
|
|
|
198 |
*/
|
|
|
199 |
public final File getConfDirForRoot() {
|
|
|
200 |
return getConfDir(getRoot());
|
|
|
201 |
}
|
|
|
202 |
|
142 |
ilm |
203 |
public final void migrateToNewDBConfDir() throws IOException {
|
|
|
204 |
final File oldFile = getOldDBConfDir();
|
|
|
205 |
final File newFile = getDBConfDir();
|
|
|
206 |
migrateToNewDir(oldFile, newFile);
|
17 |
ilm |
207 |
}
|
|
|
208 |
|
142 |
ilm |
209 |
// for migration use
|
|
|
210 |
private File getOldDBConfDir() {
|
|
|
211 |
return new File(getOldConfDir(), "dataDepedent");
|
|
|
212 |
}
|
|
|
213 |
|
|
|
214 |
// for migration use
|
|
|
215 |
@Deprecated
|
|
|
216 |
protected final File getOldConfDir(DBStructureItem<?> db) {
|
|
|
217 |
return DBItemFileCache.getDescendant(getOldDBConfDir(), DBFileCache.getJDBCAncestorNames(db, true));
|
|
|
218 |
}
|
|
|
219 |
|
|
|
220 |
private File getDBConfDir() {
|
|
|
221 |
return new File(getConfDir(), "dataDependent");
|
|
|
222 |
}
|
|
|
223 |
|
25 |
ilm |
224 |
public final File getConfDir(DBStructureItem<?> db) {
|
142 |
ilm |
225 |
return DBItemFileCache.getDescendant(getDBConfDir(), DBFileCache.getJDBCAncestorNames(db, true));
|
17 |
ilm |
226 |
}
|
|
|
227 |
|
177 |
ilm |
228 |
public Locale getLocale() {
|
|
|
229 |
return Locale.getDefault();
|
|
|
230 |
}
|
|
|
231 |
|
17 |
ilm |
232 |
/**
|
156 |
ilm |
233 |
* Add the directory of <code>o</code> to this.
|
17 |
ilm |
234 |
*
|
|
|
235 |
* @param o the configuration to add.
|
|
|
236 |
* @return this.
|
|
|
237 |
* @see SQLElementDirectory#putAll(SQLElementDirectory)
|
|
|
238 |
*/
|
|
|
239 |
public Configuration add(Configuration o) {
|
|
|
240 |
this.getDirectory().putAll(o.getDirectory());
|
|
|
241 |
return this;
|
|
|
242 |
}
|
|
|
243 |
|
|
|
244 |
/**
|
|
|
245 |
* Signal that this conf will not be used anymore.
|
|
|
246 |
*/
|
83 |
ilm |
247 |
public void destroy() {
|
|
|
248 |
synchronized (this) {
|
|
|
249 |
if (this.nonInteractiveSQLExecutor != null) {
|
|
|
250 |
this.nonInteractiveSQLExecutor.shutdown();
|
|
|
251 |
}
|
|
|
252 |
}
|
|
|
253 |
}
|
63 |
ilm |
254 |
|
65 |
ilm |
255 |
/**
|
94 |
ilm |
256 |
* Get xml value from table FWK_LIST_PREFS for an user and a table.
|
|
|
257 |
*
|
142 |
ilm |
258 |
* @param userId - Id of user
|
|
|
259 |
* @param idTable - Id of table
|
|
|
260 |
*
|
|
|
261 |
* @throws IllegalStateException
|
|
|
262 |
* @throws IllegalArgumentException
|
94 |
ilm |
263 |
*/
|
142 |
ilm |
264 |
public Document getXMLConf(final Number userId, final String idTable) throws IllegalStateException, IllegalArgumentException {
|
94 |
ilm |
265 |
final SQLElement element = this.getDirectory().getElement("FWK_LIST_PREFS");
|
|
|
266 |
final SQLTable columnPrefsTable = element.getTable();
|
|
|
267 |
final SQLSelect select = new SQLSelect();
|
|
|
268 |
select.addSelectStar(columnPrefsTable);
|
|
|
269 |
select.setWhere((new Where(columnPrefsTable.getField("ID_USER"), "=", userId)).and(new Where(columnPrefsTable.getField("ID_TABLE"), "=", idTable)));
|
|
|
270 |
final List<SQLRow> rqResult = SQLRowListRSH.execute(select);
|
|
|
271 |
if (rqResult != null && !rqResult.isEmpty()) {
|
|
|
272 |
final DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
|
142 |
ilm |
273 |
DocumentBuilder docBuilder;
|
|
|
274 |
try {
|
|
|
275 |
docBuilder = docFactory.newDocumentBuilder();
|
|
|
276 |
} catch (final ParserConfigurationException ex) {
|
|
|
277 |
throw new IllegalStateException("Impossible to create new XML document", ex);
|
|
|
278 |
}
|
|
|
279 |
|
|
|
280 |
try {
|
|
|
281 |
return docBuilder.parse(new InputSource(new StringReader(rqResult.get(0).getString("VALUE"))));
|
|
|
282 |
} catch (final SAXException ex) {
|
|
|
283 |
throw new IllegalArgumentException("Impossible to parse XML from database", ex);
|
|
|
284 |
} catch (final IOException ex) {
|
|
|
285 |
throw new IllegalStateException("Impossible to read content of database", ex);
|
|
|
286 |
}
|
94 |
ilm |
287 |
}
|
|
|
288 |
return null;
|
|
|
289 |
}
|
|
|
290 |
|
|
|
291 |
/**
|
142 |
ilm |
292 |
* Remove XML value from table FWK_LIST_PREFS for an user and a table.
|
|
|
293 |
*
|
|
|
294 |
* @param userId - Id of user
|
|
|
295 |
* @param idTable - Id of table
|
|
|
296 |
*
|
|
|
297 |
* @throws IllegalStateException
|
|
|
298 |
* @throws IllegalArgumentException
|
|
|
299 |
*/
|
|
|
300 |
public void removeXMLConf(final Number userId, final String idTable) throws IllegalStateException, IllegalArgumentException {
|
|
|
301 |
final SQLElement element = this.getDirectory().getElement("FWK_LIST_PREFS");
|
|
|
302 |
final SQLTable columnPrefsTable = element.getTable();
|
|
|
303 |
|
|
|
304 |
this.getRoot().getDBSystemRoot().getDataSource().execute("DELETE FROM " + columnPrefsTable.getSQLName().quote() + " WHERE \"ID_USER\" = " + userId + " AND \"ID_TABLE\" = '" + idTable + "'");
|
|
|
305 |
}
|
|
|
306 |
|
|
|
307 |
/**
|
65 |
ilm |
308 |
* An executor that should be used for background SQL requests. It can be used to limit the
|
|
|
309 |
* concurrent number of database connections (as establishing a connection is expensive and the
|
|
|
310 |
* server might have restrictions).
|
|
|
311 |
*
|
|
|
312 |
* @return a SQL executor.
|
|
|
313 |
*/
|
83 |
ilm |
314 |
public synchronized final Executor getNonInteractiveSQLExecutor() {
|
65 |
ilm |
315 |
if (this.nonInteractiveSQLExecutor == null) {
|
|
|
316 |
this.nonInteractiveSQLExecutor = createNonInteractiveSQLExecutor();
|
63 |
ilm |
317 |
}
|
65 |
ilm |
318 |
return this.nonInteractiveSQLExecutor;
|
63 |
ilm |
319 |
}
|
|
|
320 |
|
83 |
ilm |
321 |
protected ExecutorService createNonInteractiveSQLExecutor() {
|
63 |
ilm |
322 |
return Executors.newFixedThreadPool(2);
|
|
|
323 |
}
|
17 |
ilm |
324 |
}
|