Revision 20743

View differences:

trunk/libraries/libFMap_dataDB/src-test/org/gvsig/data/datastores/vectorial/db/jdbc/PrepareResourceObserver.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
5
*
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
10
*
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
* GNU General Public License for more details.
15
*
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
* MA  02110-1301, USA.
20
*
21
*/
22

  
23
/*
24
* AUTHORS (In addition to CIT):
25
* 2008 IVER T.I   {{Task}}
26
*/
27

  
28
/**
29
 *
30
 */
31
package org.gvsig.data.datastores.vectorial.db.jdbc;
32

  
33
import org.gvsig.data.IResourceNotification;
34
import org.gvsig.data.exception.InitializeException;
35
import org.gvsig.util.observer.IObservable;
36
import org.gvsig.util.observer.IObserver;
37

  
38
/**
39
 * @author jmvivo
40
 *
41
 */
42
public class PrepareResourceObserver implements IObserver {
43

  
44
	private String passw;
45
	private String url;
46
	public PrepareResourceObserver(String url,String passw){
47
		this.passw=passw;
48
		this.url=url;
49
	}
50

  
51
	public void update(IObservable observable, Object notification) {
52
		if (!(notification instanceof IResourceNotification)) {
53
			return;
54
		}
55
		IResourceNotification resNot = (IResourceNotification) notification;
56
		if (!resNot.getType().equals(IResourceNotification.PREPARE)){
57
			return;
58
		}
59
		JDBCResource res = (JDBCResource)resNot.getResource();
60
		if (res.getUrl().equals(url)){
61
			try {
62
				res.setPassword(passw);
63
				System.out.println("Prepared: "+res.getKey()+" Password <== "+res.getPassword());
64
			} catch (InitializeException e) {
65
				throw new RuntimeException(e);
66
			}
67
		}
68

  
69

  
70
	}
71

  
72
}
73

  
0 74

  
trunk/libraries/libFMap_dataDB/src-test/org/gvsig/data/datastores/vectorial/db/jdbc/JDBCTest.java
1 1
package org.gvsig.data.datastores.vectorial.db.jdbc;
2 2

  
3
import java.util.Iterator;
4

  
3 5
import junit.framework.TestCase;
4 6

  
5 7
import org.gvsig.data.DataManager;
8
import org.gvsig.data.IDataCollection;
6 9
import org.gvsig.data.IDataStoreParameters;
10
import org.gvsig.data.Resource;
11
import org.gvsig.data.ResourceManager;
12
import org.gvsig.data.datastores.vectorial.file.FileResource;
7 13
import org.gvsig.data.exception.CloseException;
8 14
import org.gvsig.data.exception.DataException;
9 15
import org.gvsig.data.exception.InitializeException;
10 16
import org.gvsig.data.exception.OpenException;
11 17
import org.gvsig.data.exception.ReadException;
12 18
import org.gvsig.data.exception.WriteException;
19
import org.gvsig.data.vectorial.FeatureStore;
13 20
import org.gvsig.data.vectorial.IFeature;
14 21
import org.gvsig.data.vectorial.IFeatureCollection;
15 22
import org.gvsig.data.vectorial.IFeatureStore;
16 23
import org.gvsig.data.vectorial.IFeatureType;
17
import org.gvsig.data.vectorial.IsNotFeatureSettingException;
18 24
import org.gvsig.data.vectorial.visitor.PrintlnFeaturesVisitor;
19 25
import org.gvsig.exceptions.BaseException;
26
import org.gvsig.util.observer.IObserver;
20 27

  
21 28
public abstract class JDBCTest extends TestCase {
22 29

  
......
124 131
			}
125 132
		}
126 133

  
134
	public static void doFileResourceTest(JDBCStoreParameters params){
135
		doFileResourceTest(params,true);
136
	}
