Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / SingleVectorialDBConnectionExtension.java @ 24306

History | View | Annotate | Download (9 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2006 Prodevelop and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *   Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *   +34 963862235
28
 *   gvsig@gva.es
29
 *   www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   Prodevelop Integraci?n de Tecnolog?as SL
34
 *   Conde Salvatierra de ?lava , 34-10
35
 *   46004 Valencia
36
 *   Spain
37
 *
38
 *   +34 963 510 612
39
 *   +34 963 510 968
40
 *   gis@prodevelop.es
41
 *   http://www.prodevelop.es
42
 */
43
package com.iver.cit.gvsig;
44

    
45
import java.util.ArrayList;
46

    
47
import javax.swing.JOptionPane;
48

    
49
import org.apache.log4j.Logger;
50

    
51
import com.iver.andami.PluginServices;
52
import com.iver.andami.plugins.Extension;
53
import com.iver.cit.gvsig.fmap.drivers.DBException;
54
import com.iver.cit.gvsig.fmap.drivers.db.utils.ConnectionWithParams;
55
import com.iver.cit.gvsig.fmap.drivers.db.utils.SingleVectorialDBConnectionManager;
56
import com.iver.cit.gvsig.vectorialdb.ConnectionSettings;
57
import com.iver.utiles.NotExistInXMLEntity;
58
import com.iver.utiles.XMLEntity;
59
import com.prodevelop.cit.gvsig.vectorialdb.wizard.VectorialDBConnectionManagerDialog;
60

    
61

    
62
/**
63
 * This extension allows the user to access the single connection manager dialog
64
 *
65
 * @see com.iver.cit.gvsig.fmap.drivers.db.utils.SingleVectorialDBConnectionManager
66
 *
67
 * @author jldominguez
68
 *
69
 */
70
public class SingleVectorialDBConnectionExtension extends Extension {
71
    private static Logger logger = Logger.getLogger(SingleVectorialDBConnectionExtension.class.getName());
72

    
73
    /**
74
     * This method simply loads the connections stored in gvSIG's persistence file
75
     * as closed connections
76
     */
77
    public void initialize() {
78
        loadPersistenceConnections();
79
        registerIcons();
80
    }
81

    
82
    private void registerIcons(){
83
            PluginServices.getIconTheme().register(
84
                            "conn-image",
85
                            this.getClass().getClassLoader().getResource("images/conn.png")
86
            );
87
            PluginServices.getIconTheme().register(
88
                            "disconn-image",
89
                            this.getClass().getClassLoader().getResource("images/disconn.png")
90
            );
91

    
92
    }
93

    
94
    /**
95
     * The only command available is the one to open the connection manager
96
     * dialog ("GESTOR_JDBC")
97
     */
98
    public void execute(String actionCommand) {
99
        if (actionCommand.compareToIgnoreCase("GESTOR_VECTORIALDB") == 0) {
100
            VectorialDBConnectionManagerDialog dlg = new VectorialDBConnectionManagerDialog();
101
            dlg.showDialog();
102
//            saveAllToPersistence();
103

    
104
            return;
105
        }
106
    }
107

    
108
    public boolean isEnabled() {
109
        return true;
110
    }
111

    
112
    public boolean isVisible() {
113
        return true;
114
    }
115

    
116
    public static void saveAllToPersistence() {
117
        SingleVectorialDBConnectionExtension ext=new SingleVectorialDBConnectionExtension();
118
            XMLEntity xml = PluginServices.getPluginServices(ext).getPersistentXML();
119
        xml.putProperty("literalDBName",true);
120
        xml.remove("db-connections");
121

    
122
        ConnectionWithParams[] all = SingleVectorialDBConnectionManager.instance()
123
                                                                .getAllConnections();
124

    
125
        if (all == null) {
126
                PluginServices.getPluginServices(ext).setPersistentXML(xml);
127
            return;
128
        }
129

    
130
        for (int i = 0; i < all.length; i++) {
131
            addToPersistence(all[i],ext);
132
        }
133
    }
134

    
135
    private static void addToPersistence(ConnectionWithParams cwp, Extension ext) {
136
        if (cwp == null) {
137
            return;
138
        }
139

    
140
        ConnectionSettings _cs = new ConnectionSettings();
141

    
142
        _cs.setHost(cwp.getHost());
143
        _cs.setPort(cwp.getPort());
144
        _cs.setDb(cwp.getDb());
145
        _cs.setDriver(cwp.getDrvName());
146
        _cs.setUser(cwp.getUser());
147
        _cs.setName(cwp.getName());
148

    
149
        String newstr = _cs.toString();
150

    
151
        XMLEntity xml = PluginServices.getPluginServices(ext).getPersistentXML();
152

    
153
        String[] old = null;
154

    
155
        if (xml.contains("db-connections")) {
156
            old = xml.getStringArrayProperty("db-connections");
157
        }
158

    
159
        ArrayList oldl = stringArrayToArrayList(old);
160
        oldl.add(newstr);
161

    
162
        String[] newarr = (String[]) oldl.toArray(new String[0]);
163

    
164
        xml.remove("db-connections");
165
        xml.putProperty("db-connections", newarr);
166
        PluginServices.getPluginServices(ext).setPersistentXML(xml);
167
    }
168

    
169
    private static ArrayList stringArrayToArrayList(String[] arr) {
170
        ArrayList resp = new ArrayList();
171

    
172
        if ((arr != null) && (arr.length > 0)) {
173
            for (int i = 0; i < arr.length; i++) {
174
                resp.add(arr[i]);
175
            }
176
        }
177

    
178
        return resp;
179
    }
180

    
181
    private void loadPersistenceConnections() {
182
        XMLEntity xml = PluginServices.getPluginServices(this).getPersistentXML();
183
        boolean literalDBNames;
184

    
185
        if (!xml.contains("literalDBName")){
186
                // For back compatibility
187
                literalDBNames = false;
188
        }else{
189
                // For back compatibility: Normaly always true
190
                literalDBNames = xml.getBooleanProperty("literalDBName");
191
        }
192

    
193
        if (xml == null) {
194
            xml = new XMLEntity();
195
        }
196

    
197
        if (!xml.contains("db-connections")) {
198
            String[] servers = new String[0];
199
            xml.putProperty("db-connections", servers);
200
        }
201

    
202
        // add drivers to connection manager
203
        String[] servers = null;
204

    
205
        try {
206
            servers = xml.getStringArrayProperty("db-connections");
207
        }
208
        catch (NotExistInXMLEntity e) {
209
            System.err.println(
210
                "Error while getting projects db-connections: " +
211
                e.getMessage());
212

    
213
            return;
214
        }
215

    
216
        for (int i = 0; i < servers.length; i++) {
217
            ConnectionSettings cs = new ConnectionSettings();
218
            boolean params_ok = true;
219
            try {
220
                    cs.setFromString(servers[i]);
221
                    // For back compatibility
222
                    if (!literalDBNames){
223
                            cs.setDb(cs.getDb().toLowerCase());
224
                    }
225
            } catch (Exception ex) {
226
                    logger.error("Found misconfigured connection: " + servers[i]);
227
                    params_ok = false;
228
            }
229
            if (params_ok) addDisconnected(cs);
230
        }
231

    
232
        PluginServices ps= PluginServices.getPluginServices("com.iver.cit.gvsig.jdbc_spatial");
233
        if (ps==null)
234
                return;
235
        XMLEntity xmlJDBC =ps.getPersistentXML();
236

    
237
        if (xmlJDBC==null || !xmlJDBC.contains("jdbc-connections"))
238
                return;
239

    
240
//      add drivers to connection manager
241
        String[] serversOld = null;
242

    
243
        try {
244
            serversOld = xmlJDBC.getStringArrayProperty("jdbc-connections");
245
        }
246
        catch (NotExistInXMLEntity e) {
247
            System.err.println(
248
                "Error while getting projects jdbc-connections: " +
249
                e.getMessage());
250

    
251
            return;
252
        }
253

    
254
        for (int i = 0; i < serversOld.length; i++) {
255
            ConnectionSettings cs = new ConnectionSettings();
256
            boolean params_ok = true;
257
            try {
258
                    cs.setFromString(serversOld[i]);
259
                    cs.setDb(cs.getDb().toLowerCase());
260
            } catch (Exception ex) {
261
                    logger.error("Found misconfigured connection: " + serversOld[i]);
262
                    params_ok = false;
263
            }
264
            if (params_ok) addDisconnected(cs);
265
        }
266

    
267

    
268
    }
269

    
270
    private void addDisconnected(ConnectionSettings _cs) {
271
        try {
272
            SingleVectorialDBConnectionManager.instance()
273
                                       .getConnection(_cs.getDriver(),
274
                _cs.getUser(), null, _cs.getName(), _cs.getHost(),
275
                _cs.getPort(), _cs.getDb(), false);
276
        }
277
        catch (DBException e) {
278
            System.err.println("While getting connection: " + e.getMessage());
279
            showConnectionErrorMessage(e.getMessage());
280
        }
281
    }
282

    
283
    private void showConnectionErrorMessage(String _msg) {
284
        String msg = (_msg.length() > 300) ? "" : (": " + _msg);
285
        String title = PluginServices.getText(this, "connection_error");
286
        JOptionPane.showMessageDialog(null, title + msg, title,
287
            JOptionPane.ERROR_MESSAGE);
288
    }
289

    
290
    public void terminate() {
291
            saveAllToPersistence();
292
        SingleVectorialDBConnectionManager.instance().closeAllBeforeTerminate();
293
    }
294
}