Revision 38002

View differences:

tags/v2_0_0_Build_2045/libraries/libFMap_dalfile/src-test/org/gvsig/fmap/dal/store/dgn/TestDGN.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. S.A.   {{Task}}
26
*/
27

  
28
package org.gvsig.fmap.dal.store.dgn;
29

  
30
import java.io.File;
31

  
32
import org.gvsig.fmap.dal.DataStoreParameters;
33
import org.gvsig.fmap.dal.exception.DataException;
34
import org.gvsig.fmap.dal.feature.BaseTestFeatureStore;
35
import org.gvsig.fmap.dal.feature.FeatureStore;
36

  
37
public class TestDGN extends BaseTestFeatureStore {
38

  
39
	public static final File file_prueba = new File(TestDGN.class.getResource(
40
			"data/prueba.dgn").getFile());
41
	
42
	/*
43
	 * (non-Javadoc)
44
	 *
45
	 * @see
46
	 * org.gvsig.fmap.dal.feature.BaseTestFeatureStore#getDefaultDataStoreParameters
47
	 * ()
48
	 */
49
	public DataStoreParameters getDefaultDataStoreParameters()
50
			throws DataException {
51
		DGNStoreParameters dgnParameters = null;
52

  
53
		dgnParameters = (DGNStoreParameters) dataManager
54
				.createStoreParameters(DGNStoreProvider.NAME);
55

  
56
		dgnParameters.setFile(file_prueba.getAbsolutePath());
57
		dgnParameters.setCRS("EPSG:23030");
58
		return dgnParameters;
59
	}
60

  
61
	/*
62
	 * (non-Javadoc)
63
	 *
64
	 * @see org.gvsig.fmap.dal.feature.BaseTestFeatureStore#hasExplorer()
65
	 */
66
	public boolean hasExplorer() {
67
		// TODO Auto-generated method stub
68
		return false;
69
	}
70

  
71
	public boolean usesResources() {
72
		return true;
73
	}
74

  
75

  
76
	public void testLegendAndLabeling() throws Exception {
77
		FeatureStore store = (FeatureStore) dataManager
78
				.createStore(getDefaultDataStoreParameters());
79

  
80
		assertNotNull(store.invokeDynMethod("getLegend", null));
81
		assertNotNull(store.invokeDynMethod("getLabeling", null));
82
		store.dispose();
83
	}
84
}
0 85

  
tags/v2_0_0_Build_2045/libraries/libFMap_dalfile/src-test/org/gvsig/fmap/dal/store/shp/Bug15597Test.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
package org.gvsig.fmap.dal.store.shp;
23

  
24
import java.io.File;
25

  
26
import junit.framework.TestCase;
27

  
28
import org.gvsig.fmap.dal.DALLocator;
29
import org.gvsig.fmap.dal.DataManager;
30
import org.gvsig.fmap.dal.DataTypes;
31
import org.gvsig.fmap.dal.feature.EditableFeature;
32
import org.gvsig.fmap.dal.feature.EditableFeatureType;
33
import org.gvsig.fmap.dal.feature.Feature;
34
import org.gvsig.fmap.dal.feature.FeatureSet;
35
import org.gvsig.fmap.dal.feature.FeatureStore;
36
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters;
37
import org.gvsig.fmap.dal.feature.impl.DefaultEditableFeatureType;
38
import org.gvsig.fmap.geom.Geometry;
39
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
40
import org.gvsig.fmap.geom.GeometryLocator;
41
import org.gvsig.fmap.geom.GeometryManager;
42
import org.gvsig.tools.dispose.DisposableIterator;
43
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer;
44

  
45
/**
46
 * Code to test bug gvsig-desktop#15597. This is based on the code provided by
47
 * the bug reporter (see ticket), thanks to him!!
48
 * 
49
 * @author gvSIG Team
50
 * @version $Id$
51
 */
