Revision 27389

View differences:

branches/v2_0_0_prep/extensions/extGeoDB/src/com/iver/cit/gvsig/SingleVectorialDBConnectionExtension.java
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.vectorialdb.ConnectionSettings;
54
import com.iver.utiles.NotExistInXMLEntity;
55
import com.iver.utiles.XMLEntity;
56

  
57

  
58
/**
59
 * This extension allows the user to access the single connection manager dialog
60
 *
61
 * @see org.gvsig.fmap.drivers.formats.db.utils.SingleVectorialDBConnectionManager
62
 *
63
 * @author jldominguez
64
 *
65
 */
66
public class SingleVectorialDBConnectionExtension extends Extension {
67
    private static Logger logger = Logger.getLogger(SingleVectorialDBConnectionExtension.class.getName());
68

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

  
78
    private void registerIcons(){
79
    	PluginServices.getIconTheme().register(
80
    			"conn-image",
81
    			this.getClass().getClassLoader().getResource("images/conn.png")
82
    	);
83
    	PluginServices.getIconTheme().register(
84
    			"disconn-image",
85
    			this.getClass().getClassLoader().getResource("images/disconn.png")
86
    	);
87

  
88
    }
89

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

  
105
    public boolean isEnabled() {
106
        return true;
107
    }
108

  
109
    public boolean isVisible() {
110
        return true;
111
    }
112

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

  
119
//        ConnectionWithParams[] all = SingleVectorialDBConnectionManager.instance()
120
//                                                                .getAllConnections();
121
//
122
//        if (all == null) {
123
//        	PluginServices.getPluginServices(ext).setPersistentXML(xml);
124
//            return;
125
//        }
126
//
127
//        for (int i = 0; i < all.length; i++) {
128
//            addToPersistence(all[i],ext);
129
//        }
130
    }
131

  
132
//    private static void addToPersistence(ConnectionWithParams cwp, Extension ext) {
133
//        if (cwp == null) {
134
//            return;
135
//        }
136
//
137
//        ConnectionSettings _cs = new ConnectionSettings();
138
//
139
//        _cs.setHost(cwp.getHost());
140
//        _cs.setPort(cwp.getPort());
141
//        _cs.setDb(cwp.getDb());
142
//        _cs.setDriver(cwp.getDrvName());
143
//        _cs.setUser(cwp.getUser());
144
//        _cs.setName(cwp.getName());
145
//
146
//        String newstr = _cs.toString();
147
//
148
//        XMLEntity xml = PluginServices.getPluginServices(ext).getPersistentXML();
149
//
150
//        String[] old = null;
151
//
152
//        if (xml.contains("db-connections")) {
153
//            old = xml.getStringArrayProperty("db-connections");
154
//        }
155
//
156
//        ArrayList oldl = stringArrayToArrayList(old);
157
//        oldl.add(newstr);
158
//
159
//        String[] newarr = (String[]) oldl.toArray(new String[0]);
160
//
161
//        xml.remove("db-connections");
162
//        xml.putProperty("db-connections", newarr);
163
//        PluginServices.getPluginServices(ext).setPersistentXML(xml);
164
//    }
165

  
166
    private static ArrayList stringArrayToArrayList(String[] arr) {
167
        ArrayList resp = new ArrayList();
168

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

  
175
        return resp;
176
    }
177

  
178
    private void loadPersistenceConnections() {
179
        XMLEntity xml = PluginServices.getPluginServices(this).getPersistentXML();
180
        boolean literalDBNames;
181

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

  
190
        if (xml == null) {
191
            xml = new XMLEntity();
192
        }
193

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

  
199
        // add drivers to connection manager
200
        String[] servers = null;
201

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

  
210
            return;
211
        }
212

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

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

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

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

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

  
252
            return;
253
        }
254

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

  
270

  
271
    }
272

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

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

  
293
    public void terminate() {
294
    	saveAllToPersistence();
295
//        SingleVectorialDBConnectionManager.instance().closeAllBeforeTerminate();
296
    }
297
}
0 298

  
branches/v2_0_0_prep/extensions/extGeoDB/.classpath
1
<?xml version="1.0" encoding="UTF-8"?>
2
<classpath>
3
	<classpathentry kind="src" path="src"/>
4
	<classpathentry kind="src" output="bin-test" path="src-test"/>
5
	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
6
	<classpathentry combineaccessrules="false" kind="src" path="/appgvSIG"/>
7
	<classpathentry combineaccessrules="false" kind="src" path="/_fwAndami"/>
8
	<classpathentry kind="lib" path="lib/org.gvsig.fmap.dal-spi.jar"/>
9
	<classpathentry kind="lib" path="lib/org.gvsig.fmap.dal.jar"/>
10
	<classpathentry kind="lib" path="lib/org.gvsig.tools.jar"/>
11
	<classpathentry kind="lib" path="lib/org.cresques.cts.jar"/>
12
	<classpathentry kind="lib" path="lib/org.gvsig.fmap.geometry.jar"/>
13
	<classpathentry combineaccessrules="false" kind="src" path="/libFMap_controls"/>
14
	<classpathentry combineaccessrules="false" kind="src" path="/libFMap_mapcontext"/>
15
	<classpathentry kind="lib" path="/_fwAndami/lib/org.gvsig.ui.jar" sourcepath="/libUIComponent"/>
16
	<classpathentry kind="lib" path="/_fwAndami/lib/iver-utiles.jar" sourcepath="/libIverUtiles"/>
17
	<classpathentry kind="output" path="bin"/>
18
</classpath>
0 19

  
branches/v2_0_0_prep/extensions/extGeoDB/.project
1
<?xml version="1.0" encoding="UTF-8"?>
2
<projectDescription>
3
	<name>extGeoDB</name>
4
	<comment></comment>
5
	<projects>
6
	</projects>
7
	<buildSpec>
8
		<buildCommand>
9
			<name>org.eclipse.jdt.core.javabuilder</name>
10
			<arguments>
11
			</arguments>
12
		</buildCommand>
13
	</buildSpec>
14
	<natures>
15
		<nature>org.eclipse.jdt.core.javanature</nature>
16
	</natures>
17
</projectDescription>
0 18

  

Also available in: Unified diff