137

  
138
	public static void doFileResourceTest(JDBCStoreParameters params,boolean testEdit){
139
		DataManager manager = DataManager.getManager();
140

  
141
		ResourceManager resMan = ResourceManager.getResourceManager();
142

  
143
		FeatureStore store=null;
144
		FeatureStore store2=null;
145
		FeatureStore store3=null;
146
		try {
147
			store = (FeatureStore)manager.createDataStore(params);
148
			store2 = (FeatureStore)manager.createDataStore(params);
149
			store3 = (FeatureStore)manager.createDataStore(params);
150
		} catch (InitializeException e1) {
151
			e1.printStackTrace();fail();
152
		}
153

  
154
		int i=0;
155
		Resource res = null;
156
		Resource tmpRes = null;
157
		Object obj = null;
158

  
159
		Iterator iter = resMan.iterator();
160
		while (iter.hasNext()){
161
			obj = iter.next();
162
			if (obj instanceof JDBCResource){
163
				tmpRes = (Resource)obj;
164
				if (((JDBCResource)tmpRes).getUrl().equals(params.getUrl())){
165
					i++;
166
					res=tmpRes;
167
				}
168
			}
169
		}
170

  
171
		assertEquals(1, i);
172

  
173
		assertEquals(3, res.getRefencesCount());
174

  
175
		try {
176
			store.close();
177
		} catch (CloseException e1) {
178
			e1.printStackTrace();fail();
179
		}
180

  
181
		assertEquals(false, res.isOpen());
182

  
183
		IDataCollection coll = null;
184

  
185
		try {
186
			coll =store.getDataCollection();
187
		} catch (ReadException e1) {
188
			e1.printStackTrace();fail();
189
		}
190

  
191
		coll.iterator().next();
192

  
193
		assertEquals(true, res.isOpen());
194

  
195
		coll.dispose();
196

  
197

  
198

  
199
		if (store.isEditable() && testEdit){
200
			/*Test edition notification*/
201

  
202
			int fCountOrg=0;
203
			int fCountFin=0;
204
			try {
205
				fCountOrg = store.getDataCollection().size();
206
			} catch (ReadException e2) {
207
				// TODO Auto-generated catch block
208
				e2.printStackTrace();fail();
209
			}
210
			try {
211
				store.startEditing();
212
			} catch (ReadException e1) {
213
				e1.printStackTrace();fail();
214
			}
215

  
216
			try {
217
				coll = store2.getDataCollection();
218
			} catch (ReadException e1) {
219
				e1.printStackTrace();fail();
220
			}
221

  
222
			try {
223
				store.finishEditing();
224
			} catch (WriteException e1) {
225
				e1.printStackTrace();fail();
226
			} catch (ReadException e1) {
227
				// TODO Auto-generated catch block
228
				e1.printStackTrace();fail();
229
			}
230

  
231
			try {
232
				fCountFin = store.getDataCollection().size();
233
			} catch (ReadException e2) {
234
				// TODO Auto-generated catch block
235
				e2.printStackTrace();fail();
236
			}
237

  
238
			assertEquals(fCountOrg, fCountFin);
239

  
240

  
241
			boolean isOk = false;
242
			try{
243
				coll.iterator().next();
244
			} catch (Exception e){
245
				isOk=true;
246
			}
247
			assertTrue("Resource Changed Notification fails",isOk);
248

  
249
			coll.dispose();
250

  
251
			/*Test edition notification END*/
252

  
253
		}
254

  
255

  
256
		JDBCExplorer explorer = null;
257
		try {
258
			explorer = (JDBCExplorer) store.getExplorer();
259
		} catch (ReadException e) {
260
			e.printStackTrace();fail();
261
		}
262

  
263
		if (explorer != null){
264
			assertEquals(4, res.getRefencesCount());
265

  
266
			try {
267
				explorer.list();
268
			} catch (ReadException e) {
269
				e.printStackTrace();fail();
270
			}
271

  
272
			try {
273
				explorer.dispose();
274
			} catch (DataException e1) {
275
				e1.printStackTrace();fail();
276
			}
277

  
278

  
279
			assertEquals(3, res.getRefencesCount());
280
		}
281
		try {
282
			store3.dispose();
283
		} catch (CloseException e1) {
284
			e1.printStackTrace();fail();
285
		}
286

  
287
		assertEquals(2, res.getRefencesCount());
288

  
289
		try {
290
			store2.dispose();
291
		} catch (CloseException e1) {
292
			e1.printStackTrace();fail();
293
		}
294

  
295
		assertEquals(1, res.getRefencesCount());
296

  
297
		try {
298
			store.dispose();
299
		} catch (CloseException e1) {
300
			e1.printStackTrace();fail();
301
		}
302

  
303
		assertEquals(0, res.getRefencesCount());
304
		res = null;
305

  
306
		i=0;
307
		iter = resMan.iterator();
308
		while (iter.hasNext()){
309
			obj = iter.next();
310
			if (obj instanceof FileResource){
311
				tmpRes = (Resource)obj;
312
				if (((JDBCResource)tmpRes).getUrl().equals(params.getUrl())){
313
					i++;
314
					res=tmpRes;
315
				}
316
			}
317
		}
318

  
319
		assertEquals(0, i);
320

  
321
		doPrepareFileResourceTest(params);
322

  
323
	}
324

  
325
	public static void doPrepareFileResourceTest(JDBCStoreParameters params){
326
		DataManager manager = DataManager.getManager();
327

  
328
		ResourceManager resMan = ResourceManager.getResourceManager();
329

  
330
		String passw = params.getPassw();
331
		params.setPassw("--------------");
332

  
333
		IObserver obs = new PrepareResourceObserver(params.getUrl(), passw);
334

  
335

  
336
		resMan.addObserver(obs);
337

  
338
		FeatureStore store=null;
339
		try {
340
			store = (FeatureStore)manager.createDataStore(params);
341
		} catch (InitializeException e1) {
342
			e1.printStackTrace();fail();
343
		} catch (Exception e2){
344
			e2.printStackTrace();fail();
345
		}
346

  
347
		try {
348
			store.getDataCollection().iterator().next();
349
		} catch (ReadException e) {
350
			// TODO Auto-generated catch block
351
			e.printStackTrace();fail();
352
		}
353

  
354
		try {
355
			store.close();
356
		} catch (CloseException e) {
357
			// TODO Auto-generated catch block
358
			e.printStackTrace();fail();
359
		}
360
		try {
361
			store.dispose();
362
		} catch (CloseException e) {
363
			// TODO Auto-generated catch block
364
			e.printStackTrace();fail();
365
		}
366

  
367
	}
368

  
127 369
}
trunk/libraries/libFMap_dataDB/src-test/org/gvsig/data/datastores/vectorial/db/jdbc/h2/H2Test.java
274 274
			fail("Exception:" + e);
275 275
		}
276 276

  
277
		try {
278
			fs.dispose();
279
		} catch (CloseException e) {
280
			e.printStackTrace();fail();
281
		}
277 282

  
283

  
278 284
		deleteTable(dp);
279 285

  
280 286
		System.out.println("======= /H2 (sql Mode) ==============");