52
public class Bug15597Test extends TestCase {
53
    private static final String TEMP_PATH = System.getProperty("java.io.tmpdir") + File.separator;
54
    
55
    public void testBug15642() throws Exception {
56
        new DefaultLibrariesInitializer().fullInitialize();
57
        DataManager manager = DALLocator.getDataManager();
58

  
59
        //Create a feature type with two fields: "geom" and "field1"
60
        EditableFeatureType type = new DefaultEditableFeatureType();
61
        type.add("geom", org.gvsig.fmap.geom.DataTypes.GEOMETRY)
62
            .setGeometryType(Geometry.TYPES.POINT)
63
            .setGeometrySubType(Geometry.SUBTYPES.GEOM2D);
64
        type.setDefaultGeometryAttributeName("geom");
65
        type.add("field1", DataTypes.STRING).setSize(2);
66
       
67
        //Create a store
68
        NewFeatureStoreParameters destParams =
69
            (NewFeatureStoreParameters) manager.createNewStoreParameters(
70
                "FilesystemExplorer", "Shape");
71
        destParams.setDynValue("shpfile", TEMP_PATH + "mySHP.shp");      
72
        destParams.setDynValue("crs", "EPSG:23030");
73
        destParams.setDefaultFeatureType(type);
74

  
75
        manager.newStore("FilesystemExplorer", "Shape", destParams, true);
76
        FeatureStore store =
77
            (FeatureStore) manager.openStore("Shape", destParams);
78

  
79
                
80
        //Edit the store and add a new Feature.
81
        store.edit();
82
             
83
        GeometryManager geometryManager = GeometryLocator.getGeometryManager();
84
        
85
        EditableFeature feature1 = store.createNewFeature().getEditable();
86
        feature1.setGeometry(type.getDefaultGeometryAttributeIndex(), 
87
            geometryManager.createPoint(0, 0, SUBTYPES.GEOM2D));
88
        feature1.set("field1", "Hi");                     
89
      
90
        store.insert(feature1);
91
        
92
        //Finish the edition
93
        store.finishEditing();
94

  
95
        //Edit the feature type and add a new field: "field2"
96
        store.edit();
97
       
98
        EditableFeatureType type2 = 
99
            store.getDefaultFeatureType().getEditable();        
100
        type2.add("field2", DataTypes.STRING).setSize(10);
101
        store.update(type2);    
102
        
103
        assertEquals(store.getDefaultFeatureType().getAttributeDescriptors().length, 3);
104
        
105
        //Add a second feature
106
        EditableFeature feature2 = store.createNewFeature().getEditable();
107
        feature2.setGeometry(type2.getDefaultGeometryAttributeIndex(),
108
            geometryManager.createPoint(1, 1, SUBTYPES.GEOM2D));
109
        feature2.set("field1", "Hi");    
110
        feature2.set("field2", "Max");   
111
       
112
        store.insert(feature2);
113
        
114
        //The edition is not finished. Check if all the features have the two fields
115
        FeatureSet featureSet = store.getFeatureSet();
116
        DisposableIterator it = featureSet.fastIterator();
117
        
118
        it.hasNext();
119
        Feature feature = (Feature)it.next();   
120
        assertNotNull(feature.getDefaultGeometry());
121
        assertEquals("Hi", feature.get("field1"));
122
        assertNull(feature.get("field2"));
123
        
124
        it.hasNext();
125
        feature = (Feature)it.next();   
126
        assertNotNull(feature.getDefaultGeometry());
127
        assertEquals("Hi", feature.get("field1"));
128
        assertEquals("Max", feature.get("field2"));        
129
        
130
        it.dispose();
131
        store.finishEditing();
132
        
133
        //Edit the feature type and remove the field: "field1"
134
        store.edit();
135
        
136
        EditableFeatureType type3 = 
137
            store.getDefaultFeatureType().getEditable();        
138
        type3.remove("field1");
139
        store.update(type3);    
140
        
141
        assertEquals(store.getDefaultFeatureType().getAttributeDescriptors().length, 2);
142
                
143
        //Finish the edition
144
        store.finishEditing();
145
        
146
        //Check if all the features have one field
147
        featureSet = store.getFeatureSet();
148
        it = featureSet.fastIterator();        
149
        
150
        it.hasNext();
151
        feature = (Feature)it.next();   
152
        assertNotNull(feature.getDefaultGeometry());       
153
        assertEquals("", feature.get("field2"));
154
        
155
        it.hasNext();
156
        feature = (Feature)it.next();   
157
        assertNotNull(feature.getDefaultGeometry());
158
        assertEquals("Max", feature.get("field2"));     
159
        
160
        it.dispose();
161
        store.dispose();
162
    }
163
}
0 164

  
tags/v2_0_0_Build_2045/libraries/libFMap_dalfile/src-test/org/gvsig/fmap/dal/store/shp/TestSHP.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. S.A.   {{Task}}
26
*/
27

  
28
package org.gvsig.fmap.dal.store.shp;
29

  
30
import java.io.File;
31
import java.util.Iterator;
32

  
33
import org.gvsig.fmap.dal.DataStoreParameters;
34
import org.gvsig.fmap.dal.exception.DataException;
35
import org.gvsig.fmap.dal.feature.BaseTestEditableFeatureStore;
36
import org.gvsig.tools.dispose.DisposableIterator;
37
import org.gvsig.fmap.dal.feature.EditableFeature;
38
import org.gvsig.fmap.dal.feature.Feature;
39
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
40
import org.gvsig.fmap.dal.feature.FeatureSet;
41
import org.gvsig.fmap.dal.feature.FeatureStore;
42
import org.gvsig.fmap.dal.feature.FeatureType;
43
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters;
44
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer;
45
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorerParameters;
46
import org.gvsig.fmap.dal.store.dbf.DBFStoreParameters;
47
import org.gvsig.fmap.dal.store.dbf.DBFStoreProvider;
48

  
49
public class TestSHP extends BaseTestEditableFeatureStore {
50

  
51
	private FilesystemServerExplorer myExplorer;
52

  
53
	public static final File file_prueba = new File(TestSHP.class.getResource(
54
			"data/prueba.shp").getFile());
55
	public static final File file_prueba_destino = new File(file_prueba
56
			.getParent()
57
			+ File.separatorChar + "pruebaTemp.shp");
58
	public static final File file_pruebaNull = new File(TestSHP.class
59
			.getResource("data/pruebaNull.shp").getFile());
60

  
61
	public static final File file_poly_valencia = new File(TestSHP.class
62
			.getResource("data/poly-valencia.shp").getFile());
63

  
64
	public void testSimpleIteration() throws Exception {
65

  
66
		FeatureStore store = null;
67
		SHPStoreParameters shpParameters = null;
68

  
69
		shpParameters = (SHPStoreParameters) dataManager
70
				.createStoreParameters(SHPStoreProvider.NAME);
71

  
72
		// shpParameters.setFile(file_poly_valencia);
73
		shpParameters.setFile(file_prueba);
74

  
75
		shpParameters.setCRS("EPSG:23030");
76

  
77
		store = (FeatureStore) dataManager.createStore(shpParameters);
78
		FeatureSet set;
79
		FeatureType type = store.getDefaultFeatureType();
80
		set = store.getFeatureSet();
81

  
82
		System.out.println("Num:" + set.getSize());
83
		DisposableIterator it = set.iterator();
84
		Iterator ftIt;
85
		FeatureAttributeDescriptor desc;
86

  
87
		int i = 0;
88
		Feature feature;
89
		while (it.hasNext()) {
90
			ftIt = type.iterator();
91

  
92
			feature = (Feature) it.next();
93
			// while (ftIt.hasNext()) {
94
			// desc = (FeatureAttributeDescriptor) ftIt.next();
95
			// System.out.println(desc.getName() + ":"
96
			// + feature.get(desc.getIndex()));
97
			//
98
			// }
99
			// System.out.println(feature.get("NOMBRE"));
100
			System.out.print(feature.getDefaultEnvelope() + "\t");
101
			// System.out.print(feature.getDefaultGeometry() + "\t");
102
			// System.out.println(feature.get("NOMBRE"));
103
			System.out.println("row:" + i);
104
			i++;
105

  
106

  
107
		}
108

  
109
		it.dispose();
110
		set.dispose();
111

  
112
		store.dispose();
113

  
114
	}
115

  
116
	public void testEditing(Object x) throws Exception {
117
		FeatureStore store = null;
118
		SHPStoreParameters shpParameters = null;
119

  
120
		shpParameters = (SHPStoreParameters) dataManager
121
				.createStoreParameters(SHPStoreProvider.NAME);
122

  
123
		shpParameters.setFile(file_poly_valencia);
124

  
125
		store = (FeatureStore) dataManager.createStore(shpParameters);
126
		FeatureSet set;
127
		FeatureType type = store.getDefaultFeatureType();
128
		System.err.println("Antes de la edici?n");
129
		set = store.getFeatureSet();
130
		System.out.println("Num:" + set.getSize());
131
		DisposableIterator it = set.iterator();
132
		Iterator ftIt;
133
		FeatureAttributeDescriptor desc;
134

  
135
		int i = 0;
136
		Feature feature;
137
		while (it.hasNext()) {
138
			ftIt = type.iterator();
139

  
140
			feature = (Feature) it.next();
141
			// while (ftIt.hasNext()) {
142
			// desc = (FeatureAttributeDescriptor) ftIt.next();
143
			// System.out.println(desc.getName() + ":"
144
			// + feature.get(desc.getIndex()));
145
			//
146
			// }
147
			// System.out.println(feature.get("NOMBRE"));
148
			System.out.print(feature.getDefaultEnvelope() + "\t");
149
			// System.out.print(feature.getDefaultGeometry() + "\t");
150
			// System.out.println(feature.get("NOMBRE"));
151
			System.out.println("row:" + i);
152
			i++;
153

  
154

  
155
		}
156

  
157
		it.dispose();
158
		set.dispose();
159
		store.edit();
160
		EditableFeature ef = store.createNewFeature();
161
		store.insert(ef);
162
		store.finishEditing();
163
		System.err.println("Despu?s de la edici?n");
164
		set = store.getFeatureSet();
165
		System.out.println("Num:" + set.getSize());
166
		it = set.iterator();
167

  
168
		i = 0;
169
		while (it.hasNext()) {
170
			ftIt = type.iterator();
171

  
172
			feature = (Feature) it.next();
173
			// while (ftIt.hasNext()) {
174
			// desc = (FeatureAttributeDescriptor) ftIt.next();
175
			// System.out.println(desc.getName() + ":"
176
			// + feature.get(desc.getIndex()));
177
			//
178
			// }
179
			// System.out.println(feature.get("NOMBRE"));
180
			System.out.print(feature.getDefaultEnvelope() + "\t");
181
			// System.out.print(feature.getDefaultGeometry() + "\t");
182
			// System.out.println(feature.get("NOMBRE"));
183
			System.out.println("row:" + i);
184
			i++;
185

  
186

  
187
		}
188

  
189
		it.dispose();
190
		set.dispose();
191

  
192
		store.dispose();
193

  
194
	}
195

  
196
	public void testExport(Object x) throws Exception {
197
		DBFStoreParameters dbfParameters = null;
198

  
199
		dbfParameters = (DBFStoreParameters) dataManager
200
				.createStoreParameters(DBFStoreProvider.NAME);
201

  
202
		dbfParameters.setFile(file_prueba);
203

  
204
		FeatureStore store = (FeatureStore) dataManager
205
				.createStore(dbfParameters);
206
		FilesystemServerExplorerParameters explorerParams = (FilesystemServerExplorerParameters) dataManager
207
				.createServerExplorerParameters(FilesystemServerExplorerParameters.DYNCLASS_NAME);
208
		explorerParams.setRoot(file_prueba.getParent());
209

  
210
		FilesystemServerExplorer explorer = (FilesystemServerExplorer) dataManager
211
				.createServerExplorer(explorerParams);
212

  
213
		NewFeatureStoreParameters newParams = (NewFeatureStoreParameters) explorer
214
				.getAddParameters(file_prueba_destino);
215

  
216
		store.export(explorer, DBFStoreProvider.NAME, newParams);
217

  
218
		FeatureStore result = (FeatureStore) dataManager.createStore(newParams);
219

  
220
		FeatureSet set = result.getFeatureSet();
221
		FeatureSet originalSet = store.getFeatureSet();
222
		assertEquals(set.getSize(), originalSet.getSize());
223

  
224
		DisposableIterator originalIter = originalSet.iterator();
225
		DisposableIterator iter = set.iterator();
226
		while (iter.hasNext()) {
227
			assertEquals(originalIter.next(), iter.next());
228
		}
229

  
230
		originalIter.dispose();
231
		iter.dispose();
232
		result.dispose();
233
		explorer.remove(newParams);
234

  
235
	}
236

  
237
	/*
238
	 * (non-Javadoc)
239
	 *
240
	 * @see
241
	 * org.gvsig.fmap.dal.feature.BaseTestFeatureStore#getDefaultDataStoreParameters
242
	 * ()
243
	 */
244
	public DataStoreParameters getDefaultDataStoreParameters()
245
			throws DataException {
246
		SHPStoreParameters shpParameters = (SHPStoreParameters) dataManager
247
				.createStoreParameters(SHPStoreProvider.NAME);
248

  
249
		//			shpParameters.setFile(file_poly_valencia);
250
		shpParameters.setFile(file_prueba);
251
		shpParameters.setCRS("EPSG:23030");
252
		return shpParameters;
253
	}
254

  
255
	/*
256
	 * (non-Javadoc)
257
	 *
258
	 * @see org.gvsig.fmap.dal.feature.BaseTestFeatureStore#hasExplorer()
259
	 */
260
	public boolean hasExplorer() {
261
		return true;
262
	}
263

  
264
	/*
265
	 * (non-Javadoc)
266
	 *
267
	 * @seeorg.gvsig.fmap.dal.feature.BaseTestEditableFeatureStore#
268
	 * getDefaultNewDataStoreParameters()
269
	 */
270
	public NewFeatureStoreParameters getDefaultNewDataStoreParameters()
271
			throws Exception {
272
		SHPStoreParameters parameters = (SHPStoreParameters) this
273
				.getDefaultDataStoreParameters();
274

  
275
		if (this.myExplorer == null) {
276
			FeatureStore store = (FeatureStore) dataManager
277
					.createStore(parameters);
278
			myExplorer = (FilesystemServerExplorer) store.getExplorer();
279
			store.dispose();
280
		}
281

  
282
		SHPNewStoreParameters result = (SHPNewStoreParameters) myExplorer
283
				.getAddParameters(file_prueba_destino);
284

  
285
		result.setCRS(parameters.getCRS());
286
		return result;
287
	}
288

  
289
	public boolean usesResources() {
290
		return true;
291
	}
292

  
293
	public boolean resourcesNotifyChanges() {
294
		return true;
295
	}
296

  
297
}
0 298

  
tags/v2_0_0_Build_2045/libraries/libFMap_dalfile/src-test/org/gvsig/fmap/dal/store/shp/Bug15642Test.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
package org.gvsig.fmap.dal.store.shp;
23

  
24
import junit.framework.TestCase;
25

  
26
import com.vividsolutions.jts.geom.Coordinate;
27
import com.vividsolutions.jts.geom.GeometryFactory;
28

  
29
import org.gvsig.fmap.dal.DALLocator;
30
import org.gvsig.fmap.dal.DataManager;
31
import org.gvsig.fmap.dal.DataTypes;
32
import org.gvsig.fmap.dal.feature.EditableFeature;
33
import org.gvsig.fmap.dal.feature.EditableFeatureType;
34
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
35
import org.gvsig.fmap.dal.feature.FeatureStore;
36
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters;
37
import org.gvsig.fmap.dal.feature.impl.DefaultEditableFeatureType;
38
import org.gvsig.fmap.geom.Geometry;
39
import org.gvsig.fmap.geom.GeometryLocator;
40
import org.gvsig.fmap.geom.GeometryManager;
41
import org.gvsig.fmap.geom.operation.GeometryOperation;
42
import org.gvsig.fmap.geom.operation.GeometryOperationContext;
43
import org.gvsig.fmap.geom.operation.fromjts.FromJTS;
44
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer;
45

  
46
/**
47
 * Code to test bug gvsig-desktop#15642. This is based on the code provided by
48
 * the bug reporter (see ticket), thanks to him!!
49
 * 
50
 * @author gvSIG Team
51
 * @version $Id$
52
 */
