Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / SingleVectorialDBConnectionExtension.java @ 15957

History | View | Annotate | Download (8.94 KB)

1 11971 caballero
/* 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 12826 jaume
import java.util.ArrayList;
46
47
import javax.swing.JOptionPane;
48
49
import org.apache.log4j.Logger;
50
51 11971 caballero
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 15647 jmvivo
        registerIcons();
80 11971 caballero
    }
81
82 15647 jmvivo
    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 11971 caballero
    /**
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 15684 vcaballero
//            saveAllToPersistence();
103 11971 caballero
104
            return;
105
        }
106
    }
107
108
    public boolean isEnabled() {
109
        return true;
110
    }
111
112
    public boolean isVisible() {
113
        return true;
114
    }
115
116 15684 vcaballero
    public static void saveAllToPersistence() {
117
        SingleVectorialDBConnectionExtension ext=new SingleVectorialDBConnectionExtension();
118
            XMLEntity xml = PluginServices.getPluginServices(ext).getPersistentXML();
119 14543 jmvivo
        xml.putProperty("literalDBName",true);
120 11971 caballero
        xml.remove("db-connections");
121
122
        ConnectionWithParams[] all = SingleVectorialDBConnectionManager.instance()
123
                                                                .getAllConnections();
124
125
        if (all == null) {
126
            return;
127
        }
128
129
        for (int i = 0; i < all.length; i++) {
130 15684 vcaballero
            addToPersistence(all[i],ext);
131 11971 caballero
        }
132
    }
133
134 15684 vcaballero
    private static void addToPersistence(ConnectionWithParams cwp, Extension ext) {
135 11971 caballero
        if (cwp == null) {
136
            return;
137
        }
138
139
        ConnectionSettings _cs = new ConnectionSettings();
140
141
        _cs.setHost(cwp.getHost());
142
        _cs.setPort(cwp.getPort());
143
        _cs.setDb(cwp.getDb());
144
        _cs.setDriver(cwp.getDrvName());
145
        _cs.setUser(cwp.getUser());
146
        _cs.setName(cwp.getName());
147
148
        String newstr = _cs.toString();
149
150 15684 vcaballero
        XMLEntity xml = PluginServices.getPluginServices(ext).getPersistentXML();
151 11971 caballero
152
        String[] old = null;
153
154
        if (xml.contains("db-connections")) {
155
            old = xml.getStringArrayProperty("db-connections");
156
        }
157
158
        ArrayList oldl = stringArrayToArrayList(old);
159
        oldl.add(newstr);
160
161
        String[] newarr = (String[]) oldl.toArray(new String[0]);
162
163
        xml.remove("db-connections");
164
        xml.putProperty("db-connections", newarr);
165 15684 vcaballero
        PluginServices.getPluginServices(ext).setPersistentXML(xml);
166 11971 caballero
    }
167
168 15684 vcaballero
    private static ArrayList stringArrayToArrayList(String[] arr) {
169 11971 caballero
        ArrayList resp = new ArrayList();
170
171
        if ((arr != null) && (arr.length > 0)) {
172
            for (int i = 0; i < arr.length; i++) {
173
                resp.add(arr[i]);
174
            }
175
        }
176
177
        return resp;
178
    }
179
180
    private void loadPersistenceConnections() {
181
        XMLEntity xml = PluginServices.getPluginServices(this).getPersistentXML();
182 14543 jmvivo
        boolean literalDBNames;
183 11971 caballero
184 14543 jmvivo
        if (!xml.contains("literalDBName")){
185
                // For back compatibility
186
                literalDBNames = false;
187
        }else{
188
                // For back compatibility: Normaly always true
189
                literalDBNames = xml.getBooleanProperty("literalDBName");
190
        }
191
192 11971 caballero
        if (xml == null) {
193
            xml = new XMLEntity();
194
        }
195
196
        if (!xml.contains("db-connections")) {
197
            String[] servers = new String[0];
198
            xml.putProperty("db-connections", servers);
199
        }
200
201
        // add drivers to connection manager
202
        String[] servers = null;
203
204
        try {
205
            servers = xml.getStringArrayProperty("db-connections");
206
        }
207
        catch (NotExistInXMLEntity e) {
208
            System.err.println(
209
                "Error while getting projects db-connections: " +
210
                e.getMessage());
211
212
            return;
213
        }
214
215
        for (int i = 0; i < servers.length; i++) {
216
            ConnectionSettings cs = new ConnectionSettings();
217
            boolean params_ok = true;
218
            try {
219
                    cs.setFromString(servers[i]);
220 14543 jmvivo
                    // For back compatibility
221
                    if (!literalDBNames){
222
                            cs.setDb(cs.getDb().toLowerCase());
223
                    }
224 11971 caballero
            } catch (Exception ex) {
225
                    logger.error("Found misconfigured connection: " + servers[i]);
226
                    params_ok = false;
227
            }
228
            if (params_ok) addDisconnected(cs);
229
        }
230 14543 jmvivo
231
        PluginServices ps= PluginServices.getPluginServices("com.iver.cit.gvsig.jdbc_spatial");
232
        if (ps==null)
233
                return;
234
        XMLEntity xmlJDBC =ps.getPersistentXML();
235
236
        if (xmlJDBC==null || !xmlJDBC.contains("jdbc-connections"))
237
                return;
238
239
//      add drivers to connection manager
240
        String[] serversOld = null;
241
242
        try {
243
            serversOld = xmlJDBC.getStringArrayProperty("jdbc-connections");
244
        }
245
        catch (NotExistInXMLEntity e) {
246
            System.err.println(
247
                "Error while getting projects jdbc-connections: " +
248
                e.getMessage());
249
250
            return;
251
        }
252
253
        for (int i = 0; i < serversOld.length; i++) {
254
            ConnectionSettings cs = new ConnectionSettings();
255
            boolean params_ok = true;
256
            try {
257
                    cs.setFromString(serversOld[i]);
258
                    cs.setDb(cs.getDb().toLowerCase());
259
            } catch (Exception ex) {
260
                    logger.error("Found misconfigured connection: " + serversOld[i]);
261
                    params_ok = false;
262
            }
263
            if (params_ok) addDisconnected(cs);
264
        }
265
266
267 11971 caballero
    }
268
269
    private void addDisconnected(ConnectionSettings _cs) {
270
        try {
271
            SingleVectorialDBConnectionManager.instance()
272
                                       .getConnection(_cs.getDriver(),
273
                _cs.getUser(), null, _cs.getName(), _cs.getHost(),
274
                _cs.getPort(), _cs.getDb(), false);
275
        }
276
        catch (DBException e) {
277
            System.err.println("While getting connection: " + e.getMessage());
278
            showConnectionErrorMessage(e.getMessage());
279
        }
280
    }
281
282
    private void showConnectionErrorMessage(String _msg) {
283
        String msg = (_msg.length() > 300) ? "" : (": " + _msg);
284
        String title = PluginServices.getText(this, "connection_error");
285
        JOptionPane.showMessageDialog(null, title + msg, title,
286
            JOptionPane.ERROR_MESSAGE);
287
    }
288
289
    public void terminate() {
290 15684 vcaballero
            saveAllToPersistence();
291 11971 caballero
        SingleVectorialDBConnectionManager.instance().closeAllBeforeTerminate();
292
    }
293
}