......
788 794
			fail("Exception:" + e);
789 795
		}
790 796

  
797
		try {
798
			fs.dispose();
799
		} catch (CloseException e) {
800
			e.printStackTrace();fail();
801
		}
791 802

  
803
		// TODO FALTA Notificaci?n de edici?n
804
//		JDBCTest.doFileResourceTest(dp);
805
		JDBCTest.doFileResourceTest(dp,false);
806

  
792 807
		deleteTable(dp);
793 808

  
794 809
		System.out.println("======= /H2 ==============");
......
1078 1093
	}
1079 1094

  
1080 1095

  
1081

  
1082

  
1083

  
1084

  
1085

  
1086

  
1087

  
1088

  
1089 1096
	protected void tearDown() throws Exception {
1090 1097
		// TODO Auto-generated method stub
1091 1098
		super.tearDown();
trunk/libraries/libFMap_dataDB/src/org/gvsig/data/datastores/vectorial/db/jdbc/JDBCExplorer.java
3 3
import java.sql.Connection;
4 4

  
5 5
import org.gvsig.data.DataManager;
6
import org.gvsig.data.IDataExplorerParameters;
6 7
import org.gvsig.data.IDataStore;
7 8
import org.gvsig.data.IDataStoreParameters;
8 9
import org.gvsig.data.datastores.vectorial.db.DBExplorer;
9 10
import org.gvsig.data.exception.InitializeException;
10 11
import org.gvsig.data.exception.InitializeWriterException;
12
import org.gvsig.data.exception.ReadException;
11 13

  
12 14
public abstract class JDBCExplorer extends DBExplorer {
13 15

  
14
	protected abstract Connection getConnection() throws InitializeException;
16
	protected JDBCResource resource;
17
	protected JDBCExplorerParameter parameters;
15 18

  
19

  
20
	/* (non-Javadoc)
21
	 * @see org.gvsig.data.IDataExplorer#init(org.gvsig.data.IDataExplorerParameters)
22
	 */
23
	public void init(IDataExplorerParameters parameters,JDBCResource resource) throws InitializeException {
24
		this.parameters = (JDBCExplorerParameter)parameters;
25
		this.resource = resource;
26
	}
27

  
28
	protected Connection getConnection() throws ReadException{
29
		return this.resource.getConnection();
30
	}
31

  
16 32
	public IDataStore createDataStore(IDataStoreParameters dsp) throws InitializeException, InitializeWriterException {
17 33
		DataManager manager = DataManager.getManager();
18 34
		return manager.createDataStore(dsp);
trunk/libraries/libFMap_dataDB/src/org/gvsig/data/datastores/vectorial/db/jdbc/h2/H2FeatureCollection.java
58 58
	private ResultSet getNewResulset(String aSql) throws ReadException{
59 59
		this.store.open();
60 60

  
61
		Connection conn = this.store.getCurrentConnection();
61
		Connection conn = this.store.getConnection();
62 62
		try {
63 63
			Statement st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
64 64
			return st.executeQuery(aSql);
trunk/libraries/libFMap_dataDB/src/org/gvsig/data/datastores/vectorial/db/jdbc/h2/H2Resource.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
5
*
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
10
*
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
* GNU General Public License for more details.
15
*
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
* MA  02110-1301, USA.
20
*
21
*/
22

  
23
/*
24
* AUTHORS (In addition to CIT):
25
* 2008 IVER T.I   {{Task}}
26
*/
27

  
28
/**
29
 *
30
 */
31
package org.gvsig.data.datastores.vectorial.db.jdbc.h2;
32

  
33
import java.sql.Connection;
34
import java.sql.DriverManager;
35

  
36
import org.gvsig.data.datastores.vectorial.db.jdbc.JDBCExplorerParameter;
37
import org.gvsig.data.datastores.vectorial.db.jdbc.JDBCResource;
38
import org.gvsig.data.datastores.vectorial.db.jdbc.JDBCStoreParameters;
39
import org.gvsig.data.datastores.vectorial.db.jdbc.exception.JDBCDriverNotFoundException;
40
import org.gvsig.data.exception.DataException;
41
import org.gvsig.data.exception.InitializeException;
42
import org.gvsig.data.exception.OpenException;
43
import org.gvsig.data.exception.ReadException;
44
import org.gvsig.data.exception.ResourceChangedException;
45

  
46
/**
47
 * @author jmvivo
48
 *
49
 */
50
public class H2Resource extends JDBCResource {
51

  
52
	/* (non-Javadoc)
53
	 * @see org.gvsig.data.datastores.vectorial.db.jdbc.JDBCResource#getConnection()
54
	 */
55
	protected Connection getConnection() throws ReadException {
56
		return super.getConnection();
57
	}
58

  
59
	/**
60
	 * @param params
61
	 */
62
	H2Resource(JDBCStoreParameters params) {
63
		super(params);
64
	}
65

  
66
	/**
67
	 * @param params
68
	 */
69
	H2Resource(JDBCExplorerParameter params) {
70
		super(params);
71
	}
72

  
73
	/* (non-Javadoc)
74
	 * @see org.gvsig.data.datastores.vectorial.db.jdbc.JDBCResource#createConnection()
75
	 */
76
	protected Connection createConnection() throws ReadException {
77
		Connection conn = null;
78

  
79
		try {
80
			Class.forName("org.h2.Driver");
81
		} catch (ClassNotFoundException e) {
82
			throw new JDBCDriverNotFoundException("org.h2.Driver",e);
83
		}
84
		try {
85
			conn = DriverManager.getConnection(this.getUrl(), this.getUser(), this.getPassword());
86

  
87
		} catch (java.sql.SQLException e1) {
88
			throw new InitializeException(this.getName(),e1);
89
		}
90
		return conn;
91
	}
92

  
93
	/* (non-Javadoc)
94
	 * @see org.gvsig.data.Resource#generateKey()
95
	 */
96
	protected String generateKey() {
97
		return this.getName()+";"+this.getUrl()+";"+this.getUser();
98
	}
99

  
100
	/* (non-Javadoc)
101
	 * @see org.gvsig.data.Resource#getName()
102
	 */
103
	public String getName() {
104
		return "h2";
105
	}
106

  
107
}
108

  
0 109

  
trunk/libraries/libFMap_dataDB/src/org/gvsig/data/datastores/vectorial/db/jdbc/h2/H2FeatureCollectionEditingFiltered.java
70 70
	private ResultSet getNewResulset(String aSql) throws ReadException{
71 71
		this.store.open();
72 72

  
73
		Connection con = this.store.getCurrentConnection();
73
		Connection con = this.store.getConnection();
74 74

  
75 75
		try {
76 76
			Statement st = con.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
trunk/libraries/libFMap_dataDB/src/org/gvsig/data/datastores/vectorial/db/jdbc/h2/H2FeatureCollectionEditing.java
56 56
	private ResultSet getNewResulset(String aSql) throws ReadException{
57 57
		this.store.open();
58 58

  
59
		Connection conn = this.store.getCurrentConnection();
59
		Connection conn = this.store.getConnection();
60 60

  
61 61
		try {
62 62
			Statement st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
trunk/libraries/libFMap_dataDB/src/org/gvsig/data/datastores/vectorial/db/jdbc/h2/H2FeaturesWriter.java
30 30
import com.iver.cit.gvsig.fmap.core.IGeometry;
31 31
import com.vividsolutions.jts.io.WKBWriter;
32 32

  
33
class H2FeaturesWriter extends JDBCFeaturesWriter implements ISelectiveWriter {
33
class H2FeaturesWriter extends JDBCFeaturesWriter{
34 34
	DBFeatureType featureType;
35 35
	boolean bCreateTable=false;
36 36
	private String toEncode;
......
45 45

  
46 46
	}
47 47

  
48
	public void init(IFeatureStore store) {
48
	public void init(IFeatureStore store) throws InitializeWriterException {
49 49
		super.init(store);
50 50
		H2Store h2Store = (H2Store)store;
51 51
		this.parameters=h2Store.getParametersH2();
trunk/libraries/libFMap_dataDB/src/org/gvsig/data/datastores/vectorial/db/jdbc/h2/H2Store.java
10 10
import org.gvsig.data.IDataCollection;
11 11
import org.gvsig.data.IDataExplorer;
12 12
import org.gvsig.data.IDataStoreParameters;
13
import org.gvsig.data.ResourceManager;
13 14
import org.gvsig.data.datastores.vectorial.db.DBFeatureType;
14 15
import org.gvsig.data.datastores.vectorial.db.jdbc.JDBCFeaturesWriter;
15 16
import org.gvsig.data.datastores.vectorial.db.jdbc.JDBCStore;
16 17
import org.gvsig.data.datastores.vectorial.db.jdbc.WKBParser2;
18
import org.gvsig.data.datastores.vectorial.file.dxf.DXFResource;
17 19
import org.gvsig.data.exception.CloseException;
20
import org.gvsig.data.exception.DataException;
18 21
import org.gvsig.data.exception.InitializeException;
19 22
import org.gvsig.data.exception.InitializeWriterException;
20 23
import org.gvsig.data.exception.OpenException;
......
39 42
		return (H2StoreParameters)this.parameters;
40 43
	}
41 44

  
45
    public void init(IDataStoreParameters parameters) throws InitializeException {
42 46

  
43
	public void init(IDataStoreParameters parameters) throws InitializeException {
44
		super.init(parameters);
47
		H2Resource tmpResource = new H2Resource((H2StoreParameters)parameters);
48
		H2Resource theResource;
49
		ResourceManager resMan = ResourceManager.getResourceManager();
45 50

  
51
		try {
52
			theResource = (H2Resource)resMan.addResource(tmpResource);
53
		} catch (DataException e1) {
54
			throw new InitializeException(this.getName(),e1);
55
		}
46 56

  
57
		super.init(parameters,theResource);
47 58

  
48
		this.initConnection();
59

  
49 60
		this.initFeatureType();
50 61
		this.initSqlProperties();
51

  
52

  
53

  
54

  
55

  
56
			//writer.setCreateTable(false);
57
			//writer.setWriteAll(false);
58
			//writer.initialize(lyrDef);
59

  
60 62
	}
61 63

  
62 64
	private void initFeatureType() throws InitializeException{
63 65
		H2StoreParameters dParams = this.getParametersH2();
64 66
		try {
65
			this.featureType = H2Utils.getFeatureType(this.connection, dParams);
67
			this.featureType = H2Utils.getFeatureType(this.getConnection(), dParams);
66 68
		} catch (ReadException e) {
67 69
			throw new InitializeException(DATASTORE_NAME,e);
68 70
		}
......
90 92
	}
91 93

  
92 94

  
93
	private void initConnection() throws InitializeException{
94
		H2StoreParameters dParams = this.getParametersH2();
95

  
96
		String dburl = dParams.getUrl();
97
		String dbuser = dParams.getUser();
98
		String dbpass = dParams.getPassw();
99

  
100
		this.connection = H2Utils.getConnection(dburl, dbuser, dbpass);
101

  
102
	}
103

  
104 95
	public IDataCollection getDataCollection(IFeatureType type, String filter, String order) throws ReadException {
105 96
		if (useSqlSource ){
106 97
			if (filter != null || order != null){
......
134 125
		return coll;
135 126
	}
136 127

  
137
	protected Connection getConnection(){
138
		// FIXME: OJO REsource manager
139
		return this.connection;
128
	protected Connection getConnection() throws ReadException{
129
		return ((H2Resource)this.resource).getConnection();
140 130

  
141 131
	}
142 132

  
143
	Connection getCurrentConnection(){
144
		return this.getConnection();
145
	}
146

  
147 133
	private void initSqlProperties() throws InitializeException{
148 134
		H2StoreParameters dParams = (H2StoreParameters)this.getParameters();
149 135
		if (dParams.getSqlSoure() != null){
......
181 167
		return true;
182 168
	}
183 169

  
184
	protected void doOpen() throws OpenException {
185
		// FIXME: Resource Manager
186

  
187

  
188
	}
189

  
190
	protected void doClose() throws CloseException {
191
		// FIXME: Resource Manager
192
		try {
193
			connection.close();
194
		} catch (java.sql.SQLException e) {
195
			throw new CloseException("H2",e);
196
		}
197
	}
198

  
199 170
	protected void doDispose() throws CloseException {
200 171
		super.doDispose();
201
		//TODO: FALTA!!!
202

  
203 172
	}
204 173

  
205 174
	public String getName() {
......
230 199

  
231 200
	}
232 201

  
233
	static String getConnectionResourceID(String dbUrl,String dbUser){
234
		return H2Store.CONNECTION_STRING+";"+dbUrl+";"+dbUser;
235 202

  
236
	}
237

  
238

  
239 203
	protected IFeature createFeatureFromResulset(ResultSet rs, DBFeatureType featureType2) throws ReadException{
240 204
		return new H2Feature(featureType2,this,rs);
241 205
	}
trunk/libraries/libFMap_dataDB/src/org/gvsig/data/datastores/vectorial/db/jdbc/h2/H2Explorer.java
1 1
package org.gvsig.data.datastores.vectorial.db.jdbc.h2;
2 2

  
3
import java.security.KeyException;
3 4
import java.sql.Connection;
4 5
import java.sql.PreparedStatement;
5 6
import java.sql.ResultSet;
......
14 15
import org.gvsig.data.IDataExplorerParameters;
15 16
import org.gvsig.data.IDataStoreParameters;
16 17
import org.gvsig.data.INewDataStoreParameters;
18
import org.gvsig.data.ResourceManager;
17 19
import org.gvsig.data.datastores.vectorial.db.DBAttributeDescriptor;
18 20
import org.gvsig.data.datastores.vectorial.db.DBFeatureType;
19 21
import org.gvsig.data.datastores.vectorial.db.jdbc.JDBCExplorer;
22
import org.gvsig.data.exception.CloseException;
23
import org.gvsig.data.exception.DataException;
20 24
import org.gvsig.data.exception.InitializeException;
21 25
import org.gvsig.data.exception.InitializeWriterException;
22 26
import org.gvsig.data.exception.ReadException;
......
26 30

  
27 31
public class H2Explorer extends JDBCExplorer {
28 32
	public static String DATAEXPLORER_NAME = "H2Explorer";
29
	private H2ExplorerParameters parameters;
30 33
	private String defaultSchema;
31 34

  
32
	protected Connection getConnection() throws InitializeException {
33

  
34
		String dburl = parameters.getUrl();
35
		String dbuser = parameters.getUser();
36
		String dbpass = parameters.getPassw();
37

  
38
		return H2Utils.getConnection(dburl, dbuser, dbpass);
39

  
40
	}
41

  
42 35
	private void appendFieldToCreteSQL(DBAttributeDescriptor attr,StringBuffer sql) throws InitializeException{
43 36
		/**
44 37
		 * {name dataType
......
207 200

  
208 201
	public IDataStoreParameters add(INewFeatureStoreParameters ndsp)
209 202
			throws InitializeException, InitializeWriterException {
210
		Connection conn = this.getConnection();
203
		Connection conn;
204
		try {
205
			conn = this.getConnection();
206
		} catch (ReadException e1) {
207
			throw new InitializeException(this.getName(),e1);
208
		}
211 209

  
212 210
		try {
213 211
			conn.setAutoCommit(true);
......
331 329
	}
332 330

  
333 331
	public void init(IDataExplorerParameters parameters) throws InitializeException {
334
		this.parameters = (H2ExplorerParameters)parameters;
335
		this.defaultSchema = H2Utils.getDefaultSchema(this.getConnection(), this.parameters.getCatalog());
332

  
333
		H2Resource tmpResource = new H2Resource((H2StoreParameters)parameters);
334
		H2Resource theResource;
335
		ResourceManager resMan = ResourceManager.getResourceManager();
336

  
337
		try {
338
			theResource = (H2Resource)resMan.addResource(tmpResource);
339
		} catch (DataException e1) {
340
			throw new InitializeException(this.getName(),e1);
341
		}
342

  
343
		super.init(parameters,theResource);
344
		try {
345
			this.defaultSchema = H2Utils.getDefaultSchema(this.getConnection(), this.parameters.getCatalog());
346
		} catch (ReadException e) {
347
			throw new InitializeException(this.getName(),e);
348
		}
336 349
	}
337 350

  
338 351
	private H2StoreParameters newStoreParamters(){
......
343 356
	}
344 357

  
345 358
	public IDataStoreParameters[] list() throws ReadException {
346
		return this.list(this.parameters.isShowInformationDBTables());
359
		return this.list(((H2ExplorerParameters)this.parameters).isShowInformationDBTables());
347 360
	}
348 361

  
349 362
	public IDataStoreParameters[] list(boolean showInformationDBTables) throws ReadException {
......
387 400
		return this.defaultSchema;
388 401
	}
389 402

  
403
	/* (non-Javadoc)
404
	 * @see org.gvsig.data.IDataExplorer#dispose()
405
	 */
406
	public void dispose() throws DataException {
407
		ResourceManager resMan = ResourceManager.getResourceManager();
408

  
409
    	try {
410
			resMan.remove(this.resource);
411
		} catch (DataException e1) {
412
			throw new CloseException(this.getName(),e1);
413
		} catch (KeyException e) {
414
			throw new CloseException(this.getName(),e);
415
		}
416

  
417
	}
418

  
390 419
}
trunk/libraries/libFMap_dataDB/src/org/gvsig/data/datastores/vectorial/db/jdbc/h2/H2Utils.java
444 444
		return url;
445 445
	}
446 446

  
447
	static Connection getConnection(String dbUrl,String dbUser, String dbPass) throws InitializeException{
448
		//TODO: Aqu? habria que implementar la llamada
449
		//      al Resource Manager para comprobar si ya hay
450
		//		una connexion a la BD
451
		String connID = getConnectionResourceID(dbUrl, dbUser);
452

  
453
		Connection conn = null;
454
//		IResource res = ResourceManager.getResourceManager().getResource(connID);
455

  
456

  
457

  
458
		try {
459
			Class.forName("org.h2.Driver");
460
		} catch (ClassNotFoundException e) {
461
			throw new JDBCDriverNotFoundException("org.h2.Driver",e);
462
		}
463
		try {
464
			conn = DriverManager.getConnection(dbUrl, dbUser, dbPass);
465
//			conn.setAutoCommit(false);
466

  
467
		} catch (java.sql.SQLException e1) {
468
			throw new InitializeException("H2",e1);
469
		}
470
		//TODO: Registrar en el Resource manager
471
		// ResourceManager.getResourceManager().addResource(res);
472

  
473
		return conn;
474
	}
475

  
476

  
477 447
}
trunk/libraries/libFMap_dataDB/src/org/gvsig/data/datastores/vectorial/db/jdbc/JDBCFeaturesWriter.java
8 8
import org.gvsig.data.datastores.vectorial.db.DBFeatureType;
9 9
import org.gvsig.data.datastores.vectorial.db.jdbc.h2.H2StoreParameters;
10 10
import org.gvsig.data.datastores.vectorial.db.jdbc.postgresql.PostgresqlStore;
11
import org.gvsig.data.exception.CloseException;
12
import org.gvsig.data.exception.DataException;
13
import org.gvsig.data.exception.InitializeWriterException;
11 14
import org.gvsig.data.exception.OpenException;
12 15
import org.gvsig.data.exception.ReadException;
13 16
import org.gvsig.data.exception.WriteException;
......
20 23
	protected boolean previousAutocommit;
21 24

  
22 25

  
23
	public void init(IFeatureStore store) {
26
	public void init(IFeatureStore store) throws InitializeWriterException{
24 27
		this.store = (JDBCStore)store;
25
		this.conex = this.store.getConnection();
28
		try {
29
			this.conex = this.store.getWriterConnection();
30
		} catch (ReadException e) {
31
			throw new InitializeWriterException(this.store.getName(),e);
32
		}
26 33
	}
27 34

  
35
	/* (non-Javadoc)
36
	 * @see org.gvsig.data.datastores.vectorial.IFeaturesWriter#dispose()
37
	 */
38
	public void dispose() throws DataException {
39
		try {
40
			this.conex.close();
41
		} catch (SQLException e) {
42
			throw new CloseException(this.store.getName(),e);
43
		}
44
		this.conex = null;
28 45

  
46
	}
47

  
48

  
29 49
	public void preProcess() throws WriteException, ReadException {
30 50
		try {
31 51
			previousAutocommit = conex.getAutoCommit();
trunk/libraries/libFMap_dataDB/src/org/gvsig/data/datastores/vectorial/db/jdbc/JDBCResource.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
5
*
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
10
*
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
* GNU General Public License for more details.
15
*
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
* MA  02110-1301, USA.
20
*
21
*/
22

  
23
/*
24
* AUTHORS (In addition to CIT):
25
* 2008 IVER T.I   {{Task}}
26
*/
27

  
28
/**
29
 *
30
 */
31
package org.gvsig.data.datastores.vectorial.db.jdbc;
32

  
33
import java.sql.Connection;
34

  
35
import org.gvsig.data.Resource;
36
import org.gvsig.data.datastores.vectorial.file.IFileStoreParameters;
37
import org.gvsig.data.exception.CloseException;
38
import org.gvsig.data.exception.DataException;
39
import org.gvsig.data.exception.InitializeException;
40
import org.gvsig.data.exception.OpenException;
41
import org.gvsig.data.exception.ReadException;
42
import org.gvsig.data.exception.ResourceChangedException;
43

  
44
/**
45
 * @author jmvivo
46
 *
47
 */
48
public abstract class JDBCResource extends Resource {
49
	protected Connection connection = null;
50

  
51
	private String user;
52
	private String url;
53
	private String password;
54

  
55

  
56

  
57
	/* (non-Javadoc)
58
	 * @see org.gvsig.data.Resource#doDispose()
59
	 */
60
	protected void doDispose() throws DataException {
61
		this.connection = this.createConnection();
62
		this.setOpened();
63

  
64
	}
65

  
66
	/* (non-Javadoc)
67
	 * @see org.gvsig.data.Resource#doOpen()
68
	 */
69
	protected boolean doOpen() throws OpenException {
70
		try {
71
			this.connection = this.createConnection();
72
		} catch (ReadException e) {
73
			throw new OpenException(this.getName(),e);
74
		}
75
		this.setOpened();
76
		return true;
77
	}
78

  
79
	public JDBCResource(JDBCStoreParameters params){
80
		this.loadParamsData(params);
81
	}
82

  
83
	public JDBCResource(JDBCExplorerParameter params){
84
		this.loadParamsData(params);
85
	}
86

  
87
	/* (non-Javadoc)
88
	 * @see org.gvsig.data.Resource#doClose()
89
	 */
90
	protected boolean doClose() throws CloseException {
91
		try {
92
			connection.close();
93
		} catch (java.sql.SQLException e) {
94
			throw new CloseException(this.getName(),e);
95
		}
96
		return super.doClose();
97
	}
98

  
99
	/* (non-Javadoc)
100
	 * @see org.gvsig.data.Resource#getName()
101
	 */
102
	public String getName() {
103
		// TODO Auto-generated method stub
104
		return null;
105
	}
106

  
107
	protected void loadParamsData(JDBCStoreParameters params){
108
		this.url = params.getUrl();
109
		this.user = params.getUser();
110
		this.password = params.getPassw();
111
	}
112

  
113
	protected void loadParamsData(JDBCExplorerParameter params){
114
		this.url = params.getUrl();
115
		this.user = params.getUser();
116
		this.password = params.getPassw();
117
	}
118

  
119

  
120
	protected abstract Connection createConnection() throws ReadException;
121

  
122

  
123
	/* (non-Javadoc)
124
	 * @see org.gvsig.data.Resource#description()
125
	 */
126
	public String description() {
127
		return this.getName()+" JDBC Connection Resource: url='"+this.getUrl()+"' user='"+this.getUser()+"'";
128
	}
129

  
130
	/* (non-Javadoc)
131
	 * @see org.gvsig.data.Resource#doChanged()
132
	 */
133
	protected void doChanged() {
134
		// No Operation
135
	}
136

  
137
	/**
138
	 * @return the password
139
	 */
140
	public String getPassword() {
141
		return password;
142
	}
143

  
144
	/**
145
	 * @param password the password to set
146
	 * @throws InitializeException
147
	 */
148
	public void setPassword(String password) throws InitializeException {
149
		if (this.getRefencesCount() > 0){
150
			throw new InitializeException("Resource in use",this.description());
151
		}
152
		this.password = password;
153
	}
154

  
155
	/**
156
	 * @return the url
157
	 */
158
	public String getUrl() {
159
		return url;
160
	}
161

  
162
	/**
163
	 * @param url the url to set
164
	 * @throws InitializeException
165
	 */
166
	public void setUrl(String url) throws InitializeException {
167
		if (this.getRefencesCount() > 0){
168
			throw new InitializeException("Resource in use",this.description());
169
		}
170
		this.url = url;
171
	}
172

  
173
	/**
174
	 * @return the user
175
	 */
176
	public String getUser() {
177
		return user;
178
	}
179

  
180
	/**
181
	 * @param user the user to set
182
	 * @throws InitializeException
183
	 */
184
	public void setUser(String user) throws InitializeException {
185
		if (this.getRefencesCount() > 0){
186
			throw new InitializeException("Resource in use",this.description());
187
		}
188
		this.user = user;
189
	}
190

  
191
	protected Connection getConnection() throws ReadException{
192
		this.checkOpen();
193
		return this.connection;
194
	}
195

  
196
	protected Connection getWriterConnection() throws ReadException{
197
		return this.createConnection();
198
	}
199

  
200
	/* (non-Javadoc)
201
	 * @see org.gvsig.data.Resource#checkChanged()
202
	 */
203
	protected void checkChanged() throws ResourceChangedException {
204
		// NO Operation
205

  
206
	}
207

  
208
}
209

  
0 210

  
trunk/libraries/libFMap_dataDB/src/org/gvsig/data/datastores/vectorial/db/jdbc/JDBCStore.java
1 1
package org.gvsig.data.datastores.vectorial.db.jdbc;
2 2

  
3

  
4
import java.security.KeyException;
3 5
import java.sql.Connection;
4 6
import java.sql.ResultSet;
5 7
import java.sql.Statement;
......
7 9
import java.util.Iterator;
8 10
import java.util.List;
9 11

  
12
import org.gvsig.data.IDataExplorer;
13
import org.gvsig.data.IDataStoreParameters;
14
import org.gvsig.data.Resource;
15
import org.gvsig.data.ResourceManager;
10 16
import org.gvsig.data.commands.implementation.AttributeCommand;
11 17
import org.gvsig.data.commands.implementation.FeatureCommand;
12 18
import org.gvsig.data.commands.implementation.UpdateAttributeCommand;
13 19
import org.gvsig.data.commands.implementation.UpdateFeatureCommand;
14
import org.gvsig.data.datastores.vectorial.ISelectiveWriter;
15 20
import org.gvsig.data.datastores.vectorial.db.DBFeatureID;
16 21
import org.gvsig.data.datastores.vectorial.db.DBFeatureType;
17
import org.gvsig.data.datastores.vectorial.db.jdbc.exception.SQLException;
18
import org.gvsig.data.datastores.vectorial.db.jdbc.h2.H2Utils;
22
import org.gvsig.data.exception.CloseException;
19 23
import org.gvsig.data.exception.DataException;
24
import org.gvsig.data.exception.InitializeException;
20 25
import org.gvsig.data.exception.InitializeWriterException;
26
import org.gvsig.data.exception.OpenException;
21 27
import org.gvsig.data.exception.ReadException;
22 28
import org.gvsig.data.exception.WriteException;
29
import org.gvsig.data.vectorial.CreatedFeature;
23 30
import org.gvsig.data.vectorial.FeatureStore;
24
import org.gvsig.data.vectorial.CreatedFeature;
25 31
import org.gvsig.data.vectorial.IFeature;
26 32
import org.gvsig.data.vectorial.IFeatureAttributeDescriptor;
27 33
import org.gvsig.data.vectorial.IFeatureID;
28
import org.gvsig.data.vectorial.IFeatureStore;
29 34
import org.gvsig.data.vectorial.IFeatureType;
30
import org.gvsig.data.vectorial.IsNotFeatureSettingException;
31 35
import org.gvsig.metadata.IMetadata;
32 36
import org.gvsig.metadata.IMetadataManager;
33 37
import org.gvsig.metadata.MetadataManager;
34 38

  
39

  
35 40
public abstract class JDBCStore extends FeatureStore {
36 41

  
37
	protected Connection connection = null;
42
	protected JDBCResource resource = null;
38 43
	protected String sqlSelectPart;
39 44
	protected String baseWhereClause = null;
40 45
	protected String baseOrder = null;
......
44 49
	protected IMetadata metadata;
45 50

  
46 51

  
47
	protected abstract Connection getConnection();
52
	/* (non-Javadoc)
53
	 * @see org.gvsig.data.vectorial.FeatureStore#doClose()
54
	 */
55
	protected void doClose() throws CloseException {
56
		this.resource.close();
48 57

  
58
	}
59

  
60

  
61
	/* (non-Javadoc)
62
	 * @see org.gvsig.data.vectorial.FeatureStore#doDispose()
63
	 */
64
	protected void doDispose() throws CloseException {
65
		ResourceManager resMan = ResourceManager.getResourceManager();
66

  
67
    	try {
68
			resMan.remove(this.resource);
69
		} catch (DataException e1) {
70
			throw new CloseException(this.getName(),e1);
71
		} catch (KeyException e) {
72
			// TODO Auto-generated catch block
73
			throw new CloseException(this.getName(),e);
74
		}
75
		this.featureType = null;
76
		this.metadata = null;
77
		super.doDispose();
78
	}
79

  
80
	/* (non-Javadoc)
81
	 * @see org.gvsig.data.vectorial.FeatureStore#doOpen()
82
	 */
83
	protected void doOpen() throws OpenException {
84
		//No Operation
85

  
86
	}
87

  
88

  
89
	/* (non-Javadoc)
90
	 * @see org.gvsig.data.vectorial.IFeatureStore#canAlterFeatureType()
91
	 */
92
	public boolean canAlterFeatureType() {
93
		return false;
94
	}
95

  
96
	/* (non-Javadoc)
97
	 * @see org.gvsig.data.IDataStore#getExplorer()
98
	 */
99
	public IDataExplorer getExplorer() throws ReadException {
100
		return null;
101
	}
102

  
103

  
104
	/* (non-Javadoc)
105
	 * @see org.gvsig.data.vectorial.FeatureStore#init(org.gvsig.data.IDataStoreParameters, org.gvsig.data.Resource)
106
	 */
107
	public void init(IDataStoreParameters parameters, Resource resource) throws InitializeException {
108
		super.init(parameters, resource);
109
		this.resource=(JDBCResource)resource;
110
	}
111

  
112
	protected Connection getConnection() throws ReadException{
113
		return this.resource.getConnection();
114
	}
115

  
116
	protected Connection getWriterConnection() throws ReadException{
117
		return this.resource.getWriterConnection();
118
	}
119

  
49 120
	public boolean isEditable() {
50 121
		return !this.useSqlSource;
51 122
	}
......
205 276
	}
206 277

  
207 278
	public boolean isWithDefaultLegend() {
208
		// TODO Auto-generated method stub
209 279
		return false;
210 280
	}
211 281

  
212 282
	public Object getDefaultLabelingStrategy() {
213
		// TODO Auto-generated method stub
214 283
		return null;
215 284
	}
216 285

  
217 286
	public Object getDefaultLegend() {
218
		// TODO Auto-generated method stub
219 287
		return null;
220 288
	}
221 289

  
......
242 310
				try {
243 311
					rs.close();
244 312
				} catch (java.sql.SQLException e) {
245
					// TODO ?????
246
					e.printStackTrace();
313
					throw new ReadException(this.getName(),e);
247 314
				}
248 315
			}
249 316
		}

Also available in: Unified diff