53
public class Bug15642Test extends TestCase {
54

  
55
    public void testBug15642() throws Exception {
56
        new DefaultLibrariesInitializer().fullInitialize();
57
        DataManager manager = DALLocator.getDataManager();
58

  
59
        EditableFeatureType type = new DefaultEditableFeatureType();
60
        type.add("geom", org.gvsig.fmap.geom.DataTypes.GEOMETRY)
61
            .setGeometryType(Geometry.TYPES.POINT)
62
            .setGeometrySubType(Geometry.SUBTYPES.GEOM2D);
63
        type.setDefaultGeometryAttributeName("geom");
64
        type.add("float", DataTypes.FLOAT).setSize(5);
65
        type.add("long", DataTypes.LONG).setSize(5);
66

  
67
        NewFeatureStoreParameters destParams =
68
            (NewFeatureStoreParameters) manager.createNewStoreParameters(
69
                "FilesystemExplorer", "Shape");
70
        destParams.setDynValue("shpfile", "/tmp/mySHP.shp");
71
        destParams.setDynValue("dbffile", "/tmp/mySHP.dbf");
72
        destParams.setDynValue("shxfile", "/tmp/mySHP.shx");
73
        destParams.setDynValue("crs", "EPSG:23030");
74
        destParams.setDefaultFeatureType(type);
75

  
76
        manager.newStore("FilesystemExplorer", "Shape", destParams, true);
77
        FeatureStore store =
78
            (FeatureStore) manager.openStore("Shape", destParams);
79

  
80
        store.edit();
81
        EditableFeature feature = store.createNewFeature().getEditable();
82
        com.vividsolutions.jts.geom.Geometry g =
83
            new GeometryFactory().createPoint(new Coordinate(0, 0));
84

  
85
        GeometryManager geometryManager = GeometryLocator.getGeometryManager();
86
        int opCode = geometryManager.getGeometryOperationCode(FromJTS.NAME);
87
        GeometryOperation converter =
88
            geometryManager.getGeometryOperation(opCode, Geometry.TYPES.POINT,
89
                Geometry.SUBTYPES.GEOM2D);
90
        GeometryOperationContext ctx = new GeometryOperationContext();
91
        ctx.setAttribute(FromJTS.PARAM, g);
92
        Geometry fmapGeom = (Geometry) converter.invoke(null, ctx);
93
        feature.setGeometry(feature.getType()
94
            .getDefaultGeometryAttributeIndex(), fmapGeom);
95
        feature.setFloat("float", 34.0f);
96
        feature.setLong("long", 34l);
97
        store.insert(feature);
98
        store.finishEditing();
99

  
100
        FeatureAttributeDescriptor defaultGeometryAttribute =
101
            store.getDefaultFeatureType().getDefaultGeometryAttribute();
102

  
103
        assertEquals(Geometry.TYPES.POINT,
104
            defaultGeometryAttribute.getGeometryType());
105
        assertEquals(Geometry.SUBTYPES.GEOM2D,
106
            defaultGeometryAttribute.getGeometrySubType());
107

  
108
        store.dispose();
109
    }
110

  
111
}
tags/v2_0_0_Build_2045/libraries/libFMap_dalfile/src-test/org/gvsig/fmap/dal/store/shp/Bug15643Test.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
package org.gvsig.fmap.dal.store.shp;
23

  
24
import junit.framework.TestCase;
25

  
26
import com.vividsolutions.jts.geom.Coordinate;
27
import com.vividsolutions.jts.geom.GeometryFactory;
28

  
29
import org.gvsig.fmap.dal.DALLocator;
30
import org.gvsig.fmap.dal.DataManager;
31
import org.gvsig.fmap.dal.DataTypes;
32
import org.gvsig.fmap.dal.feature.EditableFeature;
33
import org.gvsig.fmap.dal.feature.EditableFeatureType;
34
import org.gvsig.fmap.dal.feature.Feature;
35
import org.gvsig.fmap.dal.feature.FeatureStore;
36
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters;
37
import org.gvsig.fmap.dal.feature.impl.DefaultEditableFeatureType;
38
import org.gvsig.fmap.geom.Geometry;
39
import org.gvsig.fmap.geom.GeometryLocator;
40
import org.gvsig.fmap.geom.GeometryManager;
41
import org.gvsig.fmap.geom.operation.GeometryOperation;
42
import org.gvsig.fmap.geom.operation.GeometryOperationContext;
43
import org.gvsig.fmap.geom.operation.fromjts.FromJTS;
44
import org.gvsig.tools.exception.BaseException;
45
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer;
46
import org.gvsig.tools.visitor.VisitCanceledException;
47
import org.gvsig.tools.visitor.Visitor;
48

  
49
/**
50
 * Code to test bug gvsig-desktop#15643. This is based on the code provided by
51
 * the bug reporter (see ticket), thanks to him!!
52
 * 
53
 * @author gvSIG Team
54
 * @version $Id$
55
 */
