OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 19 | Rev 61 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
18 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
 package org.openconcerto.erp.config;
15
 
16
import org.openconcerto.sql.model.DBSystemRoot;
17
import org.openconcerto.sql.model.SQLServer;
18
import org.openconcerto.utils.CompareUtils;
19
import org.openconcerto.utils.cc.IClosure;
20
 
21
import java.io.File;
22
 
19 ilm 23
import javax.swing.JFrame;
24
import javax.swing.JOptionPane;
25
 
18 ilm 26
public class ServerFinderConfig {
27
    public static final String H2 = "H2";
28
    public static final String POSTGRESQL = "PostgreSQL";
29
    public static final String MYSQL = "MySQL";
30
    public String type = POSTGRESQL;
31
    private String ip;
32
    private File file;
33
    private String port;
25 ilm 34
    private String systemRoot = "OpenConcerto";
18 ilm 35
 
36
    private String dbLogin;
37
    private String dbPassword;
38
 
39
    private String openconcertoLogin = "openconcerto";
40
    private String openconcertoPassword = "openconcerto";
41
    private String product;
42
    private String error;
43
 
25 ilm 44
    public String getSystemRoot() {
45
        return systemRoot;
46
    }
47
 
48
    public void setSystemRoot(String systemRoot) {
49
        this.systemRoot = systemRoot;
50
    }
51
 
18 ilm 52
    public String getType() {
53
        return type;
54
    }
55
 
56
    public void setType(String type) {
57
        this.type = type;
58
    }
59
 
60
    public String getIp() {
61
        return ip;
62
    }
63
 
64
    public void setIp(String ip) {
65
        this.ip = ip;
66
    }
67
 
68
    public File getFile() {
69
        return file;
70
    }
71
 
72
    public void setFile(File file) {
19 ilm 73
        if (file == null) {
74
            JOptionPane.showMessageDialog(new JFrame(), "Dossier de base de donnée vide");
75
        } else if (!file.exists()) {
76
            JOptionPane.showMessageDialog(new JFrame(), "Dossier de base de donnée inexistant");
77
        }
18 ilm 78
        this.file = file;
79
    }
80
 
81
    public String getPort() {
82
        return port;
83
    }
84
 
85
    public void setPort(String port) {
86
        this.port = port;
87
    }
88
 
89
    public String getDbLogin() {
90
        return dbLogin;
91
    }
92
 
93
    public void setDbLogin(String dbLogin) {
94
        this.dbLogin = dbLogin;
95
    }
96
 
97
    public String getDbPassword() {
98
        return dbPassword;
99
    }
100
 
101
    public void setDbPassword(String dbPassword) {
102
        this.dbPassword = dbPassword;
103
    }
104
 
105
    public String getOpenconcertoLogin() {
106
        return openconcertoLogin;
107
    }
108
 
109
    public void setOpenconcertoLogin(String openconcertoLogin) {
110
        this.openconcertoLogin = openconcertoLogin;
111
    }
112
 
113
    public String getOpenconcertoPassword() {
114
        return openconcertoPassword;
115
    }
116
 
117
    public void setOpenconcertoPassword(String openconcertoPassword) {
118
        this.openconcertoPassword = openconcertoPassword;
119
    }
120
 
121
    public void setProduct(String string) {
122
        this.product = string;
123
 
124
    }
125
 
126
    public String getProduct() {
127
        return product;
128
    }
129
 
130
    public void setError(String string) {
131
        this.error = getFixedString(string);
132
    }
133
 
134
    static String getFixedString(String string) {
135
        // QuickFix UTF (PostgreSQL bug #5800)
136
        char errorUTF = 65533;
137
        String errorUTF2 = errorUTF + "" + errorUTF;
138
        int length = string.length();
139
        boolean needFix = false;
140
        for (int i = 0; i < length; i++) {
141
            if (string.charAt(i) == errorUTF) {
142
                needFix = true;
143
                break;
144
            }
145
        }
146
 
147
        if (needFix) {
148
            string = string.replace(" " + errorUTF + "chou" + errorUTF + "e ", " échouée ");
149
            string = string.replace(" " + errorUTF2 + "chou" + errorUTF2 + "e ", " échouée ");
150
            string = string.replace(errorUTF, ' ');
151
        }
152
        return string;
153
    }
154
 
155
    public String getError() {
156
        return error;
157
    }
158
 
159
    //
160
    public String test() {
161
        String result = "Erreur de connexion. \n";
162
        try {
19 ilm 163
            SQLServer server = createServer("Common");
18 ilm 164
            DBSystemRoot r = server.getSystemRoot("OpenConcerto");
165
            final boolean ok = CompareUtils.equals(1, r.getDataSource().executeScalar("SELECT 1"));
166
            if (ok) {
19 ilm 167
                result = "Connexion réussie sur la base OpenConcerto.";
168
                if (r.getChildrenNames().size() == 0) {
169
                    result = "Attention: la base OpenConcerto est vide";
170
                }
18 ilm 171
            }
172
            server.destroy();
173
        } catch (Exception e) {
174
            result += e.getMessage();
175
        }
176
        return getFixedString(result);
177
    }
178
 
179
    public boolean isOnline() {
180
 
181
        try {
182
            SQLServer server = createServer("public");
183
            DBSystemRoot r = server.getSystemRoot("OpenConcerto");
184
            return CompareUtils.equals(1, r.getDataSource().executeScalar("SELECT 1"));
185
        } catch (Exception e) {
186
            return false;
187
        }
188
 
189
    }
190
 
191
    public SQLServer createServer(final String root) {
19 ilm 192
        final String host;
193
        if (this.getType().equals(ServerFinderConfig.H2)) {
194
            host = "file:" + this.getFile().getAbsolutePath() + "/";
195
        } else {
196
            host = this.getIp();
197
        }
198
        final SQLServer server = new SQLServer(this.getType(), host, String.valueOf(this.getPort()), getOpenconcertoLogin(), getOpenconcertoPassword(), new IClosure<DBSystemRoot>() {
18 ilm 199
            @Override
200
            public void executeChecked(DBSystemRoot input) {
201
                // don't map all the database
202
                input.getRootsToMap().add(root);
203
            }
204
        }, null);
19 ilm 205
 
18 ilm 206
        return server;
207
    }
208
 
209
    /**
210
     *
211
     * @param user
212
     * @param password
213
     * @return true si l'utilisateur a été créé, false si il existe deja
214
     * @throws Exception
215
     */
216
    public boolean createUserIfNeeded(String user, String password) throws Exception {
19 ilm 217
        final String host;
218
        if (this.getType().equals(ServerFinderConfig.H2)) {
219
            host = "file:" + this.getFile().getAbsolutePath() + "/";
18 ilm 220
 
19 ilm 221
        } else {
222
            host = this.getIp();
223
        }
224
        if (this.getType().equals(ServerFinderConfig.POSTGRESQL)) {
225
            final SQLServer server = new SQLServer(this.getType(), host, String.valueOf(this.getPort()), user, password, new IClosure<DBSystemRoot>() {
18 ilm 226
 
19 ilm 227
                @Override
228
                public void executeChecked(DBSystemRoot input) {
229
                    // don't map all the database
230
                    input.getRootsToMap().add("postgres");
231
                }
232
            }, null);
233
            Number n = (Number) server.getBase("postgres").getDataSource().executeScalar("SELECT COUNT(*) FROM pg_user WHERE usename='openconcerto'");
234
            if (n.intValue() > 0) {
235
                return false;
18 ilm 236
            }
19 ilm 237
            server.getBase("postgres").getDataSource().execute("CREATE ROLE openconcerto LOGIN ENCRYPTED PASSWORD 'md51d6fb5ca62757af27ed31f93fc7751a7' SUPERUSER CREATEDB VALID UNTIL 'infinity'");
238
 
239
            // UPDATE pg_authid SET rolcatupdate=false WHERE rolname='openconcerto'
240
            server.destroy();
241
        } else {
242
            // FIXME: support MySQL & H2
243
            System.err.println("Not supported for this database");
244
 
18 ilm 245
        }
246
        return true;
247
        // CREATE ROLE openconcerto LOGIN ENCRYPTED PASSWORD 'md51d6fb5ca62757af27ed31f93fc7751a7'
248
        // SUPERUSER CREATEDB VALID UNTIL 'infinity'
249
        // UPDATE pg_authid SET rolcatupdate=false WHERE rolname='openconcerto';
250
    }
251
 
252
    @Override
253
    public String toString() {
19 ilm 254
        return this.getType() + ":" + this.getIp() + ":" + this.getPort() + " file:" + this.getFile() + " " + this.getOpenconcertoLogin() + "/" + this.getOpenconcertoPassword() + " ["
255
                + this.getDbLogin() + "/" + this.getDbPassword() + "]";
18 ilm 256
    }
25 ilm 257
 
18 ilm 258
}