OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 25 | Go to most recent revision | Details | 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;
33
 
34
/**
35
 * Regroupe les objets nécessaires au framework.
36
 *
37
 * @author Sylvain CUAZ
38
 */
39
public abstract class Configuration {
40
 
41
    public static File getDefaultConfDir() {
42
        return new File(System.getProperty("user.home"), ".java/ilm/sql-config/");
43
    }
44
 
45
    private static Configuration instance;
46
 
47
    public static SQLFieldTranslator getTranslator(SQLTable t) {
48
        return getInstance().getTranslator();
49
    }
50
 
51
    public static Configuration getInstance() {
52
        return instance;
53
    }
54
 
55
    public static final void setInstance(Configuration instance) {
56
        Configuration.instance = instance;
57
    }
58
 
59
    public abstract ShowAs getShowAs();
60
 
61
    public abstract SQLBase getBase();
62
 
63
    public abstract DBRoot getRoot();
64
 
65
    public abstract DBSystemRoot getSystemRoot();
66
 
67
    public abstract SQLFilter getFilter();
68
 
69
    public abstract SQLFieldTranslator getTranslator();
70
 
71
    public abstract SQLElementDirectory getDirectory();
72
 
73
    public abstract File getWD();
74
 
75
    public String getAppName() {
76
        return "unnamed application";
77
    }
78
 
79
    public final String getAppID() {
80
        return this.getAppName() + getAppIDSuffix();
81
    }
82
 
83
    protected String getAppIDSuffix() {
84
        return "";
85
    }
86
 
87
    public File getConfDir() {
88
        return new File(getDefaultConfDir(), this.getAppID());
89
    }
90
 
91
    /**
92
     * A directory to store data depending on this {@link #getRoot() root}.
93
     *
94
     * @return a directory for this root.
95
     */
96
    public final File getConfDirForRoot() {
97
        return getConfDir(getRoot());
98
    }
99
 
100
    /**
101
     * Move {@link #getConfDir()}/<code>name</code> to {@link #getConfDirForRoot()}/
102
     * <code>name</code> if necessary.
103
     *
104
     * @param name the name of the file or directory to move.
105
     * @return the new file in <code>getConfDirForRoot()</code>.
106
     */
107
    public final File migrateToConfDirForRoot(final String name) {
108
        final File oldFile = new File(this.getConfDir(), name);
109
        final File newFile = new File(this.getConfDirForRoot(), name);
110
        if (oldFile.exists() && !newFile.exists()) {
111
            try {
112
                FileUtils.mkdir_p(newFile.getParentFile());
113
                oldFile.renameTo(newFile);
114
            } catch (IOException e) {
115
                e.printStackTrace();
116
                FileUtils.rmR(oldFile);
117
            }
118
        }
119
        return newFile;
120
    }
121
 
122
    protected final File getConfDir(DBStructureItem<?> db) {
123
        return DBItemFileCache.getDescendant(new File(getConfDir(), "dataDepedent"), DBFileCache.getJDBCAncestorNames(db, true));
124
    }
125
 
126
    /**
127
     * Add the showAs, translator and directory of <code>o</code> to this.
128
     *
129
     * @param o the configuration to add.
130
     * @return this.
131
     * @see ShowAs#putAll(ShowAs)
132
     * @see SQLFieldTranslator#putAll(SQLFieldTranslator)
133
     * @see SQLElementDirectory#putAll(SQLElementDirectory)
134
     */
135
    public Configuration add(Configuration o) {
136
        this.getShowAs().putAll(o.getShowAs());
137
        this.getTranslator().putAll(o.getTranslator());
138
        this.getDirectory().putAll(o.getDirectory());
139
        return this;
140
    }
141
 
142
    /**
143
     * Signal that this conf will not be used anymore.
144
     */
145
    public abstract void destroy();
146
}