56
public class Bug15643Test extends TestCase {
57

  
58
    public void testBug15643() throws Exception {
59
        new DefaultLibrariesInitializer().fullInitialize();
60
        DataManager manager = DALLocator.getDataManager();
61

  
62
        EditableFeatureType type = new DefaultEditableFeatureType();
63
        type.add("geom", org.gvsig.fmap.geom.DataTypes.GEOMETRY)
64
            .setGeometryType(Geometry.TYPES.POINT)
65
            .setGeometrySubType(Geometry.SUBTYPES.GEOM2D);
66
        type.setDefaultGeometryAttributeName("geom");
67
        type.add("float", DataTypes.FLOAT).setSize(5);
68
        type.add("double", DataTypes.DOUBLE).setSize(5);
69
        type.add("int", DataTypes.INT).setSize(5);
70
        type.add("long", DataTypes.LONG).setSize(5);
71

  
72
        NewFeatureStoreParameters destParams =
73
            (NewFeatureStoreParameters) manager.createNewStoreParameters(
74
                "FilesystemExplorer", "Shape");
75
        destParams.setDynValue("shpfile", "/tmp/mySHP.shp");
76
        destParams.setDynValue("dbffile", "/tmp/mySHP.dbf");
77
        destParams.setDynValue("shxfile", "/tmp/mySHP.shx");
78
        destParams.setDynValue("crs", "EPSG:23030");
79
        destParams.setDefaultFeatureType(type);
80

  
81
        manager.newStore("FilesystemExplorer", "Shape", destParams, true);
82
        FeatureStore store =
83
            (FeatureStore) manager.openStore("Shape", destParams);
84

  
85
        store.edit();
86
        EditableFeature feature = store.createNewFeature().getEditable();
87
        com.vividsolutions.jts.geom.Geometry g =
88
            new GeometryFactory().createPoint(new Coordinate(0, 0));
89

  
90
        GeometryManager geometryManager = GeometryLocator.getGeometryManager();
91
        int opCode = geometryManager.getGeometryOperationCode(FromJTS.NAME);
92
        GeometryOperation converter =
93
            geometryManager.getGeometryOperation(opCode, Geometry.TYPES.POINT,
94
                Geometry.SUBTYPES.GEOM2D);
95
        GeometryOperationContext ctx = new GeometryOperationContext();
96
        ctx.setAttribute(FromJTS.PARAM, g);
97
        Geometry fmapGeom = (Geometry) converter.invoke(null, ctx);
98
        feature.setGeometry(feature.getType()
99
            .getDefaultGeometryAttributeIndex(), fmapGeom);
100
        feature.setFloat("float", 34.0f);
101
        feature.setDouble("double", 34.0d);
102
        feature.setInt("int", 34);
103
        feature.setLong("long", 34l);
104
        store.insert(feature);
105
        store.finishEditing();
106

  
107
        store.accept(new Visitor() {
108

  
109
            public void visit(Object obj) throws VisitCanceledException,
110
                BaseException {
111
                Feature feature = (Feature) obj;
112
                assertEquals(34.0f, feature.getFloat("float"));
113
                assertEquals(34.0d, feature.getDouble("double"));
114
                assertEquals(34, feature.getInt("int"));
115
                assertEquals(34l, feature.getLong("long"));
116
            }
117
        });
118

  
119
        store.dispose();
120
    }
121

  
122
}
tags/v2_0_0_Build_2045/libraries/libFMap_dalfile/src-test/org/gvsig/fmap/dal/store/shp/Bug15670Test.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
package org.gvsig.fmap.dal.store.shp;
23

  
24
import junit.framework.TestCase;
25

  
26
import org.gvsig.fmap.dal.DALLocator;
27
import org.gvsig.fmap.dal.DataManager;
28
import org.gvsig.fmap.dal.DataTypes;
29
import org.gvsig.fmap.dal.feature.EditableFeature;
30
import org.gvsig.fmap.dal.feature.EditableFeatureType;
31
import org.gvsig.fmap.dal.feature.Feature;
32
import org.gvsig.fmap.dal.feature.FeatureStore;
33
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters;
34
import org.gvsig.fmap.dal.feature.impl.DefaultEditableFeatureType;
35
import org.gvsig.fmap.geom.Geometry;
36
import org.gvsig.fmap.geom.GeometryLocator;
37
import org.gvsig.fmap.geom.primitive.Point;
38
import org.gvsig.tools.exception.BaseException;
39
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer;
40
import org.gvsig.tools.visitor.VisitCanceledException;
41
import org.gvsig.tools.visitor.Visitor;
42

  
43
/**
44
 * Code to test bug gvsig-desktop#15670. This is based on the code provided by
45
 * the bug reporter (see ticket), thanks to him!!
46
 * 
47
 * @author gvSIG Team
48
 * @version $Id$
49
 */
50
public class Bug15670Test extends TestCase {
51

  
52
    public void testBug15670() throws Exception {
53
        new DefaultLibrariesInitializer().fullInitialize();
54
        DataManager manager = DALLocator.getDataManager();
55

  
56
        EditableFeatureType type = new DefaultEditableFeatureType();
57
        type.add("GEOMETRY", DataTypes.GEOMETRY)
58
            .setGeometryType(Geometry.TYPES.POINT)
59
            .setGeometrySubType(Geometry.SUBTYPES.GEOM2D);
60
        type.setDefaultGeometryAttributeName("GEOMETRY");
61

  
62
        NewFeatureStoreParameters destParams =
63
            (NewFeatureStoreParameters) manager.createNewStoreParameters(
64
                "FilesystemExplorer", "Shape");
65
        destParams.setDynValue("shpfile", "/tmp/mySHP.shp");
66
        destParams.setDynValue("dbffile", "/tmp/mySHP.dbf");
67
        destParams.setDynValue("shxfile", "/tmp/mySHP.shx");
68
        destParams.setDynValue("crs", "EPSG:23030");
69
        destParams.setDefaultFeatureType(type);
70

  
71
        manager.newStore("FilesystemExplorer", "Shape", destParams, true);
72
        FeatureStore store =
73
            (FeatureStore) manager.openStore("Shape", destParams);
74

  
75
        store.edit();
76
        EditableFeature feature = store.createNewFeature().getEditable();
77
        final Point geom =
78
            GeometryLocator.getGeometryManager().createPoint(10.0, 10.0,
79
                Geometry.SUBTYPES.GEOM2D);
80
        feature.setGeometry("GEOMETRY", geom);
81
        store.insert(feature);
82
        store.finishEditing();
83

  
84
        store.accept(new Visitor() {
85

  
86
            public void visit(Object obj) throws VisitCanceledException,
87
                BaseException {
88
                Feature feature = (Feature) obj;
89
                assertEquals(geom, feature.getGeometry("GEOMETRY"));
90
            }
91
        });
92
        store.dispose();
93
    }
94
}
tags/v2_0_0_Build_2045/libraries/libFMap_dalfile/src-test/org/gvsig/fmap/dal/store/shp/Bug15617Test.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
package org.gvsig.fmap.dal.store.shp;
23

  
24
import junit.framework.TestCase;
25

  
26
import com.vividsolutions.jts.geom.Coordinate;
27
import com.vividsolutions.jts.geom.GeometryFactory;
28

  
29
import org.gvsig.fmap.dal.DALLocator;
30
import org.gvsig.fmap.dal.DataManager;
31
import org.gvsig.fmap.dal.DataTypes;
32
import org.gvsig.fmap.dal.feature.EditableFeature;
33
import org.gvsig.fmap.dal.feature.EditableFeatureType;
34
import org.gvsig.fmap.dal.feature.Feature;
35
import org.gvsig.fmap.dal.feature.FeatureStore;
36
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters;
37
import org.gvsig.fmap.dal.feature.impl.DefaultEditableFeatureType;
38
import org.gvsig.fmap.geom.Geometry;
39
import org.gvsig.fmap.geom.GeometryLocator;
40
import org.gvsig.fmap.geom.GeometryManager;
41
import org.gvsig.fmap.geom.operation.GeometryOperation;
42
import org.gvsig.fmap.geom.operation.GeometryOperationContext;
43
import org.gvsig.fmap.geom.operation.fromjts.FromJTS;
44
import org.gvsig.tools.exception.BaseException;
45
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer;
46
import org.gvsig.tools.visitor.VisitCanceledException;
47
import org.gvsig.tools.visitor.Visitor;
48

  
49
/**
50
 * Code to test bug gvsig-desktop#15617.
51
 * 
52
 * @author gvSIG Team
53
 * @version $Id$
54
 */
55
public class Bug15617Test extends TestCase {
56

  
57
    public void testBug15617() throws Exception {
58
        new DefaultLibrariesInitializer().fullInitialize();
59
        DataManager manager = DALLocator.getDataManager();
60

  
61
        EditableFeatureType type = new DefaultEditableFeatureType();
62
        type.add("geom", org.gvsig.fmap.geom.DataTypes.GEOMETRY)
63
            .setGeometryType(Geometry.TYPES.POINT)
64
            .setGeometrySubType(Geometry.SUBTYPES.GEOM2D);
65
        type.setDefaultGeometryAttributeName("geom");
66
//        type.add("float", DataTypes.FLOAT).setSize(5);
67
//        type.add("double", DataTypes.DOUBLE).setSize(5);
68
        type.add("int", DataTypes.INT).setSize(5);
69
//        type.add("long", DataTypes.LONG).setSize(5);
70
        type.add("bool", DataTypes.BOOLEAN);
71

  
72
        NewFeatureStoreParameters destParams =
73
            (NewFeatureStoreParameters) manager.createNewStoreParameters(
74
                "FilesystemExplorer", "Shape");
75
        destParams.setDynValue("shpfile", "/tmp/mySHP.shp");
76
        destParams.setDynValue("dbffile", "/tmp/mySHP.dbf");
77
        destParams.setDynValue("shxfile", "/tmp/mySHP.shx");
78
        destParams.setDynValue("crs", "EPSG:23030");
79
        destParams.setDefaultFeatureType(type);
80

  
81
        manager.newStore("FilesystemExplorer", "Shape", destParams, true);
82
        FeatureStore store =
83
            (FeatureStore) manager.openStore("Shape", destParams);
84

  
85
        store.edit();
86
        EditableFeature feature = store.createNewFeature().getEditable();
87
        com.vividsolutions.jts.geom.Geometry g =
88
            new GeometryFactory().createPoint(new Coordinate(0, 0));
89

  
90
        GeometryManager geometryManager = GeometryLocator.getGeometryManager();
91
        int opCode = geometryManager.getGeometryOperationCode(FromJTS.NAME);
92
        GeometryOperation converter =
93
            geometryManager.getGeometryOperation(opCode, Geometry.TYPES.POINT,
94
                Geometry.SUBTYPES.GEOM2D);
95
        GeometryOperationContext ctx = new GeometryOperationContext();
96
        ctx.setAttribute(FromJTS.PARAM, g);
97
        Geometry fmapGeom = (Geometry) converter.invoke(null, ctx);
98
        feature.setGeometry(feature.getType()
99
            .getDefaultGeometryAttributeIndex(), fmapGeom);
100
//        feature.setFloat("float", 34.0f);
101
//        feature.setDouble("double", 34.0d);
102
        feature.setInt("int", 25);
103
//        feature.setLong("long", 34l);
104
        feature.setBoolean("bool", true);
105
        assertEquals(true, feature.getBoolean("bool"));
106
        store.insert(feature);
107
        store.finishEditing();
108

  
109
        store.accept(new Visitor() {
110

  
111
            public void visit(Object obj) throws VisitCanceledException,
112
                BaseException {
113
                Feature feature = (Feature) obj;
114
//                assertEquals(34.0f, feature.getFloat("float"));
115
//                assertEquals(34.0d, feature.getDouble("double"));
116
                assertEquals(25, feature.getInt("int"));
117
//                assertEquals(34l, feature.getLong("long"));
118
                assertEquals(true, feature.getBoolean("bool"));
119
            }
120
        });
121

  
122
        store.dispose();
123
    }
124

  
125
}
tags/v2_0_0_Build_2045/libraries/libFMap_dalfile/src-test/org/gvsig/fmap/dal/store/shp/Bug15671Test.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
package org.gvsig.fmap.dal.store.shp;
23

  
24
import junit.framework.TestCase;
25

  
26
import org.gvsig.fmap.dal.DALLocator;
27
import org.gvsig.fmap.dal.DataManager;
28
import org.gvsig.fmap.dal.DataTypes;
29
import org.gvsig.fmap.dal.feature.EditableFeature;
30
import org.gvsig.fmap.dal.feature.EditableFeatureType;
31
import org.gvsig.fmap.dal.feature.Feature;
32
import org.gvsig.fmap.dal.feature.FeatureStore;
33
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters;
34
import org.gvsig.fmap.dal.feature.impl.DefaultEditableFeatureType;
35
import org.gvsig.fmap.geom.Geometry;
36
import org.gvsig.fmap.geom.GeometryLocator;
37
import org.gvsig.fmap.geom.GeometryManager;
38
import org.gvsig.fmap.geom.primitive.NullGeometry;
39
import org.gvsig.tools.exception.BaseException;
40
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer;
41
import org.gvsig.tools.visitor.VisitCanceledException;
42
import org.gvsig.tools.visitor.Visitor;
43

  
44
/**
45
 * Code to test bug gvsig-desktop#15671. This is based on the code provided by
46
 * the bug reporter (see ticket), thanks to him!!
47
 * 
48
 * @author gvSIG Team
49
 * @version $Id$
50
 */
51
public class Bug15671Test extends TestCase {
52

  
53
    public void testBug15671() throws Exception {
54
        new DefaultLibrariesInitializer().fullInitialize();
55
        DataManager manager = DALLocator.getDataManager();
56

  
57
        EditableFeatureType type = new DefaultEditableFeatureType();
58
        type.add("GEOMETRY", DataTypes.GEOMETRY)
59
            .setGeometryType(Geometry.TYPES.POINT)
60
            .setGeometrySubType(Geometry.SUBTYPES.GEOM2D);
61
        type.setDefaultGeometryAttributeName("GEOMETRY");
62
        type.add("double", DataTypes.DOUBLE);
63

  
64
        NewFeatureStoreParameters destParams =
65
            (NewFeatureStoreParameters) manager.createNewStoreParameters(
66
                "FilesystemExplorer", "Shape");
67
        destParams.setDynValue("shpfile", "/tmp/mySHP.shp");
68
        destParams.setDynValue("dbffile", "/tmp/mySHP.dbf");
69
        destParams.setDynValue("shxfile", "/tmp/mySHP.shx");
70
        destParams.setDynValue("crs", "EPSG:23030");
71
        destParams.setDefaultFeatureType(type);
72

  
73
        manager.newStore("FilesystemExplorer", "Shape", destParams, true);
74
        FeatureStore store =
75
            (FeatureStore) manager.openStore("Shape", destParams);
76

  
77
        store.edit();
78
        EditableFeature feature = store.createNewFeature().getEditable();
79
        GeometryManager geometryManager = GeometryLocator.getGeometryManager();
80
        NullGeometry geom =
81
            geometryManager.createNullGeometry(Geometry.SUBTYPES.GEOM2D);
82
        feature.setGeometry("GEOMETRY", geom);
83
        feature.setDouble("double", 42.0d);
84
        store.insert(feature);
85
        store.finishEditing();
86

  
87
        store.accept(new Visitor() {
88

  
89
            public void visit(Object obj) throws VisitCanceledException,
90
                BaseException {
91
                Feature feature = (Feature) obj;
92
                Geometry geometry = feature.getGeometry("GEOMETRY");
93
                assertEquals(
94
                    "Read geometry must be of Null Geometry type instead of "
95
                        + geometry.getGeometryType().getName(),
96
                    Geometry.TYPES.NULL, geometry.getType());
97
            }
98
        });
99
        store.dispose();
100
    }
101

  
102
}
tags/v2_0_0_Build_2045/libraries/libFMap_dalfile/src-test/org/gvsig/fmap/dal/store/dbf/JoinTransform.java
1
package org.gvsig.fmap.dal.store.dbf;
2

  
3
import java.util.ArrayList;
4
import java.util.Arrays;
5
import java.util.HashMap;
6
import java.util.Iterator;
7
import java.util.Map;
8
import java.util.Map.Entry;
9

  
10
import org.gvsig.fmap.dal.exception.DataException;
11
import org.gvsig.fmap.dal.feature.AbstractFeatureStoreTransform;
12
import org.gvsig.tools.dispose.DisposableIterator;
13
import org.gvsig.fmap.dal.feature.EditableFeature;
14
import org.gvsig.fmap.dal.feature.EditableFeatureType;
15
import org.gvsig.fmap.dal.feature.Feature;
16
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
17
import org.gvsig.fmap.dal.feature.FeatureQuery;
18
import org.gvsig.fmap.dal.feature.FeatureSet;
19
import org.gvsig.fmap.dal.feature.FeatureStore;
20
import org.gvsig.fmap.dal.feature.FeatureType;
21
import org.gvsig.tools.evaluator.Evaluator;
22
import org.gvsig.tools.evaluator.EvaluatorData;
23
import org.gvsig.tools.evaluator.EvaluatorException;
24
import org.gvsig.tools.evaluator.EvaluatorFieldsInfo;
25
import org.gvsig.tools.persistence.PersistentState;
26
import org.gvsig.tools.persistence.exception.PersistenceException;
27

  
28
public class JoinTransform extends AbstractFeatureStoreTransform {
29

  
30
	/**
31
	 * Store from which the join transform will get the additional attributes
32
	 */
33
	private FeatureStore store2;
34

  
35
	/**
36
	 * name of the key attr in store1 that will be used to match features in
37
	 * store2
38
	 */
39
	private String keyAttr1;
40

  
41
	/**
42
	 * name of the key attr in store2 that will be used to match features in
43
	 * store1
44
	 */
45
	private String keyAttr2;
46

  
47
	/**
48
	 * names of the attributes to join from store2 to store1
49
	 */
50
	private String[] attrs;
51

  
52
	/**
53
	 * Attribute names may change after transformation if they are repeated in
54
	 * both stores. This map keeps correspondence between store2 original names
55
	 * and their transformed counterparts.
56
	 */
57
	private Map targetNamesMap;
58

  
59
	private JoinTransformEvaluator evaluator = null;
60

  
61
	private FeatureType originalFeatureType;
62

  
63
	private String[] attrsForQuery;
64

  
65
	private String prefix1;
66

  
67
	private String prefix2;
68

  
69
	/**
70
	 * A default constructor
71
	 */
72
	public JoinTransform() {
73
		targetNamesMap = new HashMap();
74
	}
75

  
76
	/**
77
	 * Initializes all the necessary data for this transform
78
	 *
79
	 * @param store1
80
	 *            store whose default feature type is the target of this
81
	 *            transform
82
	 *
83
	 * @param store2
84
	 *            store whose default feature type will provide the new
85
	 *            attributes to join
86
	 *
87
	 * @param keyAttr1
88
	 *            key attribute in store1 that matches keyAttr2 in store2
89
	 *            (foreign key), used for joining both stores.
90
	 *
91
	 * @param keyAttr2
92
	 *            key attribute in store2 that matches keyAttr1 in store2
93
	 *            (foreign key), used for joining both stores.
94
	 *
95
	 * @param attrs
96
	 *            names of the attributes in store2 that will be joined to
97
	 *            store1.
98
	 */
99
	public void initialize(FeatureStore store1, FeatureStore store2,
100
			String keyAttr1, String keyAttr2, String prefix1, String prefix2,
101
			String[] attrs)
102
			throws DataException {
103

  
104
		if (store1 == store2) {
105
			throw new IllegalArgumentException("store1 == store2");
106
		}
107

  
108
		// Initialize needed data
109
		this.setFeatureStore(store1);
110
		this.store2 = store2;
111
		this.keyAttr1 = keyAttr1;
112
		this.keyAttr2 = keyAttr2;
113
		this.prefix1 = prefix1; // TODO
114
		this.prefix2 = prefix2; // TODO
115
		this.attrs = attrs;
116

  
117
		// calculate this transform resulting feature type
118
		// by adding all specified attrs from store2 to store1's default
119
		// feature type
120
		// FIXME for more than one FTypes ??
121
		this.originalFeatureType = this.getFeatureStore()
122
				.getDefaultFeatureType();
123

  
124
		// TODO tener en cuenta prefix1
125
		EditableFeatureType type = this.getFeatureStore().getDefaultFeatureType().getEditable();
126

  
127
		FeatureType type2 = store2.getDefaultFeatureType();
128

  
129
		// TODO tener en cuenta prefix2
130
		for (int i = 0; i < attrs.length; i++) {
131
			String name = attrs[i];
132

  
133
			// If an attribute already exists with the same name in store1's
134
			// default feature type,
135
			// calculate an alternate name and add it to our type
136
			int j = 0;
137
			while (type.getIndex(name) >= 0) {
138
				name = attrs[i] + "_" + ++j;
139
			}
140
			type.add(name,
141
					type2.getAttributeDescriptor(attrs[i]).getType());
142

  
143
			// keep correspondence between original name and transformed name
144
			this.targetNamesMap.put(attrs[i], name);
145
		}
146
		if (this.targetNamesMap.containsKey(keyAttr2)) {
147
			this.attrsForQuery = this.attrs;
148
		} else {
149
			ArrayList list = new ArrayList(this.attrs.length + 1);
150
			list.addAll(Arrays.asList(this.attrs));
151
			list.add(keyAttr2);
152
			this.attrsForQuery = (String[]) list.toArray(new String[] {});
153
		}
154

  
155
		// assign calculated feature type as this transform's feature type
156
		FeatureType[] types = new FeatureType[] { type.getNotEditableCopy() };
157
		setFeatureTypes(Arrays.asList(types), types[0]);
158
	}
159

  
160
	/**
161
	 *
162
	 *
163
	 * @param source
164
	 *
165
	 * @param target
166
	 *
167
	 * @throws DataException
168
	 */
169
	public void applyTransform(Feature source, EditableFeature target)
170
			throws DataException {
171

  
172
		// copy the data from store1 into the resulting feature
173
		this.copySourceToTarget(source, target);
174

  
175
		// ask store2 for the specified attributes, filtering by the key
176
		// attribute value
177
		// from the source feature
178
		JoinTransformEvaluator eval = this.getEvaluator();
179
		eval.updateValue(source.get(this.keyAttr1));
180

  
181
		FeatureQuery query = this.getFeatureStore().createFeatureQuery();
182
		query.setAttributeNames(attrsForQuery);
183
		query.setFilter(eval);
184

  
185
		FeatureSet set = store2.getFeatureSet(query);
186

  
187
		// In this join implementation, we will take only the first matching
188
		// feature found in store2
189

  
190
		FeatureAttributeDescriptor attr;
191
		Feature feat;
192
		String targetName;
193

  
194
		Iterator itAttr;
195

  
196
		DisposableIterator itFeat = set.iterator();
197
		if (itFeat.hasNext()) {
198
			feat = (Feature) itFeat.next();
199

  
200
			// copy all attributes from joined feature to target
201
			this.copyJoinToTarget(feat, target);
202
		}
203
		itFeat.dispose();
204
		set.dispose();
205
	}
206

  
207
	/**
208
	 * @param feat
209
	 * @param target
210
	 */
211
	private void copyJoinToTarget(Feature join, EditableFeature target) {
212
		Iterator iter = targetNamesMap.entrySet()
213
				.iterator();
214
		Entry entry;
215
		FeatureType trgType = target.getType();
216
		FeatureAttributeDescriptor attr;
217
		while (iter.hasNext()) {
218
			entry = (Entry) iter.next();
219
			attr = trgType.getAttributeDescriptor((String) entry.getValue());
220
			if (attr != null) {
221
				target.set(attr.getIndex(), join.get((String) entry.getKey()));
222
			}
223
		}
224

  
225

  
226
	}
227

  
228
	/**
229
	 * @param source
230
	 * @param target
231
	 */
232
	private void copySourceToTarget(Feature source, EditableFeature target) {
233
		FeatureAttributeDescriptor attr, attrTrg;
234
		FeatureType ftSrc = source.getType();
235
		FeatureType ftTrg = target.getType();
236

  
237

  
238
		for (int i = 0; i < source.getType().size(); i++) {
239
			attr = ftSrc.getAttributeDescriptor(i);
240
			if (ftTrg.getIndex(attr.getName()) > -1) {
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff